question

Felix Möhlmann avatar image
0 Likes"
Felix Möhlmann asked mark zhen commented

Issue with observation space in Reinforcement Learning

I recently installed a new python version. When I tested a modified version of the Reinforcement Learning demo model I encountered a problem when trying to use MultiDiscrete or Box observation/action spaces.

When I try to run the flexsim_training.py I get the following error when using MultiDiscrete as the observation space.

capture1.png

And similarly for a Box space

capture2.png

Something seems to be going wrong with the value conversion. When I set Visual Studio Code to use the old inpreter (3.7.8 instead of 3.11.3) everything works.

1682345360799.png

Does anyone have an idea what I might have forgotten/done wrong when setting up the new version that could lead to this. (I just installed gym and stable_baselines3 again. Since the Discrete space worked I assumed it was successful)

The python scripts are the unaltered (apart from the file paths) versions available from the link in the tutorial.

RL_Demo_2.fsm

FlexSim 23.1.1
reinforcement learningpythonobservation space
capture1.png (32.4 KiB)
capture2.png (1.5 KiB)
1682345360799.png (8.5 KiB)
rl-demo-2.fsm (67.8 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Phil BoBo avatar image
3 Likes"
Phil BoBo answered Abhishek K commented

The provided python scripts are example code to demonstrate how to communicate with FlexSim from a reinforcement learning algorithm. If they don't work perfectly with a particular version of python or another language, library, package, or implementation, then you should customize them according to the needs of your project. As explained in the first paragraph of Getting Started with OpenAI Gym (flexsim.com).

The observation space and the observation value are both set within the example code. If there's a mismatch, then change the code so that it isn't mismatched.

The function _convert_to_gym_space defined on line 178 creates the observation space. The function _convert_to_observation defined on line 196 returns the observation. (https://github.com/flexsim/FlexSimAI/blob/main/gym/flexsim_env.py)

Based on your error message, in the version of python and gym that you are using, you are creating an int64 observation space and recording an int32 observation. Adjust one or the other so that the observed value's type matches what the specified observation space expects.

Alter the example python script to work if it isn't working. That's why the script is provided.

· 4
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Felix Möhlmann avatar image Felix Möhlmann commented ·

Thank you for the pointers.

In case it might be useful to anyone in the future: Specifying the datatype of the MultiDiscrete space in _convert_to_gym_space of flexsim_env worked for me.

return gym .spaces.MultiDiscrete( params , dtype = np . int32 )
3 Likes 3 ·
Abhishek K avatar image Abhishek K Felix Möhlmann commented ·

Thanks a lot for posting this, that worked for training the model.

Did you have issues using flexsim_inference.py as well ?
I am getting this error message when running flexsim_inference.py

ValueError: Error: Unexpected observation shape () for MultiDiscrete environment, please use (6,) or (n_env, 6) for the observation shape.

Please let me know if you ran into the same issue and how it could be fixed. I tried editing the python script flexsim_inference.py but couldn't get success and I'm not very familiar with coding in python. Basically the observation space has 6 entries which is the last type, followed by 5 discrete values showing how many items of each type are in the queue (same example problem from tutorial), thus making it a MultiDiscrete one. I'm wondering how the flexsim_inference.py needs to be modified to use that observation space to define action for the processor as to which type it should pull next (action)

1 Like 1 ·
Felix Möhlmann avatar image Felix Möhlmann Abhishek K commented ·
No, sorry, the inference script worked without problems for me. My only guess would be that you maybe forgot to adjust the file path and are trying to run an agent that was trained on a different set of observations?
0 Likes 0 ·
Show more comments
Jeanette F avatar image
0 Likes"
Jeanette F answered mark zhen commented

Hello @Felix Möhlmann,

FlexSim does not support python version 3.11. You can try and build for a different python version as directed below. You can find this information in the manual as well.

1682960820395.png


1682960820395.png (90.3 KiB)
· 3
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Felix Möhlmann avatar image Felix Möhlmann commented ·

Thank you for your answer. I did forget to set the version in the preferences and was not aware that Python 3.11 is not supported.

However I still get the same error message when using version 3.10. Do you have an idea what else I might try to get it working?

1683008732957.png

1683008743386.png

1683008753530.png

0 Likes 0 ·
1683008732957.png (9.6 KiB)
1683008743386.png (6.5 KiB)
1683008753530.png (7.1 KiB)
mark zhen avatar image mark zhen Felix Möhlmann commented ·

Have you considered downgrading the stable baseline version

0 Likes 0 ·
Phil BoBo avatar image Phil BoBo ♦♦ commented ·
@Jeanette F The Reinforcement Learning example scripts do not use FlexSim's external code feature. They communicate with FlexSim via sockets. FlexSim's internal support for particular python versions is irrelevant to this question.
0 Likes 0 ·