def push_h_line(self, x, py): self.tasks.append( PushLine(self.game, self.buster, Line(Point(self.x, self.y), Point(x, self.y)), Line(Point(self.x, py), Point(x, py)))) self.x = x return self
def push_v_line(self, y, px): self.tasks.append( PushLine(self.game, self.buster, Line(Point(self.x, self.y), Point(self.x, y)), Line(Point(px, self.y), Point(px, y)))) self.y = y return self
def gen_initial_solution(data, K): random_centroids = np.copy(data[np.random.choice(np.arange(len(data)), K)]) centroid_points = np.array([]) i = 0 for c in random_centroids: centroid_point = Point(c, label=i) i += 1 centroid_points = np.append(centroid_points, centroid_point) # Point class wrapper data_points = np.array([Point(np.copy(x)) for x in data]) return Solution(centroid_points, data_points)
def combine_partitions(sol1, sol2, c_com, K): N = sol1.points_.size combine_p = np.array([]) for i in range(N): # x1,x2 are the same points # x: new point x = sol1.points_[i] d1 = sol1.distance_to_centroid(x, c_com[x.label_]) d2 = sol2.distance_to_centroid(x, c_com[x.label_ + K]) if d1 < d2: x_new = Point(x.xy_, x.label_) else: x_new = Point(x.xy_, x.label_ + K) combine_p = np.append(combine_p, x_new) return combine_p
def step(self): ghosts = copy.deepcopy(list(self.game.ghosts.values())) ghosts = [g for g in ghosts if g.is_visible and g.busters_cnt == 0] for ghost in ghosts: if ghost.danger_point is not None and ghost.busters_cnt == 0: ghost.move_backward(ghost.danger_point, GHOST_STEP) ghosts = [g for g in ghosts if (g.x, g.y) != (self.game.base.x, self.game.base.y) and min(self.buster.x, self.game.base.x) <= g.x <= max(self.buster.x, self.game.base.x) and min(self.buster.y, self.game.base.y) <= g.y <= max(self.buster.y, self.game.base.y)] if len(ghosts) > 0: nearest_ghost = self.buster.find_all_nearest(ghosts)[0] p = Point(self.buster.x, self.buster.y) p.move_toward(nearest_ghost, np.sqrt(self.buster.distance(nearest_ghost))-1) else: p = self.game.base return f'MOVE {p.x} {p.y}'
def test_basic(): input_str = ( "(1,-5,7,0);(1,-3,12,4);(1,0,8,2);(1,4,4,10)\n" "(1,-9,2,3);(1,-4,0,0);(1,-1,1,2)\n" "(1,-3,-5,5);(1,-1,-3,7);(0,4,-4,12);(1,8,-2,4);(1,10,-1, 3)\n" "(1,-6,-7,2);(1,-1,-8,1);(1,3,-12,5);(1,9,-8,7)\n" "(1,-5,-15,1);(1,-6,-16,8);(1,4,-15,1);(1,5,-17,2);(1,6,-15,9);(1,8,-15,7)" ) result = shortestRoute(input_str) assert result[0] == 26.885521976324867 assert result[1] == [ Point(active=1, x=0, y=8, z=2), Point(active=1, x=-1, y=1, z=2), Point(active=1, x=-3, y=-5, z=5), Point(active=1, x=-6, y=-7, z=2), Point(active=1, x=-5, y=-15, z=1), ]
def test_modified_espresso_group(): input_str = ( "(1,-5,7,0);(1,-3,12,4);(1,0,8,2);(1,4,4,10);(1,-5,6,0)\n" "(1,-9,2,3);(0,-4,0,0);(1,-1,1,2)\n" "(1,-3,-5,5);(1,-1,-3,7);(0,4,-4,12);(1,8,-2,4);(1,10,-1, 3)\n" "(1,-6,-7,2);(1,-1,-8,1);(1,3,-12,5);(1,9,-8,7)\n" "(1,-5,-15,1);(1,-6,-16,8);(1,4,-15,1);(1,5,-17,2);(1,6,-15,9);(1,8,-15,7)" ) result = shortestRoute(input_str) assert result[0] == 26.52265809695876 assert result[1] == [ Point(active=1, x=-5, y=6, z=0), Point(active=1, x=-1, y=1, z=2), Point(active=1, x=-3, y=-5, z=5), Point(active=1, x=-6, y=-7, z=2), Point(active=1, x=-5, y=-15, z=1), ]
def test_out_of_order(): input_str = ( "(1,-5,7,0);(0,-3,12,4);(0,0,8,2);(1,4,4,10)\n" "(1,-9,2,3);(0,-4,0,0);(1,-1,1,2)\n" "(1,-3,-5,5);(0,-1,-3,7);(0,4,-4,12);(1,8,-2,4);(0,10,-1, 3)\n" "(1,-6,-7,2);(1,-1,-8,1);(0,3,-12,5);(0,9,-8,7)\n" "(1,-5,-15,1);(0,-6,-16,8);(1,4,-15,1);(0,5,-17,2);(1,6,-15,9);(0,8,-15,7)" ) result = shortestRoute(input_str) assert result[0] == 27.297768938007273 assert result[1] == [ Point(active=1, x=-5, y=7, z=0), Point(active=1, x=-1, y=1, z=2), Point(active=1, x=-3, y=-5, z=5), Point(active=1, x=-6, y=-7, z=2), Point(active=1, x=-5, y=-15, z=1), ]
def test_single_path(): input_str = ( "(0,-5,7,0);(0,-3,12,4);(1,0,8,2);(0,4,4,10)\n" "(1,-9,2,3);(0,-4,0,0);(0,-1,1,2)\n" "(0,-3,-5,5);(1,-1,-3,7);(0,4,-4,12);(0,8,-2,4);(0,10,-1, 3)\n" "(1,-6,-7,2);(0,-1,-8,1);(0,3,-12,5);(0,9,-8,7)\n" "(0,-5,-15,1);(1,-6,-16,8);(0,4,-15,1);(0,5,-17,2);(0,6,-15,9);(0,8,-15,7)" ) result = shortestRoute(input_str) assert result[0] == 40.05042348818775 assert result[1] == [ Point(active=1, x=0, y=8, z=2), Point(active=1, x=-9, y=2, z=3), Point(active=1, x=-1, y=-3, z=7), Point(active=1, x=-6, y=-7, z=2), Point(active=1, x=-6, y=-16, z=8), ]
def test_solution(self): self.assertEqual( solve([ Triangle(Point(-340, 495), Point(-153, -910), Point(835, -947)), Triangle(Point(-175, 41), Point(-421, -714), Point(574, -645)) ]), 1)
def test_occupied_should_not_switch_to_empty(self): # GIVEN context = [Point(1, 1, x) for x in three_occupied_context] p = Point(1, 2, Point.OCCUPIED) p.set_context(context) # WHEN status = p.new_status() # THEN self.assertEqual(status, Point.OCCUPIED)
def test_floor_should_never_switch(self): # GIVEN context = [Point(1, x, '.') for x in range(8)] p = Point(1, 2, Point.FLOOR) p.set_context(context) # WHEN status = p.new_status() # THEN self.assertEqual(status, Point.FLOOR)
def test_get_status(self): # GIVEN context = [Point(1, x, '.') for x in range(8)] expected_status = Point.EMPTY p = Point(1, 2, expected_status) p.set_context(context) # WHEN status = p.get_status() # THEN self.assertEqual(status, expected_status)
def test_triangle_contains(self): self.assertTrue( Triangle(Point(-340, 495), Point(-153, -910), Point(835, -947)).triangleContainsPoint(Point(0, 0)))
def test_read_triangles(self): triangles = readTriangles("triangles.txt") self.assertEqual(1000, len(triangles)) self.assertEqual( triangles[0], Triangle(Point(-340, 495), Point(-153, -910), Point(835, -947)))
def test_triangle_doesnt_contain(self): self.assertFalse( Triangle(Point(-175, 41), Point(-421, -714), Point(574, -645)).triangleContainsPoint(Point(0, 0)))
from solution import Solution, Point points = [ Point(1, 1), Point(2, 2), Point(3, 3), Point(2, 0), Point(2, 1), Point(2, 3) ] points2 = [Point(1, 1), Point(2, 2), Point(3, 3), Point(2, 0)] s = Solution() print s.maxPoints(points)
def project(self, point: Point) -> Point: x = np.array([point.x, point.y]) - self.d prj = (self.v@x)/([email protected])*self.v + self.d return Point(prj[0], prj[1])
def go_to_point(self, x, y): self.x, self.y = x, y self.tasks.append(GoToPoint(self.game, self.buster, Point(x, y))) return self
def push(g): res = '' for i in g.my_ids: if g.my_id == 1: res += f'MOVE {g.base.x} {g.base.y}\n' else: H1 = 4400 H2 = 2200 if i == 0: if g.my_busters[i].y != H1: res += f'MOVE {0} {H1}\n' elif g.my_busters[i].x != 16000: targets = copy.deepcopy( [gh for gh in g.ghosts.values() if gh.x is not None]) targets = [ gh for gh in targets if (gh.x >= g.my_busters[i].x >= gh.x - 800) and ( gh.y < g.my_busters[i].y) ] current_x, current_y = g.my_busters[i].x, g.my_busters[i].y forecast(targets, [ Point(current_x + 800 * i, current_y) for i in range(4) ]) not_pushed = [ target.id for target in targets if target.y >= H2 ] targets = copy.deepcopy( [gh for gh in g.ghosts.values() if gh.x is not None]) forecast(targets, [ Point(current_x + 800 * i, current_y) for i in range(1) ]) targets = {tar.id: tar for tar in targets} if len(not_pushed) != 0: min_x = 800 min_id = not_pushed[0] for id_ in not_pushed: print(targets[id_].x, g.my_busters[i].x) if abs(targets[id_].x - g.my_busters[i].x) < min_x: min_x = targets[id_].x - g.my_busters[i].x min_id = id_ res += f'MOVE {targets[min_id].x} {H1}\n' else: res += f'MOVE {16000} {H1}\n' else: res += f'MOVE {g.my_busters[i].x} {g.my_busters[i].y}\n' if i == 1: if g.my_busters[0].x == 16000: if g.my_busters[i].y != H2: res += f'MOVE {0} {H2}\n' elif g.my_busters[i].x != 16000: targets = copy.deepcopy([ gh for gh in g.ghosts.values() if gh.x is not None ]) targets = [ gh for gh in targets if (gh.x >= g.my_busters[i].x >= gh.x - 800) and ( gh.y < g.my_busters[i].y) ] current_x, current_y = g.my_busters[i].x, g.my_busters[ i].y forecast(targets, [ Point(current_x + 800 * i, current_y) for i in range(4) ]) not_pushed = [ target.id for target in targets if target.y > 0 ] targets = copy.deepcopy([ gh for gh in g.ghosts.values() if gh.x is not None ]) forecast(targets, [ Point(current_x + 800 * i, current_y) for i in range(1) ]) targets = {tar.id: tar for tar in targets} if len(not_pushed) != 0: min_x = 800 min_id = not_pushed[0] for id_ in not_pushed: print(targets[id_].x, g.my_busters[i].x) if abs(targets[id_].x - g.my_busters[i].x) < min_x: min_x = targets[id_].x - g.my_busters[i].x min_id = id_ res += f'MOVE {targets[min_id].x} {H2}\n' else: res += f'MOVE {16000} {H2}\n' else: res += f'MOVE {g.my_busters[i].x} {g.my_busters[i].y}\n' else: res += f'MOVE {g.my_busters[i].x} {g.my_busters[i].y}\n' return res[:-1]
def mkPoints(points): res = [] for p in points: res.append(Point(p[0], p[1])) return res