def get_smt_body(formula): #print '-----need to tans formula is:', formula formula = __logicSym_to_smtSym(formula) fluents = context_operator.get_fluents() fluent_sub = '|'.join([r"(?:(%s)\(([\w,\s]*)\))" % fun for fun in fluents]) #print fluents fluent_sub_pattern = re.compile(fluent_sub) temp_formula = "" while temp_formula != formula: temp_formula = formula #formula = fluent_sub_pattern.sub(__mrepl_fluent, formula) formula = Util.repeat_replace_inner_with_pattern( fluent_sub_pattern, __mrepl_fluent, formula) #print "repl_fluent---",formula #formula = Util.repeat_replace_inner_with_pattern(most_inner_pattern, __mrepl_no_inner_formula, formula) formula = most_inner_pattern.sub(__mrepl_no_inner_formula, formula) #print "inner_pattern---",formula formula = Util.repeat_replace_inner_with_pattern( quantifier_pattern, __mrepl_quntifier, formula) #formula = quantifier_pattern.sub(__mrepl_quntifier, formula) formula = re.sub(r'.*', __mrepl_no_inner_formula, formula) repl_list = context_operator.get_replace_list() repl_list.reverse() #print repl_list formula = Util.repeat_do_function(Util.sub_lambda_exp, repl_list, formula) #print formula context_operator.clear_replace_list() return formula
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)
def __generate_head(): head_str = "" sorts = list(context_operator.get_sorts().keys()) #print ["(declare-sort "+sort for sort in sorts] head_str += "&".join( ["(declare-sort " + sort + ")" for sort in sorts if sort != "Int"]) functions = context_operator.get_fluents() functions_sorts = [sort_system.get_function_sort(fun) for fun in functions] #print functions_sorts functions_sorts = [ "(" + " ".join(sorts[0:len(sorts) - 1]) + ") " + sorts[len(sorts) - 1] for sorts in functions_sorts ] sorts_consts = context_operator.get_sort_symbols_dict() #print functions, functions_sorts #print ["(declare-sort "+ sort + ")" for sort in sorts] head_str += "&".join([ "(declare-fun " + fun + functions_sorts[e] + ")" for e, fun in enumerate(functions) ]) head_str += "&".join([ "(declare-const " + const + " " + sort + ")" for sort in sorts_consts.keys() for const in sorts_consts[sort] if sort != "Int" ]) return head_str
def __get_fluents(universe): fun_sorts_dict = context_operator.get_functions_sorts() fun_fluent_list = [f for f in context_operator.get_fluents() if f ] fun_sorts_list = [ (f, fun_sorts_dict[f]) for f in fun_fluent_list] fluent_tuple_list = list() for f, sort_list in fun_sorts_list: paras_list = itertools.product(*[ universe[s] for s in sort_list[0: len(sort_list)-1] ]) fluent_tuple_list.extend([(f,paras, sort_list[-1]) for paras in paras_list ]) return fluent_tuple_list
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
def parser(filename): with open('./input/%s'%filename,"read") as sc_file, open('./input/default_axioms.sc',"read") as basic_file,\ open('./temp/game_rule_info','write') as sc_temp: full_txt = " ".join(sc_file.readlines()).replace("\n", " ").replace( "\t", " ") full_txt += " ".join(basic_file.readlines()).replace( "\n", " ").replace("\t", " ") + ";" full_txt = full_txt.replace(' and ', '&').replace(' or ', '|').replace(' ', "") #logger.debug(full_txt) rule_list = pattern.rule3.findall(full_txt) + pattern.rule4.findall( full_txt) + pattern.rule5.findall(full_txt) sc_temp.writelines('\n'.join([str(elem) for elem in rule_list])) rule_list = __pre_parse(rule_list) sc_temp.writelines('\n') sc_temp.writelines('\n\n') sc_temp.writelines('\n'.join([str(elem) for elem in rule_list])) for k, g in groupby(sorted(rule_list, key=itemgetter(0)), key=itemgetter(0)): m_group = list(g) #print "-------",k, m_group eval("apply(__parse_" + k + "," + str(m_group) + ")") predicates = __generate_predicates( context_operator.get_fluents(), context_operator.get_symbol_sorts_dict().keys()) fun_fluents = [ fluent for fluent in context_operator.get_fluents() if fluent not in predicates ] #print context_operator.get_feature() sc_temp.writelines('\n\n') sc_temp.writelines('feature pattern for regression:\n') sc_temp.writelines('\n'.join( [str(elem) for elem in context_operator.get_feature()])) #exit(0) sc_temp.writelines('\n') sc_temp.writelines('\n actions:' + str(context_operator.get_actions())) sc_temp.writelines('\n fluents:' + str(context_operator.get_fluents())) sc_temp.writelines('\n 0arity-fluents:' + str(context_operator.get_zero_fluents())) sc_temp.writelines('\n predicates:' + str(predicates)) sc_temp.writelines('\n functional fluents:' + str(fun_fluents)) #logger.debug("\n actions :%s \n fluents %s"%(context_operator.get_actions(), context_operator.get_fluents())) #print sort_system.get_function_sort('numStone') #print context_operator.get_sort_symbols_dict() sort_const = __get_sort_const_with_fluents( context_operator.get_sort_symbols_dict(), context_operator.get_fluents()) sort_const["Bool"] = ['True', 'False'] sort_funs = __get_funs_sorts(context_operator.get_fluents() + context_operator.get_actions()) sort_preds = __generate_predicate_sorts(predicates, sort_funs) sc_temp.writelines('\n') sc_temp.writelines('\n sort for constants:' + str(sort_const)) sc_temp.writelines('\n sort for functions:' + str(sort_funs)) sc_temp.writelines('\n sort for predicates:' + str(sort_preds)) context_operator.set_sort_symbols_dict(sort_const) context_operator.set_functions_sorts(sort_funs) context_operator.add_predicates(predicates) context_operator.add_predicate_sorts(sort_preds) context_operator.add_nregx_function_patterns( __generate_nregx_function_patterns(fun_fluents)) #print z3_header context_operator.set_function_regress_lambda( __generate_fun_regress_lambda(fun_fluents, sort_funs)) context_operator.set_predicate_regress_lambda( __generate_pred_regress_lambda(predicates, sort_preds)) #exit(0) domain_name = filename.split('.')[0] __load_state_constaints(domain_name)
def interpret_model(results, max_value=99999): #print results #context_operator.set_counterexample_result(results) lambda_funs = util_z3_model.get_fun(results) #print lambda_funs #print results # a dict maps constants to SMT constants const_dict = util_z3_model.get_const(results) #print const_dict #print const_dict #print '----------',context_operator.get_sort_symbols_dict() universe = copy.deepcopy(context_operator.get_sort_symbols_dict()) min_num = min([int(e) for e in universe['Int']]) max_num = max([int(e) for e in universe['Int']]) universe['Int'] = [str(e) for e in range(min_num, max_num + 1)] #universe['Int'] = list(set(universe['Int'] +__generate_num_universe(str(lambda_funs)+str(const_dict)))) #print universe['Int'] # get fluent names from the interpretation fluents = __get_model_fluents(lambda_funs, const_dict, context_operator.get_fluents()) #Predicates is also included because they can be seen as two-value (true/false) functions. scope = dict() for fun in lambda_funs: exec(fun, scope) fluent_sorts = context_operator.get_functions_sorts() #constraints = __parse_constraints(domain_constraints, const_dict)?? flag = True value_constraints = list() while flag is True and value_constraints == list(): flag = False #elem_list = __get_model_elements(fluents, context_operator.get_fluent_constraint()) #generate parameters for fluents elem_list = __get_model_elements(fluents, add_universe=universe) const_symbols = sum(universe.values(), []) # set non-constant fluent, lile xlen, numStone elem_value_list = [(fun, tuple(), value) for fun, value in const_dict.items() if fun in fluents] # transform SMT true/false to 'True'/'False' elem_value_list = __trans_true_false(elem_value_list) elem_value_list += [(fun, paras, apply(eval(fun, scope), __name_relpace(paras, const_dict))) for fun, paras in elem_list if fun not in const_dict.keys()] # transform SMT constants to constant symbols anti_const_dict = { value: key for key, value in const_dict.items() if key not in fluents and key in const_symbols and not isinstance(value, int) and not value.isdigit() } elem_value_list = [ (fun, paras, anti_const_dict[value]) if value in anti_const_dict.keys() else (fun, paras, value) for (fun, paras, value) in elem_value_list ] ######################### for e, (fun, paras, value) in enumerate(elem_value_list): fun_sort = fluent_sorts[fun][-1] ## ??????? if str(value) not in universe[fun_sort]: flag = True if int(value) < 0: logger.info('ERROR: model has negative value \n %s' % results) exit(0) if fun_sort == 'Int' and int(value) > max_value: #print 'Hello' value_constraints.append("%s(%s)" % (fun, ','.join(paras))) elif fun_sort == 'Int': #print 'C' min_num = min([int(e) for e in universe['Int']]) universe[fun_sort] = [ str(e) for e in range(min_num, value + 1) ] else: #print 'D' universe[fun_sort].append(str(value)) if value_constraints != list(): return False, value_constraints else: model = { "%s(%s)" % (fun, ','.join(paras)): str(value) for fun, paras, value in elem_value_list } lack_fluents = [ fun for fun in context_operator.get_fluents() if fun not in fluents ] complete_part = __get_default_fluents(lack_fluents, fluent_sorts, universe) model.update(complete_part) """ get default value for fluents who have parameters of Int sort """ default_value = get_default_value(elem_list, scope, const_dict, anti_const_dict) """ """ return True, (universe, model, default_value)
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