Пример #1
0
 def h_ignore_preconditions(self, node: Node):
     """This heuristic estimates the minimum number of actions that must be
     carried out from the current state in order to satisfy all of the goal
     conditions by ignoring the preconditions required for an action to be
     executed.
     """
     # TODO implement (see Russell-Norvig Ed-3 10.2.3  or Russell-Norvig Ed-2 11.2)
     count = 0
     kb = PropKB()
     kb.tell(decode_state(node.state, self.state_map).pos_sentence())
     for clause in self.goal:
         if clause not in kb.clauses:
             count += 1
     return count
Пример #2
0
 def h_ignore_preconditions(self, node: Node):
     """This heuristic estimates the minimum number of actions that must be
     carried out from the current state in order to satisfy all of the goal
     conditions by ignoring the preconditions required for an action to be
     executed.
     """
     count = 0
     kb = PropKB()
     kb.tell(decode_state(node.state, self.state_map).pos_sentence())
     for clause in self.goal:
         #Atleast one action must be performed for this clause to be sstisfied
         if clause not in kb.clauses:
             count += 1
     return count
Пример #3
0
    def goal_test(self, state: str) -> bool:
        """ Test the state to see if goal is reached

        :param state: str representing state
        :return: bool
        """
        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())

        for clause in self.goal:
            if clause not in kb.clauses:
                return False

        return True
Пример #4
0
    def h_ignore_preconditions(self, node: Node):
        '''
        This heuristic estimates the minimum number of actions that must be
        carried out from the current state in order to satisfy all of the goal
        conditions by ignoring the preconditions required for an action to be
        executed.
        '''
        # TODO implement (see Russell-Norvig Ed-3 10.2.3  or Russell-Norvig Ed-2 11.2)
        count = 0
        prevCount = 0

        current_state = node.state
        current_actions = self.actions_list

        # get # of remaining subgoals
        kb = PropKB()
        kb.tell(decode_state(current_state, self.state_map).pos_sentence())
        current_goals = [
            clause for clause in self.goal if clause not in kb.clauses
        ]

        # calculate how many actions required between current state and goal states
        while current_goals:
            prevCount = count
            for action in current_actions:
                for effect in action.effect_add:
                    if effect in current_goals:
                        # remove subgoal
                        current_goals.remove(effect)
                        # increment count
                        count += 1
                        # update current_state w/o consideration for preconditions
                        # Note implementation is suboptimal but must avoid calling self.result
                        new_state = FluentState([], [])
                        old_state = decode_state(current_state, self.state_map)
                        for fluent in old_state.pos:
                            if fluent not in action.effect_rem:
                                new_state.pos.append(fluent)
                        for fluent in action.effect_add:
                            new_state.pos.append(fluent)
                        for fluent in old_state.neg:
                            if fluent not in action.effect_add:
                                new_state.neg.append(fluent)
                        for fluent in action.effect_rem:
                            new_state.neg.append(fluent)
                        current_state = encode_state(new_state, self.state_map)
            if prevCount == count:
                break
        return count
Пример #5
0
    def actions(self, state: str) -> list:
        """ Return the actions that can be executed in the given state.

        :param state: str
            state represented as T/F string of mapped fluents (state variables)
            e.g. 'FTTTFF'
        :return: list of Action objects
        """
        kb = PropKB()
        # extend state to a (positive) knowledge base
        kb.tell(decode_state(state, self.state_map).pos_sentence())
        possible_actions = [
            a for a in self.actions_list if a.check_precond(kb, a.args)
        ]
        return possible_actions
Пример #6
0
    def actions(self, state: str) -> list:
        """ Return the actions that can be executed in the given state.

        :param state: str
            state represented as T/F string of mapped fluents (state variables)
            e.g. 'FTTTFF'
        :return: list of Action objects
        """
        possible_actions = []
        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())
        for action in self.actions_list:
            if action.check_precond(kb, action.args):
                possible_actions.append(action)
        return possible_actions
Пример #7
0
 def h_ignore_preconditions(self, node: Node):
     """This heuristic estimates the minimum number of actions that must be
     carried out from the current state in order to satisfy all of the goal
     conditions by ignoring the preconditions required for an action to be
     executed.
     """
     count = 0
     kb = PropKB()
     # decode current state to knowledge base
     kb.tell(decode_state(node.state, self.state_map).pos_sentence())
     # count the number of goal clauses that are not fulfilled, that becomes the minimum number of actions required
     for clause in self.goal:
         if clause not in kb.clauses:
             count += 1
     return count
Пример #8
0
    def actions(self, state: str) -> list:
        """ Return the actions that can be executed in the given state.

        :param state: str
            state represented as T/F string of mapped fluents (state variables)
            e.g. 'FTTTFF'
        :return: list of Action objects
        """
        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())
        return [
            action for action in self.actions_list
            if (all([c not in kb.clauses for c in action.precond_neg])
                and all([c in kb.clauses for c in action.precond_pos]))
        ]
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
        kb = PropKB(decode_state(state, self.state_map).sentence())
        action.act(kb, action.args)
        new_state = kb.clauses
        neg = [i for i in kb.clauses if i.op == '~']
        pos = [i for i in kb.clauses if i.op != '~']
        return encode_state(FluentState(pos, neg), self.state_map)
Пример #10
0
 def h_ignore_preconditions(self, node: Node):
     '''
     This heuristic estimates the minimum number of actions that must be
     carried out from the current state in order to satisfy all of the goal
     conditions by ignoring the preconditions required for an action to be
     executed.
     '''
     kb = PropKB()
     kb.tell(decode_state(node.state, self.state_map).pos_sentence())
     kb_clauses = kb.clauses
     actions_count = 0
     for clause in self.goal:
         if clause not in kb_clauses:
             actions_count += 1
     return actions_count
    def goal_test(self, state: str) -> bool:
        """ Test the state to see if goal is reached

        :param state: str representing state
        :return: bool
        """
        # Create a knowledge base from the given state
        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())
        # Iterate through all goal literals
        for goal in self.goal:
            # If goal hasn't been satisified yet then return False
            if goal not in kb.clauses:
                return False
        return True
Пример #12
0
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())
        action.act(kb, action.args)
        new_state = FluentState(kb.clauses, [])

        return encode_state(new_state, self.state_map)
Пример #13
0
 def h_ignore_preconditions(self, node: Node):
     '''
     This heuristic estimates the minimum number of actions that must be
     carried out from the current state in order to satisfy all of the goal
     conditions by ignoring the preconditions required for an action to be
     executed.
     '''
     # TODO implement (see Russell-Norvig Ed-3 10.2.3  or Russell-Norvig Ed-2 11.2)
     kb = PropKB()
     kb.tell(decode_state(node.state, self.state_map).pos_sentence())
     overlap_clauses = [
         clause for clause in kb.clauses if clause in self.goal
     ]
     count = max(len(self.goal) - len(overlap_clauses), 0)
     return count
Пример #14
0
 def actions(self, state: str) -> list:  # of Action
     possible_actions = []
     kb = PropKB()
     kb.tell(decode_state(state, self.state_map).pos_sentence())
     for action in self.actions_list:
         is_possible = True
         for clause in action.precond_pos:
             if clause not in kb.clauses:
                 is_possible = False
         for clause in action.precond_neg:
             if clause in kb.clauses:
                 is_possible = False
         if is_possible:
             possible_actions.append(action)
     return possible_actions
    def actions(self, state: str) -> list:
        """ Return the actions that can be executed in the given state.

        :param state: str
            state represented as T/F string of mapped fluents (state variables)
            e.g. 'FTTTFF'
        :return: list of Action objects
        """
        # TODO implement

        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())
        # each action must meet the appropriate preconditions
        possible_actions = [a for a in self.actions_list if a.check_precond(kb, a.args)]
        return possible_actions
Пример #16
0
    def actions(self, state: str) -> list:
        """ Return the actions that can be executed in the given state.

        :param state: str
            state represented as T/F string of mapped fluents (state variables)
            e.g. 'FTTTFF'
        :return: list of Action objects
        """
        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())
        possible_actions = []
        for action in self.actions_list:
            if set(action.precond_pos).issubset(kb.clauses) and set(action.precond_neg).isdisjoint(kb.clauses):
                possible_actions.append(action)
        return possible_actions
    def goal_test(self, state: str) -> bool:
        """ Test the state to see if goal is reached

        :param state: str representing state
        :return: bool
        """
        # create the knowledge base
        kb = PropKB()
        # Add the pos sentence to the knowledge base
        kb.tell(decode_state(state, self.state_map).pos_sentence())

        # return False if a clause is found that doesnt exist in the kb, true if this doesnt occur
        for clause in self.goal:
            if clause not in kb.clauses:
                return False
        return True
    def goal_test(self, state: str) -> bool:
        """ Test the state to see if goal is reached

        :param state: str representing state
        :return: bool
        """

        # Get propositional logic base
        kb = PropKB()

        # In prositional logic kb, there will add the positive sentence clauses of current state
        kb.tell(decode_state(state, self.state_map).pos_sentence())
        for clause in self.goal:
            if clause not in kb.clauses:
                return False
        return True
Пример #19
0
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())
        new_state = FluentState([], [])
        new_state.pos = [x for x in kb.clauses if x not in action.effect_rem]
        new_state.pos += action.effect_add
        new_state.neg += action.effect_rem
        return encode_state(new_state, self.state_map)
Пример #20
0
 def h_ignore_preconditions(self, node: Node):
     """This heuristic estimates the minimum number of actions that must be
     carried out from the current state in order to satisfy all of the goal
     conditions by ignoring the preconditions required for an action to be
     executed.
     """
     # TODO implement (see Russell-Norvig Ed-3 10.2.3  or Russell-Norvig Ed-2 11.2)
     # Obtain handle to knowledge base of logical expressions (propositional logic)
     kb = PropKB()
     # Add all positive of current state to PL KB
     kb.tell(decode_state(node.state, self.state_map).pos_sentence())
     count = 0
     for clause in self.goal:
         if clause not in kb.clauses:
             count += 1
     return count
Пример #21
0
    def h_ignore_preconditions(self, node: Node):
        """This heuristic estimates the minimum number of actions that must be
        carried out from the current state in order to satisfy all of the goal
        conditions by ignoring the preconditions required for an action to be
        executed.
        """
        #look github answer to understand the ignore_preconditions heuristic working
        count = 0
        kb = PropKB()
        kb.tell(decode_state(node.state, self.state_map).pos_sentence())

        for state in self.goal:
            if state not in kb.clauses:
                count = count + 1

        return count
Пример #22
0
    def h_ignore_preconditions(self, node: Node):
        """This heuristic estimates the minimum number of actions that must be
        carried out from the current state in order to satisfy all of the goal
        conditions by ignoring the preconditions required for an action to be
        executed.
        """
        # TODO implement (see Russell-Norvig Ed-3 10.2.3  or Russell-Norvig Ed-2 11.2)
        kb = PropKB()
        kb.tell(decode_state(node.state, self.state_map).pos_sentence())
        clauses = set(kb.clauses)
        clauses_and_goal = clauses.intersection(self.goal)

        # Calculate number of actions away from goal
        count = len(self.goal) - len(clauses_and_goal)

        return count
 def actions(self, state: str) -> list:
     # TODO Part 2 implement (see PlanningProblem in helpers.planning_problem)
     possible_actions = []
     kb = PropKB()
     kb.tell(decode_state(state, self.state_map).pos_sentence())
     for action in self.actions_list:
         is_possible = True
         for clause in action.precond_pos:
             if clause not in kb.clauses:
                 is_possible = False
         for clause in action.precond_neg:
             if clause in kb.clauses:
                 is_possible = False
         if is_possible:
             possible_actions.append(action)
     return possible_actions
Пример #24
0
 def h_ignore_preconditions(self, node: Node):
     """This heuristic estimates the minimum number of actions that must be
     carried out from the current state in order to satisfy all of the goal
     conditions by ignoring the preconditions required for an action to be
     executed.
     """
     count = 0
     # Create knowledge base
     kb = PropKB()
     # Add the state of the node
     kb.tell(decode_state(node.state, self.state_map).pos_sentence())
     # If a part of the goal state is not solved, up the cost by 1
     for clause in self.goal:
         if clause not in kb.clauses:
             count += 1
     return count
Пример #25
0
 def h_ignore_preconditions(self, node: Node):
     """This heuristic estimates the minimum number of actions that must be
     carried out from the current state in order to satisfy all of the goal
     conditions by ignoring the preconditions required for an action to be
     executed.
     """
     # Load, Unload, and Fly all satisfy one literal at a time, so
     # the heuristic = # of unsatisfied goals.
     # So mostly the same as goal_test except we count unsatisfied clauses.
     count = 0
     kb = PropKB()
     kb.tell(decode_state(node.state, self.state_map).pos_sentence())
     for clause in self.goal:
         if clause not in kb.clauses:
             count += 1
     return count
Пример #26
0
    def goal_test(self, state: str) -> bool:
        """ Test the state to see if goal is reached

        :param state: str representing state
        :return: bool
        """
        # kb : class Propositional Knowledge Bases (PropKB)
        # kb.clauses : a list of all the sentences of the knowledge base.
        # kb.tell(self, sentence) : add a sentence to the clauses field of KB

        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())
        for clause in self.goal:
            if clause not in kb.clauses:
                return False
        return True
Пример #27
0
    def actions(self, state: str) -> list:
        """ Return the actions that can be executed in the given state.

        :param state: str
            state represented as T/F string of mapped fluents (state variables)
            e.g. 'FTTTFF'
        :return: list of Action objects
        """

        #The actions that are applicable to a state are all those whose preconditions are satisfied.
        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())
        possible_actions = [
            a for a in self.actions_list if a.check_precond(kb, a.args)
        ]
        return possible_actions
Пример #28
0
 def h_ignore_preconditions(self, node: Node):
     """This heuristic estimates the minimum number of actions that must be
     carried out from the current state in order to satisfy all of the goal
     conditions by ignoring the preconditions required for an action to be
     executed.
     """
     # TODO implement (see Russell-Norvig Ed-3 10.2.3  or Russell-Norvig Ed-2 11.2)
     count = 0
     kb = PropKB()
     kb.tell(decode_state(node.state, self.state_map).pos_sentence())
     # Compare each goal with the clauses of the current state. Every goal that is not met increases the count by one
     # since we will need a MINIMUM of one action in order to achieve that goal.
     for goal in self.goal:
         if goal not in kb.clauses:
             count += 1
     return count
Пример #29
0
 def h_ignore_preconditions(self, node: Node):
     """This heuristic estimates the minimum number of actions that must be
     carried out from the current state in order to satisfy all of the goal
     conditions by ignoring the preconditions required for an action to be
     executed.
     """
     # count goals
     count = 0
     kb = PropKB()
     kb.tell(decode_state(node.state, self.state_map).pos_sentence())
     print('goals are:  ', self.goal)
     print('clauses are: ', kb.clauses)
     for clause in self.goal:
         if clause not in kb.clauses:
             count += 1
     return count
 def h_ignore_preconditions(self, node: Node):
     """This heuristic estimates the minimum number of actions that must be
     carried out from the current state in order to satisfy all of the goal
     conditions by ignoring the preconditions required for an action to be
     executed.
     """
     knowledge_base = PropKB()
     knowledge_base.tell(
         decode_state(node.state, self.state_map).pos_sentence())
     knowledge_base_goals = knowledge_base.clauses
     goals = self.goal
     actions_count = 0
     for goal in goals:
         if goal not in knowledge_base_goals:
             actions_count += 1
     return actions_count