Ejemplo n.º 1
0
def testppddl():

    domainfile = "domain-0%d.pddl" % 1
    problemfile = "problem-0%d.pddl" % 1

    domprob = DomainProblem(domainfile, problemfile)
    print(domprob.initialstate())
Ejemplo n.º 2
0
 def test_ground(self):
     domprob = DomainProblem(self.domainfile, self.problemfile)
     freeop = domprob.domain.operators["op2"]
     all_grounded_opers = domprob.ground_operator("op2")
     for gop in all_grounded_opers:
         if gop.precondition_pos == set([('S', 'R', 'C'), ('S', 'R', 'S')]):
             self.assertTrue(True)
             return
     self.assertFalse("Missed a value")
Ejemplo n.º 3
0
 def __init__(self, dom_file: str, problem_file: str):
     self._domain_file = dom_file
     self._problem_file = problem_file
     self._domain_problem = DomainProblem(self._domain_file,
                                          self._problem_file)
     self._initial_state = self._to_set_of_tuples(self._domain_problem.
                                                  initialstate())
     self._goal_state = self._to_set_of_tuples(self._domain_problem.goals())
     self._actions = self._get_ground_operators()
     self._formulas = self._get_ground_formulas()
Ejemplo n.º 4
0
 def __init__(self, domainfile, problemfile):
     self.parser_help = PDDL_Parser()
     self.parser_help.parse_domain(domainfile)
     self.parser_help.parse_problem(problemfile)
     self.domprob = DomainProblem(domainfile, problemfile)
     self.current_state = None  # State object
     self.pos_goal = None  # tuple of literals that need to be true at goal
     self.neg_goal = None  # tuple of literals that need to be false at goal
     self.action_dic = None  # list of actions
     self.objects = None  # list of world objects
     self.reversed_objects = None  # for type of object, list of instances
     self.parser()
Ejemplo n.º 5
0
 def __init__(self, dom_file: str, problem_file: str):
     self._domain_file = dom_file
     self._problem_file = problem_file
     self._domain_problem = DomainProblem(self._domain_file,
                                          self._problem_file)
     self._initial_state = self._to_set_of_tuples(
         self._domain_problem.initialstate())
     self._goal_state = self._to_set_of_tuples(self._domain_problem.goals())
     self._actions = self._get_ground_operators()
     self._formulas = self._get_ground_formulas()
     for formula in list(self._formulas):
         if 'in' in formula and 'nil' in formula:
             self._formulas.remove(formula)
     for action in list(self._actions):
         if action.operator_name == 'unload' and \
                 action.variable_list['?c'] == 'nil':
             self._actions.remove(action)
Ejemplo n.º 6
0
def main(argv):
    demonumber = int(argv[1])
    domainfile = "../examples-pddl/domain-0%d.pddl" % demonumber
    problemfile = "../examples-pddl/problem-0%d.pddl" % demonumber
    domprob = DomainProblem(domainfile, problemfile)
    print()
    print("DOMAIN PROBLEM")
    print("objects")
    print("\t", domprob.worldobjects())
    print("operators")
    print("\t", list(domprob.operators()))
    print("init", )
    print("\t", domprob.initialstate())
    print("goal", )
    print("\t", domprob.goals())

    print()
    ops_to_test = {1: "op2", 2: "move", 3: "move", 4: "move", 5: "A1"}
    op = ops_to_test[demonumber]
    print("ground for operator", op, "applicable if (adjacent loc1 loc2)")
    for o in domprob.ground_operator(op):
        if ("adjacent", "loc1", "loc2") in o.precondition_pos:
            print()
            print("\tvars", o.variable_list)
            print("\tpre+", o.precondition_pos)
            print("\tpre-", o.precondition_neg)
            print("\teff+", o.effect_pos)
            print("\teff-", o.effect_neg)
Ejemplo n.º 7
0
def main(argv):
    demonumber = int(argv[1])
    domainfile = "../examples-pddl/domain-0%d.pddl" % demonumber
    problemfile = "../examples-pddl/problem-0%d.pddl" % demonumber
    domprob = DomainProblem(domainfile, problemfile)
    print()
    print("DOMAIN PROBLEM")
    print("objects")
    print("\t", domprob.worldobjects())
    print("operators")
    print("\t", list( domprob.operators() ))
    print("init",)
    print("\t", domprob.initialstate())
    print("goal",)
    print("\t", domprob.goals())

    print()
    ops_to_test = { 1:"op2", 2:"move", 3:"move", 4:"move", 5:"A1" }
    op = ops_to_test[demonumber]
    print("ground for operator", op, "applicable if (adjacent loc1 loc2)")
    for o in domprob.ground_operator(op):
        if ("adjacent","loc1","loc2") in o.precondition_pos:
            print()
            print( "\tvars", o.variable_list )
            print( "\tpre+", o.precondition_pos )
            print( "\tpre-", o.precondition_neg )
            print( "\teff+", o.effect_pos )
            print( "\teff-", o.effect_neg )
Ejemplo n.º 8
0
def main(argv):

    domainfile = "domain-04.pddl"  ##% demonumber
    problemfile = "problem-04.pddl"  ##% demonumber
    domprob = DomainProblem(domainfile, problemfile)
    H = 5
    W = 3
    res = possible_Movements("up", H, W, domprob)
    print(res)
    print("-------")
    res = possible_Paint(H, W, domprob)
    print(res[0])
    print(res[1])
Ejemplo n.º 9
0
def main():
    dp = DomainProblem('domain-03.pddl', 'problem-03.pddl')
    print(astar_search(PlanningProblem(dp)).solution())
Ejemplo n.º 10
0
class Domain():
    def __init__(self, domainfile, problemfile):
        self.parser_help = PDDL_Parser()
        self.parser_help.parse_domain(domainfile)
        self.parser_help.parse_problem(problemfile)
        self.domprob = DomainProblem(domainfile, problemfile)
        self.current_state = None  # State object
        self.pos_goal = None  # tuple of literals that need to be true at goal
        self.neg_goal = None  # tuple of literals that need to be false at goal
        self.action_dic = None  # list of actions
        self.objects = None  # list of world objects
        self.reversed_objects = None  # for type of object, list of instances
        self.parser()

    def parser(self):
        world_dic = self.domprob.worldobjects()
        # object list
        obj_dic = dict()
        for obj in world_dic.keys():
            wo = WorldObject(name=obj)
            wo.family = world_dic[obj]
            obj_dic[obj] = wo

        # reversed objects
        self.reversed_objects = defaultdict(list)
        for obj, typ in self.domprob.problem.objects.items():
            self.reversed_objects[typ].append(obj)
        for type, subtypes in self.parser_help.types.items(
        ):  # add reference to subtypes
            for sub in subtypes:
                self.reversed_objects[type] += self.reversed_objects[sub]

        # action list
        action_dic = dict()
        for op in self.domprob.operators():
            my_op = Action(name=op)
            action_dic[op] = my_op
        for op in action_dic.keys():
            operator = self.domprob.domain.operators[op]
            var_names = operator.variable_list.keys()
            var_types = operator.variable_list.values()
            var_combinations = itertools.product(
                *[self.reversed_objects[typ] for typ in var_types])
            for var_list in var_combinations:
                value = dict()
                # ground preconditions and effects
                value["preconditions_pos"] = {
                    prec.ground({
                        name: inst
                        for (name, inst) in zip(var_names, var_list)
                    })
                    for prec in operator.precondition_pos
                }
                value["preconditions_neg"] = {
                    prec.ground({
                        name: inst
                        for (name, inst) in zip(var_names, var_list)
                    })
                    for prec in operator.precondition_neg
                }
                value["effets_pos"] = {
                    prec.ground({
                        name: inst
                        for (name, inst) in zip(var_names, var_list)
                    })
                    for prec in operator.effect_pos
                }
                value["effets_neg"] = {
                    prec.ground({
                        name: inst
                        for (name, inst) in zip(var_names, var_list)
                    })
                    for prec in operator.effect_neg
                }
                action_dic[op].action_dic[tuple(var_list)] = value

                init_state_set = set()

        init_state = self.domprob.initialstate()
        init_state_set = set()
        for state in init_state:
            init_state_set.add(tuple(state.predicate))

        # goal
        self.pos_goal = set(self.parser_help.positive_goals)
        self.neg_goal = set(self.parser_help.negative_goals)

        ## Update ##
        self.current_state = State(true_predicates=init_state_set,
                                   pos_goal_state=self.pos_goal,
                                   neg_goal_state=self.neg_goal)
        self.action_dic = action_dic
        self.objects = obj_dic
Ejemplo n.º 11
0
class PlanningProblem(object):

    def __init__(self, dom_file: str, problem_file: str):
        self._domain_file = dom_file
        self._problem_file = problem_file
        self._domain_problem = DomainProblem(self._domain_file,
                                             self._problem_file)
        self._initial_state = self._to_set_of_tuples(self._domain_problem.
                                                     initialstate())
        self._goal_state = self._to_set_of_tuples(self._domain_problem.goals())
        self._actions = self._get_ground_operators()
        self._formulas = self._get_ground_formulas()

    @staticmethod
    def _type_symbols(variable_type, world_objects: dict):
        # if variable type is found in the world objects,
        # return list of object names, such as robr, robq
        return (k for k, v in world_objects.items() if v == variable_type)

    def _instantiate(self, variables, world_objects: dict):
        variable_ground_space = []
        for variable_name, variable_type in variables:
            c = []
            for symbol in self._type_symbols(variable_type, world_objects):
                c.append((variable_name, symbol))
            variable_ground_space.append(c)
        return itertools.product(*variable_ground_space)

    def _get_ground_operators(self) -> List[Operator]:
        ground_operators = []
        for operator in self._domain_problem.operators():
            op = self._domain_problem.domain.operators[operator]
            for ground in self._instantiate(op.variable_list.items(),
                                            self._domain_problem.
                                            worldobjects()):
                st = dict(ground)
                gop = Operator(operator)
                gop.variable_list = st
                gop.precondition_pos = set(
                    [a.ground(st) for a in op.precondition_pos])
                gop.precondition_neg = set(
                    [a.ground(st) for a in op.precondition_neg])
                gop.effect_pos = set([a.ground(st) for a in op.effect_pos])
                gop.effect_neg = set([a.ground(st) for a in op.effect_neg])
                ground_operators.append(gop)
        return ground_operators

    def _get_ground_formulas(self) -> List[tuple]:
        ground_formulas = []
        for predicate in self._domain_problem.domain.predicates:
            if predicate.name == 'adjacent':
                continue
            for ground in self._instantiate(predicate.variables.items(),
                                            self._domain_problem.
                                            worldobjects()):
                st = dict(ground)
                pred = [predicate.name]
                for k, v in st.items():
                    pred.append(v)
                ground_formulas.append(tuple(pred))
        return ground_formulas

    @staticmethod
    def _to_set_of_tuples(state: Set[Atom]) -> Set[Tuple]:
        set_of_tuples = set()
        for atom in state:
            tup = tuple(atom.predicate)
            set_of_tuples.add(tup)
        return set_of_tuples

    @property
    def initial_state(self):
        return self._initial_state

    @property
    def goal_state(self):
        return self._goal_state

    @property
    def actions(self):
        return self._actions

    @property
    def fluents(self):
        return self._formulas