def updateMutexActions(self, previousLayerMutexProposition): """ Updates the mutex list in self.actionLayer, given the mutex proposition from the previous layer. currentLayerActions are the actions in the current action layer You might want to use this function: self.actionLayer.addMutexActions(action1, action2) adds the pair (action1, action2) to the mutex list in the current action layer Note that action is *not* mutex with itself """ currentLayerActions = self.actionLayer.getActions() "*** YOUR CODE HERE ***" index = 0 for action1 in currentLayerActions: index += 1 for action2 in currentLayerActions[index:]: is_mutex = False for pre1 in action1.getPre(): for pre2 in action2.getPre(): if Pair(pre1, pre2) in previousLayerMutexProposition: is_mutex = True break if Pair(action1, action2) not in self.independentActions: is_mutex = True if is_mutex: self.actionLayer.addMutexActions(action1, action2)
def check_if_pre_conditions_are_mutex(curr_action, other_action, previous_layer_mutex_proposition, self): for curr_action_pre_condition in curr_action.get_pre(): for other_action_pre_condition in other_action.get_pre(): if Pair(curr_action_pre_condition, other_action_pre_condition ) in previous_layer_mutex_proposition: self.action_layer.add_mutex_action( Pair(curr_action, other_action)) return
def updateMutexProposition(self): """ updates the mutex propositions in the current proposition layer """ currentLayerPropositions = self.propositionLayer.getPropositions() currentLayerMutexActions = self.actionLayer.getMutexActions() for p_i in currentLayerPropositions: for p_j in currentLayerPropositions: if p_i != p_j and mutexPropositions(p_i, p_j, currentLayerMutexActions): if Pair(p_i, p_j ) not in self.propositionLayer.mutexPropositions: self.propositionLayer.mutexPropositions.append( Pair(p_i, p_j))
def gp_search(self, graph, sub_goals, _plan, level): if len(sub_goals) == 0: new_goals = [] for action in _plan: for prop in action.get_pre(): if prop not in new_goals: new_goals.append(prop) new_plan = self.extract(graph, new_goals, level - 1) if new_plan is None: return None else: return new_plan + _plan prop = sub_goals[0] providers = [] for action1 in [ act for act in graph[level].get_action_layer().get_actions() if prop in act.get_add() ]: no_mutex = True for action2 in _plan: if Pair(action1, action2) not in self.independent_actions: no_mutex = False break if no_mutex: providers.append(action1) for action in providers: new_sub_goals = [g for g in sub_goals if g not in action.get_add()] plan_clone = list(_plan) plan_clone.append(action) new_plan = self.gp_search(graph, new_sub_goals, plan_clone, level) if new_plan is not None: return new_plan return None
def isGoalState(self, state): """ Hint: you might want to take a look at goalStateNotInPropLayer function """ "*** YOUR CODE HERE ***" # explain: A state is a the goal state if it's propositions contains the goal propositions # without mutexs between them gp = state propositions = gp.propositionLayer.propositions # check if propositions contain goal isGoal = not self.goalStateNotInPropLayer(propositions) # check if propositions contain mutexs if isGoal: index = 0 for prop1 in propositions: index += 1 for prop2 in propositions[index:]: if Pair(prop1, prop2) in gp.propositionLayer.mutexPropositions: isGoal = False return isGoal
def gpSearch(self, Graph, subGoals, plan, level): if subGoals == []: newGoals = [] for action in plan: for prop in action.getPre(): if prop not in newGoals: newGoals.append(prop) newPlan = self.extract(Graph, newGoals, level - 1) if newPlan is None: return None else: return newPlan + plan prop = subGoals[0] providers = [] for action1 in [ act for act in Graph[level].getActionLayer().getActions() if prop in act.getAdd() ]: noMutex = True for action2 in plan: if Pair(action1, action2) not in self.independentActions: noMutex = False break if noMutex: providers.append(action1) for action in providers: newSubGoals = [g for g in subGoals if g not in action.getAdd()] planClone = list(plan) planClone.append(action) newPlan = self.gpSearch(Graph, newSubGoals, planClone, level) if newPlan is not None: return newPlan return None
def compute(center): global shops, polygons, esp_mapping for shop in shops.values: segment, pair = Segment2D(shop, center), Pair(shop, center) esp_mapping[pair.hash()] = geometry.esp(segment) logging.info(f'\tesp calculation completed for {center}')
def independent(self): """ Creates a list of independent actions """ for act1 in self.actions: for act2 in self.actions: if independentPair(act1, act2): self.independentActions.append(Pair(act1, act2))
def noMutexActionInPlan(self, plan, act, actionLayer): """ Helper action that you may want to use when extracting plans, returns true if there are no mutex actions in the plan """ for planAct in plan: if actionLayer.isMutex(Pair(planAct, act)): return False return True
def no_mutex_action_in_plan(plan_, act, action_layer): """ Helper action that you may want to use when extracting plans, returns true if there are no mutex actions in the plan """ for plan_act in plan_: if action_layer.is_mutex(Pair(plan_act, act)): return False return True
def independent(self): """ Creates a set of independent actions """ for i in range(len(self.actions)): for j in range(i + 1, len(self.actions)): act1 = self.actions[i] act2 = self.actions[j] if independentPair(act1, act2): self.independentActions.add(Pair(act1, act2))
def mutex_propositions(prop1, prop2, mutex_actions_list): """ complete code for deciding whether two propositions are mutex, given the mutex action from the current level (set of pairs of actions). Your update_mutex_proposition function should call this function You might want to use this function: prop1.get_producers() returns the set of all the possible actions in the layer that have prop1 on their add list """ return all([Pair(a1, a2) in mutex_actions_list for a1, a2 in itertools.product(prop1.get_producers(), prop2.get_producers())])
def mutexActions(a1, a2, mutexProps): """ This function returns true if a1 and a2 are mutex actions. We first check whether a1 and a2 are in PlanGraphLevel.independentActions, this is the list of all the independent pair of actions (according to your implementation in question 1). If not, we check whether a1 and a2 have competing needs """ if Pair(a1,a2) not in PlanGraphLevel.independentActions: return True return haveCompetingNeeds(a1, a2, mutexProps)
def standard_env(): """An environment with some Scheme standard procedures.""" env = Env() env.update(vars(math)) # sin, cos, sqrt, pi, ... env.update({ '*': lambda *x: ops.func(op.mul, *x), '+': lambda *x: ops.func(op.add, *x), '-': lambda *x: ops.func(op.sub, *x), '/': lambda *x: ops.func(op.truediv, *x), '<': lambda *x: ops.func(op.lt, *x), '<=': lambda *x: ops.func(op.le, *x), '=': lambda *x: ops.func(op.eq, *x), '>': lambda *x: ops.func(op.gt, *x), '>=': lambda *x: ops.func(op.ge, *x), 'abs': abs, 'append': op.add, 'apply': lambda x, y: x(*y), 'begin': lambda *x: x[-1], 'boolean?': lambda x: isinstance(x, bool), 'car': lambda x: x[0], 'cdr': lambda x: x[1:], 'char?': lambda x: isinstance(x, Char), 'cons': lambda *x: Pair(x), 'env': lambda: env.env(), 'eq?': op.is_, 'equal?': op.eq, 'eval': lambda *x, **y: leval(*x, **y), 'first': lambda *x: env.get('car')(*x), 'length': len, 'list': lambda *x: list(x), 'list?': lambda x: isinstance(x, list), 'map': map, 'max': max, 'min': min, 'not': op.not_, 'null?': lambda x: len(x) == 0, 'number?': lambda x: isinstance(x, int), # TODO (RCZ) - not right yet 'open': open, 'read-file': lambda x: x.read(), 'pair?': lambda x: isinstance(x, Pair), 'pprint': pprint, 'print': lambda *x: print(get_data(*x)), 'procedure?': callable, 'rest': lambda *x: env.get('cdr')(*x), 'round': round, 'string?': lambda x: isinstance(x, str), 'sum': lambda *x: sum(x), 'symbol?': lambda x: isinstance(x, Symbol), 'type': type, 'tuple': lambda x: tuple(x), 'vector': lambda *x: list(x), 'vector?': lambda x: env.get('list?')(x), 'write': lambda *x: env.get('print')(*x) }) return env
def haveCompetingNeeds(a1, a2, mutexProps): """ Complete code for deciding whether actions a1 and a2 have competing needs, given the mutex proposition from previous level (list of pairs of propositions). Hint: for propositions p and q, the command "Pair(p, q) in mutexProps" returns true if p and q are mutex in the previous level """ assert (isinstance(a1, Action)) assert (isinstance(a2, Action)) return Pair(a1.getPre(), a2.getPre()) in mutexProps
def mutexActions(a1, a2, mutexProps): """ Complete code for deciding whether actions a1 and a2 are mutex, given the mutex proposition from previous level (list of pairs of propositions). Your updateMutexActions function should call this function """ # Check if a1 and a2 have inconsistent effects or interfere if Pair(a1, a2) not in PlanGraphLevel.independentActions: return True # Get preconditions of both actions pre1 = a1.getPre() pre2 = a2.getPre() # Competing needs: Check if a1 and a2 have preconditions that are mutex for p1 in pre1: for p2 in pre2: if Pair(p1, p2) in mutexProps: return True return False
def haveCompetingNeeds(a1, a2, mutexProps): """ Complete code for deciding whether actions a1 and a2 have competing needs, given the mutex proposition from previous level (list of pairs of propositions). Hint: for propositions p and q, the command "Pair(p, q) in mutexProps" returns true if p and q are mutex in the previous level """ for a in a1.getPre(): for b in a2.getPre(): if Pair(a,b) in mutexProps: return True return False
def load_data(input_file): raw_data = open(input_file,'r') doc_list = [] doc = Document() for rline in raw_data.readlines(): if rline.strip(): entry = rline.split() docID = entry[0] if docID != doc.docID: #import pdb #if doc.docID!='': # pdb.set_trace() doc_list.append(doc) doc = Document(docID) first = Entity(entry[1],(entry[2],entry[3]),entry[4],entry[5]) second = Entity(entry[6],(entry[7],entry[8]),entry[9],entry[10]) pair = Pair(first,second) if len(entry) == 12: pair.set_label(entry[11]) doc.add_pair(pair) else: first = Entity(entry[1],(entry[2],entry[3]),entry[4],entry[5]) second = Entity(entry[6],(entry[7],entry[8]),entry[9],entry[10]) pair = Pair(first,second) if len(entry) == 12: pair.set_label(entry[11]) doc.add_pair(pair) doc_list.append(doc) return doc_list
def updateMutexActions(self, previousLayerMutexProposition): """ Updates the mutex list in self.actionLayer, given the mutex proposition from the previous layer. currentLayerActions are the actions in the current action layer """ currentLayerActions = self.actionLayer.getActions() for a_i in currentLayerActions: for a_j in currentLayerActions: if a_i != a_j and mutexActions(a_i, a_j, previousLayerMutexProposition): if Pair(a_i, a_j) not in self.actionLayer.mutexActions: self.actionLayer.addMutexActions(a_i, a_j)
def have_competing_needs(a1, a2, mutex_props): """ Complete code for deciding whether actions a1 and a2 have competing needs, given the mutex proposition from previous level (list of pairs of propositions). Hint: for propositions p and q, the command "Pair(p, q) in mutex_props" returns true if p and q are mutex in the previous level """ "*** YOUR CODE HERE ***" for cond1 in a1.get_pre(): for cond2 in a2.get_pre(): if Pair(cond1, cond2) in mutex_props: return True return False
def mutexPropositions(prop1, prop2, mutexActions): """ complete code for deciding whether two propositions are mutex, given the mutex action from the current level (set of pairs of actions). Your updateMutexProposition function should call this function You might want to use this function: prop1.getProducers() returns the set of all the possible actions in the layer that have prop1 on their add list """ for prod1 in prop1.getProducers(): for prod2 in prop2.getProducers(): if Pair(prod2, prod1) not in mutexActions: return False return True
def haveCompetingNeeds(a1, a2, mutexProps): """ Complete code for deciding whether actions a1 and a2 have competing needs, given the mutex proposition from previous level (list of pairs of propositions). Hint: for propositions p and q, the command "Pair(p, q) in mutexProps" returns true if p and q are mutex in the previous level """ propPairs = [ Pair(p1, p2) for (p1, p2) in exclusiveProduct(a1.getPre(), a2.getPre()) ] for pp in mutexProps: if pp in propPairs: return True return False
def mutexPropositions(prop1, prop2, mutexActions): """ complete code for deciding whether two propositions are mutex, given the mutex action from the current level (list of pairs of actions). Your updateMutexProposition function should call this function """ prod1 = prop1.getProducers() prod2 = prop2.getProducers() for a1 in prod1: for a2 in prod2: # Check if all actions are pairwise mutex if Pair(a1, a2) not in mutexActions: return False return True
def mutex_propositions(prop1, prop2, mutex_actions_list): """ complete code for deciding whether two propositions are mutex, given the mutex action from the current level (set of pairs of actions). Your update_mutex_proposition function should call this function You might want to use this function: prop1.get_producers() returns the set of all the possible actions in the layer that have prop1 on their add list """ "*** YOUR CODE HERE ***" for action1 in prop1.get_producers(): for action2 in prop2.get_producers(): if Pair(action1, action2) not in mutex_actions_list: return False return True
def mutexPropositions(prop1, prop2, mutexActions): """ complete code for deciding whether two propositions are mutex, given the mutex action from the current level (list of pairs of actions). Your updateMutexProposition function should call this function You might want to use this function: prop1.getProducers() returns the list of all the possible actions in the layer that have prop1 on their add list """ for a1 in prop1.getProducers(): for a2 in prop2.getProducers(): # Try to find a pair that are not mutex, if found, so send False they are not mutex if Pair(a1, a2) not in mutexActions: return False return True
def allPrecondsInLayer(self, action): """ returns true if all propositions that are preconditions of the action exist in this layer (i.e. the action can be applied) """ for pre in action.getPre(): if not(pre in self.propositions): return False for pre1 in action.getPre(): for pre2 in action.getPre(): if Pair(pre1,pre2) in self.mutexPropositions: return False return True
def have_competing_needs(a1, a2, mutex_props): """ Complete code for deciding whether actions a1 and a2 have competing needs, given the mutex proposition from previous level (list of pairs of propositions). Hint: for propositions p and q, the command "Pair(p, q) in mutex_props" returns true if p and q are mutex in the previous level """ a1_pre_list = a1.get_pre() a2_pre_list = a2.get_pre() for prop1 in a1_pre_list: for prop2 in a2_pre_list: if Pair(prop1, prop2) in mutex_props: return True return False
def mutexPropositions(prop1, prop2, mutexActions): """ complete code for deciding whether two propositions are mutex, given the mutex action from the current level (list of pairs of actions). Your updateMutexProposition function should call this function You might want to use this function: prop1.getProducers() returns the list of all the possible actions in the layer that have prop1 on their add list """ "*** YOUR CODE HERE ***" for action1 in prop1.getProducers(): for action2 in prop2.getProducers(): ####### check if this is working################################################## if not Pair(action1, action2) in mutexActions: return False return True
def updateMutexProposition(self): """ updates the mutex propositions in the current proposition layer You might want to use those functions: mutexPropositions(prop1, prop2, currentLayerMutexActions) returns true if prop1 and prop2 are mutex in the current layer self.propositionLayer.addMutexProp(prop1, prop2) adds the pair (prop1, prop2) to the mutex list of the current layer """ currentLayerPropositions = self.propositionLayer.getPropositions() currentLayerMutexActions = self.actionLayer.getMutexActions() for p1 in currentLayerPropositions: for p2 in currentLayerPropositions: if p1 != p2 and \ Pair(p1, p2) not in self.propositionLayer.getMutexProps() and \ mutexPropositions(p1, p2, currentLayerMutexActions): self.propositionLayer.addMutexProp(p1, p2)
def updateMutexActions(self, previousLayerMutexProposition): """ Updates the mutex list in self.actionLayer, given the mutex proposition from the previous layer. currentLayerActions are the actions in the current action layer You might want to use this function: self.actionLayer.addMutexActions(action1, action2) adds the pair (action1, action2) to the mutex list in the current action layer Note that action is *not* mutex with itself """ currentLayerActions = self.actionLayer.getActions() "*** YOUR CODE HERE ***" for a1 in currentLayerActions: for a2 in currentLayerActions: if a1 != a2 and mutexActions(a1, a2, previousLayerMutexProposition): if Pair(a1, a2) not in self.actionLayer.getMutexActions(): self.actionLayer.addMutexActions(a1, a2)
def updateMutexProposition(self): """ updates the mutex propositions in the current proposition layer You might want to use those functions: mutexPropositions(prop1, prop2, currentLayerMutexActions) returns true if prop1 and prop2 are mutex in the current layer self.propositionLayer.addMutexProp(prop1, prop2) adds the pair (prop1, prop2) to the mutex list of the current layer """ currentLayerPropositions = self.propositionLayer.getPropositions() currentLayerMutexActions = self.actionLayer.getMutexActions() for p in currentLayerPropositions: for q in currentLayerPropositions: if p == q: continue # a proposition is not mutex with itself if Pair(p, q) not in self.propositionLayer.getMutexProps( ) and mutexPropositions(p, q, currentLayerMutexActions): # if p and q are mutex props, and we didn't add them before, we should do so now self.propositionLayer.addMutexProp(p, q)
def load_data(input_file): raw_data = open(input_file,'r') doc_list = [] doc = Document() ne_dict = {} for rline in raw_data.readlines(): if rline.strip(): i = 0 entry = rline.split() if len(entry) == 14: i = 1 docID = entry[i] #new document if docID != doc.docID: #import pdb #if doc.docID!='': # pdb.set_trace() #record the name entity dictionary we have created doc.set_ne_dict(ne_dict) doc_list.append(doc) ne_dict = {} doc = Document(docID) first = Entity(entry[i+1],(entry[i+2],entry[i+3]),entry[i+4],entry[i+5],entry[i+6]) ne_dict[entry[i+5]] = (entry[i+1],entry[i+2]) second = Entity(entry[i+7],(entry[i+8],entry[i+9]),entry[i+10],entry[i+11],entry[i+12]) ne_dict[entry[i+11]] = (entry[i+7],entry[i+8]) pair = Pair(first,second) if i: pair.set_label(entry[0]) doc.add_pair(pair) else: first = Entity(entry[i+1],(entry[i+2],entry[i+3]),entry[i+4],entry[i+5],entry[i+6]) ne_dict[entry[i+5]] = (entry[i+1],entry[i+2]) second = Entity(entry[i+7],(entry[i+8],entry[i+9]),entry[i+10],entry[i+11],entry[i+12]) ne_dict[entry[i+11]] = (entry[i+7],entry[i+8]) pair = Pair(first,second) if i: pair.set_label(entry[0]) doc.add_pair(pair) doc.set_ne_dict(ne_dict) doc_list.append(doc) return doc_list