question

Felipe Capalbo avatar image
0 Likes"
Felipe Capalbo asked Jason Lightfoot commented

Error when local server is created for Reinforced Learning

I tried to create a reinforcement learning model to optimize a layout by changing the object's position on the X and Y axes. The observation field references the object's position in the tree, as does the action field, but this can change its value.

When I try to implement the trained model, this error shows up:

"Exception occurred during processing of request from ('127.0.0.1', 50682)
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\socketserver.py", line 317, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\socketserver.py", line 348, in process_request
    self.finish_request(request, client_address)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\socketserver.py", line 361, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\socketserver.py", line 755, in __init__
    self.handle()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\http\server.py", line 436, in handle
    self.handle_one_request()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\http\server.py", line 424, in handle_one_request
    method()
  File "c:\Users\GAIVOTA_FLEXSIM\Documents\FLEXSIM FELIPE CAPALBO\ESTUDOS ML\Exercicio ML Layout\ArquivosPY\flexsim_inference.py", line 11, in do_GET
    self._handle_reply(params)
  File "c:\Users\GAIVOTA_FLEXSIM\Documents\FLEXSIM FELIPE CAPALBO\ESTUDOS ML\Exercicio ML Layout\ArquivosPY\flexsim_inference.py", line 30, in _handle_reply
    action, _states = FlexSimInferenceServer.model.predict(observation)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\GAIVOTA_FLEXSIM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\stable_baselines3\common\base_class.py", line 553, in predict
    return self.policy.predict(observation, state, episode_start, deterministic)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\GAIVOTA_FLEXSIM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\stable_baselines3\common\policies.py", line 363, in predict
    obs_tensor, vectorized_env = self.obs_to_tensor(observation)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\GAIVOTA_FLEXSIM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\stable_baselines3\common\policies.py", line 270, in obs_to_tensor
    vectorized_env = is_vectorized_observation(observation, self.observation_space)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\GAIVOTA_FLEXSIM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\stable_baselines3\common\utils.py", line 399, in is_vectorized_observation
    return is_vec_obs_func(observation, observation_space)  # type: ignore[operator]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\GAIVOTA_FLEXSIM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\stable_baselines3\common\utils.py", line 307, in is_vectorized_multidiscrete_observation
    raise ValueError(
ValueError: Error: Unexpected observation shape () for MultiDiscrete environment, please use (22,) or (n_env, 22) for the observation shape."

ExercicioLayout.fsm

flexsim_env.py

flexsim_inference.py

flexsim_training.py

TreinamentoExercicioLayout.zip

FlexSim 23.2.2
reinforcement learninglocalhost
· 16
5 |100000

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

Kavika F avatar image Kavika F ♦ commented ·

Hey @Felipe Capalbo, the error hints that the observation you pass into

action, _states = FlexSimInferenceServer.model.predict(observation)

isn't presented in the right format. The observation space is requesting a (22,) or (n_env, 22) shape. Can you see what kind of shape is getting passed in before the error?

0 Likes 0 ·
Felipe Capalbo avatar image Felipe Capalbo Kavika F ♦ commented ·

What does this "shape" means? The observations has 22 parameter, as does the actions.
1701539978661.png
All of them are discrete values.
In the Reinforcement Learning window it is inputed as multidiscrete, because of the 22 parameters.
1701540086017.png

0 Likes 0 ·
1701539978661.png (15.0 KiB)
1701540086017.png (37.4 KiB)
Kavika F avatar image Kavika F ♦ Felipe Capalbo commented ·

@Felipe Capalbo, here's documentation on the numpy.ndarray.shape property. The example in the documentation shows the following:

>>> x = np.array([1, 2, 3, 4])
>>> x.shape
(4,)

With an array size of 4, x.shape returns (4,). This format matches what the above error is looking for - a shape of (22,). What it says you're providing is (), which seems to be an empty array or just no array at all.

I'm able to train your model, but I'm not sure how you've setup the flexsim-interface.py > main() to work. I can't help you further unless you give me detailed steps on how to send input or results from the RL model to the server it sets up.

0 Likes 0 ·
Show more comments
Jason Lightfoot avatar image Jason Lightfoot ♦♦ commented ·

Hi @Felipe Capalbo,

We haven't heard back from you. Were you able to solve your problem? If so, please add and accept an answer to let others know the solution. Or please respond to the previous comment so that we can continue to help you.

If we don't hear back in the next 3 business days, we'll assume you were able to solve your problem and we'll close this case in our tracker. You can always comment back at any time to reopen your question, or you can contact your local FlexSim distributor for phone or email help.

0 Likes 0 ·
Felipe Capalbo avatar image Felipe Capalbo Jason Lightfoot ♦♦ commented ·
Couldn't figure out yet, but I thing the concept used to define the observations could be wrong.
0 Likes 0 ·
Phil BoBo avatar image Phil BoBo ♦♦ Felipe Capalbo commented ·

The example is training an AI that takes 1 discrete observation value and makes 1 discrete action value. You've changed your model to take multiple values for the observation, not just 1 value.

So passing just a 3 as an observation doesn't make sense. You need to pass 22 numbers.

That step in the tutorial expects that you were following the rest of the tutorial up to that point, not changing it. Since you are doing something different, the tutorial isn't going to match up exactly the same. Just skip that step (which is just testing if your web service is responding to queries) and move on to the next step.

0 Likes 0 ·
Show more comments
Jason Lightfoot avatar image Jason Lightfoot ♦♦ commented ·

Hi @Felipe Capalbo,

We haven't heard back from you. Were you able to solve your problem? If so, please add and accept an answer to let others know the solution. Or please respond to the previous comment so that we can continue to help you.

If we don't hear back in the next 3 business days, we'll assume you were able to solve your problem and we'll close this case in our tracker. You can always comment back at any time to reopen your question, or you can contact your local FlexSim distributor for phone or email help.

0 Likes 0 ·

0 Answers