def apply(self, edge: Edge) -> bool: RemoveConstraintEdgeAction().apply(edge) e_index = self.polygon.edges.index(edge) self.polygon.edges.remove(edge) new_vertex = Vertex(line_middle_point(edge.v1.point, edge.v2.point)) v1_index = v2_index = -1 for i in range(len(self.polygon.vertices)): vertex = self.polygon.vertices[i] if vertex == edge.v1: v1_index = i if vertex == edge.v2: v2_index = i if v1_index < 0 or v2_index < 0: return False # first and last vertex if abs(v1_index - v2_index) == len(self.polygon.vertices) - 1: self.polygon.vertices.append(new_vertex) else: self.polygon.vertices.insert(max(v1_index, v2_index), new_vertex) self.polygon.edges.insert(e_index, Edge(edge.v1, new_vertex)) self.polygon.edges.insert(e_index + 1, Edge(edge.v2, new_vertex)) return True
def apply(self, edge: Edge) -> bool: if try_set_relation( edge, FixedEdgeLengthVertexRelation(edge.v1, edge.v2, self.length)): edge.constraint = EdgeConstraint.FIXED_LENGTH return True return False
def test_puct_non_root_node(self): np.random.seed(1) puct = PUCT(0.8, 0.2, 1) game = Game() parent_node = Node(game) parent_node.edges.append( Edge(parent_node, Node(game.move(game.get_possible_moves()[0])), 0.14805108, 29)) parent_node.edges.append( Edge(parent_node, Node(game.move(game.get_possible_moves()[1])), 0.14307857, 35)) parent_node.edges.append( Edge(parent_node, Node(game.move(game.get_possible_moves()[2])), 0.14475949, 37)) parent_node.edges.append( Edge(parent_node, Node(game.move(game.get_possible_moves()[3])), 0.1387326, 38)) parent_node.edges.append( Edge(parent_node, Node(game.move(game.get_possible_moves()[4])), 0.14208362, 39)) parent_node.edges.append( Edge(parent_node, Node(game.move(game.get_possible_moves()[5])), 0.14188258, 40)) parent_node.edges.append( Edge(parent_node, Node(game.move(game.get_possible_moves()[6])), 0.14141211, 41)) simulation_edge = puct.puct(parent_node, is_root=False) self.assertEquals(simulation_edge.action, 29)
def test_puct_root_node(self): np.random.seed(1) puct = PUCT(0.8, 0.2, 1) game = Game() parent_node = Node(game) parent_node.edges.append( Edge(parent_node, Node(game.move(game.get_possible_moves()[0])), 0.14285715, 35)) parent_node.edges.append( Edge(parent_node, Node(game.move(game.get_possible_moves()[1])), 0.14285715, 36)) parent_node.edges.append( Edge(parent_node, Node(game.move(game.get_possible_moves()[2])), 0.14285715, 37)) parent_node.edges.append( Edge(parent_node, Node(game.move(game.get_possible_moves()[3])), 0.14285715, 38)) parent_node.edges.append( Edge(parent_node, Node(game.move(game.get_possible_moves()[4])), 0.14285715, 39)) parent_node.edges.append( Edge(parent_node, Node(game.move(game.get_possible_moves()[5])), 0.14285715, 40)) parent_node.edges.append( Edge(parent_node, Node(game.move(game.get_possible_moves()[6])), 0.14285715, 41)) simulation_edge = puct.puct(parent_node, is_root=True) self.assertEquals(simulation_edge.action, 35)
def __init__(self): """ 构造函数,构造一个空的图 @author: [email protected] """ self.nodes = {} self.edges = [Edge(0, 0, 0, 0)] # 初始情况下有一个为空的 self.adjacency_list = {} self.number_of_nodes = 0
def test_move_to_leaf(self): game = Game() root = Node(game) action_encoder = ActionEncoder(DirectionResolver()) mcts = MCTS(root, config={ 'ALPHA': 0.8, 'CPUCT': 1, 'EPSILON': 0.2 }, model=None, state_encoder=None, action_encoder=action_encoder) puct = MagicMock() mcts.puct = puct child1 = Node(game.move(game.get_possible_moves()[0])) child2 = Node(game.move(game.get_possible_moves()[1])) child3 = Node(game.move(game.get_possible_moves()[2])) edge1 = Edge( root, child1, 0.33, action_encoder.convert_move_to_action_id( game.get_possible_moves()[0])) edge2 = Edge( root, child2, 0.34, action_encoder.convert_move_to_action_id( game.get_possible_moves()[1])) edge3 = Edge( root, child3, 0.33, action_encoder.convert_move_to_action_id( game.get_possible_moves()[2])) root.edges.append(edge1) root.edges.append(edge2) root.edges.append(edge3) puct.puct.return_value = edge2 leaf, value, done, breadcrumbs = mcts.move_to_leaf() self.assertEquals(leaf, child2) self.assertEquals(value, 0) self.assertEquals(done, 0) self.assertEquals(False, 0) self.assertEquals(True, 1)
def __init__(self, vertices: List[Vertex]): super().__init__() self.vertices: List[Vertex] = vertices self.edges: List[Edge] = [] self.painter_path = self.__update_painter_path__() self.is_moving = False self.move_origin: Union[QPoint, None] = None for i in range(len(vertices)): i2 = (i + 1) % len(vertices) self.edges.append(Edge(vertices[i], vertices[i2]))
def test_backfill(self): game_root = Game() root = Node(game_root) action_encoder = ActionEncoder(DirectionResolver()) position1 = game_root.move(game_root.get_possible_moves()[0]) child1 = Node(position1) edge1 = Edge( root, child1, 0.3, action_encoder.convert_move_to_action_id( game_root.get_possible_moves()[0])) position2 = position1.move(position1.get_possible_moves()[0]) child2 = Node(position2) edge2 = Edge( child1, child2, 0.2, action_encoder.convert_move_to_action_id( game_root.get_possible_moves()[0])) edge2.stats['N'] = 4 edge2.stats['W'] = 1 mcts = MCTS(root, config={ 'ALPHA': 0.8, 'CPUCT': 1, 'EPSILON': 0.2 }, model=None, state_encoder=None, action_encoder=action_encoder) mcts.backfill(child2, -1, [edge2, edge1]) self.assertEquals(edge2.stats['N'], 5) self.assertEquals(edge2.stats['W'], 2) self.assertEquals(edge2.stats['Q'], 2 / 5) self.assertEquals(edge1.stats['N'], 1) self.assertEquals(edge1.stats['W'], -1) self.assertEquals(edge1.stats['Q'], -1)
def _get_feat(self, db, f_class): if f_class == 'color': f_c = Color() elif f_class == 'daisy': f_c = Daisy() elif f_class == 'edge': f_c = Edge() elif f_class == 'gabor': f_c = Gabor() elif f_class == 'hog': f_c = HOG() elif f_class == 'vgg': f_c = VGGNetFeat() elif f_class == 'res': f_c = ResNetFeat() return f_c.make_samples(db, verbose=False)
def apply(self, edge: Edge) -> bool: if [ rel for rel in edge.v1.relations if rel.constraint == EdgeConstraint.HORIZONTAL ]: return False if [ rel for rel in edge.v2.relations if rel.constraint == EdgeConstraint.HORIZONTAL ]: return False if try_set_relation(edge, HorizontalEdgeVertexRelation(edge.v1, edge.v2)): edge.constraint = EdgeConstraint.HORIZONTAL return True return False
def add_edge(self, from_point_id, to_point_id, edge_type): """ 向图中添加一条边,如果边的起点或终点为0,那么将不执行添加操作 @param from_point_id: 起点的点id @param to_point_id: 终点的点id @param edge_type: 边的类型,如果该值为1, 那么就代表迭代间依赖,否则表示迭代内依赖 @author: [email protected] """ if from_point_id == "0" or to_point_id == "0": # 若起点或终点为0就直接结束 return from_point_id, to_point_id, edge_type = int(from_point_id), int( to_point_id), int(edge_type) self.edges.append( Edge(from_point_id, to_point_id, edge_type, self.adjacency_list[from_point_id])) self.adjacency_list[ from_point_id] = self.number_of_edges # 将当前边的前一条边加入 self.number_of_edges += 1
def apply(self, edge: Edge) -> bool: if not edge.constraint: return True matching_relations = [ rel for rel in edge.v1.relations if rel.constraint == edge.constraint ] removed = [ rel for rel in edge.v2.relations if rel in matching_relations ] if len(removed) != 1: return False edge.v1.relations.remove(removed[0]) edge.v2.relations.remove(removed[0]) edge.constraint = None return True
def visit_vertex(self, vertex: Vertex) -> bool: if len(self.polygon.vertices) == 3: return False self.polygon.vertices.remove(vertex) del_edges = [ e for e in self.polygon.edges if e.v1 == vertex or e.v2 == vertex ] v1 = __opposite_vertex__(del_edges[0], vertex) v2 = __opposite_vertex__(del_edges[1], vertex) del_index = self.polygon.edges.index(del_edges[0]) remove_constraint = RemoveConstraintEdgeAction() for edge in del_edges: remove_constraint.apply(edge) self.polygon.edges.remove(edge) self.polygon.edges.insert(del_index, Edge(v1, v2)) return True
def add_edge(self, id1: int, id2: int, weight: float) -> bool: e1 = Edge(id1, id2, weight) self.Edges[str(id1) + "," + str(id2)] = e1
def test_act_tau_0(self): config = { 'ALPHA': 0.8, 'CPUCT': 1, 'EPSILON': 0.2, 'ACTION_SIZE': 32 * 4 * 7, 'MCTS_SIMULATIONS': 3 } action_encoder = ActionEncoder(DirectionResolver()) agent = Agent(model=None, action_encoder=action_encoder, state_encoder=StateEncoder(), name='player1', config=config) game_root = Game() root_node = Node(game_root) child1 = Node(game_root.move(game_root.get_possible_moves()[0])) edge1 = Edge(root_node, child1, 0.33, 8) edge1.stats['N'] = 10 edge1.stats['Q'] = 0.2 root_node.edges.append(edge1) child2 = Node(game_root.move(game_root.get_possible_moves()[1])) edge2 = Edge(root_node, child2, 0.5, 104) edge2.stats['N'] = 20 edge2.stats['Q'] = 0.5 root_node.edges.append(edge2) child3 = Node(game_root.move(game_root.get_possible_moves()[2])) edge3 = Edge(root_node, child3, 0.17, 9) edge3.stats['N'] = 15 edge3.stats['Q'] = 0.3 root_node.edges.append(edge3) agent.prepare_mcts_for_next_action = MagicMock() mcts = MagicMock() mcts.root = root_node mcts.evaluate_leaf.return_value = 0.7 agent.mcts = mcts mcts.move_to_leaf.return_value = (root_node, 0.5, False, []) action, pi, value = agent.act(game_root, tau=0) self.assertEqual(action, [9, 14]) self.assertEqual(value, 0.5) self.assertEqual(pi[8], 10/(10 + 20 + 15)) self.assertEqual(pi[9], 15/(10 + 20 + 15)) self.assertEqual(pi[8 + 3*32], 20/(10 + 20 + 15))
def test_puct_non_root_node_exploration(self): np.random.seed(1) game = Game() puct = PUCT(0.8, 0.2, 1) parent_node = Node(game) edge1 = Edge(parent_node, Node(game.move(game.get_possible_moves()[0])), 0.14805108, 29) edge1.stats['N'] = 100 parent_node.edges.append(edge1) edge2 = Edge(parent_node, Node(game.move(game.get_possible_moves()[1])), 0.14307857, 35) edge2.stats['N'] = 100 parent_node.edges.append(edge2) edge3 = Edge(parent_node, Node(game.move(game.get_possible_moves()[2])), 0.14475949, 37) edge3.stats['N'] = 100 parent_node.edges.append(edge3) edge4 = Edge(parent_node, Node(game.move(game.get_possible_moves()[3])), 0.1387326, 38) edge4.stats['N'] = 10 parent_node.edges.append(edge4) edge5 = Edge(parent_node, Node(game.move(game.get_possible_moves()[4])), 0.14208362, 39) edge5.stats['N'] = 100 parent_node.edges.append(edge5) edge6 = Edge(parent_node, Node(game.move(game.get_possible_moves()[5])), 0.14188258, 40) edge6.stats['N'] = 100 parent_node.edges.append(edge6) edge7 = Edge(parent_node, Node(game.move(game.get_possible_moves()[6])), 0.14141211, 41) edge7.stats['N'] = 100 parent_node.edges.append(edge7) simulation_edge = puct.puct(parent_node, is_root=False) self.assertEquals(simulation_edge.action, 38)