-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ReinforcementLearningAgent.py
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import gym | ||
import numpy as np | ||
from stable_baselines3 import PPO | ||
|
||
class ReinforcementLearningAgent: | ||
def __init__(self, env_name, model_path): | ||
self.env = gym.make(env_name) | ||
self.model = PPO.load(model_path) | ||
|
||
def train(self, num_episodes): | ||
for episode in range(num_episodes): | ||
obs = self.env.reset() | ||
done = False | ||
rewards = 0 | ||
while not done: | ||
action, _ = self.model.predict(obs) | ||
obs, reward, done, _ = self.env.step(action) | ||
rewards += reward | ||
print(f"Episode {episode+1}, Reward: {rewards}") | ||
|
||
def test(self, num_episodes): | ||
for episode in range(num_episodes): | ||
obs = self.env.reset() | ||
done = False | ||
rewards = 0 | ||
while not done: | ||
action, _ = self.model.predict(obs) | ||
obs, reward, done, _ = self.env.step(action) | ||
rewards += reward | ||
print(f"Episode {episode+1}, Reward: {rewards}") |