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
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
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
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 # Estimate # of actions that are yet to be carried out. 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 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 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)
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()) kb_clauses = kb.clauses for c in self.goal: if c 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. """ 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
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
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 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
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
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
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. The code is based on the assumption that an action won't satisfy more than one goal which is the case for all the actions in this project """ # 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
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
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 will be used as rough cost estimate, increased by 1 for every step required toward 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
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
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)
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
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. """ # This heuristic also makes the subgoal independence assumption. # This means that the number of required actions is just the number # of unsatisfied goals. 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. """ # Implemented per Russell-Norvig Ed-2 11.2, based on goal test # returns the minimum number of actions to goal # while ignoring preconditions for all actions 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) #count the number of states that need to change to satisfy the goal. #loop though goal states, and see if they are in the current clauses count = 0 kb = PropKB() kb.tell(decode_state(node.state, self.state_map).pos_sentence()) for goal in self.goal: if goal not in kb.clauses: count += 1 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
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
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()) count = len(self.goal) for goal in self.goal: if goal in kb.clauses: count -= 1 return 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 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 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: """ 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
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
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 """ fs = decode_state(state,self.state_map) # TODO implement fs=decode_state(state,self.state_map) pos = fs.pos neg=fs.neg self.initial_state_TF #for x in pos: # print("x: ", x) #if x not in self.goal: kb = PropKB() kb.tell(decode_state(state, self.state_map).pos_sentence()) # For each action check if all clauses required as positive precondtion are met # Similarly check that all clauses as negative pre conditions are not present possible_actions=[] i=0 for x in self.actions_list: pos_conditions = x.precond_pos neg_conditions = x.precond_neg isTrue=True for pos in pos_conditions: if pos not in kb.clauses: isTrue=False break for neg in neg_conditions: if neg in kb.clauses: isTrue=False break if isTrue: possible_actions.append(x) return possible_actions
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 #If action do not have positive precon in kb; make it impossible for clause in action.precond_neg: if clause in kb.clauses: is_possible = False #If kb has clause that is negative precondition for action; make that action impossible 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) current_state = decode_state(node.state, self.state_map) return len([ clause for clause in self.goal if clause not in current_state.pos ]) kb = PropKB() kb.tell(decode_state(node.state, self.state_map).pos_sentence()) return sum([1 for clause in self.goal if clause not in kb.clauses])
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