In [1]:
import numpy as np
In [2]:
blank = 0
vcar = 4
vtruck = 5
hcar = 6
htruck = 7
In [3]:
v1 = np.array([blank]*36).reshape(6,6)
for x in range(5):
v1[x:x+2,x] = vcar
v1[4-x:4-x+2,x] = vcar
v1[4:6,5] = vcar
#v1[2,3:5] = hcar
#v1[4,1:3] = hcar
#v1[2:4,0] = vcar
#v1[3:,5] = vtruck
v1
Out[3]:
In [4]:
v2 = np.array([blank]*36).reshape(6,6)
for x in range(4):
v2[x:x+3,x] = vtruck
v2[0:3,4]=vtruck
v2[0:3,5] = vtruck
v2[3:6,5] = vtruck
v2
Out[4]:
In [5]:
v3 = np.array([blank]*36).reshape(6,6)
v3[0:3,4]=vtruck
v3[1:3,3] = vcar
v3
Out[5]:
In [102]:
v4 = np.array([blank]*36).reshape(6,6)
v4[2,:2] = hcar
v4[4,4:6] = hcar
v4[5,2:5] = htruck
v4
Out[102]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [251]:
# move down. Look for vcar, vtruck.
#function input: v
v = v3
ret = []
mv_x = -v + np.roll(v,1,0)
mv_x = mv_x[1:]
#np.array([v,mv_x])
mv_car = np.where(mv_x==vcar)
mv_truck = np.where(mv_x ==vtruck)
mv_car
mv_down_car = list(zip(mv_car[0],mv_car[1]))
mv_down_truck = list(zip(mv_truck[0], mv_truck[1]))
for x,y in mv_down_car:
new = np.copy(v)
new[x+1,y] = vcar
new[x-1,y] = blank
ret.append(new)
for x,y in mv_down_truck:
#x,y = mv_down_truck[0]
new = np.copy(v)
new[x+1,y] = vtruck
new[x-2,y] = blank
ret.append(new)
np.array([v]+ret)
Out[251]:
In [264]:
# Move up
#function input: v
v = v2
ret = []
mv_x = -v + np.roll(v,-1,0)
mv_x = mv_x[:5]
#np.array([v,mv_x,mv_x[:5]])
mv_car = np.where(mv_x==vcar)
mv_truck = np.where(mv_x ==vtruck)
mv_car
mv_up_car = list(zip(mv_car[0],mv_car[1]))
mv_up_truck = list(zip(mv_truck[0], mv_truck[1]))
for x,y in mv_up_car:
new = np.copy(v)
new[x,y] = vcar
new[x+2,y] = blank
ret.append(new)
for x,y in mv_up_truck:
#x,y = mv_up_truck[0]
new = np.copy(v)
new[x,y] = vtruck
new[x+3,y] = blank
ret.append(new)
np.array([v]+ret)
Out[264]:
In [318]:
# Move Left
#function input
v = v4
ret = []
mv_x = -v + np.roll(v,-1,1)
mv_x = mv_x[:,:5]
mv_car = np.where(mv_x==hcar)
mv_truck = np.where(mv_x ==htruck)
mv_left_car = list(zip(mv_car[0],mv_car[1]))
mv_left_truck = list(zip(mv_truck[0], mv_truck[1]))
for x,y in mv_left_car:
new = np.copy(v)
new[x,y] = hcar
new[x,y+2] = blank
ret.append(new)
for x,y in mv_left_truck:
new = np.copy(v)
new[x,y] = htruck
new[x,y+3] = blank
ret.append(new)
np.array([v]+ret)
Out[318]:
In [105]:
# Move right
#function input
v = v4
ret = []
mv_x = -v + np.roll(v,1,1)
mv_x = mv_x[:,1:]
mv_car = np.where(mv_x==hcar)
mv_truck = np.where(mv_x ==htruck)
mv_right_car = list(zip(mv_car[0],mv_car[1]))
mv_right_truck = list(zip(mv_truck[0], mv_truck[1]))
for x,y in mv_right_car:
new = np.copy(v)
new[x,y+1] = hcar
new[x,y-1] = blank
ret.append(new)
for x,y in mv_right_truck:
new = np.copy(v)
new[x,y+1] = htruck
new[x,y-2] = blank
ret.append(new)
#v, mv_x,mv_left_car
out = np.array([v]+ret)
out,mv_right_car
Out[105]:
In [83]:
v4
Out[83]:
In [284]:
t = np.array(np.arange(6)).reshape(2,3)
t
Out[284]:
In [294]:
v[:,:5]
Out[294]:
In [29]:
car_up
Out[29]:
In [45]:
x=car_up[0][0]
y =car_up[1][0]
x,y
Out[45]:
In [49]:
up = list(zip(car_up[0],car_up[1]))
Out[49]:
In [32]:
v2 = np.arange(36).reshape(6,6)
In [33]:
v2
Out[33]:
In [34]:
v2[3,4]
Out[34]:
In [37]:
np.where(v2==16)
Out[37]:
In [ ]:
In [ ]:
In [ ]:
In [122]:
x = np.arange(6)
x
Out[122]:
In [106]:
def state_nbrs(v,red_cols):
'''
Compute all neighbrs via legal moves in the rush hour puzzle.
Input: ndarray of shape (6,6) with values modeling a legal configuration of pieces on the rush hour puzzle board.
output: set of triples (x,red_col_1,red_col_2):
x: integer represenation of of the (6,6) ndarray
red_co_1, red_col_2: model the positions of the red car
'''
ret = set()
# Move right
mv_x = -v + np.roll(v,1,1)
mv_x = mv_x[:,1:]
mv_car = np.where(mv_x==hcar)
mv_truck = np.where(mv_x ==htruck)
mv_right_car = list(zip(mv_car[0],mv_car[1]))
mv_right_truck = list(zip(mv_truck[0], mv_truck[1]))
for x,y in mv_right_car:
new = np.copy(v)
new[x,y+1] = hcar
new[x,y-1] = blank
if y+2 in red_cols:
ret.add( (board_to_int(new),(x,y)) )
else:
ret.add( (board_to_int(new),red_cols) )
for x,y in mv_right_truck:
new = np.copy(v)
new[x,y+1] = htruck
new[x,y-2] = blank
ret.add( (board_to_int(new),red_cols) )
# Move Left
mv_x = -v + np.roll(v,-1,1)
mv_x = mv_x[:,:5]
mv_car = np.where(mv_x==hcar)
mv_truck = np.where(mv_x ==htruck)
mv_left_car = list(zip(mv_car[0],mv_car[1]))
mv_left_truck = list(zip(mv_truck[0], mv_truck[1]))
for x,y in mv_left_car:
new = np.copy(v)
new[x,y] = hcar
new[x,y+2] = blank
if y+2 in red_cols:
ret.add( (board_to_int(new),(x,y)) )
else:
ret.add( (board_to_int(new),red_cols) )
for x,y in mv_left_truck:
new = np.copy(v)
new[x,y] = htruck
new[x,y+3] = blank
ret.add( (board_to_int(new),red_cols) )
# Move up
mv_x = -v + np.roll(v,-1,0)
mv_x = mv_x[:5]
mv_car = np.where(mv_x==vcar)
mv_truck = np.where(mv_x ==vtruck)
mv_up_car = list(zip(mv_car[0],mv_car[1]))
mv_up_truck = list(zip(mv_truck[0], mv_truck[1]))
for x,y in mv_up_car:
new = np.copy(v)
new[x,y] = vcar
new[x+2,y] = blank
ret.add( (board_to_int(new),red_cols) )
for x,y in mv_up_truck:
#x,y = mv_up_truck[0]
new = np.copy(v)
new[x,y] = vtruck
new[x+3,y] = blank
ret.add( (board_to_int(new),red_cols) )
# move down
mv_x = -v + np.roll(v,1,0)
mv_x = mv_x[1:]
mv_car = np.where(mv_x==vcar)
mv_truck = np.where(mv_x ==vtruck)
mv_down_car = list(zip(mv_car[0],mv_car[1]))
mv_down_truck = list(zip(mv_truck[0], mv_truck[1]))
for x,y in mv_down_car:
new = np.copy(v)
new[x+1,y] = vcar
new[x-1,y] = blank
ret.add( (board_to_int(new),red_cols) )
for x,y in mv_down_truck:
new = np.copy(v)
new[x+1,y] = vtruck
new[x-2,y] = blank
ret.add( (board_to_int(new),red_cols) )
return ret
In [109]:
v4
Out[109]:
In [110]:
v1[2,4:6] = hcar
v1
Out[110]:
In [36]:
vec_bitstring_3 = np.vectorize(lambda x: np.binary_repr(x,width=3) )
def board_to_int(v):
t = vec_bitstring_3(v)
return int(''.join(np.apply_along_axis(lambda x: ''.join(x), 1,t)),2)
def int_to_board(i):
#i = '154444257952488798331863040'
s = bin(int(i))[2:].zfill(108)
v = np.array([int(s[i:i+3],2) for i in range(0,len(s),3)])
return v.reshape((6,6))
In [121]:
nbrs = state_nbrs(v4,(3,4))
In [122]:
len(nbrs)
Out[122]:
In [123]:
s = [int_to_board(x[0]) for x in nbrs]
In [124]:
np.array([v4]+ s)
Out[124]:
In [116]:
s[3]
Out[116]:
In [117]:
v4 - s[3]
Out[117]:
In [118]:
diffs = [v4-x for x in s]
In [119]:
diffs[0]
Out[119]:
In [ ]: