Commit f4360684 authored by hazrmard's avatar hazrmard
Browse files

bugfix in ah-api - now ignores older format files UU-YYYY-store.csv. Reshaping...

bugfix in ah-api - now ignores older format files UU-YYYY-store.csv. Reshaping new_input to in control loop function to have 1 row and 4 columns. Errors still with controller.py tests
parent ffe27b32
......@@ -4,20 +4,21 @@ __pycache__
.idea
.vscode
*.ini
results*/
*store.csv
store
control
is_valid.csv
SAT_Setpoint.csv
store
control
log.txt
test.txt
.out
*.ipynb
*.out
relearn.pkl
*.hdf5
_demo_source
results*/
rl_results_local/
rl_perf_plot.py
modeltrainnb/
energyfreqanalysis/
TestRelearning.ipynb
*.out
relearn.pkl
_demo_source
\ No newline at end of file
energyfreqanalysis/
\ No newline at end of file
......@@ -117,7 +117,7 @@ def weeklysolardata(datafolder: str = './energyfreqanalysis/', weeks: int = 4):
# including current week.
# This assumes *store.csv files are named in a YYYY-UU format or any scheme
# where the names when sorted alphabetically are put in chronological order.
flist = sorted(glob(os.path.join(datafolder, '*store.csv')), reverse=True)[:weeks+1]
flist = sorted(glob(os.path.join(datafolder, '[0-9][0-9][0-9][0-9]-[0-9][0-9]-store.csv')), reverse=True)[:weeks+1]
if len(flist) == 0:
warnings.warn('No *store.csv files found containing historical measurements.')
......@@ -146,6 +146,7 @@ def weeklysolardata(datafolder: str = './energyfreqanalysis/', weeks: int = 4):
dflist.append(dfr)
print(len(dflist))
df = merge_df_rows(dflist)
df.index = df.index.round('-5min')
df = droprows(df)
......
......@@ -12,7 +12,7 @@ from datetime import datetime
from importlib import import_module
from collections import deque
from threading import Thread, Lock
from multiprocessing import Process, Event
from multiprocessing import Event
# Set up logging with a global variable "log"
logging.captureWarnings(True)
......@@ -176,7 +176,7 @@ def control_loop(source: str, store: str, save: str, valid: str, agent: DDPGAgen
# Get measurements from source:
t = None
if source == '_demo_source':
raw = np.genfromtxt(source) # read measurements as numpy arrays
raw = np.genfromtxt(source) # read measurements as numpy arrays
# This is the time format from the production data
t = cur_time.strftime('%d-%b-%y %I:%M %p CDT')
else:
......@@ -238,8 +238,8 @@ def control_loop(source: str, store: str, save: str, valid: str, agent: DDPGAgen
# Execute new control logic when a new measurement becomes available:
# Applying control
new_input = raw[-1] # take only latest measurement for control
ctrl = agent.select_action(new_input[np.newaxis, :])[0]
new_input = raw[-1, np.newaxis] # take only latest measurement for control
ctrl = agent.select_action(new_input)[0]
ctrl_window.append(ctrl) # record action in history
with open(save, 'w') as cfile:
final_ctrl = np.mean(ctrl_window) # final control is mean of window
......@@ -267,6 +267,9 @@ def control_loop(source: str, store: str, save: str, valid: str, agent: DDPGAgen
# Saving historical data. Data storage is aggregated on a calendar-
# week basis.
# The saved file is named as YYYY-UU-store.csv. Any changes to naming
# format must be reflected in ah_api.py where stored files are read
# back for re-learning.
dfile_path = os.path.join(store, datetime.now().strftime('%Y-%U-store.csv'))
with open(dfile_path, 'a') as dfile:
dfile.seek(0, 2) # go to end of destination file...
......@@ -326,17 +329,18 @@ def learn_control(source: str, save_to: str, relearn_window: int, duration: int,
relearndf.to_pickle('relearn.pkl')
# Train it with the newly initialized environment
log.info('Adapting control policy.')
log.info('RELEARN: Adapting control policy.')
with lock_policy:
if signal_stop.is_set():
log.info('Stopping re-learning.')
return
# retrain on the same model
log.info('RELEARN: Loading existing LSTM model of energy consumption.')
lstm = load_model('weights.best.hdf5')
log.info('Re-training LSTM model of energy consumption.')
log.info('RELEARN: Re-training LSTM model of energy consumption.')
retrain(lstm, relearndf, epochs=25)
log.info('LSTM energy model training finished.')
log.info('RELEARN: LSTM energy model training finished.')
# create new model of environment
env = Env(datapath='relearn.pkl', modelpath = 'weights.best.hdf5')
......@@ -345,13 +349,13 @@ def learn_control(source: str, save_to: str, relearn_window: int, duration: int,
# Do not reinitialize the agent, instead use a COPY of the existing agent file
agent = get_agent(env)
agent.load_weights('agent_weights.h5f')
log.info('Training new control agent.')
log.info('RELEARN: Training new control agent.')
train_agent(agent=agent, env=env, steps=duration, dest=save_to)
log.info('Control policy adapted.')
log.info('RELEARN: Control policy adapted.')
signal_reload.set()
except Exception as e:
log.error('Control re-learning error: %s', str(e))
log.error('RELEARN: %s', str(e))
log.debug(e, exc_info=True)
......
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