Skip to content

Commit

Permalink
Merge pull request #4 from rlutes/issue277_ExampleControllers
Browse files Browse the repository at this point in the history
Issue277 example controllers
  • Loading branch information
SenHuang19 authored Aug 24, 2021
2 parents d92e561 + f0d9af1 commit 4c50b24
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 288 deletions.
42 changes: 11 additions & 31 deletions examples/python/controllers/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import pandas as pd


class Controller(object):
def __init__(self, module, forecast_config, step):

class Control(object):
def __init__(self, module, forecast_config):

"""Controller object - instantiates concrete implementation of controller configured
in configuration
Expand All @@ -14,45 +16,23 @@ def __init__(self, module, forecast_config, step):
path to concrete implementation of controller
forecast_config : list of strings, dict
list of data names obtained by instatiator get/forecast method
step: int
step size in simulation used to store forecasts from forecast as simulation progresses
"""
try:
module = importlib.import_module(module)
controller = importlib.import_module(module)
except ModuleNotFoundError:
print("Cannot find specified controller: {}".format(module))
sys.exit()

if forecast_config is not None:
forecasts_store = pd.DataFrame(columns=forecast_config)
self.update_forecast = module.update_forecast
self.update_forecasts = controller.update_forecasts
self.use_forecast = True
else:
forecasts_store = None
self.use_forecast = False
self.compute_control = module.compute_control
self.initialize = module.initialize
self.forecasts_store = forecasts_store
self.forecast_config = forecast_config
self.step = step
self.compute_control = controller.compute_control
self.initialize = controller.initialize
self.controller = controller



def forecast(self, forecast, iteration):
"""Pass forecast value from interface to concrete implementation of the controller

Parameters
----------
forecast : str, required
path to concrete implementation of controller.
iteration: int, required
Number of timesteps simulation has progressed though.
Returns
-----------
forecasts: list
forecast for data point in simulation returned by call to /forecast
"""
forecasts = self.update_forecast(self.forecast_config, forecast)
if self.forecasts_store is not None:
self.forecasts_store.loc[(iteration + 1) * self.step, self.forecasts_store.columns] = forecasts
return forecasts

17 changes: 9 additions & 8 deletions examples/python/controllers/pidTwoZones.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ def initialize():
return u


def update_forecast(prediction_config, forecast):
"""Update the control input from forecast.
def update_forecasts(forecast_config, forecast):
"""Compute the control input from the measurement.
Parameters
----------
prediction_config : list
forecast_config : list
list of data points that names contained in forecast
[<input_name1>, <input_name2>]
forecast : dict
Expand All @@ -105,15 +106,15 @@ def update_forecast(prediction_config, forecast):
Returns
-------
predictions : dict
forecasts : dict
Defines the control input to be used for the next step.
{<input_name> : <input_value>}
"""
predictions = []
forecasts = []
if forecast:
for j in range(len(prediction_config)):
predictions.append(forecast[prediction_config[j]][0])
for j in range(len(forecast_config)):
forecasts.append(forecast[forecast_config[j]][0])
else:
print("Cannot do controller prediction - No forecast!")
return predictions
return forecasts
Loading

0 comments on commit 4c50b24

Please sign in to comment.