-
Notifications
You must be signed in to change notification settings - Fork 0
/
autodidactic_decode_p.py
136 lines (85 loc) · 3.52 KB
/
autodidactic_decode_p.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
import tensorflow.keras.backend as K
from tensorflow.keras.layers import Dense, Input, LeakyReLU
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from utils import gen_sample, action_map, perc_solved_cube
import numpy as np
from utils import action_map_small, gen_sequence, get_all_possible_actions_cube_small, chunker, \
flatten_1d_b
def acc(y_true, y_pred):
return K.cast(K.equal(K.max(y_true, axis=-1),
K.cast(K.argmax(y_pred, axis=-1), K.floatx())),
K.floatx())
def get_model(lr=0.0001):
input1 = Input((324,))
d1 = Dense(1024)
d2 = Dense(1024)
d3 = Dense(1024)
d4 = Dense(50)
x1 = d1(input1)
x1 = LeakyReLU()(x1)
x1 = d2(x1)
x1 = LeakyReLU()(x1)
x1 = d3(x1)
x1 = LeakyReLU()(x1)
x1 = d4(x1)
x1 = LeakyReLU()(x1)
out_value = Dense(1, activation="linear", name="value")(x1)
out_policy = Dense(len(action_map_small), activation="softmax", name="policy")(x1)
model = Model(input1, [out_value, out_policy])
model.compile(loss={"value": "mae", "policy": "sparse_categorical_crossentropy"}, optimizer=Adam(lr),
metrics={"policy": acc})
#model.summary()
return model
if __name__ == "__main__":
file_path = "auto.h5"
model = get_model()
model.load_weights(file_path)
#generate 1 sample
sample_X, sample_Y, cubes = gen_sample(4)
cube = cubes[0]
cube.score = 0
list_sequences = [[cube]]
existing_cubes = set()
#print(list_sequences)
preview_cube = cube
#show cube before solving
print([preview_cube])
print("start solve......")
for j in range(50):
print("step: {}".format(j + 1))
X = [flatten_1d_b(x[-1]) for x in list_sequences]
value, policy = model.predict(np.array(X), batch_size=1024)
new_list_sequences = []
for x, policy in zip(list_sequences, policy):
new_sequences = [x + [x[-1].copy()(action)] for action in action_map]
pred = np.argsort(policy)
take_action = list(action_map.keys())[pred[-1]]
print("take action : ", take_action)
#print(list(action_map.keys())[pred[-2]])
cube_1 = x[-1].copy()(list(action_map.keys())[pred[-1]])
#cube_2 = x[-1].copy()(list(action_map.keys())[pred[-2]])
new_list_sequences.append(x + [cube_1])
#new_list_sequences.append(x + [cube_2])
#print("new_list_sequences", len(new_list_sequences))
last_states_flat = [flatten_1d_b(x[-1]) for x in new_list_sequences]
value, _ = model.predict(np.array(last_states_flat), batch_size=1024)
value = value.ravel().tolist()
for x, v in zip(new_list_sequences, value):
x[-1].score = v if str(x[-1]) not in existing_cubes else -1
new_list_sequences.sort(key=lambda x: x[-1].score , reverse=True)
new_list_sequences = new_list_sequences[:100]
existing_cubes.update(set([str(x[-1]) for x in new_list_sequences]))
list_sequences = new_list_sequences
list_sequences.sort(key=lambda x: perc_solved_cube(x[-1]), reverse=True)
#print(list_sequences[0])
preview_cube(take_action)
print([preview_cube])
prec = perc_solved_cube((list_sequences[0][-1]))
#print(prec)
if prec == 1:
break
#print(list_sequences[0])
#print(perc_solved_cube(list_sequences[0][-1]))
print("final: \n", [list_sequences[0][-1]])
#print(list_sequences[0])