Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
use gymnasium instead of gym
Browse files Browse the repository at this point in the history
  • Loading branch information
rageSpin committed Feb 12, 2023
1 parent b3a18d0 commit cc2f249
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions Reinforcement Learning/CartPole/cartpole-DQN.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import random
import gym
import gymnasium as gym
import numpy as np
from collections import deque
from keras.models import Sequential
Expand All @@ -19,7 +19,7 @@

EXPLORATION_MAX = 1.0
EXPLORATION_MIN = 0.01
EXPLORATION_DECAY = 0.995
EXPLORATION_DECAY = 0.


class DQNSolver:
Expand Down Expand Up @@ -59,7 +59,7 @@ def experience_replay(self):
q_values[0][action] = q_update
#print(state.shape, q_values.shape)
x[i], y[i] = state, q_values
self.model.fit(x, y, verbose=1)
self.model.fit(x, y, verbose=0)
self.exploration_rate *= EXPLORATION_DECAY
self.exploration_rate = max(EXPLORATION_MIN, self.exploration_rate)

Expand All @@ -73,14 +73,15 @@ def cartpole():
run = 0
while True:
run += 1
state = env.reset()
# print(env.reset())
state = env.reset()[0]
state = np.reshape(state, [1, observation_space])
step = 0
while True:
step += 1
#env.render()
action = dqn_solver.act(state)
state_next, reward, terminal, info = env.step(action)
state_next, reward, terminal, truncated, _ = env.step(action)
reward = reward if not terminal else -reward
state_next = np.reshape(state_next, [1, observation_space])
dqn_solver.remember(state, action, reward, state_next, terminal)
Expand Down

0 comments on commit cc2f249

Please sign in to comment.