Skip to content

Instantly share code, notes, and snippets.

@kvalv
Created September 20, 2017 16:30
Show Gist options
  • Select an option

  • Save kvalv/e2f0cbf276a121a7080f94ef6911c487 to your computer and use it in GitHub Desktop.

Select an option

Save kvalv/e2f0cbf276a121a7080f94ef6911c487 to your computer and use it in GitHub Desktop.
import numpy as np
import seaborn as sns
from parse import state_to_occupancy
from graphics import Rectangle, Point
import time
def gen_rectangle(o, x, y, l, color='black', dx=100, dy=100):
p1 = Point(x*dx, y*dy)
if o == 0: # horizontal
p2 = Point(x*dx + dx*l, (1+y)*dy)
else:
p2 = Point((1+x)*dx, y*dy + dy*l)
rectangle = Rectangle(p1, p2)
rectangle.setFill(color)
return rectangle
class Node():
def __init__(self, state):
self.state = state
self.g = np.inf
self.h = np.inf
self.f = np.inf
self.parent = None
self.children = []
n_colors = len(self.state)
self.color_palette = ['#ff0000'] + sns.color_palette('muted', n_colors).as_hex()
def __repr__(self):
return f'{str(state_to_occupancy(self.state, debug=True))}\nf={self.f}\ng={self.g}, h={self.h}'
def __eq__(self, other):
return self.state == other.state
def __neq__(self, other):
return not self.__eq__(other)
def get_ancestors(self):
chain = [self]
current = self
while current.parent:
current = current.parent
chain.append(current)
# if len(chain) != 1:
# chain.append(current)
return chain
def visualize_state(self, window):
while window.items:
window.items[0].undraw()
rectangles = []
for (state, color) in zip(self.state, self.color_palette):
rectangle = gen_rectangle(*state, color=color)
rectangles.append(rectangle)
rectangle.draw(window)
# time.sleep(0.1)
def visualize_trace(self, window):
for each in self.get_ancestors()[::-1]:
import pdb; pdb.set_trace()
each.visualize_state(window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment