Hi !
In my code I create an instance of MicrogridEnv
:
from microgridRLsimulator.gym_wrapper import MicrogridEnv
env = MicrogridEnv(from_date,to_date, case)
Then, no matter which action I take, I get this error
File "./abstract/manager/manager.py", line 100, in _train_simulate
o_r_d_i = env.step(action)
File "/home/da/Documents/emploi/postdoc/2018_2019/dev/rl/venv/lib/python3.6/site-packages/gym/core.py", line 304, in step
observation, reward, done, info = self.env.step(action)
File "/home/da/Documents/emploi/postdoc/2018_2019/dev/rl/venv/src/microgridrlsimulator/microgridRLsimulator/gym_wrapper/microgrid_env.py", line 62, in step
state_formatted = self.state_formatting(state)
File "/home/da/Documents/emploi/postdoc/2018_2019/dev/rl/venv/src/microgridrlsimulator/microgridRLsimulator/gym_wrapper/microgrid_env.py", line 91, in state_formatting
delta_t = state_array[n + 2]
IndexError: list index out of range
It seems that it comes from these functions:
def state_refactoring(self, state):
"""
Convenience function that flattens the received state into an array
:param state: State of the agent as a list
:return: Flattened representation of the state as an array
"""
consumption = state[0]
storages_soc = state[1]
production = state[2]
delta_t = state[3]
state_array = np.concatenate((np.array([consumption]), np.array(storages_soc).reshape(-1), np.array([production]), np.array([delta_t])),
axis=0)
return state_array
def state_formatting(self, state_array):
"""
Inverse of state_refactoring
"""
n= len(self.simulator.grid.storages)
consumption = state_array[0]
storages_soc = list(state_array[1:1+n])
production = state_array[n + 1]
delta_t = state_array[n + 2]
state = [consumption, storages_soc, production, delta_t]
return state
Indeed, here is the print of state_array
[3.52440585725, [6.0, 6.0], 0.0, 0.0]
which is a list of : a float, a list of two floats and two other floats. Since in state_formatting
function, n=2
, it is not possible to access to delta_t = state_array[n + 2]
. That's probably because next_state
is not flatten in step function
def step(self, action, state=None):
"""
Step function, as in gym.
May also accept a state as input (useful for MCTS, for instance).
"""
assert self.action_space.contains(action), "%r (%s) invalid" % (action, type(action))
if state is None:
state = self.state
state_formatted = self.state_formatting(state)
next_state, reward, done = self.simulator.step(high_level_action = action, state = state_formatted)
self.state = self.state_refactoring(next_state)
return np.array(self.state), reward, done, {}
A possible fix could be to modify state_formatting
function into:
def state_formatting(self, state_array):
"""
Inverse of state_refactoring
"""
consumption = state_array[0]
storages_soc = list(state_array[1])
production = state_array[2]
delta_t = state_array[3]
state = [consumption, storages_soc, production, delta_t]
return state
Any idea ? :) Thanks !