Commit 2b6e814c authored by hazrmard's avatar hazrmard
Browse files

forwarded fallback input values to control loop, more debug printing when...

forwarded fallback input values to control loop, more debug printing when input exceeds threshold, only most recent raw input is added to buffer, averaged input is of buffer *and* new measurement
parent 4e0f94f6
......@@ -20,3 +20,4 @@ energyfreqanalysis/
TestRelearning.ipynb
*.out
relearn.pkl
_demo_source
\ No newline at end of file
......@@ -122,7 +122,8 @@ parser.add_argument('--halt_on_error', action='store_true', default=False,
def control_loop(source: str, store: str, save: str, valid: str, agent: DDPGAgent, policy: str,
lock_policy: Lock, signal_stop: Event, signal_reload: Event,
period: float, control_mean_window: int, control_thresh: float,
input_thresh: np.ndarray, dev_server: bool, halt_on_error: bool):
input_thresh: np.ndarray, control_input_fallback: np.ndarray,
dev_server: bool, halt_on_error: bool):
"""
Main control loop for the script. Runs in a separate thread until a stop
signal is set by the main thread.
......@@ -146,6 +147,8 @@ def control_loop(source: str, store: str, save: str, valid: str, agent: DDPGAgen
control_thresh {float} -- Maximum amount output can change between intervals.
input_thresh {np.ndarray} -- Maximum amount each input can change between
intervals to be considered normal.
control_input_fallback {np.ndarray} -- Fallback values to use in case
no prior inputs to average anomalous measurements with.
dev_server {bool} -- Whether environment is a development or a production
server.
halt_on_error {bool} -- Stop control loop if exception occurs.
......@@ -208,13 +211,20 @@ def control_loop(source: str, store: str, save: str, valid: str, agent: DDPGAgen
if np.any(np.abs(buffer_in[-1] - raw[-1]) > input_thresh):
is_alumni_vars_updated = False
log.warning('Input change > threshold. Using mean inputs')
if len(buffer_in) > control_mean_window:
raw = np.asarray([np.mean(buffer_in, axis=0)])
log.debug('Last input: {}, Current input: {}'\
.format(buffer_in[-1], raw[-1]))
if len(buffer_in) >= control_mean_window:
arr = np.concatenate((np.asarray(buffer_in), raw))
raw = np.mean(arr, axis=0, keepdims=True)
log.debug(('Using averaged input of buffer and latest input:'
'\n{}\n=={}').format(arr, raw[-1]))
else:
raw = np.asarray([FALLBACK_INPUTS])
raw = np.asarray([control_input_fallback])
log.debug('Using fallback input: {}'.format(raw[-1]))
# Once input has been validated and transformed, add it to buffer
buffer_in.append(raw)
# Once input has been validated and transformed, add most recent
# measurement to buffer.
buffer_in.append(raw[-1])
# Execute new control logic when a new measurement becomes available:
# Applying control
......@@ -394,7 +404,7 @@ def demo_source(dest: str, signal_stop: Event, period: float, mean: np.ndarray,
readings = sigma * randn(NUM_INPUT_VARS) + mean
with open(dest, 'w') as file:
file.write('{:.2f}\t{:.2f}\t{:.2f}\t{:.2f}\n'.format(*readings))
log.info('Wrote new values to source.')
log.info('Demo values written to source.')
signal_stop.wait(timeout=abs(period + 0.2 * randn()))
except Exception as e:
log.critical('Demo source error: %s', str(e))
......@@ -451,26 +461,27 @@ if __name__ == '__main__':
signal_stop,
args.period,
np.asarray(args.control_input_fallback),
np.asarray(args.control_input_threshold)))
2 * np.asarray(args.control_input_threshold)))
pr_demo.start()
# start control loop thread
th_ctrl = Thread(target=control_loop, daemon=False, kwargs={
'source': '_demo_source' if args.demo else args.read_from,
'store': args.store_at,
'save': args.save_to,
'valid': args.is_valid,
'agent': agent,
'policy': args.use_control,
'signal_stop': signal_stop,
'signal_reload': signal_reload,
'lock_policy': lock_policy,
'period': args.period,
'control_mean_window': args.control_mean_window,
'dev_server': args.dev_server,
'control_thresh': args.control_output_threshold,
'input_thresh': np.asarray(args.control_input_threshold),
'halt_on_error': args.halt_on_error
'source': '_demo_source' if args.demo else args.read_from,
'store': args.store_at,
'save': args.save_to,
'valid': args.is_valid,
'agent': agent,
'policy': args.use_control,
'signal_stop': signal_stop,
'signal_reload': signal_reload,
'lock_policy': lock_policy,
'period': args.period,
'control_mean_window': args.control_mean_window,
'control_thresh': args.control_output_threshold,
'input_thresh': np.asarray(args.control_input_threshold),
'control_input_fallback': np.asarray(args.control_input_fallback),
'dev_server': args.dev_server,
'halt_on_error': args.halt_on_error
})
th_ctrl.start()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment