-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLastMoveSmallBatchNN.py
75 lines (68 loc) · 2.18 KB
/
LastMoveSmallBatchNN.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
import random, gym
from math import *
from sklearn.svm import SVC
rewardSum = 0
num = 0
average = 0
X = []
y = []
agent = SVC(gamma=0.0001, max_iter=10)
#There need to be 2 classes in the inputs to fit
class1 = False
class0 = False
env = gym.make('CartPole-v0')
for i_episode in range(200):
observation = env.reset()
exX = 0
exY = 0
tempX = []
tempY = []
totalReward = 0
for t in range(250):
#Render
env.render()
#Action
if not(class1 and class0):
action = env.action_space.sample()
else:
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])])
exX = [float(observation[0]), float(observation[1]), float(observation[2]), float(observation[3])]
if action == 1:
exY = 1
tempY.append(1)
else:
exY = 0
tempY.append(0)
#Observe
observation, reward, done, info = env.step(action)
totalReward += reward
if done:
#Update NN
print(totalReward)
rewardSum += totalReward
num += 1
average = (rewardSum+totalReward)/num
diff = totalReward-average
relativeDiff = diff/average
print("average = "+str(average))
if relativeDiff > 0.75:
print("BIg step:" +str(average))
for weight in range(int(10*relativeDiff)):
for newX in tempX:
X.append(newX)
for newY in tempY:
y.append(newY)
#X.append(exX)
exY = abs(exY-1)
#y.append(exY)
if not(class1 and class0):
for classnum in y:
if classnum == 0:
class0 = True
else:
class1 = True
if class1 and class0:
agent = SVC(gamma=0.0001, max_iter=10)
agent.fit(X, y)
break