From d2a35bdfd41df85b575e52f309e94f6781a107a2 Mon Sep 17 00:00:00 2001 From: Amerik Singh Date: Thu, 21 Nov 2024 21:12:49 +0100 Subject: [PATCH] BF: Controlled system PT1 #1084 --- .../bf/systems/pool/first_order_system.py | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/mlpro/bf/systems/pool/first_order_system.py b/src/mlpro/bf/systems/pool/first_order_system.py index e8d7be4e0..a14a0dcc6 100644 --- a/src/mlpro/bf/systems/pool/first_order_system.py +++ b/src/mlpro/bf/systems/pool/first_order_system.py @@ -8,11 +8,13 @@ ## -- 2024-11-05 0.1.0 ASP Initial implementation class PT1 ## -- 2024-11-05 0.2.0 ASP class PT1: update methods __init__(), _setup_spaces() ## -- 2024-11-10 0.3.0 ASP class PT1: update methods __init__(), _setup_spaces() -# - update singature of __init___() +## - update singature of __init___() +## -- 2024-11-16 0.4.0 ASP class PT1: update methods _simulate_reaction() +## - changed dt = p_step to dt = p_step.total_seconds() ## ------------------------------------------------------------------------------------------------- """ -Ver. 0.3.0 (2024-11-10) +Ver. 0.4.0 (2024-11-16) This module provides a simple demo system that represent a first-order-system Further information: https://www.circuitbread.com/tutorials/first-order-systems-2-2 """ @@ -39,13 +41,14 @@ class PT1 (System): C_BOUNDARIES = [-1000,1000] C_PLOT_ACTIVE = False - C_LATENCY = timedelta( seconds = 1 ) + C_LATENCY = timedelta( seconds = 0.1 ) ## ------------------------------------------------------------------------------------------------- def __init__( self, p_K:float, p_T: float, p_sys_num:int, + p_y_start: float =0, p_id=None, p_name = C_NAME, p_latency : timedelta = None, @@ -78,6 +81,8 @@ def __init__( self, self.K = p_K self.T = p_T self._sys_num = p_sys_num + self._y_start = p_y_start + self._y_prev = None self._state_space, self._action_space = self._setup_spaces(p_sys_num=p_sys_num) @@ -114,13 +119,17 @@ def _simulate_reaction(self, p_state: State, p_action: Action, p_step = None): # get control Variable u = p_action.get_elem(p_id=agent_id).get_values()[0] - # get current state - y_prev = p_state.values[0] + # first run: assign start value + if self._y_prev is None: + self._y_prev = self._y_start + else: + # get current state + self._y_prev = p_state.values[0] - dt = p_step + dt = p_step.total_seconds() # rekursions function of first oder system - y = (dt* self.K * u + self.T * y_prev) / (self.T + dt) + y = (dt* self.K * u + self.T * self._y_prev) / (self.T + dt) #set values of new state state new_state.values = [y]