import whrandom from params import * size = 2**size_code perturb = 2**perturb_code eps = 0.01 * eps_code if fill_level > 0: def givecolor (pivot): "pivot: 3 coordinates, multiples of 3" pivot = (pivot[0]//3,pivot[1]//3,pivot[2]//3) return clr[pivot] else: def givecolor (pivot): "pivot: 3 coordinates, multiples of 3" pivot = (pivot[0]//3,pivot[1]//3,pivot[2]//3) if pivot in clr: return clr[pivot] clr[pivot] = whrandom.randint(0,1) return clr[pivot] def boundary(): # colored boundary for k in range(size+1): clr[(k,-k,0)] = 0 clr[(k,1,-k-1)] = 1 for k in range(size+1,2*size+1): clr[(k,-size,size-k)] = 0 clr[(k,size-k+1,-size-1)] = 1 #free boundary clr[(2*size+1,-size,-size-1)] = 2 def fill_all(): for k in range(1,size+1): for l in range(1,k+1): clr[(k,l-k,-l)] = whrandom.randint(0,1) for k in range(size+1,2*size): for l in range(k-size+1,size+1): clr[(k,l-k,-l)] = whrandom.randint(0,1) def exploration(): state = (0,(1,1,-2)) explor = [] explor.append(state[1]) for t in range(2*size**2): state = next(state) if not state: break explor.append(state[1]) else: assert False, "too many moves" return explor def forget_middle(): for k in range(size+1-(perturb+1)//2,size+1): for l in range(1,k+1): clr.pop((k,l-k,-l),0) for k in range(size+1,size+1+perturb//2): for l in range(k-size+1,size+1): clr.pop((k,l-k,-l),0) def fill_middle(): for k in range(size+1-(perturb+1)//2,size+1): for l in range(1,k+1): clr[(k,l-k,-l)] = whrandom.randint(0,1) for k in range(size+1,size+1+perturb//2): for l in range(k-size+1,size+1): clr[(k,l-k,-l)] = whrandom.randint(0,1) def shorten(lst,eps): cur = lst[0] res = [cur] for elm in lst[1:]: d = (elm[0]-cur[0])**2 + (elm[1]-cur[1])**2 + (elm[2]-cur[2])**2 if d>eps: cur = elm res.append(cur) return res def next ((direction,point)): "point: 3 coordinates, fine lattice" if point[0]%3 == 1: parity = 1 else: parity = -1 if direction == 1: point = ( point[2], point[0], point[1] ) elif direction == -1: point = ( point[1], point[2], point[0] ) pivot = ( point[0]+2*parity, point[1]-parity, point[2]-parity ) # pivot: 3 coordinates, multiples of 3 if direction == -1: pivot = ( pivot[2], pivot[0], pivot[1] ) elif direction == 1: pivot = ( pivot[1], pivot[2], pivot[0] ) color = givecolor(pivot) if color == 2: return if color == 1: point = ( point[0]+parity, point[1]-2*parity, point[2]+parity ) else: point = ( point[0]+parity, point[1]+parity, point[2]-2*parity ) if direction == -1: point = ( point[2], point[0], point[1] ) elif direction == 1: point = ( point[1], point[2], point[0] ) if color==0: direction += 1 else: direction += -1 if direction == -2: direction = 1 elif direction == 2: direction = -1 return (direction,point) def distance(explor1,explor2,big): l1,l2 = len(explor1),len(explor2) d = {0:0} for t in range(1,l1+l2-1): d0 = d d = {} for t1 in d0: former = d0[t1] for s1 in (t1,t1+1): if s1 >= l1: continue s2 = t - s1 if s2 >= l2: continue pt1 = explor1[s1] pt2 = explor2[s2] new = (pt1[0]-pt2[0])**2+(pt1[1]-pt2[1])**2+(pt1[2]-pt2[2])**2 if new < former: new = former if new < d.get(s1,big): d[s1] = new # print "s1=%d s2=%d new=%4.2f" % (s1,s2,new) return d.get(l1-1,big)