0

I'm currently struggling with the following problem:

My folder structer looks like:

master
 - resources
   customFile.fmu
fileCallingFMU.py

While executing the fileCallingFMU.py I pass a path string like

path = "./resources/customFile.fmu"

My script contains a super function where I pass the path variable. But everytime I execute the script it trips with an exception:

Exception has occurred: FileNotFoundError
[Errno 2] No such file or directory: b'2021-11-16_./resources/customFile.fmu.txt'
  File "[projectFolder]\fileCallingFMU.py", line 219, in __init__
    super().__init__(path, config, log_level)
  File "[projectFolder]\fileCallingFMU.py", line 86, in <module>
    env = gym.make(env_name)

My urging question now is the following:

Why and how does python manipulate the path variable with a date-prefix and .txt as file extension?!

Hope anyone can enlighten me on this one...

EDIT

I'm trying to get the example of ModelicaGym running.

My fileCallingFMU.py contains the following code:

path = "./resources/customFile.fmu"
    env_entry_point = 'cart_pole_env:JModelicaCSCartPoleEnv'

    config = {
        'path': path,
        'm_cart': m_cart,
        'm_pole': m_pole,
        'theta_0': theta_0,
        'theta_dot_0': theta_dot_0,
        'time_step': time_step,
        'positive_reward': positive_reward,
        'negative_reward': negative_reward,
        'force': force,
        'log_level': log_level
    }

    from gym.envs.registration import register
    env_name = env_name

    register(
        id=env_name,
        entry_point=env_entry_point,
        kwargs=config
    )

env = gym.make(env_name)

The full code for the entryPoint can be found here.

Viktor Katzy
  • 162
  • 10
  • 2
    It's not base Python that manipulates the path variable. Rather, it looks like something that ModelicaGym does in the [`get_log_file_name()`](https://github.com/ucuapps/modelicagym/blob/5cde90bcacb0d3d10179b63dd868426558f217bf/modelicagym/environment/modelica_base_env.py#L176) method of the `ModelicaBaseEnv` class. Looks like that method runs when that class is somehow initialized. – jjramsey Nov 16 '21 at 13:19
  • That's the hint that saved my day! – Viktor Katzy Nov 17 '21 at 08:48

1 Answers1

1

As jjramsey pointed out the problem was burried within the ModelicaGym library.

The logger could not create a propper log file, because the model name was not properly stored in the self.model variable.

Source of this error lied in the line

self.model_name = model_path.split(os.path.sep)[-1]

due to the fact that the os library was not able to separate my path string

"./resources/customFile.fmu"

After changing it to

".\\resources\\customFile.fmu"

everythig works as expected.

Thanks again!

Viktor Katzy
  • 162
  • 10
  • 2
    FYI, for a platform-independent approach, I would suggest importing the `os.path` module and writing your file path as `os.path.join("resources", "customFile.fmu")`. That path should end up with backslashes on Windows, and forward slashes on other platforms, should you need them. – jjramsey Nov 17 '21 at 13:21
  • Thanks again for the advice! I'll implement it that way :) – Viktor Katzy Nov 19 '21 at 06:32