-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExperimentLoop.py
163 lines (150 loc) · 7.91 KB
/
ExperimentLoop.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from Algos import Algos
import gymnasium as gym
from gymnasium.spaces import Box, Dict, Discrete
from gymnasium.utils.env_checker import check_env
import matplotlib.pyplot as plt
import numpy as np
import ML_Env
class ExperimentLoop:
def __init__(self, num_episodes = 1000, num_rows = 11, num_cols = 5, time_step = 1, episode_length = 300):
self.num_episodes = num_episodes
self.num_rows = num_rows
self.num_cols = num_cols
self.env = gym.make(
"ML_Env/ML_RL_Env-v0",
render_mode=None,
numRows=num_rows,
numCols=num_cols,
timeStep=time_step,
episodeLength=episode_length)
def clamp(self, num, min_value, max_value):
return max(min(num, max_value), min_value)
def SMA(self, data, step):
w = np.repeat(1, step) / step
result = np.convolve(w, data, mode="valid")
return result
def perform_random_simulation(self):
# Choose A from S using epsilon-greedy policy
action = (np.random.randint(0, self.num_rows), np.random.randint(0, self.num_cols))
totalCumRewards = np.array([])
episodeCumRewards = np.array([])
for index in range(self.num_episodes):
self.env.reset()
terminated = False
episodeRewards = np.array([])
while not terminated:
observation, reward, terminated, truncated, info = self.env.step(action)
episodeRewards = np.append(episodeRewards, float(reward))
# Randomly choose a location
action = (np.random.randint(0, self.num_rows), np.random.randint(0, self.num_cols))
if terminated or truncated:
episodeCumRewards = np.append(episodeCumRewards, (episodeRewards.sum(0)))
break
return episodeCumRewards
def perform_sarsa_simulation(self, alpha=1, gamma=0.9, epsilon=0.9):
algos = Algos(numRows=self.num_rows, numCols=self.num_cols, alpha=alpha, gamma=gamma, epsilon=epsilon)
# initialize S
state = (5, 3)
# Choose A from S using epsilon-greedy policy
action = algos.getNextAction(state, algos.epsilon)
totalCumRewards = np.array([])
episodeCumRewards = np.array([])
for index in range(self.num_episodes):
self.env.reset()
terminated = False
episodeRewards = np.array([])
algos.alpha = 1 - (float(index) / self.num_episodes)
while not terminated:
observation, reward, terminated, truncated, info = self.env.step(action)
episodeRewards = np.append(episodeRewards, float(reward))
state = tuple(observation.get("prevPos"))
state_2 = tuple(observation.get("position"))
# Choose A from S using epsilon-greedy policy
action_2 = algos.getNextAction(state_2, algos.epsilon)
algos.updateQTable_Sarsa(
state, action, state_2, action_2, reward, algos.epsilon
)
action = action_2
state = state_2
if terminated or truncated:
algos.q_table_sum_SarsaLearning = algos.updateQTableSum(
algos.q_table_sum_SarsaLearning
)
episodeCumRewards = np.append(episodeCumRewards, (episodeRewards.sum(0)))
break
return episodeCumRewards, algos.q_table_sum_SarsaLearning
def perform_qlearning_simulation(self, alpha=1, gamma=0.9, epsilon=0.9):
algos = Algos(numRows=self.num_rows, numCols=self.num_cols, alpha=alpha, gamma=gamma, epsilon=epsilon)
# initialize S
state = (5, 3)
# Choose A from S using epsilon-greedy policy
action = algos.getNextAction(state, algos.epsilon)
totalCumRewards = np.array([])
episodeCumRewards = np.array([])
for index in range(self.num_episodes):
self.env.reset()
terminated = False
episodeRewards = np.array([])
algos.alpha = 1 - (float(index) / self.num_episodes)
while not terminated:
observation, reward, terminated, truncated, info = self.env.step(action)
episodeRewards = np.append(episodeRewards, float(reward))
state = tuple(observation.get("prevPos"))
state_2 = tuple(observation.get("position"))
# Choose A from S using epsilon-greedy policy
action_2 = algos.getNextAction(state_2, algos.epsilon)
algos.updateQTable_QLearning(
state, action, state_2, action_2, reward, algos.epsilon
)
action = action_2
state = state_2
if terminated or truncated:
algos.q_table_sum_QLearning = algos.updateQTableSum(
algos.q_table_sum_QLearning
)
episodeCumRewards = np.append(episodeCumRewards, (episodeRewards.sum(0)))
break
return episodeCumRewards, algos.q_table_sum_QLearning
if __name__ == "__main__":
num_episodes = 5000
loop = ExperimentLoop(num_episodes, 11, 5, 1, 300)
random_total_rewards = loop.perform_random_simulation()
sarsa_total_rewards_09, sarsa_qtable_sum = loop.perform_sarsa_simulation(1, 0.9, 0.9)
sarsa_total_rewards_07, _ = loop.perform_sarsa_simulation(1, 0.9, 0.7)
sarsa_total_rewards_05, _ = loop.perform_sarsa_simulation(1, 0.9, 0.5)
qlearning_total_rewards_09, qlearning_qtable_sum = loop.perform_qlearning_simulation(1, 0.9, 0.9)
qlearning_total_rewards_07, _ = loop.perform_qlearning_simulation(1, 0.9, 0.7)
qlearning_total_rewards_05, _ = loop.perform_qlearning_simulation(1, 0.9, 0.5)
print("RandomTotalRewards", np.sum(random_total_rewards, 0))
print("SarsaTotalRewards \u03B5 = 0.9", np.sum(sarsa_total_rewards_09, 0))
print("SarsaTotalRewards \u03B5 = 0.5", np.sum(sarsa_total_rewards_05, 0))
print("QLearningTotalRewards \u03B5 = 0.9", np.sum(qlearning_total_rewards_09, 0))
print("QLearningTotalRewards \u03B5 = 0.5", np.sum(qlearning_total_rewards_05, 0))
plt.plot(loop.SMA(random_total_rewards, 100), label="SMA of Rewards - Random")
plt.plot(loop.SMA(sarsa_total_rewards_09, 100), label="SMA of Rewards - Sarsa")
plt.plot(loop.SMA(qlearning_total_rewards_09, 100), label="SMA of Rewards - QLearning")
font = {"weight": "bold"}
plt.rc("font", **font)
plt.xlabel("Episode")
plt.ylabel("Sum of Rewards for Each Episode")
plt.title("SMA of Sum of Rewards for Each Episode")
plt.legend()
plt.show()
plt.plot(loop.SMA(random_total_rewards, 100), label="SMA of Rewards - Random")
plt.plot(loop.SMA(sarsa_total_rewards_09, 100), label="SMA of Rewards - Sarsa \u03B5 = 0.9")
plt.plot(loop.SMA(sarsa_total_rewards_07, 100), label="SMA of Rewards - Sarsa \u03B5 = 0.7")
plt.plot(loop.SMA(sarsa_total_rewards_05, 100), label="SMA of Rewards - Sarsa \u03B5 = 0.5")
plt.plot(loop.SMA(qlearning_total_rewards_09, 100), label="SMA of Rewards - QLearning \u03B5 = 0.9")
plt.plot(loop.SMA(qlearning_total_rewards_07, 100), label="SMA of Rewards - QLearning \u03B5 = 0.7")
plt.plot(loop.SMA(qlearning_total_rewards_05, 100),label="SMA of Rewards - QLearning \u03B5 = 0.5")
font = {"weight": "bold"}
plt.rc("font", **font)
plt.xlabel("Episode")
plt.ylabel("Sum of Rewards for Each Episode")
plt.title("SMA of Sum of Rewards for Each Episode with Varying \u03B5")
plt.legend()
plt.show()
algos = Algos(numRows=11, numCols=5, alpha=1, gamma=0.9, epsilon=0.9)
qTableDivide = np.full((algos.rows, algos.cols), algos.rows * algos.cols * num_episodes)
algos.Average_And_Visualize_QTable(sarsa_qtable_sum, qTableDivide, "QTable of Sarsa-Learning")
algos.Average_And_Visualize_QTable(qlearning_qtable_sum, qTableDivide, "QTable of Q-Learning")