Exemplo n.º 1
0
def __get_ispl_poss(action_tuple_list, model, fluent_tuple_list):
	#feature_list = Util.get
	#context_operator.get
	ispl_fluent_list = ['%s%s'%(f,'_'.join(para_list)) for f, para_list, sort in fluent_tuple_list]
	fluent_list = ['%s(%s)'%(f,','.join(para_list)) for f, para_list, sort in fluent_tuple_list]
	ispl_action_list = ['%s%s'%(a,'_'.join(para_list)) for a, para_list in action_tuple_list]
	action_list = ['%s(%s)'%(a,','.join(para_list)) for a, para_list in action_tuple_list]

	pred_fluent_list = ['%s(%s)'%(f,','.join(para_list)) for f, para_list, sort in fluent_tuple_list \
				if f in context_operator.get_predicates()]

	action_poss_list = [ atomic_regress.poss_or_ssa(action) for action in action_list ]
	#print '\n    '.join(action_poss_list)
	action_poss_list = [ Formula.transform_entailment(poss) for poss in action_poss_list ]
	action_poss_list = [ ____add_env(poss, fluent_list) for poss in action_poss_list ]
	action_poss_list = [ Formula.grounding(poss, model) for poss in action_poss_list ]

	action_poss_list = [ ____ispl_simplify(poss, 'state') for poss in action_poss_list]
	action_poss_list = [ ____to_ispl_preds(poss, pred_fluent_list) for poss in action_poss_list ]
	action_poss_list = [ ____to_ispl_vars(poss, fluent_list, ispl_fluent_list) for poss in action_poss_list]
	action_poss_list = [ ____to_ispl_logic(poss) for poss in action_poss_list]
	action_poss_list = [ "%s:{%s};"%(poss, ispl_action_list[e]) for e, poss in enumerate(action_poss_list) if poss !='false']
	
	#return '\n    '.join(action_poss_list)
	return action_poss_list
Exemplo n.º 2
0
def __get_ispl_formula(formula, model, fluent_tuple_list):
	#print formula
	ispl_fluent_list = ['%s%s'%(f,'_'.join(para_list)) for f, para_list, sort in fluent_tuple_list]
	fluent_list = ['%s(%s)'%(f,','.join(para_list)) for f, para_list, sort in fluent_tuple_list]
	pred_fluent_list = ['%s(%s)'%(f,','.join(para_list)) for f, para_list, sort in fluent_tuple_list \
			if f in context_operator.get_predicates()]


	formula = Formula.transform_entailment(formula)
	formula = ____add_env(formula, fluent_list)
	#print formula
	formula = Formula.grounding(formula, model)
	#print formula
	formula = ____ispl_simplify(formula, 'Environment.Action')
	#print formula
	formula = ____to_ispl_preds(formula, pred_fluent_list)
	#print formula
	#formula = ____add_env(formula, fluent_list)
	#print formula
	formula = Util.endecode_string(formula, fluent_list, ispl_fluent_list)
	#print formula
	formula = ____to_ispl_logic(formula)
	#print formula
	#print 
	return formula
Exemplo n.º 3
0
def progress(action_str, model):     # here action is a ground term
	
	universe, assignment, default_value = model
	fluent_sorts = context_operator.get_functions_sorts()
	fluents = context_operator.get_fluents()
	predicates = context_operator.get_predicates()
	predicate_sorts = context_operator.get_predicate_sorts()

	fun_fluents_sorts = [(fluent, sorts) for fluent, sorts in fluent_sorts.iteritems() if fluent not in predicates and fluent in fluents]
	pred_fluents_sorts = [(fluent, sorts) for fluent, sorts in predicate_sorts.iteritems()]

	#print fluents, fun_fluents_sorts, pred_fluents_sorts

	new_model_list = list()
	#model_elem_set = set(assignment.keys())
	for fluent_name, sorts in fun_fluents_sorts:
		#print sorts
		consts = [universe[sort] for e, sort in enumerate(sorts) ]
		f_fluent_tuples = [(fluent_name+"("+",".join(list(elem)[0:len(elem)-1]) + ")", elem[len(elem)-1]) for elem in list(itertools.product(*consts))]
		f_fluents = [ '%s=%s'%(fun,value) for fun, value in f_fluent_tuples]
		#add_list.extend([fun for fun,value in f_fluent_tuples if fun not in model_elem_set])
		new_model_list.extend([f_fluent for f_fluent in f_fluents if __model_ssa(f_fluent, action_str,  model)])


	for fluent_name, sorts in pred_fluents_sorts:
		#print fluent_name, sorts
		consts = [universe[sort] for e, sort in enumerate(sorts) ]
		#print consts
		p_fluents = [fluent_name+"("+",".join(list(elem)[0:len(elem)]) + ")" for elem in list(itertools.product(*consts))]
		new_model_list.extend([p_fluent+"="+str(__model_ssa(p_fluent, action_str, model)) for p_fluent in p_fluents  ])

	new_assignment = { fluent.split("=")[0]: fluent.split("=")[1] for fluent in new_model_list}

	return (universe, new_assignment, default_value)
Exemplo n.º 4
0
def __get_ispl_update(fluent_tuple_list, p1_action_tuple_list, p2_action_tuple_list, model):

	universe, assignment, default_value = model
	ispl_fluent_list = ['%s%s'%(f,'_'.join(para_list)) for f, para_list, sort in fluent_tuple_list]
	fluent_list = ['%s(%s)'%(f,','.join(para_list)) for f, para_list, sort in fluent_tuple_list]

	predicates = context_operator.get_predicates()
	fun_fluent_list = [('%s(%s)'%(f,','.join(para_list)), sort) for f, para_list, sort in fluent_tuple_list if f not in predicates]
	pred_fluent_list = ['%s(%s)'%(f,','.join(para_list)) for f, para_list, sort in fluent_tuple_list if f in predicates and f!='turn']
	#print fun_fluent_list
	#print pred_fluent_list
	fluent_value_list = sum([list(itertools.product([fluent],universe[sort])) \
		for fluent, sort in fun_fluent_list] ,[])
	fluent_value_list = ['%s=%s'%(fluent,value) for fluent, value in fluent_value_list]
	fluent_value_list.extend(pred_fluent_list)


	ispl_p1_action_list = ['Player1.Action = %s%s'%(a,'_'.join(para_list)) for a, para_list in p1_action_tuple_list]
	p1_action_list = ['%s(%s)'%(a,','.join(para_list)) for a, para_list in p1_action_tuple_list]
	ispl_p2_action_list = ['Player2.Action = %s%s'%(a,'_'.join(para_list)) for a, para_list in p2_action_tuple_list]
	p2_action_list = ['%s(%s)'%(a,','.join(para_list)) for a, para_list in p2_action_tuple_list]

	action_pair_list = [(a,b) for a, b in zip(p1_action_list+p2_action_list, ispl_p1_action_list+ispl_p2_action_list)]
	update_pair_list = [ (a,b,c) for a, (b,c) in itertools.product(fluent_value_list, action_pair_list)]
	update_pair_list = [ (fluent, atomic_regress.poss_or_ssa(action, fluent), ispl_action) \
						for fluent, action, ispl_action in update_pair_list]
	# transform (=>) to (not or)
	update_pair_list = [ (a, Formula.transform_entailment(b), c) for a, b, c in update_pair_list]
	update_pair_list = [ (a, Formula.grounding(b,model), c) for a, b, c in update_pair_list]
	update_pair_list = [ (a, ____ispl_simplify(b,'Player1.Action'), c) for a, b, c in update_pair_list]
	#update_pair_list = [ (a, b, c) for a, b, c in update_pair_list if b is not None]

	pred_fluent_list = ['%s(%s)'%(f,','.join(para_list)) for f, para_list, sort in fluent_tuple_list if f in predicates]
	# add =true to each predicates
	update_pair_list = [ (fluent, ____to_ispl_preds(update, pred_fluent_list), ispl_action) for fluent, update, ispl_action in  update_pair_list]

	fun_update_pair_list = [ (fluent, update, ispl_action) for fluent, update, ispl_action in  update_pair_list if fluent not in pred_fluent_list and update !='false'] 
	pred_update_pair_list = [ (fluent, update, ispl_action) for fluent, update, ispl_action in  update_pair_list if fluent in pred_fluent_list] 

	update_list = ['%s if (%s) and %s;'%(fluent, update, ispl_action) for fluent, update, ispl_action in  fun_update_pair_list ]
	update_list += ['%s=true if (%s) and %s;'%(fluent, update, ispl_action) for fluent, update, ispl_action in  pred_update_pair_list if update !='false' ]
	update_list += ['%s=false if !(%s) and %s;'%(fluent, update, ispl_action) for fluent, update, ispl_action in  pred_update_pair_list if update !='false' ]
	update_list += ['%s=false if %s;'%(fluent, ispl_action) for fluent, update, ispl_action in  pred_update_pair_list if update =='false' ]
	update_list += turn_list
	#update_list = [____add_env(formula, fluent_list) for formula in update_list]
	update_list = [ Util.endecode_string(formula, fluent_list, ispl_fluent_list) for formula in update_list]
	update_list = [ ____to_ispl_logic(formula) for formula in update_list]

	#return '\n    '.join(update_list)
	return update_list
Exemplo n.º 5
0
def find_var(pred):
	var_list =[ var for var in var_pattern.findall(pred) if var not in context_operator.get_predicates() ]
	return (var_list, pred)
Exemplo n.º 6
0
def genPreds(constraints=dict()):

    fluent_sorts = context_operator.get_functions_sorts()
    sort_consts = context_operator.get_sort_symbols_dict()
    functions = list(
        set(context_operator.get_fluents()) -
        set(context_operator.get_predicates()))
    predicates = context_operator.get_predicates()

    fun_parasorts_list = [(fluent, sorts[0:len(sorts) - 1])
                          for fluent, sorts in fluent_sorts.iteritems()
                          if fluent in functions]
    fun_valuesort_list = [(fluent, sorts[len(sorts) - 1])
                          for fluent, sorts in fluent_sorts.iteritems()
                          if fluent in functions]
    pred_parasorts_list = [(fluent, sorts[0:len(sorts) - 1])
                           for fluent, sorts in fluent_sorts.iteritems()
                           if fluent in predicates]
    sort_consts = {
        sort: consts + ['X' + str(e)]
        for e, (sort, consts) in enumerate(sort_consts.iteritems())
    }

    # generate terms like x+1 from template "@var @fun @const", where the first is variable,  the second is math function
    # the third is constant
    math_terms = __get_math_term_from_template('Int', math_functions,
                                               sort_consts, "@var @fun @const")
    # generate terms like x+x
    math_terms += __get_math_term_from_template('Int', math_functions,
                                                sort_consts, "@var @fun @var")
    # generate terms like cell(x,x)
    fluent_terms = __gen_fluent_term(fun_parasorts_list, sort_consts)
    #fluent_terms = __fluentTerm_filter_via_constraints(fluent_terms, constraints)

    # generate predicates like x>1, x>=1, x=1
    math_var_predicates = __gen_pred_from_template('Int', [], ['>', '>=', '='],
                                                   sort_consts,
                                                   "@var @rel @const")
    # generate predicates like x>x, x>=1, x=1
    math_var_predicates += __gen_pred_from_template('Int', [],
                                                    ['>', '>=', '='],
                                                    sort_consts,
                                                    "@var @rel @var")
    # generate predicates like x%2=1
    math_var_predicates += __gen_pred_from_template('Int', math_terms, ['='],
                                                    sort_consts,
                                                    "@fun @rel @const")
    # generate predicates like x+1 = x
    math_var_predicates += __gen_pred_from_template('Int', math_terms, ['='],
                                                    sort_consts,
                                                    "@fun @rel @var")

    # generate fluent predicates like turn(p1)
    fluent_predicates = __gen_fluent_pred(pred_parasorts_list, sort_consts)

    # generate fluent predicates by combining fluent terms with constants or variables under certain relation (e.g.,=,>=)
    const_funs = [
        fun + "()" for fun in context_operator.get_zero_fluents()
        if fun in context_operator.get_fluents() if fun not in predicates
    ]
    fluent_predicates += __gen_equal_pred_from_template(
        fun_valuesort_list, fluent_terms, sort_consts, "@fun @rel @const")
    fluent_predicates += __gen_equal_pred_from_template(
        fun_valuesort_list, fluent_terms, sort_consts, "@fun @rel @var")
    fluent_predicates += __gen_pred_from_template('Int', const_funs,
                                                  ['>=', '>'], sort_consts,
                                                  "@fun @rel @var")

    #print __gen_var_rel_pred(fun_valuesort_list, fluent_terms, ['='], "@left @rel @right")
    #print __gen_var_rel_pred(fun_valuesort_list, fluent_terms, ['>=', '>'], "@left @rel @right", ['Int'])
    #exit(0)

    #fluent_predicates += __two_var_limited(__del_zero(__gen_var_rel_pred(fun_valuesort_list, fluent_terms, ['='], "@left @rel @right")))
    ##print __two_var_limited(__del_zero(__gen_var_rel_pred(fun_valuesort_list, fluent_terms, ['='], "@left @rel @right")))
    #print '4',fluent_predicates
    #fluent_predicates += __two_var_limited(__del_zero(__gen_var_rel_pred(fun_valuesort_list, fluent_terms, ['>=', '>'], "@left @rel @right", ['Int'])))
    ##print  __gen_var_rel_pred(fun_valuesort_list, fluent_terms, ['>=', '>'], "@left @rel @right", ['Int'])
    #print '5',fluent_predicates

    math_var_predicates = [
        util_pred.find_var(pred) for pred in math_var_predicates
    ]
    fluent_predicates = [
        util_pred.find_var(pred) for pred in fluent_predicates
    ]
    #print '6',fluent_predicates
    math_var_predicates = [(var_list,
                            util_pred.find_var_sort(var_list,
                                                    sort_consts), pred)
                           for var_list, pred in math_var_predicates]
    fluent_predicates = [(var_list,
                          util_pred.find_var_sort(var_list, sort_consts), pred)
                         for var_list, pred in fluent_predicates]
    #print '7',fluent_predicates

    math_var_predicates = sum(
        [util_pred.devars(pred) for pred in math_var_predicates], [])
    fluent_predicates = sum(
        [util_pred.devars(pred) for pred in fluent_predicates], [])

    math_var_predicates += [(var_list, sorts, "! " + body)
                            for (var_list, sorts, body) in math_var_predicates]
    fluent_predicates += [(var_list, sorts, "! " + body)
                          for (var_list, sorts, body) in fluent_predicates]

    return math_var_predicates, fluent_predicates