-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenvs.py
65 lines (46 loc) · 1.7 KB
/
envs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from collections import deque
import gymnasium as gym
class FrozenLakeMod(gym.Wrapper):
num_steps = 0
rew = None
def __init__(self, env, rew=[0,0,1]):
super().__init__(env)
self.rew = rew
def step(self, action):
self.num_steps += 1
obs, reward, terminated, truncated, info = self.env.step(action)
if terminated:
#reward = 100 if reward == 1 else -100
reward = self.rew[2] if reward == 1 else self.rew[1]
else:
#reward = -10
reward = self.rew[0]
return obs, reward, terminated, truncated, info
def plot_v_history(V_history, targets=None):
import numpy as np
import matplotlib.pyplot as plt
history = {}
num_episodes = len(V_history)
for k in range(num_episodes):
V = V_history[k]
for s in V.keys():
if s not in history.keys():
history[s] = np.zeros(num_episodes)
history[s][k] = V[s]
for s in sorted(list(history.keys())):
plt.plot(history[s], label=s)
if targets is not None:
plt.axhline(y=targets[s], linewidth=1, linestyle='--')
plt.legend(bbox_to_anchor=(1,1))
plt.show()
class MtnCarContMod(gym.Wrapper):
def __init__(self, env, rew_struct):
super().__init__(env)
self.positions = deque([], maxlen=20)
self.rew_struct = rew_struct
def step(self, action):
#self.num_steps += 1
state, reward, terminated, truncated, info = self.env.step(action)
self.positions.append(state[0])
reward = self.rew_struct(state)
return state, reward, terminated, truncated, info