示例#1
0
    def update_proposition_layer(self):
        """
        Updates the propositions in the current proposition layer,
        given the current action layer.
        don't forget to update the producers list!
        Note that same proposition in different layers might have different producers lists,
        hence you should create two different instances.
        current_layer_actions is the set of all the actions in the current layer.
        You might want to use those functions:
        dict() creates a new dictionary that might help to keep track on the propositions that you've
               already added to the layer
        self.proposition_layer.add_proposition(prop) adds the proposition prop to the current layer

        """
        current_layer_actions = self.action_layer.get_actions()
        props_and_producers = {}
        for a in current_layer_actions:
            added_propositions = a.get_add()
            for added in added_propositions:
                if added not in props_and_producers:
                    props_and_producers[added] = [a]
                else:
                    props_and_producers[added].append(a)
        for key, value in props_and_producers.items():
            new_prop = Proposition(key.name)
            new_prop.set_producers(value)
            self.proposition_layer.add_proposition(new_prop)
示例#2
0
  def updatePropositionLayer(self):
    """
    Updates the propositions in the current proposition layer,
    given the current action layer.
    don't forget to update the producers list!
    Note that same proposition in different layers might have different producers lists,
    hence you should create two different instances.
    currentLayerActions is the list of all the actions in the current layer.
    You might want to use those functions:
    dict() creates a new dictionary that might help to keep track on the propositions that you've
           already added to the layer
    self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer       
    
    """
    currentLayerActions = self.actionLayer.getActions()
    "*** YOUR CODE HERE ***"
    # create dictionary with proposition and actions
    # leading to it
    propositionDict = dict()
    for action in currentLayerActions:
      for prop in action.getAdd():
        if prop.getName() in propositionDict.keys():
          propositionDict[prop.getName()].append(action)
        else:
          actionList = list()
          actionList.append(action)
          propositionDict.update({prop.getName(): actionList})

    #create proposition object and update their actions
    for key in propositionDict.keys():
      p = Proposition(key)
      p.setProducers(propositionDict[key])
      self.propositionLayer.addProposition(p)
示例#3
0
    def update_proposition_layer(self):
        """
        Updates the propositions in the current proposition layer,
        given the current action layer.
        don't forget to update the producers list!
        Note that same proposition in different layers might have different producers lists,
        hence you should create two different instances.
        current_layer_actions is the set of all the actions in the current layer.
        You might want to use those functions:
        dict() creates a new dictionary that might help to keep track on the propositions that you've
               already added to the layer
        self.proposition_layer.add_proposition(prop) adds the proposition prop to the current layer

        """
        current_layer_actions = self.action_layer.get_actions()

        updated_props = {}

        # checks for every action and adds to the dictionary of updated props the acts that led to is
        for act in current_layer_actions:
            for prop in act.get_add():
                if prop not in updated_props:
                    updated_props[prop] = list()
                updated_props[prop].append(act)

        # create new Proposition and add it to the new layer
        for proposition in updated_props:

            # create new prop
            new_prop = Proposition(proposition.get_name())
            # add its producers
            new_prop.set_producers(updated_props[proposition][:])
            # add to new layer
            self.proposition_layer.add_proposition(new_prop)
示例#4
0
    def updatePropositionLayer(self):
        """
        Updates the propositions in the current proposition layer,
        given the current action layer.
        don't forget to update the producers list!
        Note that same proposition in different layers might have different producers lists,
        hence you should create two different instances.
        currentLayerActions is the list of all the actions in the current layer.
        You might want to use those functions:
        dict() creates a new dictionary that might help to keep track on the propositions that you've
               already added to the layer
        self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer

        """
        currentLayerActions = self.actionLayer.getActions()
        props_dict = dict()
        for action in currentLayerActions:
            for add_props in action.getAdd():
                if add_props.getName() not in props_dict:
                    props_dict[add_props.getName()] = [action]
                else:
                    props_dict[add_props.getName()] = \
                        props_dict[add_props.getName()]+[action]

        for key in props_dict:
            prop_temp = Proposition(key)
            prop_temp.setProducers(props_dict[key])    #takes care of adding procedure
            self.propositionLayer.addProposition(prop_temp)
示例#5
0
    def updatePropositionLayer(self):
        """
    Updates the propositions in the current proposition layer,
    given the current action layer.
    don't forget to update the producers list!
    Note that same proposition in different layers might have different producers lists,
    hence you should create two different instances.
    currentLayerActions is the list of all the actions in the current layer.
    You might want to use those functions:
    dict() creates a new dictionary that might help to keep track on the propositions that you've
           already added to the layer
    self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer       
    
    """
        currentLayerActions = self.actionLayer.getActions()
        "*** YOUR CODE HERE ***"

        Propositions = dict()

        for action in currentLayerActions:
            for prop in action.getAdd():

                name = prop.getName()
                if Propositions.has_key(name):
                    new_prop = Propositions[name]
                else:
                    new_prop = Proposition(name)
                    Propositions[name] = new_prop
                    self.propositionLayer.addProposition(new_prop)

                new_prop.addProducer(action)
    def update_proposition_layer(self):
        """
        Updates the propositions in the current proposition layer,
        given the current action layer.
        don't forget to update the producers list!
        Note that same proposition in different layers might have different producers lists,
        hence you should create two different instances.
        current_layer_actions is the set of all the actions in the current layer.
        You might want to use those functions:
        dict() creates a new dictionary that might help to keep track on the propositions that you've
               already added to the layer
        self.proposition_layer.add_proposition(prop) adds the proposition prop to the current layer

        """
        current_layer_actions = self.action_layer.get_actions()
        new_props_set = set()
        for action1 in current_layer_actions:
            list_of_propo = action1.get_add()
            for propo in list_of_propo:
                new_props_set.add(propo.name)
        props_dict = dict.fromkeys(new_props_set, set())
        for action2 in current_layer_actions:
            list_of_propo2 = action2.get_add()
            for propo2 in list_of_propo2:
                props_dict[propo2.get_name()].add(action2)
        for prop in props_dict:
            my_prop = Proposition(prop)
            my_prop.set_producers(list(props_dict[prop]))
            self.proposition_layer.add_proposition(my_prop)
示例#7
0
  def updatePropositionLayer(self):
    """
    Updates the propositions in the current proposition layer,
    given the current action layer.
    don't forget to update the producers list!
    Note that same proposition in different layers might have different producers lists,
    hence you should create two different instances.
    currentLayerActions is the list of all the actions in the current layer.
    You might want to use those functions:
    dict() creates a new dictionary that might help to keep track on the propositions that you've
           already added to the layer
    self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer       
    
    """
    currentLayerActions = self.actionLayer.getActions()
    "*** YOUR CODE HERE ***"

    props = dict()

    for action in currentLayerActions:
      for pro in action.getAdd():
        if pro.getName() not in props:
          props[pro.getName()] = [action]
        else:
          props[pro.getName()] = props[pro.getName()] + [action]

    for pro in props:
      prop = Proposition(pro)
      prop.setProducers(props[pro])
      self.propositionLayer.addProposition(prop)
示例#8
0
    def update_proposition_layer(self):
        """
        Updates the propositions in the current proposition layer,
        given the current action layer.
        don't forget to update the producers list!
        Note that same proposition in different layers might have different producers lists,
        hence you should create two different instances.
        current_layer_actions is the set of all the actions in the current layer.
        You might want to use those functions:
        dict() creates a new dictionary that might help to keep track on the propositions that you've
               already added to the layer
        self.proposition_layer.add_proposition(prop) adds the proposition prop to the current layer

        """
        dic = dict()
        current_layer_actions = self.action_layer.get_actions()
        "*** YOUR CODE HERE ***"
        for action in current_layer_actions:
            for proposition in action.get_add():
                if proposition.get_name() not in dic.values():
                    new_prop = Proposition(proposition.get_name())
                    self.proposition_layer.add_proposition(new_prop)
                    dic[new_prop] = new_prop.get_name()
                keys = list(dic.keys())
                values = list(dic.values())
                new_prop = keys[values.index(proposition.name)]
                new_prop.add_producer(action)
示例#9
0
 def parse_problem(self):
     init = []
     goal = []
     f = open(self.problem_file, 'r')
     line = f.readline()
     words = [word.rstrip() for word in line.split(" ") if len(word.rstrip()) > 0]
     for i in range(2, len(words)):
         init.append(Proposition(words[i]))
     line = f.readline()
     words = [word.rstrip() for word in line.split(" ") if len(word.rstrip()) > 0]
     for i in range(2, len(words)):
         goal.append(Proposition(words[i]))
     return init, goal
示例#10
0
    def updatePropositionLayer(self):
        """
        Updates the propositions in the current proposition layer,
        given the current action layer.
        don't forget to update the producers list!
        Note that same proposition in different layers might have different producers lists,
        hence you should create two different instances.
        currentLayerActions is the list of all the actions in the current layer.
        You might want to use those functions:
        dict() creates a new dictionary that might help to keep track on the propositions that you've
               already added to the layer
        self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer
        """

        currentLayerActions = self.actionLayer.getActions()
        props = dict()

        for a in currentLayerActions:
            for prop in a.getAdd():
                name = prop.getName()
                if name not in props.keys():
                    props[name] = Proposition(name)
                props[name].addProducer(a)

        for prop in props.values():
            self.propositionLayer.addProposition(prop)
示例#11
0
    def update_proposition_layer(self):
        """
        Updates the propositions in the current proposition layer,
        given the current action layer.
        don't forget to update the producers list!
        Note that same proposition in different layers might have different producers lists,
        hence you should create two different instances.
        current_layer_actions is the set of all the actions in the current layer.
        You might want to use those functions:
        dict() creates a new dictionary that might help to keep track on the propositions that you've
               already added to the layer
        self.proposition_layer.add_proposition(prop) adds the proposition prop to the current layer

        """
        current_layer_actions = self.action_layer.get_actions()
        proposition_layer = self.get_proposition_layer()
        previous_layer_propositions = proposition_layer.get_propositions()
        new_propositions = {
            prop.name: Proposition(prop.name)
            for prop in previous_layer_propositions
        }
        for action in current_layer_actions:
            for proposition_to_add in action.get_add():
                add_proposition_to_next_layer(action, new_propositions,
                                              proposition_layer,
                                              proposition_to_add)
示例#12
0
def add_proposition_to_next_layer(action, new_propositions, proposition_layer,
                                  proposition_to_add):
    proposition_layer.add_proposition(proposition_to_add)
    if proposition_to_add.name not in new_propositions:
        new_propositions[proposition_to_add.name] = Proposition(
            proposition_to_add.name)
    new_propositions[proposition_to_add.name].add_producer(action)
示例#13
0
  def updatePropositionLayer(self):
    """
    Updates the propositions in the current proposition layer,
    given the current action layer.
    don't forget to update the producers list!
    Note that same proposition in different layers might have different producers lists,
    hence you should create two different instances.
    currentLayerActions is the set of all the actions in the current layer.
    You might want to use those functions:
    dict() creates a new dictionary that might help to keep track on the propositions that you've
           already added to the layer
    self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer       
    
    """
    currentLayerActions = self.actionLayer.getActions()
    propostions = {}
    for action in currentLayerActions:
      for prop in action.getAdd():
        if not prop.getName() in propostions:
          newProp = Proposition(prop.getName())
          propostions[prop.getName()] = newProp

        propostions[prop.getName()].addProducer(action)

    for prop in propostions:
      self.propositionLayer.addProposition(propostions[prop])
示例#14
0
    def getSuccessors(self, state):
        """   
    For a given state, this should return a list of triples, 
    (successor, action, stepCost), where 'successor' is a 
    successor to the current state, 'action' is the action
    required to get there, and 'stepCost' is the incremental 
    cost of expanding to that successor, 1 in our case.
    You might want to this function:
    For a list of propositions l and action a,
    a.allPrecondsInList(l) returns true if the preconditions of a are in l
    """
        self._expanded += 1
        "*** YOUR CODE HERE ***"

        # explain: Successors are the list of actions can be done from the current level
        #   I build the action lyer using pgNext.updateActionLayer
        #   then for each action I build a the Level that wholud have been created if only this action was selected

        Successors = []

        pg = state
        previousPropositionLayer = pg.getPropositionLayer()
        previousLayerPropositions = previousPropositionLayer.propositions

        pgNext = PlanGraphLevel()
        pgNext.updateActionLayer(previousPropositionLayer)

        for Action in pgNext.actionLayer.actions:

            if Action.isNoOp():
                continue

            pgNextAction = PlanGraphLevel()
            pgNextAction.actionLayer.addAction(Action)
            for prop in previousLayerPropositions:
                pgNextAction.propositionLayer.addProposition(prop)
            for prop in Action.getAdd():
                new_prop = Proposition(prop.getName())
                new_prop.addProducer(Action)
                pgNextAction.propositionLayer.addProposition(new_prop)
            for prop in Action.getDelete():
                pgNextAction.propositionLayer.removePropositions(prop)
            pgNextAction.updateMutexProposition()

            Successors.append((pgNextAction, Action, 1))

        return Successors
示例#15
0
    def parse_actions_and_propositions(self):
        propositions = []
        f = open(self.domain_file, 'r')
        _ = f.readline()
        proposition_line = f.readline()
        words = [word.rstrip() for word in proposition_line.split(" ") if len(word.rstrip()) > 0]
        for i in range(0, len(words)):
            propositions.append(Proposition(words[i]))
        actions = []
        f = open(self.domain_file, 'r')
        line = f.readline()
        while line != '':
            words = [word.rstrip() for word in line.split(" ") if len(word.rstrip()) > 0]
            if words[0] == 'Name:':
                name = words[1]
                line = f.readline()
                precond = []
                add = []
                delete = []
                words = [word.rstrip() for word in line.split(" ") if len(word.rstrip()) > 0]
                for i in range(1, len(words)):
                    precond.append(Proposition(words[i]))
                line = f.readline()
                words = [word.rstrip() for word in line.split(" ") if len(word.rstrip()) > 0]
                for i in range(1, len(words)):
                    add.append(Proposition(words[i]))
                line = f.readline()
                words = [word.rstrip() for word in line.split(" ") if len(word.rstrip()) > 0]
                for i in range(1, len(words)):
                    delete.append(Proposition(words[i]))
                act = Action(name, precond, add, delete)
                for prop in add:
                    self.find_prop_by_name(prop, propositions).add_producer(act)
                actions.append(act)
            line = f.readline()

        for a in actions:
            new_pre = [p for p in propositions if p.name in [q.name for q in a.pre]]
            new_add = [p for p in propositions if p.name in [q.name for q in a.add]]
            new_delete = [p for p in propositions if p.name in [q.name for q in a.delete]]
            a.pre = new_pre
            a.add = new_add
            a.delete = new_delete

        return [actions, propositions]
示例#16
0
    def update_proposition_layer(self):
        """
        Updates the propositions in the current proposition layer,
        given the current action layer.
        don't forget to update the producers list!
        Note that same proposition in different layers might have different producers lists,
        hence you should create two different instances.
        current_layer_actions is the set of all the actions in the current layer.
        You might want to use those functions:
        dict() creates a new dictionary that might help to keep track on the propositions that you've
               already added to the layer
        self.proposition_layer.add_proposition(prop) adds the proposition prop to the current layer

        """
        current_layer_actions = self.action_layer.get_actions()
        props = {}
        for act in current_layer_actions:
            for p in (set(act.get_pre()) - set(act.get_delete())) | set(act.get_add()):
                if p not in props.keys():
                    props[p] = Proposition(p.get_name())
                props[p].add_producer(act)

        for prop in props.values():
            self.proposition_layer.add_proposition(prop)
示例#17
0
    def __init__(self,
                 depTree,
                 sentenceId,
                 const_t=0,
                 time_annotations=0,
                 nombank=0):
        if const_t:
            depTree.mark_function_tags(const_t)
        if nombank:
            depTree.mark_nominals(nombank, const_t)

        self.sentenceId = sentenceId
        self.constTree = const_t
        self.depTree = depTree
        self.originalSentence = self.depTree.get_original_sentence()
        self.numOfWords = len(self.originalSentence.split())

        self.propositions = {}
        #self.predicates = depTree.collect_predicates([DepTree.is_copular_predicate])
        self.predicates = depTree.collect_predicates([
            DepTree.is_verbal_predicate, DepTree.is_appositional_predicate,
            DepTree.is_adjectival_predicate, DepTree.is_copular_predicate,
            DepTree.is_possesive_predicate, DepTree.is_conditional_predicate,
            DepTree.is_relative_clause, DepTree.is_nominal_predicate
        ])

        self.printFunctions = {
            syntactic_item.VERBAL_PREDICATE_FEATURE_FUNCTION_PREFIX:
            printVerbal,
            syntactic_item.APPOSITIONAL_PREDICATE_FEATURE_FUNCTION_PREFIX:
            printAppos,
            syntactic_item.ADJECTIVAL_PREDICATE_FEATURE_FUNCTION_PREFIX:
            printAdj,
            syntactic_item.COPULAR_PREDICATE_FEATURE_FUNCTION_PREFIX: printCop,
            syntactic_item.POSSESSIVE_PREDICATE_FEATURE_FUNCTION_PREFIX:
            printPoss,
            syntactic_item.CONDITIONAL_PREDICATE_FEATURE_FUNCTION_PREFIX:
            printCond,
            syntactic_item.RELCLAUSE_PREDICATE_FEATURE_FUNCTION_PREFIX:
            printRCmod
        }

        self.graphFunctions = {
            syntactic_item.VERBAL_PREDICATE_FEATURE_FUNCTION_PREFIX: False,
            syntactic_item.APPOSITIONAL_PREDICATE_FEATURE_FUNCTION_PREFIX:
            False,
            syntactic_item.ADJECTIVAL_PREDICATE_FEATURE_FUNCTION_PREFIX: False,
            syntactic_item.COPULAR_PREDICATE_FEATURE_FUNCTION_PREFIX:
            graphCopular,
            syntactic_item.POSSESSIVE_PREDICATE_FEATURE_FUNCTION_PREFIX: False,
            syntactic_item.CONDITIONAL_PREDICATE_FEATURE_FUNCTION_PREFIX:
            False,
            syntactic_item.RELCLAUSE_PREDICATE_FEATURE_FUNCTION_PREFIX: False
        }

        self.propCount = 0
        for l in self.predicates:
            if l:
                self.propCount += 1
        #self.propCount = reduce(lambda x,y:x+len(y), self.predicates,0)
        self.propPerWord = self.propCount / self.numOfWords

        self.pref = [
            syntactic_item.APPOSITIONAL_PREDICATE_FEATURE_FUNCTION_PREFIX,
            syntactic_item.ADJECTIVAL_PREDICATE_FEATURE_FUNCTION_PREFIX,
            syntactic_item.COPULAR_PREDICATE_FEATURE_FUNCTION_PREFIX,
            syntactic_item.POSSESSIVE_PREDICATE_FEATURE_FUNCTION_PREFIX,
            syntactic_item.CONDITIONAL_PREDICATE_FEATURE_FUNCTION_PREFIX,
            syntactic_item.RELCLAUSE_PREDICATE_FEATURE_FUNCTION_PREFIX
        ]

        # verbal
        self.propositions[
            syntactic_item.VERBAL_PREDICATE_FEATURE_FUNCTION_PREFIX] = []
        for verbSubtree in self.predicates[0]:
            time_ann_of_verb, tmp_function_tag_of_verb = self.collect_time_indications(
                verbSubtree, const_t, time_annotations)
            self.propositions[
                syntactic_item.
                VERBAL_PREDICATE_FEATURE_FUNCTION_PREFIX].append(
                    Proposition(
                        predSubtree=verbSubtree,
                        predicate_prefix=syntactic_item.
                        VERBAL_PREDICATE_FEATURE_FUNCTION_PREFIX,
                        argument_prefix=syntactic_item.
                        VERBAL_ARGUMENT_FEATURE_FUNCTION_PREFIX,
                        time_ann_of_verb=time_ann_of_verb,
                        tmp_function_tag_of_verb=tmp_function_tag_of_verb))
        # all the others
        for subtrees, prefix in zip(self.predicates[1:], self.pref):
            self.propositions[prefix] = []
            for subtree in subtrees:
                self.propositions[prefix].append(Proposition(subtree, prefix))