-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrain.py
More file actions
50 lines (37 loc) · 1.08 KB
/
Train.py
File metadata and controls
50 lines (37 loc) · 1.08 KB
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
import numpy as np
import matplotlib.pyplot as plt
from Environment import Game
from Agent import Agent
env = Game()
## Not saving
nActions = env.nActions
nInputs = env.nInputs
brain = Agent(gamma=0.99, epsilon=0.95, lr=0.002, inputs=nInputs, nActions=5,memSize=1000000 , batchSize=32, epsilonDecrease=0.02)
episodes = 1
scores = []
for i in range(episodes):
done = False
state = env.reset()
score = 0
if i % 10 == 4:
brain.updateNetwork()
j = 0
while not done and j < 1500:
action = brain.choose(state)
newState, reward, done = env.step(action)
score += reward
if i % 5 == 0:
env.render()
brain.store((state, newState, reward, done, action))
brain.learn()
state = newState
j += 1
scores.append(score)
avgScore = np.mean(scores[-100:])
print("Episode: ", i, "\tScore: ", score, "\tAverage Score: ", avgScore, "\tEpsilon: ", brain.epsilon)
brain.updateEpsilon()
env.close()
brain.save('./car_model.pt')
x = [i + 1 for i in range(episodes)]
plt.plot(x, scores)
plt.show()