-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel_factories.py
284 lines (252 loc) · 16.5 KB
/
level_factories.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import hashlib
import random
from utils import Images
from models import LinearModel
from levels import *
class BaseLevelFactory(object):
def get_tensor(self, points):
return torch.tensor(points, dtype=torch.float)
def get_outputs(self, model, points):
return model(self.get_tensor(points)).view(-1).tolist()
def check_outputs(self, outputs, step_size):
for i, output in enumerate(outputs):
if (output+1e-4)%step_size > 1e-3:
print(i, output)
assert(False)
class StudyPlaneLevelFactory(BaseLevelFactory):
def get_points(self, target_model, points, step_size):
outputs = self.get_outputs(target_model, points)
self.check_outputs(outputs, step_size)
return [PointInfo(x, y, z) for (x, y), z in zip(points, outputs)]
def get_learning_rate_levels(self):
model = LinearModel(-0.5, 0.5, 0.0, False)
target_model = LinearModel(0.33, -0.33, 0.0, False)
points = [(-2.0, -2.0), (-2.0, 1.0), (-2.0, 2.0), (1.0, 2.0), (2.0, 0.0)]
step_size = 0.33
points = self.get_points(target_model, points, step_size)
learning_rate = 1.0
yield LearningRateLevel(learning_rate, model, points, step_size, ErrorType.SUM_LINEAR, 0.0001)
model = LinearModel(0.4, 0.4, 0.2)
target_model = LinearModel(-0.25, -0.5, 0.0)
points = [(-2, -2), (-2, 2), (2, -2), (2, 2)]
step_size = 0.5
points = self.get_points(target_model, points, step_size)
learning_rate = 0.5
yield LearningRateLevel(learning_rate, model, points, step_size, ErrorType.SUM_LINEAR, 0.5)
model = LinearModel(-0.5, 0.5, 0.5)
target_model = LinearModel(0.33, -0.33, 0.0)
points = [(-2.0, -2.0), (-2.0, 1.0), (-2.0, 2.0), (1.0, 2.0), (2.0, 0.0)]
step_size = 0.33
points = self.get_points(target_model, points, step_size)
learning_rate = 1.0
yield LearningRateLevel(learning_rate, model, points, step_size, ErrorType.SUM_LINEAR, 0.0001)
def get_study_levels(self):
model = LinearModel(0.1, 0.1, 0.5)
target_model = LinearModel(0.5, 0.5, -0.5)
points = [(1, 2), (-2, 1)]
step_size = 1.0
points = self.get_points(target_model, points, step_size)
yield StudyPlaneLevel(model, points, step_size, ErrorType.SUM_LINEAR, 0.5, 1, 3)
model = LinearModel(0.4, 0.4, 0.2)
target_model = LinearModel(-0.25, -0.5, 0.0)
points = [(-2, -2), (-2, 2), (2, -2), (2, 2)]
step_size = 0.5
points = self.get_points(target_model, points, step_size)
yield StudyPlaneLevel(model, points, step_size, ErrorType.SUM_LINEAR, 0.5, 2, 3)
model = LinearModel(-0.5, -0.5, 0.5)
target_model = LinearModel(1.0, 0.5, -1.0)
points = [(1, 2), (1, 0), (2, -2), (-2, 2), (1, -2), (-2, -2)]
step_size = 1.0
points = self.get_points(target_model, points, step_size)
yield StudyPlaneLevel(model, points, step_size, ErrorType.SUM_LINEAR, 2.0, 3, 3)
class MonstersLevelsFactory(BaseLevelFactory):
def get_monsters(self, target_model, points, step_size):
outputs = self.get_outputs(target_model, points)
self.check_outputs(outputs, step_size)
return [MonsterInfo(x, y, z, Images.DINO_MONSTER) for (x, y), z in zip(points, outputs)]
def get_learning_rate_levels(self):
model = LinearModel(-0.5, 0.50, 0.50)
target_model = LinearModel(0.5, -0.5, 0)
points = [(-2.0, -2.0), (-2.0, 1.0), (-2.0, 2.0), (1.0, 2.0), (2.0, 0.0)]
step_size = 0.5
points = self.get_monsters(target_model, points, step_size)
yield LearningRateMonstersLevel(model, points, step_size, ErrorType.SUM_LINEAR, 0.5).set_max_iterations(30).set_learning_rate(1.0)
model = LinearModel(-0.2, 0.6, 0.4)
target_model = LinearModel(0.2, -0.6, -0.4)
points = [(-2.0, -2.0), (-2.0, 1.0), (-2.0, 2.0), (1.0, 2.0), (2.0, 0.0)]
step_size = 0.2
points = self.get_monsters(target_model, points, step_size)
yield LearningRateMonstersLevel(model, points, step_size, ErrorType.SUM_LINEAR, 0.5).set_max_iterations(30).set_hide_spell(True).set_learning_rate(1.0)
model = LinearModel(-0.22, 0.64, 0.12)
target_model = LinearModel(0.34, -0.23, -0.4)
points = []
for x in range(-2, 3):
for y in range(-2, 3):
points.append((x, y))
step_size = 0.01
points = self.get_monsters(target_model, points, step_size)
yield LearningRateMonstersLevel(model, points, step_size, ErrorType.SUM_LINEAR, 0.5).set_max_iterations(30)
def get_multi_split_levels(self):
model = LinearModel(0.0, 0.5, 0.2)
target_model = LinearModel(1.0, 0.5, -0.5)
points = [(-2.0, -1.0), (0.0, 1.0), (2.0, 1.0)]
step_size = 1.0
points = self.get_monsters(target_model, points, step_size)
yield MultiSplitMonstersLevel(model, points, step_size, ErrorType.SUM_LINEAR, 0.5, 1, 3).set_max_iterations(30)
model = LinearModel(-0.5, 0.5, 0.5)
target_model = LinearModel(0.3, -0.3, 0.0)
points = []
for x in range(-2, 3):
for y in range(-2, 3):
points.append((x, y))
step_size = 0.3
points = self.get_monsters(target_model, points, step_size)
yield MultiSplitMonstersLevel(model, points, step_size, ErrorType.SUM_LINEAR, 0.5, 2, 3).set_max_iterations(60).set_hide_spell(True)
model = LinearModel(-0.2, 0.6, 0.4)
target_model = LinearModel(0.2, -0.6, -0.4)
points = [(-2.0, -2.0), (-2.0, 1.0), (-2.0, 2.0), (1.0, 2.0), (2.0, 0.0)]
step_size = 0.2
points = self.get_monsters(target_model, points, step_size)
yield MultiSplitMonstersLevel(model, points, step_size, ErrorType.SUM_LINEAR, 0.5, 3, 3).set_max_iterations(60)
def get_intro_level(self):
model = LinearModel(0.5, 0.0, 0.0)
levels = [-100, 0, 100]
colors = ['#0f90bf', '#dbd1ed']
monsters = [
MonsterInfo(-2.0, -2.0, Images.DINO_MONSTER, 0),
MonsterInfo(0.0, -2.0, Images.DINO_MONSTER, 0),
MonsterInfo(2.0, -2.0, Images.DINO_MONSTER, 0),
MonsterInfo(-2.0, 2.0, Images.SNOW_MONSTER, 1),
MonsterInfo(0.0, 2.0, Images.SNOW_MONSTER, 1),
MonsterInfo(2.0, 2.0, Images.SNOW_MONSTER, 1)
]
yield SplitMonstersLevel(model, levels, colors, monsters, 1, 1).set_max_iterations(3).set_hide_restart_button(True)
def get_main_levels(self):
model = LinearModel(0.5, 0.0, 0.0)
levels = [-100, 0, 100]
colors = ['#0f90bf', '#dbd1ed']
monsters = [
MonsterInfo(-2.0, -2.0, Images.DINO_MONSTER, 0),
MonsterInfo(0.0, -2.0, Images.DINO_MONSTER, 0),
MonsterInfo(2.0, -2.0, Images.DINO_MONSTER, 0),
MonsterInfo(-2.0, 2.0, Images.SNOW_MONSTER, 1),
MonsterInfo(0.0, 2.0, Images.SNOW_MONSTER, 1),
MonsterInfo(2.0, 2.0, Images.SNOW_MONSTER, 1)
]
yield SplitMonstersLevel(model, levels, colors, monsters, 1, 3)
model = LinearModel(0.5, 0.0, 0.0)
levels = [-100, 0, 100]
colors = ['#0f90bf', '#dbd1ed']
monsters = [
MonsterInfo(-2.0, 2.0, Images.DINO_MONSTER, 0),
MonsterInfo(0.0, 2.0, Images.DINO_MONSTER, 0),
MonsterInfo(2.0, 2.0, Images.DINO_MONSTER, 0),
MonsterInfo(-2.0, 1.0, Images.SNOW_MONSTER, 1),
MonsterInfo(0.0, 1.0, Images.SNOW_MONSTER, 1),
MonsterInfo(2.0, 1.0, Images.SNOW_MONSTER, 1)
]
yield SplitMonstersLevel(model, levels, colors, monsters, 2, 3).set_max_iterations(30)
model = LinearModel(-0.5, 0.5, 1.0)
levels = [-100, 0, 100]
colors = ['#0f90bf', '#dbd1ed']
monsters = [
MonsterInfo(1.0, 2.0, Images.DINO_MONSTER, 0),
MonsterInfo(2.0, 1.0, Images.DINO_MONSTER, 0),
MonsterInfo(-2.0, -2.0, Images.DINO_MONSTER, 0),
MonsterInfo(2.0, -2.0, Images.SNOW_MONSTER, 1),
]
yield SplitMonstersLevel(model, levels, colors, monsters, 3, 3).set_max_iterations(30)
class HigherDimensionsLevelsFactory(BaseLevelFactory):
def get_points(self, target_model, vectors, step_size):
outputs = self.get_outputs(target_model, vectors)
self.check_outputs(outputs, step_size)
return [Point(vector, output) for vector, output in zip(vectors, outputs)]
def gen_model_weights(self, dim):
possible_weights = list(map(lambda x: x/10.0, range(-5, 6)))
return random.choices(possible_weights, k=dim)
def gen_points(self, n, dim):
possible_coord = list(map(lambda x: x/10.0, range(-20,21)))
return [random.choices(possible_coord, k=dim) for i in range(n)]
def get_higher_dimensions_levels(self):
random.seed(42)
weights = self.gen_model_weights(1)
for dim in range(2, 10):
model = LinearModel(weights+[-1.0], True)
weights = self.gen_model_weights(dim)
target_model = LinearModel(weights, True)
points = self.gen_points(7, dim-1)
step_size = 0.01
points = self.get_points(target_model, points, step_size)
learning_rate = 0.1
yield HigherDimensionsLevel(learning_rate, model, points, step_size, ErrorType.SUM_LINEAR, 1.0)
class DevLevelsFactory(BaseLevelFactory):
def get_dev_levels(self):
yield DevLevel()
class MainLevelsFactory(BaseLevelFactory):
def set_level_codes(self, salt, gen):
levels = list(gen)
for i, level in enumerate(levels):
sha = hashlib.sha256()
sha.update(f'{salt}:{i}'.encode())
code = sha.hexdigest()[:6].strip().lower()
yield level.set_code(code)
def set_level_numbers(self, gen):
levels = list(gen)
for i, level in enumerate(levels):
yield level.set_level_number(i+1).set_number_of_levels(len(levels))
def all_levels(self):
#yield from self.first_level()
#yield from self.second_level()
yield from self.set_level_codes("SALT_3", self.all_levels_without_codes())
def all_levels_without_codes(self):
#yield from self.first_level()
#yield from self.second_level()
#yield from self.third_level()
yield from self.fourth_levels()
#yield from self.dev_levels()
def dev_levels(self):
yield from self.set_level_numbers(DevLevelsFactory().get_dev_levels())
def fourth_levels(self):
yield InfoLevel("Follow me to 9 dimension", "./images/dream.jpg", None, "9 dimension is special for Creators of The Future")
yield from self.set_level_numbers(HigherDimensionsLevelsFactory().get_higher_dimensions_levels())
yield InfoLevel("Congratulations", "./images/dream.jpg", None, "Congratulations! Now you can work in 9 dimension, this dimension will help you to kill <b>Xorisimus</b> one day").set_hide_next_button(True)
def third_level(self):
yield InfoLevel("Let me prepare myself for next night", "./images/wake_up.jpg", None, "It was easy after preparation, let's prepare today too")
yield from self.set_level_numbers(StudyPlaneLevelFactory().get_learning_rate_levels())
yield InfoLevel("You are back", "./images/dream.jpg", None, "There are new <b>Lernos Ratos</b> spells for you:</br><b>Minisimus</b> - set lernos ratos to minimum value, you can cast <b>Iteratimus</b> spells for free</br><b>Restorisimus</b> - restore lernos ratos to previous value</br><b>Incrisimus/Dicrisimus</b> - increase/decrease lernos ratos, note you can not use it till you call Restorisimus after Minisimus")
yield from self.set_level_numbers(MonstersLevelsFactory().get_learning_rate_levels())
yield InfoLevel("Congratulations", "./images/dream.jpg", None, "Congratulations! You once again show your potential, the way you handlered hide spell was impressive").set_hide_next_button(True)
def second_level(self):
yield InfoLevel("Let me prepare myself for next night", "./images/wake_up.jpg", None, "It was easy after preparation, let's prepare today too")
yield from StudyPlaneLevelFactory().get_study_levels()
yield InfoLevel("You are back", "./images/dream.jpg", None, "Warning, <b>Iterasimums</b> learned a new hide spell and he can hide battle field from you, but <b>Acurasimus</b> bring his friend <b>Erorisimus</b> to help out in such situation")
yield from SplitMonstersLevelsFactory().get_multi_split_levels()
yield InfoLevel("Congratulations", "./images/dream.jpg", None, "Congratulations! You once again show your potential, the way you handlered hide spell was impressive").set_hide_next_button(True)
def first_level(self):
yield from self.intro_levels()
yield from self.study_line_levels()
yield from self.monster_levels()
def intro_levels(self):
yield InfoLevel("After a long day", "./images/sleep.jpg", "after_a_long_day", "After a long day, it's time to go to sleep<br/>Click Next level, to continue...")
yield InfoLevel("Welcome", "./images/dream.jpg", "welcome", "Welcome to the creators world. You have been choosen to fight on the side of the future. There is no time to get a proper training, since we are in the middle of the battle, so I will set up magic interface for you. Just cast spells and we hope you will lead <b>Acurasimus</b> to the victory over <b>Iterasimus<b/>")
yield from SplitMonstersLevelsFactory().get_intro_level()
yield InfoLevel("You had no chance", "./images/apoke.16_00040.png", "you_had_no_chance", "You had no chance to beat <b>Iterasimus</b>")
yield InfoLevel("Oh... no", "./images/dream.jpg", "o_no", "Oh... no, you lost the battle. But the fight for the future is ongoing. We see the potential in you to became the greatest creator of all times, we will give you instructions how to prepare youself for the next night ...")
def study_line_levels(self):
yield InfoLevel("What a strange night", "./images/wake_up.jpg", "what_a_strange_night", "What a strange night! Let me prepare myself for the next battle")
yield StudyLineLevel(StudyLineModel(0.0, -1.0, 0.0), StudyLineModel(0.0, -1.0, -0.5), [True, True, True, True, True, False], 1, 10)
yield StudyLineLevel(StudyLineModel(1.0, 0.1, 0.0), StudyLineModel(1.0, 0.1, 0.5), [True, True, True, True, False, True], 2, 10)
yield StudyLineLevel(StudyLineModel(1.0, 0.9, 0.0), StudyLineModel(1.0, 0.4, 0.0), [True, True, True, False, True, True], 3, 10)
yield StudyLineLevel(StudyLineModel(1.0, -2.0, 0.0), StudyLineModel(1.0, -1.5, 0.0), [True, True, False, True, True, True], 4, 10)
yield StudyLineLevel(StudyLineModel(-1.0, -2.0, 0.0), StudyLineModel(-1.5, -2.0, 0.0), [True, False, True, True, True, True], 5, 10)
yield InfoLevel("You have no chance", "./images/apoke.16_00040.png", "you_have_no_chance", "You have no chance, I will win again ...")
yield StudyLineLevel(StudyLineModel(1.0, -2.0, 0.0), StudyLineModel(1.5, -2.0, 0.0), [False, True, True, True, True, True], 6, 10)
yield StudyLineLevel(StudyLineModel(0.5, 0.5, 0.0), StudyLineModel(0.7, 0.3, 0.0), [False, False, False, False, True, True], 7, 10)
yield StudyLineLevel(StudyLineModel(0.5, 0.5, 0.0), StudyLineModel(0.7, 0.3, 0.5), [], 8, 10)
yield StudyLineLevel(StudyLineModel(-0.5, -0.5, 0.0), StudyLineModel(0.7, 0.3, -0.3), [], 9, 10)
yield StudyLineLevel(StudyLineModel(1.0, 0.3, 0.0), StudyLineModel(-1.0, 0.3, -0.7), [], 10, 10)
def monster_levels(self):
yield InfoLevel("Last night was crazy", "./images/sleep.jpg", "last_night", "Last night was crazy. I can not believe I spent half of the day in the training. What a silly move from my side! Time to go into the darkness and get some rest ...")
yield InfoLevel("You are back", "./images/dream.jpg", "you_are_back", "You are back, we were waiting for you! Don't panic, you can win now. I know that you are not a real creator yet and you are scared, but I believe in you. Go and bring us a victory this time!")
yield from SplitMonstersLevelsFactory().get_main_levels()
yield InfoLevel("Congratulations", "./images/dream.jpg", "congratulations", "Congratulations! You earned your place among creators of the future. Now, you are ready to know what means to be a creator. By playing this game you actually studied machine learning. Join our secret group to continue your education and access to the next chapter of the game. Creators of the future are waiting for you https://www.facebook.com/groups/458107258671703")