def problem_2(actions): path = Path() for action in actions: path.move(action) if len(path.intersections) > 0: break if path.intersections: return path.intersections[0].tc_distance(Point(0, 0)) return path.position.point.tc_distance(Point(0, 0))
def problem_1(m): start = m[Point(1, 1)] goal = m[Point(31, 39)] came_from, cost_so_far = m.a_star_search(start=start, goal=goal) path = m.reconstruct_path(came_from=came_from, start=start, goal=goal) for tail in path: tail.bread_crumbs = True print "Problem 1:\n\tThe fewest number of steps required" \ "for going from (1, 1) to (31, 39) are %s" % (len(path)-1) print m
def get_target(self, world: World, tick: int): center_x = world.width / 2 center_y = world.height / 2 if self.last_target is None: self.last_target = (Point(center_x + random.uniform(-1, 1) * center_x, center_y + random.uniform(-1, 1) * center_y), tick) else: if tick - self.last_target[1] > 50: self.last_target = (Point(center_x + random.uniform(-1, 1) * center_x, center_y + random.uniform(-1, 1) * center_y), tick) return self.last_target[0]
def _connect_maze(self): for point in self.maze: tail = self.maze[point] if point.x+1 < self.x: tail.r = self.maze[Point(point.x+1, point.y)] if point.x-1 >= 0: tail.l = self.maze[Point(point.x-1, point.y)] if point.y+1 < self.y: tail.u = self.maze[Point(point.x, point.y+1)] if point.y-1 >= 0: tail.d = self.maze[Point(point.x, point.y-1)]
def __repr__(self): rep = '' for y in range(self.y): for x in range(self.x): if isinstance(self.maze[Point(x, y)], Corridor): if self.maze[Point(x, y)].bread_crumbs: rep += '0' else: rep += '.' else: rep += '+' rep += '\n' return rep
def problem_1(m): start = m[Point(0, 0)] goal = m[Point(3, 3)] print 'Start search...' came_from, cost_so_far = m.non_linear_search(start=start, goal=goal) print came_from print 'Search ended...' print 'Retrieving path...' path = m.reconstruct_path(came_from=came_from, start=start, goal=goal) for tail in path: tail.bread_crumbs = True print "Problem 1:\n\tThe shortest path is %s" % (len(path) - 1) print m
class Position(object): def __init__(self, x=0, y=0, orientation='N'): self.orientation = CardinalPoints(orientation) self.point = Point(x, y) def __str__(self): return 'Orientation {}, Position {}'.format( self.orientation, self.point) def move(self, steps): if self.orientation.position == 'N': self.point.move(0, steps) if self.orientation.position == 'W': self.point.move(-steps, 0) if self.orientation.position == 'S': self.point.move(0, -steps) if self.orientation.position == 'E': self.point.move(steps, 0) def turn_left(self): self.orientation.turn_left() def turn_right(self): self.orientation.turn_right()
def _init_maze(self): for x in range(self.x): for y in range(self.y): origin = (x * x + 3 * x + 2 * x * y + y + y * y) + self.seed # Find the binary representation of origin; count the # number of bits that are 1 conditioner = len( filter(lambda val: int(val) == 1, bin(origin)[2:])) if conditioner % 2 == 0: corridor = Corridor(Point(x, y)) self.maze[corridor.tail_id] = corridor else: wall = Wall(Point(x, y)) self.maze[wall.tail_id] = wall
class TestPoint(unittest.TestCase): def setUp(self): self.p = Point(0, 0) @speed_test def test_move(self): self.p.move(1, 2) self.assertEqual(self.p.x, 1) self.assertEqual(self.p.y, 2) @speed_test def test_tc_distance(self): new_p = Point(3, 5) tc_d = self.p.tc_distance(new_p) self.assertEqual(tc_d, -8)
def __init__(self, data): cur_fr = data self.id = cur_fr.get('Id') self.pos = Point(cur_fr.get('X'), cur_fr.get('Y')) self.radius = cur_fr.get('R') self.mass = cur_fr.get('M') self.speed = Vector(cur_fr.get('SX'), cur_fr.get('SY'))
def problem_2(m): start = m[Point(1, 1)] tails, _ = m.find_all(start, steps=50) for tail in tails: tail.bread_crumbs = True print "Problem 2:\n\t Can reach in at most 50 " \ "steps a total of %s tails" % (len(tails)) print m
def __repr__(self): rep = '' for y in range(self.y): for x in range(self.x): if self.maze[Point(x, y)].bread_crumbs: rep += '0' else: rep += '.' rep += '\n' return rep
def samplePoints(function, sample_space, ordered_params): for param in sample_space.keys(): exec("{0} = Symbol('{0}')".format(param)) combinations = itertools.product(*(sample_space[param] for param in ordered_params)) symbols = [] for symbol in ordered_params: symbols.append(eval(symbol)) function = function.replace("^", "**") function = parse_expr(function) results = [] for values in combinations: result = function.subs(dict(zip(symbols, values))) if not result.has(oo, -oo, zoo, nan): results.append(Point(values, float(result))) return results
def __init__(self, item): self.pos = Point(item.get('X'), item.get('Y'))
def __init__(self, x=0, y=0, orientation='N'): self.orientation = CardinalPoints(orientation) self.point = Point(x, y)
def test_move(self): self.p.move(10) res = Point(0, 10) self.assertEqual(self.p.point, res)
def problem_1(actions): path = Path() for action in actions: path.move(action) return path.position.point.tc_distance(Point(0, 0))
def test_tc_distance(self): new_p = Point(3, 5) tc_d = self.p.tc_distance(new_p) self.assertEqual(tc_d, -8)
def _init_maze(self): for x in range(self.x): for y in range(self.y): corridor = Corridor(Point(x, y)) self.maze[corridor.tail_id] = corridor
def setUp(self): self.p = Point(0, 0)
def __init__(self, item): self.pos = Point(item.get('X'), item.get('Y')) self.mass = item.get('M') self.id = item.get('Id')
def test_move(self): self.p.move('L3') self.assertEqual(self.p.position.orientation.position, 'W') self.assertEqual(self.p.position.point, Point(-3, 0))