-
Notifications
You must be signed in to change notification settings - Fork 2
/
algorithm.py
383 lines (315 loc) · 13.4 KB
/
algorithm.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from model import Move, Type_, Chain, Progression
from model import connect_to_db, db
from random import choice, shuffle
# import doctest
DANCE_LENGTH = 64
FUNKY_MOVES = {
# "start from even: 3 / start from odd: 1"
2311: {0: 3, 1: 1, 2: 3, 3: 1},
# "start from even: 3 / start from odd: 0"
2310: {0: 3, 1: 0, 2: 3, 3: 0},
# "start from even: 0 / start from odd: 1"
2011: {0: 0, 1: 1, 2: 0, 3: 1}
}
class MoveObj(object):
def __init__(self, move_code):
self.move_code = move_code
self.get_attributes()
def get_attributes(self):
self.type_code, self.beats, self.name = db.session.query(Move.type_code, Move.beats, Move.move_name).filter(Move.move_code == self.move_code).one()
self.move_lead, self.move_follow, self.same_side = db.session.query(Move.leads_move, Move.follows_move, Move.same_side).filter(Move.move_code == self.move_code).one()
self.min, self.max = db.session.query(Type_.min_repeats, Type_.max_repeats).filter(Type_.type_code == self.type_code).one()
self.orphanable = self.orphanable()
self.values = self.get_values()
def get_values(self):
"""Query chains table for moves that may follow this one.
"""
values = db.session.query(Chain.value).filter(Chain.key_ == self.move_code).all()
values_list = []
for value in values:
values_list.append(value[0])
return values_list
def orphanable(self):
"""Determine whether move needs special treatment to avoid orphaning.
"""
return self.min != 0
def __repr__(self):
return "<MoveObj move_code={}>".format(self.move_code)
class DanceObj(object):
# def __init__(self, move_dict, logger=defaultLogger):
def __init__(self, move_dict):
self.move_dict = move_dict
self.last_move, self.first_move, self.start = self.pick_progression()
self.beats_to_fill = self.len_left_init(self.last_move)
self.dance_moves = self.all_together_now()
def pick_progression(self):
"""Randomly choose a first and last move from the database.
"""
prog_list = db.session.query(Progression.last, Progression.first, Progression.start).all()
last_move, first_move, start_position = choice(prog_list)
return (last_move, first_move, start_position)
def len_left_init(self, last_move):
"""Initial determination of remaining beats to be filled by tree recursion.
"""
move_beats = self.move_dict[last_move].beats
minus_last = DANCE_LENGTH - move_beats
return minus_last
def count_dance(self, dance):
"""Count dance from build_dance; should be 64 beats.
"""
count = 0
for each_move in dance:
move_time = self.move_dict[each_move].beats
count += move_time
return count
def find_follows(self, dance):
"""Given a partially-built dance, find the follows.
"""
follow_count = 0
for each_move in dance:
follows_code = self.move_dict[each_move].move_follow
if follows_code < 10:
follows_move = follows_code
else:
follows_move = FUNKY_MOVES[follows_code][follow_count]
follow_count += follows_move
follow_count = follow_count % 4
return follow_count
def find_leads(self, dance):
"""Given a partially-built dance, find the follows.
"""
lead_count = 1
for each_move in dance:
leads_code = self.move_dict[each_move].move_lead
if leads_code < 10:
leads_move = leads_code
else:
leads_move = FUNKY_MOVES[leads_code][lead_count]
lead_count += leads_move
lead_count = lead_count % 4
return lead_count
def check_same(self, follows_at, leads_at):
"""Determine if dancers are on the same side as their partners.
"""
positions = set([follows_at, leads_at])
print "POSITIONS: ", positions
if positions == set([0, 1]) or positions == set([2, 3]):
return False
elif positions == set([0, 3]) or positions == set([1, 2]):
return True
else:
print "SOMETHING IS VERY WRONG WITH THESE POSITIONS"
return
def try_positions(self, test_value, dance):
"""Check that dancers are in appropriate position to flow into test_value.
"""
follows_at = self.find_follows(dance)
follows_code = self.move_dict[test_value].move_follow
if follows_code < 10:
follows_move = follows_code
else:
follows_move = FUNKY_MOVES[follows_code][follows_at]
follows_to = (follows_at + follows_move) % 4
print "FOLLOWS MOVE ", follows_move, "FROM ", follows_at, "TO ", follows_to
leads_at = self.find_leads(dance)
leads_code = self.move_dict[test_value].move_lead
if leads_code < 10:
leads_move = leads_code
else:
leads_move = FUNKY_MOVES[leads_code][leads_at]
leads_to = (leads_at + leads_move) % 4
print "LEADS MOVE ", leads_move, "FROM ", leads_at, "TO ", leads_to
with_partners = self.check_same(follows_at, leads_at)
start_same = self.move_dict[test_value].same_side
print "START SAME: ", start_same, "WITH PARTNERS: ", with_partners
# Do set math to check that follow/lead positions are or are not on same side
if start_same == 2:
return True
elif (start_same == 1 and with_partners) or (start_same == 0 and not with_partners):
return True
elif (start_same == 1 and not with_partners) or (start_same == 0 and with_partners):
return False
else:
print "SOMETHING IS WRONG WITH POSITIONS"
return False
def too_many(self, test_value, new_dance):
"""Check that the addition of a move does not violate the max_repeats rules for type.
"""
test_type = self.move_dict[test_value].type_code
max_repeats = self.move_dict[test_value].max
# print "TYPE: ", test_type, "MAX REPEATS:", max_repeats
if self.move_dict[new_dance[-1]].type_code != test_type:
return False
elif max_repeats == 0:
return True
elif test_type == "hey":
if self.count_dance(new_dance) in set([16, 32, 48]):
return True
else:
return False
else:
danger_zone = new_dance[-(max_repeats + 1):]
# print "DANGER ZONE: ", danger_zone
repeats = 0
for old_move in danger_zone:
if self.move_dict[old_move].type_code == test_type:
repeats += 1
# print "REPEATS: ", repeats
if repeats == (max_repeats + 1):
return True
else:
return False
def orphan_wrangling(self, curr_key, dance, curr_len):
"""Ensure that non-standalone moves get at least one repeat.
"""
if self.move_dict[curr_key].orphanable is False:
curr_values = self.move_dict[curr_key].values
shuffle(curr_values)
elif self.move_dict[curr_key].orphanable is True:
if self.move_dict[curr_key].type_code != self.move_dict[dance[-1]].type_code:
if curr_len in [16, 32, 48]:
print curr_key, " NEEDS MORE TIME"
return None
else:
curr_values = [curr_key]
print "TO PREVENT ORPHANS, ", curr_values, "IS THE ONLY CURRENT VALUE"
elif self.move_dict[curr_key].type_code == self.move_dict[dance[-1]].type_code:
if self.move_dict[curr_key].type_code == 'swing' and curr_len not in [16, 32, 48]:
curr_values = [curr_key]
print "TO FILL THE BUCKET, ", curr_values, " IS THE ONLY CURRENT VALUE"
else:
curr_values = self.move_dict[curr_key].values
shuffle(curr_values)
else:
print "THE ORPHANS ARE MAKING TROUBLE"
return None
return curr_values
def try_last_flow(self, curr_key, curr_values, last_move):
"""Check that final move is in values for potential penultimate move.
"""
if last_move in curr_values:
works = True
else:
print "DIDN'T FLOW - VALUES"
works = False
return works
def try_last_position(self, start, last_move, dance):
"""Check that potential penultimate move leaves dancers in correct position for final move.
TODO: doesn't seem perfect, maybe want to have a leads start to check, refactor try_positions so this can use that code.
"""
if self.find_follows(dance) == start and self.try_positions(last_move, dance):
works = True
else:
print "DIDN'T FLOW - POSITIONS"
works = False
return works
def try_leaf(self, curr_key, curr_values, last_move, start, dance):
"""Check all end-of-dance base cases.
"""
potential_whole = list(dance)
potential_whole.append(curr_key)
if self.try_last_flow(curr_key, curr_values, last_move) is True and self.try_last_position(start, last_move, dance) is True:
return True
else:
return False
def build_dance(self, curr_key, last_move, start, dance=None):
"""Recursively create dance, backtrack if a move doesn't work.
"""
if not dance:
dance = []
new_dance = list(dance)
new_dance.append(curr_key)
curr_len = self.count_dance(new_dance)
beats_left = self.beats_to_fill - curr_len
works = False
# Prevent orphans, ensure that swings fill buckets
curr_values = self.orphan_wrangling(curr_key, dance, curr_len)
if not curr_values:
return dance, works
# Fail condition
if beats_left < 0:
print "TOO LONG"
return dance, works
elif self.count_dance(dance) < 16 < curr_len:
print "CROSSES 16"
return dance, works
elif self.count_dance(dance) < 32 < curr_len:
print "CROSSES 32"
return dance, works
elif self.count_dance(dance) < 48 < curr_len:
print "CROSSES 48"
return dance, works
# Base case
elif beats_left == 0:
if self.try_leaf(curr_key, curr_values, last_move, start, dance) is True:
dance.append(curr_key)
dance.append(last_move)
works = True
else:
works = False
return dance, works
# Recursive call
elif beats_left > 0:
for next_key in curr_values:
print "............................................................."
# print "BEATS TO FILL: ", beats_left
print "DANCE: ", new_dance
print "TRYING: ", next_key, "(", self.move_dict[next_key].beats, ") beats"
if self.too_many(next_key, new_dance) is False:
if self.try_positions(next_key, new_dance) is True:
dance, works = self.build_dance(next_key, last_move, start, new_dance)
if works is True:
break
else:
print "POSITIONS DON'T WORK"
return dance, works
else:
print "TOO MANY", self.move_dict[next_key].type_code, "NOT ADDING MORE"
return dance, works
else:
print "----------Something is wrong.----------"
pass
return dance, works
def all_together_now(self):
"""Run build_dance and check outcomes.
"""
print "PROGRESSION: ", self.last_move, self.first_move, self.start
print "BEATS TO FILL: ", self.beats_to_fill
entire_dance, works = self.build_dance(curr_key=self.first_move, last_move=self.last_move, start=self.start)
print "DANCE CREATED: ", entire_dance
total_time = self.count_dance(entire_dance)
print "TOTAL TIME: ", total_time
# If something goes wrong, scrap it and try again.
if total_time != 64:
print ". . . . . . . . . . . . . . . . . . ."
print ". . . . . . . . . . . . . . . . . . ."
print ". . . . . . . . . . . . . . . . . . ."
print ". . . . . . . . . . . . . . . . . . ."
print ". . . . . . . . . . . . . . . . . . ."
return self.all_together_now()
return entire_dance
def pull_move_codes():
"""Queries database for all move codes.
"""
all_codes = db.session.query(Move.move_code).all()
return all_codes
def make_moves():
"""Creates dictionary of all MoveObj objects.
"""
all_moves = pull_move_codes()
move_dict = {}
for code in all_moves:
move_code = code[0]
move_dict[move_code] = MoveObj(move_code)
return move_dict
def do_it_all():
da_dict = make_moves()
new_dance = DanceObj(da_dict)
the_prog = ", ".join([new_dance.last_move, new_dance.first_move])
return new_dance.dance_moves, da_dict, the_prog
if __name__ == "__main__":
from server import app
connect_to_db(app)
print "Connected to DB."
# doctest.testmod(verbose=True)
do_it_all()