Esempio 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
Esempio 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
Esempio n. 3
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
Esempio n. 4
0
def sat_formula(model, formula):

    universe, assignment, default_value = model
    formula = __to_python_formula(formula)
    #print '1,---------',formula
    formula = Formula.transform_entailment(formula)
    #print '2,---------',formula
    ground_formula = Formula.grounding(formula, model)
    #print '3,--------',ground_formula
    logical_formula = __assignment(ground_formula, assignment)
    #print '4,--------model replace',logical_formula
    #print 'kkkk',context_operator.get_sort_symbols_dict()
    #logger.debug("Checking formula %s with model %s \n formula after grounding: %s \n after model_replace %s"%(formula,model,ground_formula,logical_formula))
    scope = __get_const_value(context_operator.get_sort_symbols_dict(),
                              context_operator.get_fluents(), assignment)
    flag = eval(logical_formula, scope)
    #logger.debug('sat?: \n%s'%flag)
    return flag