-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmallBatchNN.py
80 lines (69 loc) · 2.51 KB
/
SmallBatchNN.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
import random, gym
from math import *
from sklearn.neural_network import MLPClassifier, MLPRegressor
from sklearn.svm import SVC
five = 10
topFiveX = []
topFivey = []
topFiveRewards = []
smallestReward = 0
X = []
y = []
agent = SVC(gamma=0.001, max_iter=10)
#agent = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(10, 6, 2), random_state=1)
env = gym.make('CartPole-v0')
for i_episode in range(200):
observation = env.reset()
tempX = []
tempY = []
totalReward = 0
for t in range(250):
#Render
env.render()
#Action
if smallestReward < 30:
action = env.action_space.sample()
else:
#print(action)
action = agent.predict([[float(observation[0]), float(observation[1]), float(observation[2]), float(observation[3])]])[0]
tempX.append([float(observation[0]), float(observation[1]), float(observation[2]), float(observation[3])])
if action == 1:
tempY.append(1)
else:
tempY.append(0)
#Observe
observation, reward, done, info = env.step(action)
totalReward += reward
if done:
#Update NN
print(totalReward)
if len(topFiveRewards) < five:
for weight in range(int(totalReward)):
for temp in tempX:
X.append(temp)
for temp in tempY:
y.append(temp)
topFiveX.append(tempX)
topFivey.append(tempY)
topFiveRewards.append(totalReward)
smallestReward = min(topFiveRewards)
elif totalReward > smallestReward:
index = topFiveRewards.index(min(topFiveRewards))
topFiveRewards[index] = totalReward
topFiveX[index] = tempX
topFivey[index] = tempY
X = []
y = []
for i in range(len(topFiveX)):
for weight in range(int(topFiveRewards[i])):
for new in topFiveX[i]:
X.append(new)
for i in range(len(topFivey)):
for weight in range(int(topFiveRewards[i])):
for new in topFivey[i]:
y.append(new)
print(topFiveRewards)
agent = SVC(gamma=0.001, max_iter=10)
agent.fit(X, y)
smallestReward = min(topFiveRewards)
break