-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_dqn.py
119 lines (92 loc) · 3.45 KB
/
train_dqn.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
import os
import sys
import numpy as np
import torch
from PyQt5.QtWidgets import QApplication
from matplotlib import pyplot as plt
from tqdm import tqdm
from agent.dqn import DQNAgent
from env.maze_env import MazeEnv, MainWindow
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
def main():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
maze_array = np.array([
[0, 8, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 9, 0]
])
env = MazeEnv(maze_array)
state_size = maze_array.size
action_size = 4
agent = DQNAgent(state_size, action_size, batch_size=64, gamma=0.99, epsilon=1.0, eps_decay=0.995, eps_min=0.01,
target_update=10, memory_capacity=10000, device=device)
num_episodes = 100000
rewards = []
for i_episode in tqdm(range(num_episodes), desc="Training Progress"):
state = env.reset().flatten()
state = torch.tensor(state, dtype=torch.float32).unsqueeze(0).to(device)
total_reward = 0
for t in range(100):
action = agent.select_action(state)
next_state, reward, done, _ = env.step(action.item())
next_state = torch.tensor(next_state.flatten(), dtype=torch.float32).unsqueeze(0).to(device)
reward = torch.tensor([reward], dtype=torch.float32).to(device)
total_reward += reward.item()
agent.memory.push(state, action, next_state, reward)
state = next_state
agent.optimize_model()
if done:
break
rewards.append(total_reward)
if i_episode % 1000 == 0:
print(
f"Episode {i_episode}/{num_episodes} complete - Total Reward: {total_reward}, Epsilon: {agent.epsilon}")
if rewards:
plt.plot(rewards)
plt.xlabel('Episode')
plt.ylabel('Total Reward')
plt.title('Training Rewards Over Time')
plt.savefig('training_rewards.png')
# plt.show()
print("Training complete")
plt.plot(rewards)
plt.xlabel('Episode')
plt.ylabel('Total Reward')
plt.title('Training Rewards Over Time')
plt.savefig('training_rewards.png')
plt.show()
agent.save("dqn_model.pth")
app = QApplication(sys.argv)
main_window = MainWindow(env)
sys.exit(app.exec_())
def test_model():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
maze_array = np.array([
[0, 8, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 9, 0]
])
env = MazeEnv(maze_array)
app = QApplication(sys.argv)
main_window = MainWindow(env, isTest=True)
main_window.test()
sys.exit(app.exec_())
if __name__ == '__main__':
# main()
test_model()