forked from ellisk42/ec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtowerPrimitives.py
155 lines (140 loc) · 5.68 KB
/
towerPrimitives.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
from program import *
from arithmeticPrimitives import *
from functools import reduce
class TowerState:
def __init__(self, hand=0, orientation=1, history=None):
# List of (State|Block)
self.history = history
self.hand = hand
self.orientation = orientation
def __str__(self): return f"S(h={self.hand},o={self.orientation})"
def __repr__(self): return str(self)
def left(self, n):
return TowerState(hand=self.hand - n, orientation=self.orientation,
history=self.history if self.history is None \
else self.history + [self])
def right(self, n): return TowerState(hand=self.hand + n, orientation=self.orientation,
history=self.history if self.history is None \
else self.history + [self])
def reverse(self): return TowerState(hand=self.hand, orientation=-1*self.orientation,
history=self.history if self.history is None \
else self.history + [self])
def move(self, n): return TowerState(hand=self.hand + n*self.orientation, orientation=self.orientation,
history=self.history if self.history is None \
else self.history + [self])
def recordBlock(self, b):
if self.history is None: return self
return TowerState(hand=self.hand,
orientation=self.orientation,
history=self.history + [b])
def _empty_tower(h): return (h,[])
def _left(d):
return lambda k: lambda s: k(s.left(d))
def _right(d):
return lambda k: lambda s: k(s.right(d))
def _loop(n):
def f(start, stop, body, state):
if start >= stop: return state,[]
state, thisIteration = body(start)(state)
state, laterIterations = f(start + 1, stop, body, state)
return state, thisIteration + laterIterations
def sequence(b,k,h):
h,bodyBlocks = f(0,n,b,h)
h,laterBlocks = k(h)
return h,bodyBlocks+laterBlocks
return lambda b: lambda k: lambda h: sequence(b,k,h)
def _simpleLoop(n):
def f(start, body, k):
if start >= n: return k
return body(start)(f(start + 1, body, k))
return lambda b: lambda k: f(0,b,k)
def _embed(body):
def f(k):
def g(hand):
bodyHand, bodyActions = body(_empty_tower)(hand)
# Record history if we are doing that
if hand.history is not None:
hand = TowerState(hand=hand.hand,
orientation=hand.orientation,
history=bodyHand.history)
hand, laterActions = k(hand)
return hand, bodyActions + laterActions
return g
return f
def _moveHand(n):
return lambda k: lambda s: k(s.move(n))
def _reverseHand(k):
return lambda s: k(s.reverse())
class TowerContinuation(object):
def __init__(self, x, w, h):
self.x = x
self.w = w*2
self.h = h*2
def __call__(self, k):
def f(hand):
thisAction = [(self.x + hand.hand,self.w,self.h)]
hand = hand.recordBlock(thisAction[0])
hand, rest = k(hand)
return hand, thisAction + rest
return f
# name, dimensions
blocks = {
# "1x1": (1.,1.),
# "2x1": (2.,1.),
# "1x2": (1.,2.),
"3x1": (3, 1),
"1x3": (1, 3),
# "4x1": (4.,1.),
# "1x4": (1.,4.)
}
ttower = baseType("tower")
common_primitives = [
Primitive("tower_loopM", arrow(tint, arrow(tint, ttower, ttower), ttower, ttower), _simpleLoop),
Primitive("tower_embed", arrow(arrow(ttower,ttower), ttower, ttower), _embed),
] + [Primitive(name, arrow(ttower,ttower), TowerContinuation(0, w, h))
for name, (w, h) in blocks.items()] + \
[Primitive(str(j), tint, j) for j in range(1,9) ]
primitives = common_primitives + [
Primitive("left", arrow(tint, ttower, ttower), _left),
Primitive("right", arrow(tint, ttower, ttower), _right)
]
new_primitives = common_primitives + [
Primitive("moveHand", arrow(tint, ttower, ttower), _moveHand),
Primitive("reverseHand", arrow(ttower, ttower), _reverseHand)
]
def executeTower(p, timeout=None):
try:
return runWithTimeout(lambda : p.evaluate([])(_empty_tower)(TowerState())[1],
timeout=timeout)
except RunWithTimeout: return None
except: return None
def animateTower(exportPrefix, p):
print(exportPrefix, p)
from tower_common import renderPlan
state,actions = p.evaluate([])(_empty_tower)(TowerState(history=[]))
print(actions)
trajectory = state.history + [state]
print(trajectory)
print()
assert tuple(z for z in trajectory if not isinstance(z, TowerState) ) == tuple(actions)
def hd(n):
h = 0
for state in trajectory[:n]:
if isinstance(state, TowerState):
h = state.hand
return h
animation = [renderPlan([b for b in trajectory[:n] if not isinstance(b, TowerState)],
pretty=True, Lego=True,
drawHand=hd(n),
masterPlan=actions,
randomSeed=hash(exportPrefix))
for n in range(0,len(trajectory) + 1)]
import scipy.misc
import random
r = random.random()
paths = []
for n in range(len(animation)):
paths.append(f"{exportPrefix}_{n}.png")
scipy.misc.imsave(paths[-1], animation[n])
os.system(f"convert -delay 10 -loop 0 {' '.join(paths)} {exportPrefix}.gif")
# os.system(f"rm {' '.join(paths)}")