コード例 #1
0
ファイル: actions.py プロジェクト: aldukeman/fdss
 def parse(alist):
     iterator = iter(alist)
     assert iterator.next() == ":action"
     name = iterator.next()
     parameters_tag_opt = iterator.next()
     if parameters_tag_opt == ":parameters":
         parameters = pddl_types.parse_typed_list(iterator.next(),
                                                  only_variables=True)
         precondition_tag_opt = iterator.next()
     else:
         parameters = []
         precondition_tag_opt = parameters_tag_opt
     if precondition_tag_opt == ":precondition":
         precondition = conditions.parse_condition(iterator.next())
         precondition = precondition.simplified()
         effect_tag = iterator.next()
     else:
         precondition = conditions.Conjunction([])
         effect_tag = precondition_tag_opt
     assert effect_tag == ":effect"
     effect_list = iterator.next()
     eff = []
     try:
         cost = effects.parse_effects(effect_list, eff)
     except ValueError, e:
         raise SystemExit("Error in Action %s\nReason: %s." % (name, e))
コード例 #2
0
 def parse(alist):
     iterator = iter(alist)
     assert iterator.next() == ":action"
     name = iterator.next()
     parameters_tag_opt = iterator.next()
     if parameters_tag_opt == ":parameters":
         parameters = pddl_types.parse_typed_list(iterator.next(),
                                                  only_variables=True)
         precondition_tag_opt = iterator.next()
     else:
         parameters = []
         precondition_tag_opt = parameters_tag_opt
     if precondition_tag_opt == ":precondition":
         precondition = conditions.parse_condition(iterator.next())
         effect_tag = iterator.next()
     else:
         precondition = conditions.Conjunction([])
         effect_tag = precondition_tag_opt
     assert effect_tag == ":effect"
     effect_list = iterator.next()
     eff = []
     try:
         cost = effects.parse_effects(effect_list, eff)
     except ValueError, e:
         raise SystemExit("Error in Action %s\nReason: %s." % (name, e))
コード例 #3
0
 def parse(alist):
     iterator = iter(alist)
     assert iterator.next() == ":action"
     name = iterator.next()
     parameters_tag_opt = iterator.next()
     if parameters_tag_opt == ":parameters":
         parameters = pddl_types.parse_typed_list(iterator.next(),
                                                  only_variables=True)
         precondition_tag_opt = iterator.next()
     else:
         parameters = []
         precondition_tag_opt = parameters_tag_opt
     if precondition_tag_opt == ":precondition":
         precondition = conditions.parse_condition(iterator.next())
         effect_tag = iterator.next()
     else:
         precondition = conditions.Conjunction([])
         effect_tag = precondition_tag_opt
     assert effect_tag == ":effect"
     effect_list = iterator.next()
     eff = []
     effects.parse_effects(effect_list,eff)
     for rest in iterator:
         assert False, rest
     return Action(name, parameters, precondition, eff)
コード例 #4
0
 def parse(alist):
     assert len(alist) == 3
     assert alist[0] == ":derived"
     predicate = predicates.Predicate.parse(alist[1])
     condition = conditions.parse_condition(alist[2])
     return Axiom(predicate.name, predicate.arguments,
                  len(predicate.arguments), condition)
コード例 #5
0
    def parse(action_list):
        iterator = iter(action_list)

        if (iterator.next() != ":action"):
	    raise Error("STRIPS file does not contain actions")

        name = iterator.next()
        params = iterator.next()
        if params == ":parameters":
            parameters = strip_types.parse_typed_list(iterator.next())
            precond = iterator.next()
        else:
            parameters = []
            precond = params
        if precond == ":precondition":
            precondition = conditions.parse_condition(iterator.next())
            effect_iter = iterator.next()
        else:
            precondition = conditions.Conjunction([])
            effect_iter = precond
        assert effect_iter == ":effect"
        effect_list = iterator.next()
        eff = []
        cost = effects.parse_effects(effect_list, eff)
        for rest in iterator:
            assert False, rest
        return Action(name, parameters, precondition, eff)
コード例 #6
0
ファイル: effects.py プロジェクト: AT-GROUP/AT-PLANNER
def parse_effect(alist,durative=False):
    tag = alist[0]
    if tag == "and":
        return TmpEffect([parse_effect(eff,durative) for eff in alist[1:]])
    elif tag == "forall":
        assert len(alist)==3
        return UniversalEffect(pddl_types.parse_typed_list(alist[1]),
                               parse_effect(alist[2],durative))
    elif tag == "when":
        assert len(alist)==3
        if durative:
            condition = conditions.parse_durative_condition(alist[1])
            effect = parse_timed_effect(alist[2])
        else:
            condition = conditions.parse_condition(alist[1])
            effect = parse_cond_effect(alist[2])
        return ConditionalEffect(condition,effect)
    elif tag == "at" and durative:
        return parse_timed_effect(alist)
    elif tag == "change":
        assert durative
        new_alist = ["and", ["at", "start", ["assign", alist[1], "undefined"]],
                           ["at", "end", ["assign", alist[1], alist[2]]]]
        return parse_effect(new_alist,durative)
    else:
        return parse_cond_effect(alist)
コード例 #7
0
    def parse(alist):
        iterator = iter(alist)
        action_tag = next(iterator)
        assert action_tag == ':action'
        name = next(iterator)
        parameters_tag_opt = next(iterator)
        if parameters_tag_opt == ':parameters':
            parameters = pddl_types.parse_typed_list(next(iterator),
                                                     only_variables=True)
            precondition_tag_opt = next(iterator)
        else:
            parameters = []
            precondition_tag_opt = parameters_tag_opt
        if precondition_tag_opt == ':precondition':
            precondition_list = next(iterator)
            if not precondition_list:

                # Note that :precondition () is allowed in PDDL.

                precondition = conditions.Conjunction([])
            else:
                precondition = \
                    conditions.parse_condition(precondition_list)
                precondition = precondition.simplified()
            effect_tag = next(iterator)
        else:
            precondition = conditions.Conjunction([])
            effect_tag = precondition_tag_opt
        assert effect_tag == ':effect'
        effect_list = next(iterator)
        try:
            cost_eff_pairs = effects.parse_effects(effect_list)
            if 1 == len(cost_eff_pairs):
                cost_eff_pairs = [(cost_eff_pairs[0][0], cost_eff_pairs[0][1],
                                   '')]
            else:

                # Convert floats to fractions to output
                # TODO : Benchmark this fraction conversion

                all_fractions = []
                for cep in cost_eff_pairs:
                    all_fractions.append(
                        fractions.Fraction(cep[2]).limit_denominator())
                lcm = reduce(lambda a, b: a * b / fractions.gcd(a, b),
                             [f.denominator for f in all_fractions], 1)

                # Use the fractions and lcm to build the weights

                #for i in range(len(cost_eff_pairs)):
                #    for j in range(len(cost_eff_pairs[i])):
                #        print(cost_eff_pairs[i][j])

                cost_eff_pairs = [(cost_eff_pairs[i][0], cost_eff_pairs[i][1],
                                   '_DETDUP_%d_WEIGHT_%d_%d' %
                                   (i, all_fractions[i].numerator,
                                    all_fractions[i].denominator))
                                  for i in range(len(cost_eff_pairs))]
        except ValueError, e:
            raise SystemExit('Error in Action %s\nReason: %s.' % (name, e))
コード例 #8
0
ファイル: effects.py プロジェクト: speckdavid/shakey2016
def parse_effect(alist, durative=False):
    tag = alist[0]
    if tag == "and":
        return TmpEffect([parse_effect(eff, durative) for eff in alist[1:]])
    elif tag == "forall":
        assert len(alist) == 3
        return UniversalEffect(pddl_types.parse_typed_list(alist[1]),
                               parse_effect(alist[2], durative))
    elif tag == "when":
        assert len(alist) == 3
        if durative:
            condition = conditions.parse_durative_condition(alist[1])
            effect = parse_timed_effect(alist[2])
        else:
            condition = conditions.parse_condition(alist[1])
            effect = parse_cond_effect(alist[2])
        return ConditionalEffect(condition, effect)
    elif tag == "at" and durative:
        return parse_timed_effect(alist)
    elif tag == "change":
        assert durative
        new_alist = [
            "and", ["at", "start", ["assign", alist[1], "undefined"]],
            ["at", "end", ["assign", alist[1], alist[2]]]
        ]
        return parse_effect(new_alist, durative)
    else:
        return parse_cond_effect(alist)
コード例 #9
0
ファイル: actions.py プロジェクト: ramonpereira/igraph
 def parse(alist):
     iterator = iter(alist)
     assert iterator.next() == ":action"
     name = iterator.next()
     parameters_tag_opt = iterator.next()
     if parameters_tag_opt == ":parameters":
         parameters = pddl_types.parse_typed_list(iterator.next(),
                                                  only_variables=True)
         precondition_tag_opt = iterator.next()
     else:
         parameters = []
         precondition_tag_opt = parameters_tag_opt
     if precondition_tag_opt == ":precondition":
         precondition = conditions.parse_condition(iterator.next())
         effect_tag = iterator.next()
     else:
         precondition = conditions.Conjunction([])
         effect_tag = precondition_tag_opt
     assert effect_tag == ":effect"
     effect_list = iterator.next()
     eff = []
     cost = effects.parse_effects(effect_list, eff)
     for rest in iterator:
         assert False, rest
     return Action(name, parameters, precondition, eff, cost)
コード例 #10
0
def parse_effect(alist):
    tag = alist[0]
    if tag == "and":
        return [
            ConjunctiveEffect(conjuncts) for conjuncts in cartesian_product(
                *[parse_effect(eff) for eff in alist[1:]])
        ]
    elif tag == "forall":
        assert len(alist) == 3
        parameters = pddl_types.parse_typed_list(alist[1])
        effects = parse_effect(alist[2])
        assert 1 == len(
            effects
        ), "Error: Cannot embed non-determinism inside of a forall (for now)."
        return [UniversalEffect(parameters, effect) for effect in effects]
    elif tag == "when":
        assert len(alist) == 3
        condition = conditions.parse_condition(alist[1])
        effects = parse_effect(alist[2])
        return [ConditionalEffect(condition, effect) for effect in effects]
    elif tag == "increase":
        assert len(alist) == 3
        assert alist[1] == ['total-cost']
        assignment = f_expression.parse_assignment(alist)
        return [CostEffect(assignment)]
    elif tag == "oneof":
        options = []
        for eff in alist[1:]:
            options.extend(parse_effect(eff))
        return options
    else:
        return [SimpleEffect(conditions.parse_literal(alist))]
コード例 #11
0
def parse_task(task_pddl):
    iterator = iter(task_pddl)

    assert iterator.next() == "define"
    problem_line = iterator.next()
    assert problem_line[0] == "problem" and len(problem_line) == 2
    yield problem_line[1]
    domain_line = iterator.next()
    assert domain_line[0] == ":domain" and len(domain_line) == 2
    yield domain_line[1]

    objects_opt = iterator.next()
    if objects_opt[0] == ":objects":
        yield pddl_types.parse_typed_list(objects_opt[1:])
        init = iterator.next()
    else:
        yield []
        init = objects_opt

    assert init[0] == ":init"
    initial = []
    for fact in init[1:]:
        if fact[0] == "=":
            if conditions.is_function_comparison(fact):  # numeric function
                initial.append(f_expression.parse_assignment(fact))
            else:  # object function
                function = conditions.parse_term(fact[1])
                terms = function.args
                terms.append(conditions.parse_term(fact[2]))
                atomname = conditions.function_predicate_name(function.name)
                initial.append(conditions.Atom(atomname, terms))
        else:
            initial.append(
                conditions.Atom(
                    fact[0],
                    [conditions.parse_term(term) for term in fact[1:]]))
    yield initial

    goal = iterator.next()
    assert goal[0] == ":goal" and len(goal) == 2
    yield conditions.parse_condition(goal[1])

    metric_defined = False
    for entry in iterator:
        if entry[0] == ":metric":
            metric_defined = True
            #        if entry[2][0] in ["total-time", "total-cost"] :
            metric = (entry[1], entry[2])

            yield metric
    if not metric_defined:
        metric = ()
        yield metric
コード例 #12
0
def parse_task(task_pddl):
  iterator = iter(task_pddl)

  assert iterator.next() == "define"
  problem_line = iterator.next()
  assert problem_line[0] == "problem" and len(problem_line) == 2
  yield problem_line[1]
  domain_line = iterator.next()
  assert domain_line[0] == ":domain" and len(domain_line) == 2
  yield domain_line[1]

  module_opt = iterator.next()
  if module_opt[0] == ":moduleoptions":
    yield [modules.ModuleInit.parse(mi) for mi in module_opt[1:]]
    module_exit_opt = iterator.next()
  else:
    yield []
    module_exit_opt = module_opt

  if module_exit_opt[0] == ":moduleexitoptions":
    yield [modules.ModuleExit.parse(mi) for mi in module_exit_opt[1:]]
    objects_opt = iterator.next()
  else:
    yield []
    objects_opt = module_exit_opt

  if objects_opt[0] == ":objects":
    yield pddl_types.parse_typed_list(objects_opt[1:])
    init = iterator.next()
  else:
    yield []
    init = objects_opt

  assert init[0] == ":init"
  initial = []
  timed_initial = []
  for fact in init[1:]:
    if fact[0] == "at" and len(fact) == 3 and f_expression.isFloat(fact[1]):
        timed_initial.append(timed_initials.TimedInitial(float(fact[1]),fact[2]))
    else:
        initial.append(parse_init_fact(fact))
  yield initial
  yield timed_initial

  goal = iterator.next()
  assert goal[0] == ":goal" and len(goal) == 2
  yield conditions.parse_condition(goal[1])

  for entry in iterator:
    if entry[0] == ":metric" and entry[1]=="minimize":
        if entry[2][0] in ["total-time", "total-cost"] :
            continue
    assert False, "Can only minimize total-time or total-cost, got: " + str(entry)
コード例 #13
0
def parse_problem(problem_strips_generator):
    iterator = iter(problem_strips_generator)

    if (iterator.next() != "define"):
	raise Error("STRIPS problem file does not contain define")

    problem_definition = iterator.next()
    #Check for errors in the problem file
    if (problem_definition[0] == "problem" and len(problem_definition) == 2):
	yield problem_definition[1]
    else:
	raise Error("STRIPS problem file is not structured properly")
    
    domain_description = iterator.next()
    #Check for issues in the problem's domain description
    if (domain_description[0] == ":domain" and len(domain_description) == 2):
	yield domain_description[1]
    else:
	raise Error("STRIPS problem file does not contain a domain description")   	

    objects = iterator.next()
    """if requirements[0] == ":requirements":
        yield requirements[1] #contains :typing
        objects = iterator.next()
    else:
	objects = requirements	"""

    if objects[0] == ":objects":
        yield strip_types.parse_typed_list(objects[1:])
        init = iterator.next()
    else:
        yield []
        init = objects

    if (init[0] != ":init"):
	raise Error("STRIPS problem contains no init state")

    initial = []
    for fact in init[1:]:
        initial.append(conditions.Atom(fact[0], fact[1:]))
    yield initial

    goal = iterator.next()

    if (goal[0] == ":goal" and len(goal) == 2):
	yield conditions.parse_condition(goal[1])
    else:
	raise Error("STRIPS problem contains no goal state")
コード例 #14
0
ファイル: effects.py プロジェクト: AT-GROUP/AT-PLANNER
def parse_cond_effect(alist, durative=False):
    tag = alist[0]
    if tag == "and":
        return ConjunctiveEffect([parse_cond_effect(eff,durative) for eff in alist[1:]])
    elif tag in ("scale-up", "scale-down", "increase", "decrease"):
        return ConjunctiveEffect([f_expression.parse_assignment(alist,durative)])
    elif tag == "assign":
        symbol = alist[1]
        if isinstance(symbol,list):
            symbol = symbol[0]
        if tasks.Task.FUNCTION_SYMBOLS.get(symbol,"object")=="number":
            return ConjunctiveEffect([f_expression.parse_assignment(alist,durative)])
        else:
            return ConjunctiveEffect([ObjectFunctionAssignment(conditions.parse_term(alist[1]),conditions.parse_term(alist[2]))])
    else:
        return ConjunctiveEffect([conditions.parse_condition(alist)])
コード例 #15
0
def parse_cond_effect(alist, durative=False):
    tag = alist[0]
    if tag == "and":
        return ConjunctiveEffect([parse_cond_effect(eff, durative) for eff in alist[1:]])
    elif tag in ("scale-up", "scale-down", "increase", "decrease"):
        return ConjunctiveEffect([f_expression.parse_assignment(alist, durative)])
    elif tag == "assign":
        symbol = alist[1]
        if isinstance(symbol, list):
            symbol = symbol[0]
        if tasks.Task.FUNCTION_SYMBOLS.get(symbol, "object") == "number":
            return ConjunctiveEffect([f_expression.parse_assignment(alist, durative)])
        else:
            return ConjunctiveEffect(
                [ObjectFunctionAssignment(conditions.parse_term(alist[1]), conditions.parse_term(alist[2]))])
    else:
        return ConjunctiveEffect([conditions.parse_condition(alist)])
コード例 #16
0
ファイル: tasks.py プロジェクト: aldukeman/lmtd
def parse_task(task_pddl):
  iterator = iter(task_pddl)

  assert iterator.next() == "define"
  problem_line = iterator.next()
  assert problem_line[0] == "problem" and len(problem_line) == 2
  yield problem_line[1]
  domain_line = iterator.next()
  assert domain_line[0] == ":domain" and len(domain_line) == 2
  yield domain_line[1]

  objects_opt = iterator.next()
  if objects_opt[0] == ":objects":
    yield pddl_types.parse_typed_list(objects_opt[1:])
    init = iterator.next()
  else:
    yield []
    init = objects_opt

  assert init[0] == ":init"
  initial = []
  for fact in init[1:]:
    if fact[0] == "=":
        if conditions.is_function_comparison(fact): # numeric function
            initial.append(f_expression.parse_assignment(fact))
        else: # object function
            function = conditions.parse_term(fact[1])
            terms = function.args
            terms.append(conditions.parse_term(fact[2]))
            atomname = conditions.function_predicate_name(function.name)
            initial.append(conditions.Atom(atomname, terms))
    else:
        initial.append(conditions.Atom(fact[0], [conditions.parse_term(term) for term in fact[1:]]))
  yield initial

  goal = iterator.next()
  assert goal[0] == ":goal" and len(goal) == 2
  yield conditions.parse_condition(goal[1])

  for entry in iterator:
    if entry[0] == ":metric" and entry[1]=="minimize":
        if entry[2][0] in ["total-time", "total-cost"] :
            continue
    assert False, entry
コード例 #17
0
def parse_task(task_pddl):
  iterator = iter(task_pddl)

  assert iterator.next() == "define"
  problem_line = iterator.next()
  assert problem_line[0] == "problem" and len(problem_line) == 2
  yield problem_line[1]
  domain_line = iterator.next()
  assert domain_line[0] == ":domain" and len(domain_line) == 2
  yield domain_line[1]

  objects_opt = iterator.next()
  if objects_opt[0] == ":objects":
    yield pddl_types.parse_typed_list(objects_opt[1:])
    init = iterator.next()
  else:
    yield []
    init = objects_opt

  assert init[0] == ":init"
  initial = []
  for fact in init[1:]:
    if fact[0] == "=":
        initial.append(f_expression.parse_assignment(fact))
    else:
        initial.append(conditions.Atom(fact[0], fact[1:]))
  yield initial

  goal = iterator.next()
  assert goal[0] == ":goal" and len(goal) == 2
  yield conditions.parse_condition(goal[1])

  use_metric = False
  for entry in iterator:
    if entry[0] == ":metric":
      if entry[1]=="minimize" and entry[2][0] == "total-cost":
        use_metric = True
      else:
        assert False, "Unknown metric."  
  yield use_metric
          
  for entry in iterator:
    assert False, entry
コード例 #18
0
ファイル: effects.py プロジェクト: yanxi0830/PDDL-RM
def parse_effect(alist):
    tag = alist[0]
    if tag == "and":
        return ConjunctiveEffect([parse_effect(eff) for eff in alist[1:]])
    elif tag == "forall":
        assert len(alist) == 3
        parameters = pddl_types.parse_typed_list(alist[1])
        effect = parse_effect(alist[2])
        return UniversalEffect(parameters, effect)
    elif tag == "when":
        assert len(alist) == 3
        condition = conditions.parse_condition(alist[1])
        effect = parse_effect(alist[2])
        return ConditionalEffect(condition, effect)
    elif tag == "increase":
        assert len(alist) == 3
        assert alist[1] == ['total-cost']
        assignment = f_expression.parse_assignment(alist)
        return CostEffect(assignment)
    else:
        return SimpleEffect(conditions.parse_literal(alist))
コード例 #19
0
def parse_effect(alist):
    tag = alist[0]
    if tag == "and":
      return ConjunctiveEffect([parse_effect(eff) for eff in alist[1:]])
    elif tag == "forall":
      assert len(alist) == 3
      parameters = pddl_types.parse_typed_list(alist[1])
      effect = parse_effect(alist[2])
      return UniversalEffect(parameters, effect)
    elif tag == "when":
      assert len(alist) == 3
      condition = conditions.parse_condition(alist[1])
      effect = parse_effect(alist[2])
      return ConditionalEffect(condition, effect)
    elif tag == "increase":
      assert len(alist) == 3
      assert alist[1] == ['total-cost']
      assignment = f_expression.parse_assignment(alist)
      return CostEffect(assignment)
    else:
        return SimpleEffect(conditions.parse_literal(alist))
コード例 #20
0
 def parse(alist):
     iterator = iter(alist)
     action_tag = next(iterator)
     assert action_tag == ":action"
     name = next(iterator)
     parameters_tag_opt = next(iterator)
     if parameters_tag_opt == ":parameters":
         parameters = pddl_types.parse_typed_list(next(iterator),
                                                  only_variables=True)
         precondition_tag_opt = next(iterator)
     else:
         parameters = []
         precondition_tag_opt = parameters_tag_opt
     if precondition_tag_opt == ":precondition":
         precondition_list = next(iterator)
         if not precondition_list:
             # Note that :precondition () is allowed in PDDL.
             precondition = conditions.Conjunction([])
         else:
             precondition = conditions.parse_condition(precondition_list)
             precondition = precondition.simplified()
         effect_tag = next(iterator)
     else:
         precondition = conditions.Conjunction([])
         effect_tag = precondition_tag_opt
     assert effect_tag == ":effect"
     effect_list = next(iterator)
     try:
         cost_eff_pairs = effects.parse_effects(effect_list)
         if 1 == len(cost_eff_pairs):
             cost_eff_pairs = [(cost_eff_pairs[0][0], cost_eff_pairs[0][1], '')]
         else:
             cost_eff_pairs = [(cost_eff_pairs[i][0], cost_eff_pairs[i][1], "_DETDUP_%d" % i) for i in range(len(cost_eff_pairs))]
     except ValueError as e:
         raise SystemExit("Error in Action %s\nReason: %s." % (name, e))
     for rest in iterator:
         assert False, rest
     return [Action(name + suffix, parameters, len(parameters), precondition, eff, cost) for (cost, eff, suffix) in cost_eff_pairs]
コード例 #21
0
ファイル: tasks.py プロジェクト: aldukeman/fdss
        init = objects_opt

    assert init[0] == ":init"
    initial = []
    for fact in init[1:]:
        if fact[0] == "=":
            try:
                initial.append(f_expression.parse_assignment(fact))
            except ValueError, e:
                raise SystemExit("Error in initial state specification\n" +
                                 "Reason: %s." %  e)
        else:
            initial.append(conditions.Atom(fact[0], fact[1:]))
    yield initial

    goal = iterator.next()
    assert goal[0] == ":goal" and len(goal) == 2
    yield conditions.parse_condition(goal[1])

    use_metric = False
    for entry in iterator:
        if entry[0] == ":metric":
            if entry[1]=="minimize" and entry[2][0] == "total-cost":
                use_metric = True
            else:
                assert False, "Unknown metric."
    yield use_metric

    for entry in iterator:
        assert False, entry
コード例 #22
0
ファイル: tasks.py プロジェクト: aig-upf/fond-sat
def parse_task(task_pddl):
    iterator = iter(task_pddl)

    define_tag = next(iterator)
    assert define_tag == "define"
    problem_line = next(iterator)
    assert problem_line[0] == "problem" and len(problem_line) == 2
    yield problem_line[1]
    domain_line = next(iterator)
    assert domain_line[0] == ":domain" and len(domain_line) == 2
    yield domain_line[1]

    requirements_opt = next(iterator)
    if requirements_opt[0] == ":requirements":
        requirements = requirements_opt[1:]
        objects_opt = next(iterator)
    else:
        requirements = []
        objects_opt = requirements_opt
    yield Requirements(requirements)

    if objects_opt[0] == ":objects":
        yield pddl_types.parse_typed_list(objects_opt[1:])
        init = next(iterator)
    else:
        yield []
        init = objects_opt

    assert init[0] == ":init"
    initial = []
    initial_true = set()
    initial_false = set()
    initial_assignments = dict()
    for fact in init[1:]:
        if fact[0] == "=":
            try:
                assignment = f_expression.parse_assignment(fact)
            except ValueError as e:
                raise SystemExit("Error in initial state specification\n" +
                                 "Reason: %s." % e)
            if not isinstance(assignment.expression,
                              f_expression.NumericConstant):
                raise SystemExit("Illegal assignment in initial state " +
                                 "specification:\n%s" % assignment)
            if assignment.fluent in initial_assignments:
                prev = initial_assignments[assignment.fluent]
                if assignment.expression == prev.expression:
                    print("Warning: %s is specified twice" % assignment,
                          "in initial state specification")
                else:
                    raise SystemExit("Error in initial state specification\n" +
                                     "Reason: conflicting assignment for " +
                                     "%s." % assignment.fluent)
            else:
                initial_assignments[assignment.fluent] = assignment
                initial.append(assignment)
        elif fact[0] == "not":
            atom = conditions.Atom(fact[1][0], fact[1][1:])
            check_atom_consistency(atom, initial_false, initial_true, False)
            initial_false.add(atom)
        else:
            atom = conditions.Atom(fact[0], fact[1:])
            check_atom_consistency(atom, initial_true, initial_false)
            initial_true.add(atom)
    initial.extend(initial_true)
    yield initial

    goal = next(iterator)
    assert goal[0] == ":goal" and len(goal) == 2
    yield conditions.parse_condition(goal[1])

    use_metric = False
    for entry in iterator:
        if entry[0] == ":metric":
            if entry[1] == "minimize" and entry[2][0] == "total-cost":
                use_metric = True
            else:
                assert False, "Unknown metric."
    yield use_metric

    for entry in iterator:
        assert False, entry
コード例 #23
0
ファイル: actions.py プロジェクト: speckdavid/shakey2016
    def parse(alist):
        iterator = iter(alist)
        assert iterator.next() == ":durative-action"
        name = iterator.next()
        print "Parsing durative-action", name
        parameters_tag_opt = iterator.next()
        if parameters_tag_opt == ":parameters":
            parameters = pddl_types.parse_typed_list(iterator.next(),
                                                     only_variables=True)
            grounding_tag = iterator.next()
        else:
            parameters = []
            grounding_tag = parameters_tag_opt

        if grounding_tag == ":grounding":
            grounding_list = iterator.next()
            grounding_call = conditions.parse_condition(grounding_list)
            duration_tag = iterator.next()
        else:
            grounding_call = None
            duration_tag = grounding_tag

        assert duration_tag == ":duration"
        duration_list = iterator.next()
        if duration_list[0] == "and":
            duration_list = duration_list[1:]
        else:
            duration_list = [duration_list]
        duration_start = []
        duration_end = []
        for item in duration_list:  # each item is a simple-duration-constraint
            duration = duration_start
            if item[0] == "at":
                if item[1] == "end":
                    duration = duration_end
                item = item[2]
            assert item[0] in ("<=", ">=", "=")
            if len(item) == 3:  # = ?duration 1
                assert item[1] == "?duration"
                op = item[0]
                value = f_expression.parse_expression(item[2])
                duration += [(op, value)]
            else:  # should be module: = ?duration [module]
                assert item[1] == "?duration"
                op = item[0]
                mod_list = item[2:]
                #print "Modlist:", mod_list
                value = conditions.parse_condition(mod_list)
                #print "parse to ", value
                duration += [(op, value)]
                pass
        condition_tag = iterator.next()
        if condition_tag == ":condition":
            condition = conditions.parse_durative_condition(iterator.next())
            effect_tag = iterator.next()
        else:
            condition = conditions.parse_durative_condition([])
            effect_tag = condition_tag
        assert effect_tag == ":effect"

        effect_list = iterator.next()
        effect = [[], []]
        effects.parse_durative_effects(effect_list, effect)
        for rest in iterator:
            assert False, rest
        return DurativeAction(name, parameters, grounding_call,
                              (duration_start, duration_end), condition,
                              effect)
コード例 #24
0
        init = objects_opt

    assert init[0] == ":init"
    initial = []
    for fact in init[1:]:
        if fact[0] == "=":
            try:
                initial.append(f_expression.parse_assignment(fact))
            except ValueError, e:
                raise SystemExit("Error in initial state specification\n" +
                                 "Reason: %s." % e)
        else:
            initial.append(conditions.Atom(fact[0], fact[1:]))
    yield initial

    goal = iterator.next()
    assert goal[0] == ":goal" and len(goal) == 2
    yield conditions.parse_condition(goal[1])

    use_metric = False
    for entry in iterator:
        if entry[0] == ":metric":
            if entry[1] == "minimize" and entry[2][0] == "total-cost":
                use_metric = True
            else:
                assert False, "Unknown metric."
    yield use_metric

    for entry in iterator:
        assert False, entry
コード例 #25
0
    def parse(alist):
        iterator = iter(alist)
        assert iterator.next() == ":durative-action"
        name = iterator.next()
        print "Parsing durative-action", name
        parameters_tag_opt = iterator.next()
        if parameters_tag_opt == ":parameters":
            parameters = pddl_types.parse_typed_list(iterator.next(),
                                                     only_variables=True)
            grounding_tag = iterator.next()
        else:
            parameters = []
            grounding_tag = parameters_tag_opt

        if grounding_tag == ":grounding":
            grounding_list = iterator.next()
            grounding_call = conditions.parse_condition(grounding_list)
            duration_tag = iterator.next()
        else:
            grounding_call = None
            duration_tag = grounding_tag

        assert duration_tag == ":duration"
        duration_list = iterator.next()
        if duration_list[0] == "and":
            duration_list = duration_list[1:]
        else:
            duration_list = [duration_list]
        duration_start = []
        duration_end = []
        for item in duration_list: # each item is a simple-duration-constraint
            duration = duration_start
            if item[0] == "at":
                if item[1] == "end":
                    duration = duration_end
                item = item[2]
            assert item[0] in ("<=",">=","=")
            if len(item) == 3:    # = ?duration 1
              assert item[1] == "?duration"
              op = item[0]
              value = f_expression.parse_expression(item[2])
              duration += [(op,value)]
            else:                 # should be module: = ?duration [module]
              assert item[1] == "?duration"
              op = item[0]
              mod_list = item[2:]
              #print "Modlist:", mod_list
              value = conditions.parse_condition(mod_list)
              #print "parse to ", value
              duration += [(op,value)]
              pass
        condition_tag = iterator.next()
        if condition_tag == ":condition":
            condition = conditions.parse_durative_condition(iterator.next())
            effect_tag = iterator.next()
        else:
            condition = conditions.parse_durative_condition([])
            effect_tag = condition_tag
        assert effect_tag == ":effect"

        effect_list = iterator.next()
        effect = [[],[]]
        effects.parse_durative_effects(effect_list, effect)
        for rest in iterator:
            assert False, rest
        return DurativeAction(name, parameters, grounding_call, (duration_start,duration_end), condition, effect)
コード例 #26
0
ファイル: tasks.py プロジェクト: speckdavid/shakey2016
def parse_task(task_pddl):
    iterator = iter(task_pddl)

    assert iterator.next() == "define"
    problem_line = iterator.next()
    assert problem_line[0] == "problem" and len(problem_line) == 2
    yield problem_line[1]
    domain_line = iterator.next()
    assert domain_line[0] == ":domain" and len(domain_line) == 2
    yield domain_line[1]

    module_opt = iterator.next()
    if module_opt[0] == ":moduleoptions":
        yield [modules.ModuleInit.parse(mi) for mi in module_opt[1:]]
        module_exit_opt = iterator.next()
    else:
        yield []
        module_exit_opt = module_opt

    if module_exit_opt[0] == ":moduleexitoptions":
        yield [modules.ModuleExit.parse(mi) for mi in module_exit_opt[1:]]
        objects_opt = iterator.next()
    else:
        yield []
        objects_opt = module_exit_opt

    if objects_opt[0] == ":objects":
        yield pddl_types.parse_typed_list(objects_opt[1:])
        init = iterator.next()
    else:
        yield []
        init = objects_opt

    assert init[0] == ":init"
    initial = []
    for fact in init[1:]:
        if fact[0] == "=":
            if conditions.is_function_comparison(fact):  # numeric function
                initial.append(f_expression.parse_assignment(fact))
            else:  # object function
                function = conditions.parse_term(fact[1])
                terms = function.args
                terms.append(conditions.parse_term(fact[2]))
                atomname = conditions.function_predicate_name(function.name)
                initial.append(conditions.Atom(atomname, terms))
        else:
            initial.append(
                conditions.Atom(
                    fact[0],
                    [conditions.parse_term(term) for term in fact[1:]]))
    yield initial

    goal = iterator.next()
    assert goal[0] == ":goal" and len(goal) == 2
    yield conditions.parse_condition(goal[1])

    for entry in iterator:
        if entry[0] == ":metric" and entry[1] == "minimize":
            if entry[2][0] in ["total-time", "total-cost"]:
                continue
        assert False, "Can only minimize total-time or total-cost, got: " + str(
            entry)
コード例 #27
0
ファイル: tasks.py プロジェクト: vtran/CPCL
def parse_task(task_pddl):
  iterator = iter(task_pddl)

  assert iterator.next() == "define"
  problem_line = iterator.next()
  assert problem_line[0] == "problem" and len(problem_line) == 2
  yield problem_line[1]
  domain_line = iterator.next()
  assert domain_line[0] == ":domain" and len(domain_line) == 2
  yield domain_line[1]

  objects_opt = iterator.next()
  if objects_opt[0] == ":objects":
    yield pddl_types.parse_typed_list(objects_opt[1:])
    init = iterator.next()
  else:
    yield []
    init = objects_opt

  assert init[0] == ":init"
  oneofs = []
  initial = []
  ors = []
  belief_state = [set()]
  skip_loop = False
  for fact in init[1:]:
    if fact[0] == "=":
        initial.append(f_expression.parse_assignment(fact))
    elif fact[0] == "oneof":
	oneofs.append(conditions.parse_condition(fact))
    elif fact[0] == "or":
	ors.append(conditions.parse_condition(fact))
    else:
        initial.append(conditions.Atom(fact[0], fact[1:]))
  if ors != []:
	for oneof in oneofs:
  	        tmp_state = []
		oneof.dump()
		for i in range(0,len(oneof.parts)):
			for j in range(0,len(belief_state)):
				state_set = belief_state[j]
				tmp_state_set = set()
				for x in state_set:
					tmp_state_set.add(x)
				tmp_state_set.add(oneof.parts[i])
				tmp_state.append(tmp_state_set)
		for i in range(0,len(belief_state)):
			belief_state.pop()

		'''print "belief after cartesian product"'''
		for x in tmp_state:
			'''print "state"
			for atom in x:
				print atom'''
			belief_state.append(x)
		index_to_remove = []
		for i in range(0,len(belief_state)):
			remove = False
			tmp_state_set = belief_state[i]
			'''print "checking state"
			for atom in tmp_state_set:
				print atom'''
			for orclause in ors:
				'''print "using or"
				orclause.dump()'''
				skip = False
				for k in range(0,len(orclause.parts)):
					if isinstance(orclause.parts[k], conditions.Atom):
						skip = True
						break
				if skip == True:
					continue
				else:
					remove = True
					for k in range(0,len(orclause.parts)):
						if orclause.parts[k].negate() not in tmp_state_set:
							remove = False
							break
					if remove == True:
						if tmp_state_set in belief_state:
							'''print "remove state"
							for atom in tmp_state_set:
								print atom
							print "because"
							orclause.dump()'''
							index_to_remove.insert(0,i)						
						break
			if remove == False:
				for oneof in oneofs:
					skip = False
					for part in oneof.parts:
						if part in  tmp_state_set:
							for other_part in oneof.parts:
								if other_part != part and other_part in  tmp_state_set:
									skip = True
									if tmp_state_set in belief_state:
										index_to_remove.insert(0,i)						
										break
							if skip == True:
								break
						if skip == True:
							break
					if skip == True:
						break
		for i in range(0, len(index_to_remove)):
			belief_state.pop(index_to_remove[i])
		print "size of belief state"
		print len(belief_state)
		print "we are here"
 				
		'''print "belief after removing invalid state"
		for x in belief_state:
			print "state"
			for atom in x:
				print atom'''
	index_to_remove = []
	for i in range(0,len(belief_state)):
		tmp_state_set = belief_state[i]
		for orclause in ors:
			remove = False
			stop = False
			for k in range(0,len(orclause.parts)):
				if isinstance(orclause.parts[k], conditions.Atom):
					print orclause.parts[k]
					if orclause.parts[k] in tmp_state_set:
						stop = True
						break
				elif isinstance(orclause.parts[k], conditions.NegatedAtom):
					if orclause.parts[k].negate() not in tmp_state_set:
						stop = True
						break
			if stop == False:
				remove = True
			if remove == True:
				if tmp_state_set in belief_state:
					index_to_remove.insert(0,i)						
					'''print "remove state"
					for atom in tmp_state_set:
						print atom
					print "because"
					orclause.dump()'''
					break
	for i in range(0, len(index_to_remove)):
		belief_state.pop(index_to_remove[i]) 
	print "size of belief state"
	print len(belief_state)
	'''for tmp_state_set in belief_state:
		print "state"
		for atom in tmp_state_set:
			print atom'''
	if len(belief_state) > 0:
		state_set = belief_state[0]
		for atom in state_set:
			initial.append(atom)
	'''oneofsize = 1
	for oneof in oneofs:
		oneofsize = oneofsize * len(oneof.parts)
	belief_state = {}
	exclude_list = []
	for n in range(0,oneofsize):
		for i in range(0,len(oneofs)):
			bucketsize = 1
			for j in range(i+1,len(oneofs)):
				bucketsize = bucketsize * len(oneofs[j].parts)
			index_i = (n / bucketsize) % len(oneofs[i].parts)
			oneof_valuename = oneofs[i].print_atom(index_i)
			oneof_valuename_str = str(oneof_valuename)
			skip_loop = false
			for var_no, var_key in enumerate(translation_key):
				for value, value_name in enumerate(var_key):
		    			if oneof_valuename_str.find(value_name) != -1: 
						if var_no in belief_state:
							if belief_state[var_no] = value:
								
								
							
						print >> belief_file, "var%d \n" % var_no,
						print >> belief_file, "%d \n" % value,
		print >> belief_file, "END_BELIEF"'''			
	
  else:
	for oneof in oneofs: 
		atoms = set()
		atoms = oneof.get_atoms()
		for atom in atoms:
			atom.dump(),
			initial.append(atom)

  yield initial
  yield oneofs
  yield ors
  yield belief_state

  goal = iterator.next()
  assert goal[0] == ":goal" and len(goal) == 2
  yield conditions.parse_condition(goal[1])
  originalgoal = conditions.parse_condition(goal[1])
  yield originalgoal
  use_metric = False
  for entry in iterator:
    if entry[0] == ":metric":
      if entry[1]=="minimize" and entry[2][0] == "total-cost":
        use_metric = True
      else:
        assert False, "Unknown metric."  
  yield use_metric
          
  for entry in iterator:
    assert False, entry
コード例 #28
0
ファイル: axioms.py プロジェクト: aldukeman/fdss
 def parse(alist):
     assert len(alist) == 3
     assert alist[0] == ":derived"
     predicate = predicates.Predicate.parse(alist[1])
     condition = conditions.parse_condition(alist[2])
     return Axiom(predicate.name, predicate.arguments, condition)