diff --git a/nbs/05_environments.ipynb b/nbs/05_environments.ipynb index a75856c0..6c2d6fcb 100644 --- a/nbs/05_environments.ipynb +++ b/nbs/05_environments.ipynb @@ -651,49 +651,67 @@ "outputs": [], "source": [ "#| export\n", - "class WindTurbine(YawEnv):\n", - " \"A function that creates and runs the YawEnv environment fro a wind turbine.\"\n", + "class WindTurbine(ControlEnvironment):\n", + " \"A function that creates and runs the YawEnv environment for a wind turbine.\"\n", " \n", - " def __init__(self, render=False, value=0, name=\"YawEnv\", \n", - " seed=None, links=None, new_name=True, early_termination=True, namespace=None, **cargs): \n", - " super().__init__('YawEnv', wind_timeseries,start_index,stop_index,ancestors,filter_duration,yaw_parameters,keep_history=True,\n", - " value=value, name=name, links=links, new_name=new_name, namespace=namespace,\n", - " early_termination=early_termination, **cargs)\n", + " def __init__(self, value=0, name=\"WindTurbine\", links=None, new_name=True, namespace=None, **cargs): \n", + " super().__init__(value=value, links=links, name=name, new_name=new_name, namespace=namespace, **cargs)\n", " \n", - " self.really_done = False\n", + " self.num_links=1\n", + " self.env_name='YawEnv'\n", + "\n", " \n", " def __call__(self, verbose=False): \n", " super().__call__(verbose)\n", " \n", " return self.value\n", "\n", + " def set_properties(self, props):\n", + " self.initialise(props)\n", + " # wind_timeseries,start_index,stop_index,ancestors,filter_duration,yaw_parameters,\n", + "\n", " def early_terminate(self):\n", - " if self.early_termination:\n", - " if self.really_done:\n", - " raise Exception(f'1000: OpenAIGym Env: {self.env_name} has terminated.')\n", - " if self.done:\n", - " self.reward = 0\n", - " self.really_done = True\n", + " pass\n", "\n", " def process_inputs(self):\n", " self.input = self.links[0].get_value()\n", " \n", " def process_actions(self):\n", - " force = min(max(self.input, self.min_action), self.max_action)\n", - " self.input=[force]\n", + " raise Exception(f'TBD {self.__class__.__name__}:{self.env_name}.')\n", " \n", + " \n", + " def apply_actions_get_obs(self):\n", + " return self.env.step([self.input])\n", + "\n", + " def parse_obs(self):\n", + " raise Exception(f'TBD {self.__class__.__name__}:{self.env_name}.')\n", + "\n", " def process_values(self):\n", - " reward = self.obs[1]\n", - " if reward > 90:\n", - " reward = 0\n", - " self.reward = - reward\n", - " pos = self.value[0] + 1.2\n", - " self.value = np.append(self.value, pos)\n", + " raise Exception(f'TBD {self.__class__.__name__}:{self.env_name}.')\n", + "\n", + " def get_config(self, zero=1):\n", + " config = super().get_config(zero=zero)\n", + " return config\n", + "\n", + " def get_graph_name(self):\n", + " return super().get_graph_name() \n", + " \n", + " def set_render(self, render):\n", + " self.render=render\n", + " \n", + " def reset(self, full=True, seed=None): \n", + " raise Exception(f'TBD {self.__class__.__name__}:{self.env_name}.')\n", + "\n", + " def summary(self, extra=False):\n", + " super().summary(\"\", extra=extra)\n", + "\n", + " def close(self):\n", + " self.env.close()\n", "\n", " class Factory:\n", - " def create(self, seed=None): return MountainCarContinuousV0(seed=seed)\n", + " def create(self, seed=None): return WindTurbine(seed=seed)\n", " class FactoryWithNamespace:\n", - " def create(self, namespace=None, seed=None): return MountainCarContinuousV0(namespace=namespace, seed=seed) " + " def create(self, namespace=None, seed=None): return WindTurbine(namespace=namespace, seed=seed) " ] }, { @@ -1341,7 +1359,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'type': 'Pendulum', 'name': 'Pendulum', 'value': 0, 'links': {0: 'constant1'}, 'env_name': 'Pendulum-v1', 'reward': 0, 'done': False, 'info': {}}\n" + "{'type': 'Pendulum', 'name': 'Pendulum', 'value': 0, 'links': {0: 'constant'}, 'env_name': 'Pendulum-v1', 'reward': 0, 'done': False, 'info': {}}\n" ] } ], diff --git a/nbs/12_yaw_module.ipynb b/nbs/12_yaw_module.ipynb index 07b89967..afbd9bff 100644 --- a/nbs/12_yaw_module.ipynb +++ b/nbs/12_yaw_module.ipynb @@ -180,16 +180,16 @@ "source": [ "#| export\n", "class YawEnv(Env):\n", - " def __init__(\n", - " self,\n", - " wind_timeseries,\n", - " start_index,\n", - " stop_index,\n", - " ancestors,\n", - " filter_duration,\n", - " params,\n", - " keep_history=False,\n", - " ):\n", + "\n", + " def initialise(self, properties ):\n", + "\n", + " wind_timeseries=properties['wind_timeseries']\n", + " start_index=properties['wind_timeseries']\n", + " stop_index=properties['wind_timeseries']\n", + " ancestors=properties['wind_timeseries']\n", + " filter_duration=properties['wind_timeseries']\n", + " params=properties['wind_timeseries']\n", + " keep_history=properties['wind_timeseries']\n", "\n", " self.wind_timeseries = wind_timeseries\n", " self.start_index = start_index\n", @@ -201,6 +201,7 @@ " self.yaw_rate_max = params[\"yaw_rate_max\"]\n", " self.cycle_period = params[\"cycle_period\"]\n", " self.w2 = params[\"w2\"]\n", + "\n", " self.episode_len = stop_index - start_index\n", " self.history = None\n", " self.ref_speed = params[\"ref_speed\"]\n", @@ -233,7 +234,6 @@ " shape=(self.ancestors, 4),\n", " )\n", "\n", - "\n", " def step(self, action):\n", " \"\"\"\n", " Apply yaw control action and compute reward obtained **after yaw actuation**\n", @@ -268,8 +268,6 @@ " + self.w2 * (self.step_since_last_2 > self.filter_duration and self.step_since_last_0 > self.filter_duration)\n", "\n", "\n", - "\n", - "\n", " if self.keep_history:\n", " power_control = self.get_power(self.wind_timeseries[\"wind_speed\"][self.index_wind_timeseries],new_yaw_error)\n", " power_no_loss = self.get_power(self.wind_timeseries[\"wind_speed\"][self.index_wind_timeseries], 0)\n", diff --git a/pct/_modidx.py b/pct/_modidx.py index e11dfb04..a5c7d19f 100644 --- a/pct/_modidx.py +++ b/pct/_modidx.py @@ -326,14 +326,29 @@ 'pct/environments.py'), 'pct.environments.WindTurbine.__init__': ( 'environments.html#windturbine.__init__', 'pct/environments.py'), + 'pct.environments.WindTurbine.apply_actions_get_obs': ( 'environments.html#windturbine.apply_actions_get_obs', + 'pct/environments.py'), + 'pct.environments.WindTurbine.close': ('environments.html#windturbine.close', 'pct/environments.py'), 'pct.environments.WindTurbine.early_terminate': ( 'environments.html#windturbine.early_terminate', 'pct/environments.py'), + 'pct.environments.WindTurbine.get_config': ( 'environments.html#windturbine.get_config', + 'pct/environments.py'), + 'pct.environments.WindTurbine.get_graph_name': ( 'environments.html#windturbine.get_graph_name', + 'pct/environments.py'), + 'pct.environments.WindTurbine.parse_obs': ( 'environments.html#windturbine.parse_obs', + 'pct/environments.py'), 'pct.environments.WindTurbine.process_actions': ( 'environments.html#windturbine.process_actions', 'pct/environments.py'), 'pct.environments.WindTurbine.process_inputs': ( 'environments.html#windturbine.process_inputs', 'pct/environments.py'), 'pct.environments.WindTurbine.process_values': ( 'environments.html#windturbine.process_values', - 'pct/environments.py')}, + 'pct/environments.py'), + 'pct.environments.WindTurbine.reset': ('environments.html#windturbine.reset', 'pct/environments.py'), + 'pct.environments.WindTurbine.set_properties': ( 'environments.html#windturbine.set_properties', + 'pct/environments.py'), + 'pct.environments.WindTurbine.set_render': ( 'environments.html#windturbine.set_render', + 'pct/environments.py'), + 'pct.environments.WindTurbine.summary': ('environments.html#windturbine.summary', 'pct/environments.py')}, 'pct.errors': { 'pct.errors.BaseErrorCollector': ('errors.html#baseerrorcollector', 'pct/errors.py'), 'pct.errors.BaseErrorCollector.__init__': ('errors.html#baseerrorcollector.__init__', 'pct/errors.py'), 'pct.errors.BaseErrorCollector.__repr__': ('errors.html#baseerrorcollector.__repr__', 'pct/errors.py'), @@ -948,8 +963,8 @@ 'pct.webots.WebotsHelper.get_sensor_names': ('webots.html#webotshelper.get_sensor_names', 'pct/webots.py'), 'pct.webots.WebotsHelper.get_sensor_values': ('webots.html#webotshelper.get_sensor_values', 'pct/webots.py')}, 'pct.yaw_module': { 'pct.yaw_module.YawEnv': ('yaw_module.html#yawenv', 'pct/yaw_module.py'), - 'pct.yaw_module.YawEnv.__init__': ('yaw_module.html#yawenv.__init__', 'pct/yaw_module.py'), 'pct.yaw_module.YawEnv.get_power': ('yaw_module.html#yawenv.get_power', 'pct/yaw_module.py'), + 'pct.yaw_module.YawEnv.initialise': ('yaw_module.html#yawenv.initialise', 'pct/yaw_module.py'), 'pct.yaw_module.YawEnv.reset': ('yaw_module.html#yawenv.reset', 'pct/yaw_module.py'), 'pct.yaw_module.YawEnv.step': ('yaw_module.html#yawenv.step', 'pct/yaw_module.py'), 'pct.yaw_module.get_comparaison_metrics': ('yaw_module.html#get_comparaison_metrics', 'pct/yaw_module.py'), diff --git a/pct/environments.py b/pct/environments.py index 42bb5231..5709b5b5 100644 --- a/pct/environments.py +++ b/pct/environments.py @@ -505,49 +505,67 @@ class FactoryWithNamespace: def create(self, namespace=None, seed=None): return MountainCarContinuousV0(namespace=namespace, seed=seed) # %% ../nbs/05_environments.ipynb 13 -class WindTurbine(YawEnv): - "A function that creates and runs the YawEnv environment fro a wind turbine." +class WindTurbine(ControlEnvironment): + "A function that creates and runs the YawEnv environment for a wind turbine." - def __init__(self, render=False, value=0, name="YawEnv", - seed=None, links=None, new_name=True, early_termination=True, namespace=None, **cargs): - super().__init__('YawEnv', wind_timeseries,start_index,stop_index,ancestors,filter_duration,yaw_parameters,keep_history=True, - value=value, name=name, links=links, new_name=new_name, namespace=namespace, - early_termination=early_termination, **cargs) + def __init__(self, value=0, name="WindTurbine", links=None, new_name=True, namespace=None, **cargs): + super().__init__(value=value, links=links, name=name, new_name=new_name, namespace=namespace, **cargs) - self.really_done = False + self.num_links=1 + self.env_name='YawEnv' + def __call__(self, verbose=False): super().__call__(verbose) return self.value + def set_properties(self, props): + self.initialise(props) + # wind_timeseries,start_index,stop_index,ancestors,filter_duration,yaw_parameters, + def early_terminate(self): - if self.early_termination: - if self.really_done: - raise Exception(f'1000: OpenAIGym Env: {self.env_name} has terminated.') - if self.done: - self.reward = 0 - self.really_done = True + pass def process_inputs(self): self.input = self.links[0].get_value() def process_actions(self): - force = min(max(self.input, self.min_action), self.max_action) - self.input=[force] + raise Exception(f'TBD {self.__class__.__name__}:{self.env_name}.') + + def apply_actions_get_obs(self): + return self.env.step([self.input]) + + def parse_obs(self): + raise Exception(f'TBD {self.__class__.__name__}:{self.env_name}.') + def process_values(self): - reward = self.obs[1] - if reward > 90: - reward = 0 - self.reward = - reward - pos = self.value[0] + 1.2 - self.value = np.append(self.value, pos) + raise Exception(f'TBD {self.__class__.__name__}:{self.env_name}.') + + def get_config(self, zero=1): + config = super().get_config(zero=zero) + return config + + def get_graph_name(self): + return super().get_graph_name() + + def set_render(self, render): + self.render=render + + def reset(self, full=True, seed=None): + raise Exception(f'TBD {self.__class__.__name__}:{self.env_name}.') + + def summary(self, extra=False): + super().summary("", extra=extra) + + def close(self): + self.env.close() class Factory: - def create(self, seed=None): return MountainCarContinuousV0(seed=seed) + def create(self, seed=None): return WindTurbine(seed=seed) class FactoryWithNamespace: - def create(self, namespace=None, seed=None): return MountainCarContinuousV0(namespace=namespace, seed=seed) + def create(self, namespace=None, seed=None): return WindTurbine(namespace=namespace, seed=seed) # %% ../nbs/05_environments.ipynb 14 class VelocityModel(BaseFunction): diff --git a/pct/yaw_module.py b/pct/yaw_module.py index 438f9e68..1610c90f 100644 --- a/pct/yaw_module.py +++ b/pct/yaw_module.py @@ -116,16 +116,16 @@ def get_dataset_from_simu(path="dataset.csv", cycle_period=10, rolling_average_d # %% ../nbs/12_yaw_module.ipynb 7 class YawEnv(Env): - def __init__( - self, - wind_timeseries, - start_index, - stop_index, - ancestors, - filter_duration, - params, - keep_history=False, - ): + + def initialise(self, properties ): + + wind_timeseries=properties['wind_timeseries'] + start_index=properties['wind_timeseries'] + stop_index=properties['wind_timeseries'] + ancestors=properties['wind_timeseries'] + filter_duration=properties['wind_timeseries'] + params=properties['wind_timeseries'] + keep_history=properties['wind_timeseries'] self.wind_timeseries = wind_timeseries self.start_index = start_index @@ -137,6 +137,7 @@ def __init__( self.yaw_rate_max = params["yaw_rate_max"] self.cycle_period = params["cycle_period"] self.w2 = params["w2"] + self.episode_len = stop_index - start_index self.history = None self.ref_speed = params["ref_speed"] @@ -169,7 +170,6 @@ def __init__( shape=(self.ancestors, 4), ) - def step(self, action): """ Apply yaw control action and compute reward obtained **after yaw actuation** @@ -204,8 +204,6 @@ def step(self, action): + self.w2 * (self.step_since_last_2 > self.filter_duration and self.step_since_last_0 > self.filter_duration) - - if self.keep_history: power_control = self.get_power(self.wind_timeseries["wind_speed"][self.index_wind_timeseries],new_yaw_error) power_no_loss = self.get_power(self.wind_timeseries["wind_speed"][self.index_wind_timeseries], 0)