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 get_possible_moves(current_state: State, env_config: EnvironmentConfiguration) -> List[Edge]:
     current_vertex_name = current_state.get_current_vertex_name()
     vertexes_dict = env_config.get_vertexes()
     edges_dict = {k: v for k, v in env_config.get_edges().items() if k not in env_config.get_blocked_edges()}
     current_vertex = vertexes_dict[current_vertex_name]
     names_of_edges = [edge for edge in current_vertex.get_edges() if edge not in env_config.get_blocked_edges()]
     possible_edges = []
     for edge_name in names_of_edges:
         possible_edges.append(edges_dict[edge_name])
     return possible_edges
    def print_environment(env_config: EnvironmentConfiguration):
        num_of_vertex = env_config.get_vertices_num()
        deadline = env_config.get_deadline()
        edges_dict = env_config.get_edges()
        vertexes_dict = env_config.get_vertexes()

        print(EnvironmentUtils._NUMBER_OF_VERTICES_PREFIX + EnvironmentUtils._SPACE_SEPARATOR + str(
            num_of_vertex))
        print(EnvironmentUtils._DEADLINE_PREFIX + EnvironmentUtils._SPACE_SEPARATOR + str(deadline))

        for vertex in vertexes_dict.values():
            EnvironmentUtils.__print_vertex(vertex)
        for edge in edges_dict.values():
            EnvironmentUtils.__print_edge(edge)
        print("Blocked edges: ", env_config.get_blocked_edges())
    def read_configuration(file_path: str):
        with open(file_path, 'r') as f:
            lines = [
                line.split(ConfigurationReader.COMMENT_SEPARATOR)[0].strip()
                for line in f if line.strip()
            ]  # removes comments & empty lines.

        vertexes_dict = {}
        edges_dict = {}
        vertices_num = -1
        deadline = -1  # default values.
        for current_line in lines:
            if current_line.startswith("#N"):
                vertices_num = int(
                    current_line.split(ConfigurationReader.SPACE_SEPARATOR)[1])
            elif current_line.startswith("#D"):
                deadline = float(
                    current_line.split(ConfigurationReader.SPACE_SEPARATOR)[1])
            elif current_line.startswith("#V"):
                name, vertex = ConfigurationReader.create_vertex(current_line)
                vertexes_dict[name] = vertex
            elif current_line.startswith("#E"):
                name, edge = ConfigurationReader.create_edge(current_line)
                edges_dict[name] = edge
                # add the edge name to relevant vertexes
                first_vertex, second_vertex = edge.get_vertex_names()
                vertexes_dict[first_vertex].add_edge_name(edge.get_edge_name())
                vertexes_dict[second_vertex].add_edge_name(
                    edge.get_edge_name())
        return EnvironmentConfiguration(vertices_num, deadline, vertexes_dict,
                                        edges_dict)
 def get_required_vertexes(
         env_config: EnvironmentConfiguration) -> Dict[str, bool]:
     required_vertexes = {}
     for vertex_name in env_config.get_vertexes().values():
         if vertex_name.get_people_num() > 0:
             required_vertexes[vertex_name.get_vertex_name()] = False
     return required_vertexes
Example #7
0
 def __make_node(self, state: State, env_conf: EnvironmentConfiguration):
     name = state.get_current_vertex_name()
     vertex = env_conf.get_vertexes()[name]
     vertex.set_state(state)
     vertex.set_cost(
         len(state.get_required_vertexes()) -
         sum(state.get_required_vertexes().values()))
     return vertex
Example #8
0
 def get_saved_people_num(state: State, current_traveled_states, env_conf: EnvironmentConfiguration) -> List[int]:
     score = 0
     traveled_vertexes = [vertex_name for vertex_name in StateUtils.get_state_traveled_vertexes(state) if
                          vertex_name not in current_traveled_states]
     current_traveled_states.append(state.get_current_vertex_name())
     vertexes_dict = env_conf.get_vertexes()
     for vertex in traveled_vertexes:
         score += vertexes_dict[vertex].get_people_num()
     return score
 def g(node: Vertex, env_conf: EnvironmentConfiguration) -> int:
     current_node = copy.deepcopy(node)
     edges = env_conf.get_edges()
     edges_of_path = []
     cost = 0
     while current_node is not None:
         edges_of_path.append(current_node.get_action() if current_node.get_action() is not None else "")
         current_node = current_node.get_parent_vertex()
     # calculate the cost to the solution
     for edge_name in filter(None, edges_of_path):
         cost += edges[edge_name].get_weight()
     return cost
Example #10
0
    def __result(self, action: str, state: State, is_max: bool,
                 env_config: EnvironmentConfiguration) -> State:
        """

        :param action: edge name
        :param state: current state
        :return: next state after moving on edge action from the given state
        """
        next_vertex = env_config.get_vertexes()[
            state.get_current_vertex_name()]
        next_vertex.set_state(state)
        return EnvironmentUtils.get_next_vertex(next_vertex, action,
                                                self.step_cost, env_config,
                                                is_max).get_state()
Example #11
0
    def __successor_func(
            self, node: Vertex,
            env_conf: EnvironmentConfiguration) -> List[Tuple[str, Vertex]]:
        current_state = node.get_state()
        edges_list = EnvironmentUtils.get_possible_moves(
            current_state, env_conf)
        self._expansions_num += 1

        names_of_edges = [edge.get_edge_name() for edge in edges_list]
        edge_to_next_state_list = []
        for edge_name in names_of_edges:
            next_vertex = EnvironmentUtils.get_next_vertex(
                node, edge_name, self.step_cost, env_conf)
            env_conf.get_vertexes()[
                next_vertex.get_vertex_name()] = next_vertex
            edge_to_next_state_list.append((edge_name, next_vertex))
        return edge_to_next_state_list
Example #12
0
    def restore_solution(
            self, goal_node: Vertex,
            env_conf: EnvironmentConfiguration) -> Tuple[List, int]:
        vertexes_path = []
        current_node = goal_node
        edges = env_conf.get_edges()
        edges_of_path = []
        cost = 0
        while current_node is not None:
            edges_of_path.append(current_node.get_action() if current_node.
                                 get_action() is not None else "")
            vertexes_path.append(copy.deepcopy(current_node))
            current_node = current_node.get_parent_vertex()
        vertexes_path.reverse()

        # calculate the cost to the solution
        for edge_name in filter(None, edges_of_path):
            cost += edges[edge_name].get_weight()
        return vertexes_path, cost
Example #13
0
    def minimax(self, state: State, action_to_state: str, depth: int,
                alpha: int, beta: int, is_max_player: bool,
                env_config: EnvironmentConfiguration):
        if TerminalEvaluator.was_deadline_passed(state,
                                                 env_config.get_deadline()):
            return None, TerminalEvaluator.terminate_eval(
                state.get_parent_state(), self.__mode, is_max_player)
        if TerminalEvaluator.are_no_more_people(state):
            return action_to_state, TerminalEvaluator.terminate_eval(
                state, self.__mode, is_max_player)
        if depth == 0:
            return action_to_state, TerminalEvaluator.cut_off_utility_eval(
                state, is_max_player, env_config.get_vertexes())
        possible_edges = EnvironmentUtils.get_possible_moves(state, env_config)
        possible_actions = [edge.get_edge_name() for edge in possible_edges]
        if is_max_player:
            # Max Player
            best_action = None
            max_utility_value = -10000000
            max_opponent_utility = -10000000
            best_score = None

            for action in possible_actions:
                possible_next_state = self.__result(action,
                                                    copy.deepcopy(state),
                                                    is_max_player, env_config)
                is_max_next_player = False if self.__mode == MiniMaxAgent.ADVERSARIAL_MODE else True
                new_action, scores = self.minimax(
                    copy.deepcopy(possible_next_state), action, depth - 1,
                    alpha, beta, is_max_next_player, env_config)
                print("cost of possible_next_state = ",
                      possible_next_state.get_cost())
                current_utility, opponent_utility = scores
                if self.__is_better_score(max_utility_value, current_utility,
                                          max_opponent_utility,
                                          opponent_utility):
                    max_utility_value = current_utility
                    max_opponent_utility = opponent_utility
                    best_score = scores
                    best_action = action
                alpha = max(alpha, current_utility)
                if self.__mode == MiniMaxAgent.ADVERSARIAL_MODE and beta <= alpha:
                    break
            return best_action, best_score

        else:
            # Min Player
            min_utility_value = 10000000
            best_action = None
            best_score = None

            for action in possible_actions:
                possible_next_state = self.__result(action, state,
                                                    is_max_player, env_config)
                _, scores = self.minimax(copy.deepcopy(possible_next_state),
                                         action, depth - 1, alpha, beta, True,
                                         env_config)
                current_utility = scores[1]  # score of the minimum player
                if current_utility < min_utility_value:
                    min_utility_value = current_utility
                    best_score = scores
                    best_action = action
                beta = min(beta, current_utility)
                if self.__mode == MiniMaxAgent.ADVERSARIAL_MODE and beta <= alpha:
                    break
            return best_action, best_score