2

Connecting Energyplus simulation with OpenAI Gym Environment through Python API

I'm trying to implement OpenAI Gym environment (for reinforcement learning training) with EnergyPlus building environment. I recently made an effort to achieve this by connecting Python with EnergyPlus through Python API. Currently, my understanding is that E+ simulation is run with few lines of code as below

ARGS = [
    '--weather',
    weather_file_path,
    '--output-directory',
    output_dir,
    '--readvars',
    idf_file_path
]

api = EnergyPlusAPI()
state = api.state_manager.new_state()
api.runtime.run_energyplus(state, ARGS)

and the only way I can interact with each timestep is through callback functions provided by Runtime API.

OpenAI Gym environment requires us to implement a "step" function in a class which will be repeatedly called during RL model training. A pseudo-code of what flow inside a step function with EnergyPlus Python API should look like is shown below

 def step(self, action)
    set_eplus_actuators_at_start_timestep(action)
    new_state = get_eplus_sensor_values_at_end_timestep(.....)
    reward = calculate_reward_or_penalty(new_state)
    return new_state, reward

However, since each action towards EnergyPlus running simulation can only be done through callback functions, the step function above cannot be achieved through typical single-process programming.

Any suggestion on how to solve this?

Andaman's avatar
21
Andaman
asked 2022-08-12 00:43:07 -0500
Aaron Boranian's avatar
14.1k
Aaron Boranian
updated 2022-08-12 10:41:40 -0500
edit flag offensive 0 remove flag close merge delete

Comments

I had trouble with this too, but never took the time to try and figure it out - I'm not sure it's possible since you have to pass callback functions to calling points before running the simulation. These callback functions aren't necessarily static since you can pass an object into it, and keep track of data and take actions thru that object.

I have 2 repos for RL (PyTorch) + EnergyPlus - they are a mess but maybe they'll help https://github.com/mechyai/RL-EmsPy https://github.com/mechyai/rl_bca

mechyai's avatar mechyai (2022-08-17 22:01:19 -0500) edit

This function https://github.com/mechyai/RL-EmsPy/b... decorates a standard callback function, which you can pass an 'observation' and 'actuation' method for a given calling point

mechyai's avatar mechyai (2022-08-17 22:02:19 -0500) edit

Thanks for your comments. I will definitely check your repos!

I'm currently trying to separate OpenAI Gym env from Eplus with multiprocessing. By using Pipe to pause and wait for input from each other, I might be able to sync the runtime between both processes.

Andaman's avatar Andaman (2022-08-23 22:27:24 -0500) edit
add a comment see more comments

1 Answer

1

You can find a working example in rllib-energyplus. It integrates EnergyPlus python API with OpenAI Gym (actually, Gymnasium, a fork by the original authors of Gym - Gym isn't maintained anymore) and Ray RLlib.

antoineg's avatar
198
antoineg
answered 2023-03-06 10:24:11 -0500
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments