-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
150 lines (110 loc) · 5.1 KB
/
solver.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
import math
import time
import random
from threading import Event, Thread
import numpy as np
import tensorflow.math as tfm
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
import cube
class Solver() :
def __init__(self, cube) :
self.cube = cube
self.cancelEvent = Event()
self.thread = None
self._createModel()
def _run(self) :
self.cancelEvent.clear()
eps = 1.0
infos = np.array([])
for epi in range(0, 170, 10) :
if epi % 10 == 0 : eps = 1.0
win = 0
step = 0
while win<45 :
# for step in range(0, 200) :
self.cube.reset()
# self.cube.shuffleCube()
while self.cube.isSolved() :
self.cube.shuffle(epi//10+1)
self.cube.normalize()
state = self.cube.save()
stepInfos = np.array([])
for move in range(0, epi//10+10) :
if (self.cancelEvent.is_set()) : return
action = self.pickAction(state, eps)
self.cube.rotate(action//2, action%2)
self.cube.normalize()
reward = 1. if self.cube.isSolved() else 0.
next_state = self.cube.save()
action_array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
action_array[action] = 1
info = { "state": state, "action": action_array, "reward": [0], "next_state": next_state }
stepInfos = np.append(stepInfos, [info])
index = random.randint(0, infos.size)
if index == infos.size :
infos = np.append(infos, [info])
else :
infos = np.insert(infos, index, [info])
state = next_state
if reward > 0. : break
if (reward==0.) : reward = -1.
else : win += 1
for i,info in enumerate(stepInfos) :
info["reward"] = [ reward ]
# info["reward"] = [ (i+1)*reward/stepInfos.size ]
eps = max(0.1, eps*0.999)
if (step+1) % 50 == 0 :
infos = infos[0:1000]
print("epi:{1};\tstep:{2};\twin:{3};\tsize:{4};\teps:{0};\t".format(eps, epi, step, win, infos.size))
win = 0
self.trainModel(infos)
step += 1
def pickAction(self, state, eps) :
if random.random()<eps :
return random.randint(0, 11)
else :
with self.graph.as_default():
# state = tf.constant(state, shape=[1, 24])
state = np.reshape(state, (1,24))
label = self.model.predict(state, steps=1)
return np.argmax(label)
def trainModel(self, infos) :
with self.graph.as_default():
states = tf.constant([i["state"].tolist() for i in infos], shape=[infos.size, 24])
rewards = tf.constant([i["reward"] for i in infos], dtype=tf.float32, shape=[infos.size, 1])
# next_states = tf.constant([i["next_state"].tolist() for i in infos], shape=[infos.size, 24])
actions = tf.constant([i["action"] for i in infos], dtype=tf.float32, shape=[infos.size, 12])
# Qtargets = tf.constant(self.model.predict(next_states, steps=1), shape=[infos.size, 12])
# Recupere l'etat actuel
targets = tf.constant(self.model.predict(states, steps=1), shape=[infos.size, 12])
# Calcure le mask negatif
mask = tf.ones([infos.size, 12], dtype=tf.float32)
mask = tfm.subtract(mask, actions)
# Applique le mask négatif
targets = tfm.multiply(targets, mask)
# Calcure le mask positif
mask = tfm.multiply(rewards, actions)
# Applique le mask positif
targets = tfm.add(targets, mask)
self.model.fit(states, targets, steps_per_epoch = 200) # 1000
def start(self) :
# self._run()
if self.thread is not None and self.thread.is_alive() : return
self.thread = Thread(target=self._run)
self.thread.start()
def stop(self) :
if self.thread is None or not self.thread.is_alive() : return
self.cancelEvent.set()
self.thread.join()
def is_alive(self) :
return self.thread is not None and self.thread.is_alive()
def _createModel(self) :
self.graph = tf.get_default_graph()
self.model = Sequential([ \
Dense(48, activation=tf.keras.activations.relu, input_dim=24), \
Dense(12, activation=tf.keras.activations.linear) \
])
self.model.compile(optimizer = tf.train.AdamOptimizer(0.01), \
loss = tf.losses.mean_squared_error)