Exemple #1
0
    def parse(alist):
        iterator = iter(alist)
        assert iterator.next() == ":durative-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)
            duration_tag = iterator.next()
        else:
            parameters = []
            duration_tag = parameters_tag_opt
        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 ("<=", ">=", "=")
            assert len(item) == 3
            assert item[1] == "?duration"
            op = item[0]
            value = f_expression.parse_expression(item[2])
            duration += [(op, value)]
        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, (duration_start, duration_end),
                              condition, effect)
Exemple #2
0
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)
Exemple #3
0
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)
Exemple #4
0
    def parse(alist):
        iterator = iter(alist)
        assert iterator.next() == ":durative-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)
            duration_tag = iterator.next()
        else:
            parameters = []
            duration_tag = parameters_tag_opt
        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 ("<=",">=","=")
            assert len(item) == 3
            assert item[1] == "?duration"
            op = item[0]
            value = f_expression.parse_expression(item[2])
            duration += [(op,value)]
        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, (duration_start,duration_end), condition, effect)
    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)
def schedule_timed_initials(predicates_, actions_, init_, goal_, timed_initials_):
	# find all predicates and fuctions and build value timeline
	predicate_values = {} # {predicate: [ti1=TimedInitial(), ti2, ...]}
	for ti in timed_initials_:
		if ti.predicate not in predicate_values:
			predicate_values[ti.predicate] = [ti.create_initial_value(init_)]
		predicate_values[ti.predicate].append(ti)

	# add init
	initial_state_predicate = predicates.Predicate('initial_state', [])
	predicates_.append(initial_state_predicate)

	init_.append(conditions.Atom('initial_state', []))
	disable_initial_state = '\n      (at start (not (initial_state)))'
	
	# merge timed initials occuring at the same time into one conjuction
	merged_initials = {} # {time: [ti1=TimedInitial, ti2, ...]}
	for ti in timed_initials_:
		if not ti.time in merged_initials:
			merged_initials[ti.time] = [ti]
		else:
			merged_initials[ti.time].append(ti)
	
	goal_list = []
	ti_actions = []
	counter = itertools.count()
	for tis in sorted(merged_initials.values(), key=lambda tis: tis[0].time):
		name = 'timed_initial_{}__'.format(next(counter))
		# add predicates
		scheduled_predicate = predicates.Predicate(name+'-scheduled', [])
		predicates_.append(scheduled_predicate)
		# add goal
		goal_list.append(conditions.Atom(scheduled_predicate.name, []))

		# add action
		facts = []
		previous_facts = []
		for ti in tis:
			facts.append('      (at end {})'.format(ti.to_effect()))
			value_timeline = predicate_values[ti.predicate]
			#print map(str, value_timeline)
			previous_value = max((ti2 for ti2 in value_timeline if ti2.time < ti.time), key=lambda ti2: ti2.time)
			#print previous_value.time, previous_value.to_effect()
			previous_facts.append('      (at end {})'.format(previous_value.to_effect()))
		action_string = '''
  (:durative-action {name}scheduled
    :parameters ()
    :duration (= ?duration {time})
    :condition (and
      (at start (initial_state))
{previous_facts}
    )
    :effect (and{disable_initial_state}
      (at end ({predicate}))
{facts}
    )
  )'''.format(name=name, time=ti.time, predicate=scheduled_predicate.name,
			disable_initial_state=disable_initial_state, facts='\n'.join(facts), previous_facts='\n'.join(previous_facts))
		print(action_string)
		#assert 0
		action = actions.DurativeAction.parse(parser.parse_nested_list([action_string]))
		ti_actions.append(action)
		disable_initial_state = ''
	# add goals
	goal_.parts += tuple(goal_list)
	#goal_.dump()
	# add negated precontition to all other actions
	[at_start, overall, at_end] = conditions.parse_durative_condition(parser.parse_nested_list(['(at start (not (initial_state)))']))
	for da in actions_:
		da.condition[0].parts += (at_start, )
	actions_.extend(ti_actions)
Exemple #7
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)