Пример #1
0
 def goal_test(self, state: str) -> 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
    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
 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:
       if clause not in kb.clauses:
         count += 1
     return count
Пример #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
     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 = count + 1
     return count
Пример #5
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
Пример #6
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())
        # logging.info('Goal: %s', self.goal)
        for clause in self.goal:
            if clause not in kb.clauses:
                count += 1

        return count
Пример #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

        logicKnowledgeBase = PropKB()
        logicKnowledgeBase.tell(
            decode_state(node.state, self.state_map).pos_sentence())

        for sitP in self.goal:
            if sitP not in logicKnowledgeBase.clauses:
                count += 1

        return count
Пример #8
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

        # count how many goal states are in pos
        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
Пример #9
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)
        #https://discussions.udacity.com/t/understanding-ignore-precondition-heuristic/225906
        #Similar aspect to goal_test
        kb = PropKB()
        kb.tell(decode_state(node.state, self.state_map).pos_sentence())

        #Count
        count = 0
        for clause in self.goal:
            if clause not in kb.clauses:
                count += 1
        return count
    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'
            Sai: 意思即係T = positive sentence's clauses of the problem we are gonna deal with,
            F = negative sentence's clauses of the problem we are gonna deal with
        :return: list of Action objects

        Sai: Just a recap of what is fluentsrefer to the materials of AIND.
        L15 - Planning(Situation Calculus 2)
        These types of predicate that can vary from one situation to another are called fluents(state variables) .
        eg: At(), In() etc...
        """
        # TODO implement

        possible_actions = []

        # Create a knowledge base datatype from logic.py of logical expressions (propositional logic)
        kb = PropKB()

        # Add the current state's positive sentence's clauses to the propositional logic KB
        kb.tell(decode_state(state, self.state_map).pos_sentence())

        # From all the concrete actions we can perform, select the ones that can be executed
        # from the current state. An action is executable if it satisfies a problem's preconditions.
        # ==========How it find an action which satisfies a problem's preconditions?==================
        # That is, it must appear in a problem's positive preconditions and not be in a problem's negative ones.
        for action in self.actions_list:
            is_possible = True
            # Adding the fluents that are positive literals in the action’s effects.
            for clause in action.precond_pos:
                if clause not in kb.clauses:
                    is_possible = False
                    break
            # Removing the fluents that appear as negative literals in the action’s effects
            for clause in action.precond_neg:
                if clause in kb.clauses:
                    is_possible = False
                    break
            if is_possible:
                possible_actions.append(action)
        return possible_actions
Пример #11
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
        """
        # TODO implement
        possible_actions = []
        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())
        for action in self.actions_list:
            if (all(precond_pos in kb.clauses
                    for precond_pos in action.precond_pos)
                    and all(precond_neg not in kb.clauses
                            for precond_neg in action.precond_neg)):
                possible_actions.append(action)
        return possible_actions
Пример #12
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)
     # We just need to count the number of unsatisfied goals for the passed
     # state. This is because:
     #   - When ignoring preconditions, all actions become applicable and
     #   can achieve any single goal in one step
     #   - We need to be careful in cases where we have actions that modify
     #   multiple fluents in as in this case we could be achieving more than
     #   one goal at once. NOTE that this is not the case for this problem,
     #   as each action only modifies one single fluent
     kb = PropKB()
     kb.tell(decode_state(node.state, self.state_map).pos_sentence())
     return len(
         [clause for clause in self.goal if clause not in kb.clauses])
Пример #13
0
 def h_ignore_delete_lists(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. It achieves this by assuming all goals and preconditions
     contain only positive literals and creates a relaxed version of the
     original problem that's easier to solve by removing the delete lists
     from all actions (i.e. removing all negative effects) so no
     action ever undoes progress made by another action.
     """
     # Implemented with reference to Russell-Norvig Ed-3 10.2.3, p. 377
     count = 0
     kb = PropKB()
     kb.tell(decode_state(node.state, self.state_map).pos_sentence())
     for action in self.actions_list:
         for clause in self.goal:
             if clause in action.effect_rem:
                 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.
     """
     kb = PropKB()
     kb.tell(decode_state(node.state, self.state_map).pos_sentence())
     
     # This heuristic returns all actions required to achieve the goal
     # from current state, assuming direct execution of action with no-preconditions on it. 
     # This is auto generated heuristic by relaxing the problem constraint
    
     count = 0
     for clause in self.goal:
         if clause not in kb.clauses:
             count += 1
             
     return count
Пример #15
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.
        '''
        # implement (see Russell-Norvig Ed-3 10.2.3  or Russell-Norvig Ed-2 11.2)
        # knowledge base of logical expressions
        kb = PropKB()

        kb.tell(decode_state(node.state, self.state_map).pos_sentence())

        # Use a slightly modified version of the goal_test() fn
        count = 0
        for clause in self.goal:
            if clause not in kb.clauses:
                count += 1
        return count
Пример #16
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.
        References:
            Russell-Norvig Ed-3 10.2.3
        """
        count = 0
        propkb = PropKB()  # Instantiate propositional logic

        # Update propsitional logic with positive sentence clauses
        propkb.tell(decode_state(node.state, self.state_map).pos_sentence())

        # goal_test() logic
        for clause in self.goal:
            if clause not in propkb.clauses:
                count += 1
        return count
Пример #17
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)

        # Per instruction here https://discussions.udacity.com/t/understanding-ignore-precondition-heuristic/225906/2
        # we need to figure out how many of the goals states are not yet satisfied in the current state since
        # the number of unsatisfied goals equals the minimum number of actions to satisfy all goals."
        kb = PropKB()
        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
Пример #18
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()
        #print("State Map", self.state_map)
        kb.tell(decode_state(node.state, self.state_map).pos_sentence())
        #print("Clauses", kb.clauses)
        for clause in self.goal:
            if clause not in kb.clauses:
               count = count + 1

        #print ("Count", count)
        return count
Пример #19
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
        # we implement a simple heuristic that count
        #   number of goal hasn't satisfied
        # here we assume for a goal to be satisfied, it requires at least one action
        # and no action achieves multiple goal
        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
Пример #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)
     #remove preconditions from action
     #we use the fact that each action only has one effect and every clause has an action that can
     #make it satisfied , thus after ignoring precondtions
     #and action effects that contradict any clause in the goal state, the minimum number
     #of actions is the same as the number of unsatisfied clauses in the current state
     kb = PropKB()
     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.
        """
        # TODO implement (see Russell-Norvig Ed-3 10.2.3  or Russell-Norvig Ed-2 11.2)
        count = 0
        state = node.state

        fluentState = decode_state(state, self.state_map)
        pos = fluentState.pos_sentence()
        kb = PropKB()
        kb.tell(pos)
        for goal in self.goal:
            if goal not in kb.clauses:  # unsatisfy goal
                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.
        """
        # TODO implement (see Russell-Norvig Ed-3 10.2.3  or Russell-Norvig Ed-2 11.2)

        # Create a knowledge base. Then we can add the state positive clauses to the knowledge base
        kb = PropKB()
        kb.tell(decode_state(node.state, self.state_map).pos_sentence())

        # We just add a counter similar to goal_test
        count = 0
        for clause in self.goal:
            if clause not in kb.clauses:
                count += 1

        return count
Пример #23
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:
            # all positive pre-conditions must exist
            is_positive_preconds = set(action.precond_pos).issubset(set(kb.clauses))
            # not even one negative pre-condition mus exist
            is_negative_preconds = not bool(set(action.precond_neg).intersection(set(kb.clauses)))
            if is_positive_preconds and is_negative_preconds:
                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.
     """
     # 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())
     # Count the number of goal conditions already met
     count = 0
     for goal in self.goal:
         if goal not in kb.clauses:
             count += 1
     # By this time we should have the number of goals that are unsatisfied initially
     # We should assume that there is an action that achieves 1 or more of the goals
     # We can ignore the multiple goal satisfactions because we know for this domain
     # each action only results in one added effect
     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.
        '''
        # implement (see Russell-Norvig Ed-3 10.2.3  or Russell-Norvig Ed-2 11.2)
        # The ignore pre-conditions heuristic drops all preconditions from actions.
        # Every action becomes applicable in every state, and any single goal fluent can be achieved in one step
        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
    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)
        #ignore all preconditions and count #actions that will take you to goal
        #goal_test(node)?

        count = 0
        kb = PropKB()
        kb.tell(decode_state(node.state, self.state_map).pos_sentence()
                )  #Relaxing by removing negatives and preconditions

        for clause in self.goal:
            if clause not in kb.clauses:
                count += 1
        return count
    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 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)
        # By ignoring preconditions all actions becomes applicable/ more like the action becomes applicable irrespective
        # of the current state.

        kb = PropKB()
        kb.tell(decode_state(node.state, self.state_map).pos_sentence())
        count = 0

        for precondition in self.goal:
            if precondition 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.
        """
        # TODO implement (see Russell-Norvig Ed-3 10.2.3  or Russell-Norvig Ed-2 11.2)
        count = 0
        #pg = PlanningGraph(self, node.state)

        # need to delete preconditions
        # to drop all preconditions from actions
        # number of steps remaining is the huersitic we are looking for
        kb = PropKB()
        kb.tell(decode_state(node.state, self.state_map).pos_sentence())
        for clouse in self.goal:
            if clouse not in kb.clauses:
                count += 1
        return count
Пример #30
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
        """
        # TODO implement

        # return list of allowable actions
        possible_actions = []

        # instantiate propositional knowledge base to test whether each action is valid
        # for the current state of the world
        knowledge_base = PropKB()
        knowledge_base.tell(
            sentence=decode_state(state, self.state_map).pos_sentence())

        # test each action's precondition clauses for validity against the KB's SOTW
        for action in self.actions_list:
            # action will be invalid if a single assertion fails
            is_action_ok = True
            # find invalid pos clauses - precond does not exist in SOTW
            for precond_pos_clause in [
                    clause for clause in action.precond_pos
                    if clause not in knowledge_base.clauses
            ]:
                is_action_ok = False
                break
            # find invalid neg clauses - negative precond exists in SOTW
            if is_action_ok:
                for precond_neg_clause in [
                        clause for clause in action.precond_neg
                        if clause in knowledge_base.clauses
                ]:
                    is_action_ok = False
                    break

            if is_action_ok:
                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
        """
        possible_actions = []

        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())

        for action in self.actions_list:
            valid_pos = all([pc in kb.clauses for pc in action.precond_pos])
            valid_neg = all([pc not in kb.clauses for pc in action.precond_neg])
            if valid_pos and valid_neg: possible_actions.append(action)

        return possible_actions
    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())

        #        count = 0
        #        for clause in self.goal:
        #            if clause not in knowledge_base.clauses:
        #                count += 1
        #       return count
        return sum([
            1 for clause in self.goal if clause not in knowledge_base.clauses
        ])
Пример #33
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)

        # get the clauses in positive sentence of the current state
        kb = PropKB()
        kb.tell(decode_state(node.state, self.state_map).pos_sentence())

        # Note that for the current case, for any action, effect_add is length 1.
        count = 0
        for clause in self.goal:
            if clause not in kb.clauses:
                count += 1

        return count
Пример #34
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.
        '''
        # Added implementation for (see Russell-Norvig Ed-3 10.2.3  or Russell-Norvig Ed-2 11.2)
        # Logic based on "Understanding ignore precondition heuristic" found at
        # https://discussions.udacity.com/t/understanding-ignore-precondition-heuristic/225906/2
        # also based on goal_test(self, state: str) -> bool:

        pkb = PropKB()
        pkb.tell(decode_state(node.state, self.state_map).pos_sentence())
        count = 0
        for goal_clause in self.goal:
            if goal_clause not in pkb.clauses:
                count = 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.
     '''
     # COMPLETED (see Russell-Norvig Ed-3 10.2.3  or Russell-Norvig Ed-2 11.2)
     # Each action only has 1 positive effect so will not achieve multiple goals
     # Without preconditions the Unload action will achieve a goal from any state
     # The negative effect of Unload does not impact achieved goals
     # So minimum actions to achieve goal is the no. of unreached goal conditions
     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
Пример #36
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:
            possible = True
            for clause in action.precond_pos: 
                if clause not in kb.clauses:
                    possible = False
            for clause in action.precond_neg:
                if clause in kb.clauses:
                    possible = False
            if possible:
                possible_actions.append(action)
        return possible_actions