|
24 | 24 | # options are: 'static-plus-rule' and 'prop-outflow' (or 'uncontrolled') |
25 | 25 | # static plus rule is fixed positions for the 4 weirs plus a height to open the infiltration valve at |
26 | 26 | # prop outflow adds proportional feedback to weir openings to increase outflows when far below the threshold |
27 | | -evaluating = 'static-plus-rule' |
| 27 | +evaluating = 'prop-outflow' |
28 | 28 | verbose = True |
29 | 29 | version = "2" # options are "1" and "2" |
30 | | -level = "1" # options are "1" , "2", and "3" |
| 30 | +level = "3" # options are "1" , "2", and "3" |
31 | 31 | #hysteresis = 0.05 # hysteresis for the infiltration valve to avoid rapid cycling between open and closed |
32 | 32 | plot = True # plot True significantly increases the memory usage. |
33 | 33 | # set the working directory to the directory of this script |
|
98 | 98 |
|
99 | 99 | states = pd.DataFrame(columns = env.config['states']) |
100 | 100 | actions = pd.DataFrame(columns = env.config['action_space']) |
| 101 | + state = env.state(level=level) # get the initial state of the environment |
| 102 | + old_state = state.copy() |
| 103 | + old_old_state = old_state.copy() |
101 | 104 |
|
102 | 105 | while not done: |
103 | 106 | # take control actions? |
104 | 107 | if env.env.sim.current_time.minute % 5 == 0 and (env.env.sim.current_time > last_eval + datetime.timedelta(minutes=2)): |
105 | 108 | last_eval = env.env.sim.current_time |
106 | 109 | state = env.state(level=level) |
| 110 | + old_old_state = old_state.copy() # save the old state before updating |
| 111 | + old_state = state.copy() # save the current state as the old state for next iteration |
| 112 | + state_for_control = (state + old_state + old_old_state) / 3.0 # use the average of the current, previous, and previous previous states for control to smooth out noise in the state space |
107 | 113 |
|
108 | 114 | # first bit is the same for all 3 controllers |
109 | 115 | # set the weirs |
|
118 | 124 | u_open_pct[-1] = 0.0 # close the valve to preserve capacity in the infiltration basin |
119 | 125 | ''' |
120 | 126 | # try proprtional valve opening between two setpoint depths |
121 | | - if state[0] < optimal_static_settings[-2]: # below lower threshold |
| 127 | + if state_for_control[0] < optimal_static_settings[-2]: # below lower threshold |
122 | 128 | u_open_pct[-1] = 0.0 # close the valve to preserve capacity in the infiltration basin |
123 | | - elif state[0] > optimal_static_settings[-1]: # above upper threshold |
| 129 | + elif state_for_control[0] > optimal_static_settings[-1]: # above upper threshold |
124 | 130 | u_open_pct[-1] = 1.0 # fully open valve above upper threshold depth in basin c |
125 | 131 | else: # between the two thresholds |
126 | 132 | # linearly interpolate the valve opening based on the current depth in basin C |
|
129 | 135 | upper_threshold = optimal_static_settings[-1] # upper threshold depth |
130 | 136 | range_threshold = upper_threshold - lower_threshold |
131 | 137 | # calculate the current depth in the range |
132 | | - current_depth_in_range = state[0] - lower_threshold |
| 138 | + current_depth_in_range = state_for_control[0] - lower_threshold |
133 | 139 | # calculate the percentage of the way through the range |
134 | 140 | percentage_in_range = current_depth_in_range / range_threshold |
135 | 141 | # set the valve opening based on the percentage in range |
|
149 | 155 | op_bound_width = operational_bounds_df.loc[name,"Upper Limit"] - operational_bounds_df.loc[name, "Lower Limit"] |
150 | 156 | lower_tight_bound = operational_bounds_df.loc[name, "Lower Limit"] + 0.1 * op_bound_width |
151 | 157 | upper_tight_bound = operational_bounds_df.loc[name,"Upper Limit"] - 0.25 * op_bound_width |
152 | | - if state[idx_state[0]] < upper_tight_bound and state[idx_state[0]] > lower_tight_bound: |
| 158 | + if state_for_control[idx_state[0]] < upper_tight_bound and state_for_control[idx_state[0]] > lower_tight_bound: |
153 | 159 | # only use proportional feedback on the outflow if we're safe regarding depth bounds |
154 | 160 | u_open_pct[idx] += optimal_prop * flow_capacity |
155 | | - elif state[idx_state[0]] >= upper_tight_bound and optimal_prop*flow_capacity > 0: # allow extra opening if close to exceeding upper bound |
| 161 | + elif state_for_control[idx_state[0]] >= upper_tight_bound and optimal_prop*flow_capacity > 0: # allow extra opening if close to exceeding upper bound |
156 | 162 | u_open_pct[idx] += optimal_prop * flow_capacity |
157 | 163 | if u_open_pct[idx] > 1.0: |
158 | 164 | u_open_pct[idx] = 1.0 |
|
0 commit comments