def __print_edge(edge: Edge): first_vertex_name, second_vertex_name = edge.get_vertex_names() print( EnvironmentUtils._EDGE_PREFIX + edge.get_edge_name() + EnvironmentUtils._SPACE_SEPARATOR + first_vertex_name + EnvironmentUtils._SPACE_SEPARATOR + second_vertex_name + EnvironmentUtils._SPACE_SEPARATOR + EnvironmentUtils._WEIGHT_PREFIX + str( edge.get_weight()))
def step_cost(self, parent_node: Vertex, action: Edge, new_node: Vertex) -> int: g_value = action.get_weight( ) # weights until g was calculated within get_next_vertex h_value = self.__heuristic_func.calc_estimation_from_goal( new_node.get_state(), None) return g_value + h_value
def get_next_vertex(current_vertex: Vertex, edge_name: str, step_cost: Callable, env_config: EnvironmentConfiguration) -> Vertex: """ :param current_vertex: the current state :param edge_name: edge name from current vertex to the next vertex :param step_cost: function that receives parent_vertex, action, new_node and returns the step cost. :param env_config: environment configuration :return: The new vertex """ current_state = current_vertex.get_state() current_vertex_name = current_vertex.get_vertex_name() edges_dict = env_config.get_edges() vertexes_dict = env_config.get_vertexes() if edge_name not in edges_dict: current_vertex.set_state(current_state) print("No operation for this agent") current_vertex.set_cost( current_vertex.get_cost() + step_cost(current_vertex, Edge("", 0, ("", "")), current_vertex)) return current_vertex # No operation edge = edges_dict[edge_name] first_vertex, sec_vertex = edge.get_vertex_names() next_vertex_name = first_vertex if sec_vertex == current_vertex_name else sec_vertex next_vertex = vertexes_dict[next_vertex_name] next_state = State(next_vertex_name, copy.deepcopy(current_state.get_required_vertexes())) if next_vertex_name in current_state.get_required_vertexes(): next_state.set_visited_vertex(next_vertex_name) next_vertex.set_state(next_state) people_in_next_vertex = next_vertex.get_people_num() new_next_vertex = Vertex(people_in_next_vertex, next_state, next_vertex.get_edges(), current_vertex, edge.get_edge_name(), current_vertex.get_depth(), EnvironmentUtils.g(current_vertex, env_config) + step_cost(current_vertex, edge, next_vertex)) return new_next_vertex
def get_next_vertex(current_vertex: Vertex, edge_name: str, step_cost: Callable, env_config: EnvironmentConfiguration, is_max_player: bool = True) -> Vertex: """ :param current_vertex: the current state :param edge_name: edge name from current vertex to the next vertex :param step_cost: function that receives parent_vertex, action, new_node and returns the step cost. :param is_max_player: True if this is the max player, false otherwise :param env_config: environment configuration :return: The new vertex """ current_state = current_vertex.get_state() current_vertex_name = current_vertex.get_vertex_name() edges_dict = env_config.get_edges() vertexes_dict = env_config.get_vertexes() if edge_name not in edges_dict: current_vertex.set_state(current_state) print("edge_name= ", edge_name) print("No operation for this agent") current_vertex.set_cost(current_vertex.get_cost() + step_cost( current_vertex, Edge("", 0, ("", "")), current_vertex)) return current_vertex # No operation edge = edges_dict[edge_name] first_vertex, sec_vertex = edge.get_vertex_names() next_vertex_name = first_vertex if sec_vertex == current_vertex_name else sec_vertex next_vertex = vertexes_dict[next_vertex_name] scores_of_agents = current_state.get_scores_of_agents() if next_vertex_name in current_state.get_required_vertexes( ) and not current_state.get_required_vertexes()[next_vertex_name]: scores_of_agents = (scores_of_agents[0] + next_vertex.get_people_num(), scores_of_agents[1]) if is_max_player else ( scores_of_agents[0], scores_of_agents[1] + next_vertex.get_people_num()) next_state = State( next_vertex_name, scores_of_agents, copy.deepcopy(current_state.get_required_vertexes()), current_state.get_cost() + step_cost(current_vertex, edge, next_vertex)) if next_vertex_name in current_state.get_required_vertexes(): next_state.set_visited_vertex(next_vertex_name) next_vertex.set_state(next_state) people_in_next_vertex = next_vertex.get_people_num() next_state.set_parent_state(current_state) new_next_vertex = Vertex( people_in_next_vertex, next_state, next_vertex.get_edges(), current_vertex, edge.get_edge_name(), current_vertex.get_depth(), EnvironmentUtils.g(current_vertex, env_config) + step_cost(current_vertex, edge, next_vertex)) return new_next_vertex
def step_cost(self, parent_node: Vertex, action: Edge, new_node: Vertex) -> int: operation = self.__operations[self.__operation_inx - 1] if operation == SaboteurAgent.NO_OPS: return 0 # does nothing if operation == SaboteurAgent.BLOCK: return 1 # takes 1 time unit else: # traverses a lowest-cost remaining edge return action.get_weight()
def create_edge(input_line: str) -> Optional[Tuple[str, Edge]]: parts = input_line.split(ConfigurationReader.SPACE_SEPARATOR) if len(parts) != 4: print( f'input line: {input_line} is invalid. Correct format: #E1 1 2 W1' ) return None name = parts[0].replace("#E", "") first_vertex = parts[1] second_vertex = parts[2] weight = int(parts[3].replace("W", "")) return name, Edge(name, weight, (first_vertex, second_vertex))
def step_cost(self, parent_node: Vertex, action: Edge, new_node: Vertex) -> int: return action.get_weight()