Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gymnasium support #40

Merged
merged 5 commits into from
Feb 7, 2024
Merged

Gymnasium support #40

merged 5 commits into from
Feb 7, 2024

Conversation

Alex2782
Copy link
Contributor

The render() function is not yet compatible with SB3 + Gymnasium. (DLR-RM/stable-baselines3#1327)

Training works with: A2C, PPO, RecurrentPPO, TRPO (added gym-mtsim/examples/train_SB3_gymnasium.py)

Random actions vs. SB3 - Agents x [50K, 250K, 500K] learning_timesteps

random_sb3_agents

@AminHP
Copy link
Owner

AminHP commented Aug 27, 2023

Hi @Alex2782 , is this PR ready to review?

@Alex2782
Copy link
Contributor Author

The render function is not yet compatible with SB3 + Gymnasium.
I don't know how. advanced_figure does not fit into the concept of gymnasium.

train_SB3_gymnasium.py (without render) shows only the training result at the end
image

@Alex2782
Copy link
Contributor Author

@AminHP
image

5 months ago I already changed your 'README.ipynb'. I find 'Jupyter Notebook' terrible. I can no longer track what I have customized and what changes are still needed.

@AminHP
Copy link
Owner

AminHP commented Aug 27, 2023

You could use VSCode for editing the notebooks. It highlights the changes and provides some other useful features.

@AminHP
Copy link
Owner

AminHP commented Aug 27, 2023

I'll check the render function.

@Alex2782
Copy link
Contributor Author

Alex2782 commented Aug 27, 2023

i just added new comment here #new line, VS code changes the whole file after save

image

my VS Code Extensions
image

@Alex2782
Copy link
Contributor Author

Alex2782 commented Aug 27, 2023

ok the changes look like this, but you have to scroll a lot and look closely, is not so well solved.

image

The arrows to navigate through the changes do not work either
image

@AminHP
Copy link
Owner

AminHP commented Aug 27, 2023

How about the Github changes page here?

@Alex2782
Copy link
Contributor Author

it is enough only if ipython is executed, then already very much changes at the structure.
image

I have found my changes, but you have to search longer than usual, some changes are not comprehensible.
image

@AminHP
Copy link
Owner

AminHP commented Aug 28, 2023

It seems that Gymnasium doesn't allow passing additional args to the render method. I managed to fix it by the below code, however, I don't know if it is a correct solution based on Gymnasium rules.

import numpy as np
import random
import torch
from stable_baselines3 import A2C

import gymnasium as gym
from gym_mtsim import (
    Timeframe, SymbolInfo,
    MtSimulator, OrderType, Order, SymbolNotFound, OrderNotFound,
    MtEnv,
    FOREX_DATA_PATH, STOCKS_DATA_PATH, CRYPTO_DATA_PATH, MIXED_DATA_PATH,
)


env_name = 'forex-hedge-v0'
env = gym.make(env_name)

# reproduce training and test
seed = 42
env.reset(seed=seed)
torch.manual_seed(seed)
random.seed(seed)
np.random.seed(seed)

model = A2C('MultiInputPolicy', env, verbose=0)
model.learn(total_timesteps=10000)


observation, info = env.reset(seed=seed)

while True:
    action, _states = model.predict(observation)
    observation, reward, terminated, truncated, info = env.step(action)
    done = terminated or truncated

    if done:
        break

env.unwrapped.render('advanced_figure', time_format='%Y-%m-%d')

@Alex2782
Copy link
Contributor Author

I think that is okay

https://gymnasium.farama.org/api/env/#gymnasium.Env.render
Compute the render frames is not possible in project mtsim -> advanced_figure

@AminHP
Copy link
Owner

AminHP commented Sep 1, 2023

Great! Let's continue with this then. Thanks @Alex2782 for checking the docs.

@AminHP
Copy link
Owner

AminHP commented Sep 16, 2023

Hi @Alex2782
When do you think you will have the time to finish this PR?

@Alex2782
Copy link
Contributor Author

Hi @AminHP,

which changes exactly are still necessary? only adjust the examples?

@AminHP
Copy link
Owner

AminHP commented Sep 16, 2023

Yes, the examples should probably use the changes we discussed in the above code. It is also better to have them in ipynb format, like the one in anytrading.

- env.unwrapped.render
@Alex2782
Copy link
Contributor Author

Alex2782 commented Sep 17, 2023

Hi @AminHP,

I have checked and adjusted your file 'README.ipynb'.

image

Note: without the first change this error comes, it may also be due to my python or numpy version.
image

@Alex2782
Copy link
Contributor Author

Alex2782 commented Sep 17, 2023

I think the documentation would also have to be adapted, for example there is still the file README.md. Currently I have unfortunately no time to check it more closely.

My example train_SB3_gymnasium.py does not use 'render()'. In the 'matplotlib' output only one model comparison is shown: random actions, A2C and PPO.

@AminHP
Copy link
Owner

AminHP commented Sep 18, 2023

Thanks for the changes. I will update and revise the docs later.

The last change is providing the examples in ipynb format.

@AminHP
Copy link
Owner

AminHP commented Sep 29, 2023

Any updates @Alex2782 ?

@Alex2782
Copy link
Contributor Author

Should I remove the file 'train_SB3_gymnasium.py'? I think the 'README.ipynb' is enough.
Currently I have no time to convert my example to ipynb.

@AminHP
Copy link
Owner

AminHP commented Oct 2, 2023

No, there is no need to remove the example. I will try to change it to ipynb version. The example is beneficial as it compares A2C and PPO.

@Alex2782
Copy link
Contributor Author

Alex2782 commented Oct 2, 2023

@AminHP
https://github.com/AminHP/gym-anytrading/blob/master/examples/SB3_a2c_ppo.ipynb

I had 'train_SB3_gymnasium.py' also in the project 'gym-anytrading' to test 'gymnasium' support.

@Snoup97
Copy link

Snoup97 commented Jan 19, 2024

hello ✋🏿 any updates on the merge of this feature?

@AminHP AminHP merged commit 5efc3af into AminHP:main Feb 7, 2024
@AminHP
Copy link
Owner

AminHP commented Feb 7, 2024

I have finally fixed the minor issues, updated the examples, and merged the PR :))
Thanks a lot @Alex2782 for your contribution.

@Snoup97
Copy link

Snoup97 commented Feb 9, 2024

thanks guys, appreciate the effort 👍🏿

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants