def get_seed_rule(tried_seed_rules, kernel_set, gl): import utils, asp, functs, excps seed_model, seed_rule, found_seed = None, None, False str_tried_seed_rules = [r.as_string for r in tried_seed_rules] for c in kernel_set: utils.analyze_use_try([c], [], incremental_search=True) (_, pos, negs, optimization_score) = asp.ind(kernel_generalization=True, incremental_search=True) (all_solutions, _, _) = asp.ind(find_all_optimal=True, incremental_search=True, opt=optimization_score) for solution in all_solutions: if len(solution) > 1: (use_2, _) = functs.split_use_2_3(solution) (ok, use_head_body_map) = functs.head_body_use_atoms_filter(use_2) if ok: new = utils.form_new_clauses( use_head_body_map, incremental_kernel_search=True) if not new[0].as_string in str_tried_seed_rules: seed_model = use_2 seed_rule = new[0] found_seed = True break else: msg = 'Found a solution use(i,j) atom with no corresponding use(i,0) atom' raise excps.Use_2_HeadNotAbducedException(msg, gl.logger) if found_seed: break return (seed_model, seed_rule, pos, negs, optimization_score)
def generate_all_seed_rules(kernel_set, gl): import utils, asp, functs, excps generated_seeds, str_generated_seeds = [], [] for c in kernel_set: for seed in generated_seeds: utils.analyze_use_try([c], [], incremental_search=True, preserve=generated_seeds) (_, pos, negs, optimization_score) = asp.ind(kernel_generalization=True, incremental_search=True) (all_solutions, _, _) = asp.ind(find_all_optimal=True, incremental_search=True, opt=optimization_score) for solution in all_solutions: if len(solution) > 1: (use_2, _) = functs.split_use_2_3(solution) (ok, use_head_body_map ) = functs.head_body_use_atoms_filter(use_2) if ok: new = utils.form_new_clauses( use_head_body_map, incremental_kernel_search=True) if not new[0].as_string in str_generated_seeds: generated_seeds.append(new[0]) str_generated_seeds.append(new[0].as_string) else: msg = 'Found a solution use(i,j) atom with no corresponding use(i,0) atom' raise excps.Use_2_HeadNotAbducedException( msg, gl.logger) print('seeds:') utils.see(generated_seeds) #return (seed_model,seed_rule,pos,negs,optimization_score) return generated_seeds
def generate_all_seed_rules(kernel_set,gl): import utils,asp,functs,excps generated_seeds,str_generated_seeds = [],[] for c in kernel_set: for seed in generated_seeds: utils.analyze_use_try([c],[],incremental_search=True,preserve=generated_seeds) (_,pos,negs,optimization_score) = asp.ind(kernel_generalization=True, incremental_search=True) (all_solutions,_,_) = asp.ind(find_all_optimal=True, incremental_search=True, opt=optimization_score) for solution in all_solutions: if len(solution) > 1: (use_2,_) = functs.split_use_2_3(solution) (ok,use_head_body_map) = functs.head_body_use_atoms_filter(use_2) if ok : new = utils.form_new_clauses(use_head_body_map,incremental_kernel_search=True) if not new[0].as_string in str_generated_seeds: generated_seeds.append(new[0]) str_generated_seeds.append(new[0].as_string) else: msg = 'Found a solution use(i,j) atom with no corresponding use(i,0) atom' raise excps.Use_2_HeadNotAbducedException(msg,gl.logger) print('seeds:') utils.see(generated_seeds) #return (seed_model,seed_rule,pos,negs,optimization_score) return generated_seeds
def get_seed_rule(tried_seed_rules,kernel_set,gl): import utils,asp,functs,excps seed_model,seed_rule,found_seed = None,None,False str_tried_seed_rules = [r.as_string for r in tried_seed_rules] for c in kernel_set: utils.analyze_use_try([c],[],incremental_search=True) (_,pos,negs,optimization_score) = asp.ind(kernel_generalization=True, incremental_search=True) (all_solutions,_,_) = asp.ind(find_all_optimal=True, incremental_search=True, opt=optimization_score) for solution in all_solutions: if len(solution) > 1: (use_2,_) = functs.split_use_2_3(solution) (ok,use_head_body_map) = functs.head_body_use_atoms_filter(use_2) if ok : new = utils.form_new_clauses(use_head_body_map,incremental_kernel_search=True) if not new[0].as_string in str_tried_seed_rules: seed_model = use_2 seed_rule = new[0] found_seed = True break else: msg = 'Found a solution use(i,j) atom with no corresponding use(i,0) atom' raise excps.Use_2_HeadNotAbducedException(msg,gl.logger) if found_seed: break return (seed_model,seed_rule,pos,negs,optimization_score)
def incremental_solve(input_program,prior_theory_,**kwargs): analyze_use_try(input_program,prior_theory_) can_do_better = True # get an initial model: if not 'opt' in kwargs: (initial_model,optimization) = asp.ind(kernel_generalization=True, incremental_solve=True, init_model=True) else: (initial_model,optimization) = asp.ind(kernel_generalization=True, incremental_solve=True, improve_model=True,opt=kwargs['opt']) if initial_model != 'UNSATISFIABLE': (solution,use_2,use_3) = form_program(initial_model,incremental_solve=True) while can_do_better: print('optimization: %s'%(str(optimization))) optimization -= 1 if optimization == 28: stop = 'stop' analyze_use_try(solution,prior_theory_) (better_model,_) = asp.ind(kernel_generalization=True, incremental_solve=True, improve_model=True,opt=optimization) if better_model == 'UNSATISFIABLE': # There are two cases here: Either we reached the true optimal # theory size, or we took a path (with the initial "seed" model) that # cannot be further improved, while in fact better hypotheses # are possible. So first try to back-jump to the initial Kernel Set, # to see if we can find a better "seed" model of the current size. # If that fails, then we arrived at the optimum. return incremental_solve(input_program, prior_theory_, opt=optimization, last_model=solution, use2model=use_2, use3model=use_3) can_do_better = False else: (solution,use_2,use_3) = form_program(better_model,incremental_solve=True) else: solution = kwargs['last_model'] use_2 = kwargs['use2model'] use_3 = kwargs['use3model'] """ =================================================== Remember to update supports for the final solution =================================================== """ print('Found optimal') return (solution,use_2,use_3)
def search_kernel_by_subsets(prior_theory_): var_kernel = gl.current_var_kernel found_solution = False while not found_solution: for i in range(6,len(var_kernel)+1): i_subsets = itertools.combinations(var_kernel, i) subset_counter = 0 for subset_ in i_subsets: subset = list(subset_) print('Searching #%s kernel %s-subset:'%(str(subset_counter),str(i))) analyze_use_try(subset,prior_theory_) #analyze_use_try(gl.current_var_kernel,prior_theory_) out = asp.ind(kernel_generalization=True,incr_kernel_search=True) print(out) if out != 'UNSATISFIABLE': (use_2,use_3) = functs.split_use_2_3(out) (ok,use_head_body_map) = functs.head_body_use_atoms_filter(use_2) if ok : new = form_new_clauses(use_head_body_map) else: msg = 'Found a solution use(i,j) atom with no corresponding use(i,0) atom' raise excps.Use_2_HeadNotAbducedException(msg,gl.logger) found_solution = True subset_counter += 1 if found_solution: break if not found_solution: print('Failed to find a solution') sys.exit()
def incremental_search(**kwargs): import utils, core, asp, functs, excps gl = core.global_vals gl.use_dict = {} is_new_example = kwargs['is_new_example'] retained, new, refined = kwargs['retcl'], kwargs['newcl'], kwargs['refcl'] prior_theory = [x for y in [retained, new, refined] for x in y] target_score = gl.current_example_object.positive_count if is_new_example: utils.generate_kernel(**kwargs) # revise the clauses in the prior theory one by one first for c in prior_theory: # TODO pass var_kernel = gl.current_var_kernel # ===================================== generate_all_seed_rules(var_kernel, gl) # ===================================== found_solution = False #current_opt_score = float('Inf') #pos_covered = 0 #negs_covered = float('Inf') best_score = 0 tried_seed_rules = [] while not found_solution: partial_rules, tried_kernel_clauses = [], [] kernel_loop_counter = 0 for c in var_kernel: if 'terminatedAt' in c.as_string: stop = 'stop' tried_kernel_clauses.append(c) partial_rules.append(c) utils.analyze_use_try(partial_rules, [], incremental_search=True) if kernel_loop_counter == 0: (model, seed_rule, pos, negs, optimization_score) = get_seed_rule( tried_seed_rules, var_kernel, gl) tried_seed_rules.append(seed_rule) print('New seed rules: ') for r in tried_seed_rules: print(r.as_string) else: (model, pos, negs, optimization_score) = asp.ind(kernel_generalization=True, incremental_search=True) partial_rules.extend(tried_seed_rules) #(all_solutions,pos,negs) = asp.ind(find_all_optimal=True, # incremental_search=True, # opt=optimization_score) #new_score = pos - negs - average_size(all_solutions) new_score = pos - negs - average_size_(model) if new_score > best_score: best_score = new_score print('pos,negs,average_size,best_score: ', pos, negs, average_size_(model), best_score) #if pos >= pos_covered and negs <= negs_covered: if False: pos_covered, negs_covered = pos, negs else: # At this point, including any one solution from all_solutions reduces # the current score. Hence, we will use one such solution as a new # addition and search each tried kernel clause again, in order to retrieve # the previous score (or even improve it) #new_addition,added = [],[] """ for solution in all_solutions: if len(solution) > 1: (use_2,_) = functs.split_use_2_3(solution) (ok,use_head_body_map) = functs.head_body_use_atoms_filter(use_2) if ok : new = utils.form_new_clauses(use_head_body_map,incremental_kernel_search=True) for n in new: if not n.as_string in added: new_addition.append(n) added.append(n.as_string) """ (use_2, _) = functs.split_use_2_3(model) new = [] if len(use_2) > 1: (ok, use_head_body_map ) = functs.head_body_use_atoms_filter(use_2) if ok: new = utils.form_new_clauses( use_head_body_map, incremental_kernel_search=True) #new_addition.extend(new) old_use_dict, improvedit = gl.use_dict, False for c_ in tried_kernel_clauses: partial_rules_copy = [ rule for rule in partial_rules if rule != c ] partial_rules_copy.extend(new) partial_rules_copy.append(c_) utils.analyze_use_try(partial_rules_copy, [], incremental_search=True) (model_, pos, negs, optimization_score) = asp.ind( kernel_generalization=True, incremental_search=True) #(lastmodel,optimization_score) = asp.ind(kernel_generalization=True,incremental_search=True) #(pos,negs,_,_,_) = get_score(lastmodel,target_score) #(all_solutions_,pos,negs) = asp.ind(find_all_optimal=True, # incremental_search=True, # opt=optimization_score) #if pos >= pos_covered and negs <= negs_covered: new_score = pos - negs - average_size_(model_) if new_score > best_score: #all_solutions = all_solutions_ model = model_ best_score = new_score improvedit = True print('improved it!') #break if not improvedit: gl.use_dict = old_use_dict """ if not improvedit: gl.use_dict = old_use_dict if pos < pos_covered: pos_covered = pos if negs > negs_covered: negs_covered = negs """ #if optimization_score != current_opt_score: if True: new_partial_rules, added = [], [] #for solution in all_solutions: for solution in [model]: if len(solution) > 1: (use_2, use_3) = functs.split_use_2_3(solution) (ok, use_head_body_map ) = functs.head_body_use_atoms_filter(use_2) if ok: new = utils.form_new_clauses( use_head_body_map, incremental_kernel_search=True) for c in new: if not c.as_string in added: new_partial_rules.append(c) added.append(c.as_string) else: msg = 'Found a solution use(i,j) atom with no corresponding use(i,0) atom' raise excps.Use_2_HeadNotAbducedException( msg, gl.logger) partial_rules = new_partial_rules tried_kernel_clauses.extend(partial_rules) stop = 'stop' kernel_loop_counter += 1 if False: found_solution = True else: pass
def incremental_search(**kwargs): import utils,core,asp,functs,excps gl = core.global_vals gl.use_dict = {} is_new_example = kwargs['is_new_example'] retained,new,refined = kwargs['retcl'],kwargs['newcl'],kwargs['refcl'] prior_theory = [x for y in [retained,new,refined] for x in y] target_score = gl.current_example_object.positive_count if is_new_example: utils.generate_kernel(**kwargs) # revise the clauses in the prior theory one by one first for c in prior_theory: # TODO pass var_kernel = gl.current_var_kernel # ===================================== generate_all_seed_rules(var_kernel,gl) # ===================================== found_solution = False #current_opt_score = float('Inf') #pos_covered = 0 #negs_covered = float('Inf') best_score = 0 tried_seed_rules = [] while not found_solution: partial_rules,tried_kernel_clauses = [],[] kernel_loop_counter = 0 for c in var_kernel: if 'terminatedAt' in c.as_string: stop = 'stop' tried_kernel_clauses.append(c) partial_rules.append(c) utils.analyze_use_try(partial_rules,[],incremental_search=True) if kernel_loop_counter == 0: (model,seed_rule,pos,negs,optimization_score) = get_seed_rule(tried_seed_rules, var_kernel,gl) tried_seed_rules.append(seed_rule) print('New seed rules: ') for r in tried_seed_rules: print(r.as_string) else: (model,pos,negs,optimization_score) = asp.ind(kernel_generalization=True, incremental_search=True) partial_rules.extend(tried_seed_rules) #(all_solutions,pos,negs) = asp.ind(find_all_optimal=True, # incremental_search=True, # opt=optimization_score) #new_score = pos - negs - average_size(all_solutions) new_score = pos - negs - average_size_(model) if new_score > best_score: best_score = new_score print('pos,negs,average_size,best_score: ',pos,negs,average_size_(model),best_score) #if pos >= pos_covered and negs <= negs_covered: if False: pos_covered,negs_covered = pos,negs else: # At this point, including any one solution from all_solutions reduces # the current score. Hence, we will use one such solution as a new # addition and search each tried kernel clause again, in order to retrieve # the previous score (or even improve it) #new_addition,added = [],[] """ for solution in all_solutions: if len(solution) > 1: (use_2,_) = functs.split_use_2_3(solution) (ok,use_head_body_map) = functs.head_body_use_atoms_filter(use_2) if ok : new = utils.form_new_clauses(use_head_body_map,incremental_kernel_search=True) for n in new: if not n.as_string in added: new_addition.append(n) added.append(n.as_string) """ (use_2,_) = functs.split_use_2_3(model) new = [] if len(use_2) > 1: (ok,use_head_body_map) = functs.head_body_use_atoms_filter(use_2) if ok : new = utils.form_new_clauses(use_head_body_map,incremental_kernel_search=True) #new_addition.extend(new) old_use_dict,improvedit = gl.use_dict,False for c_ in tried_kernel_clauses: partial_rules_copy = [rule for rule in partial_rules if rule != c] partial_rules_copy.extend(new) partial_rules_copy.append(c_) utils.analyze_use_try(partial_rules_copy,[],incremental_search=True) (model_,pos,negs,optimization_score) = asp.ind(kernel_generalization=True, incremental_search=True) #(lastmodel,optimization_score) = asp.ind(kernel_generalization=True,incremental_search=True) #(pos,negs,_,_,_) = get_score(lastmodel,target_score) #(all_solutions_,pos,negs) = asp.ind(find_all_optimal=True, # incremental_search=True, # opt=optimization_score) #if pos >= pos_covered and negs <= negs_covered: new_score = pos - negs - average_size_(model_) if new_score > best_score: #all_solutions = all_solutions_ model = model_ best_score = new_score improvedit = True print('improved it!') #break if not improvedit: gl.use_dict = old_use_dict """ if not improvedit: gl.use_dict = old_use_dict if pos < pos_covered: pos_covered = pos if negs > negs_covered: negs_covered = negs """ #if optimization_score != current_opt_score: if True: new_partial_rules,added = [],[] #for solution in all_solutions: for solution in [model]: if len(solution) > 1: (use_2,use_3) = functs.split_use_2_3(solution) (ok,use_head_body_map) = functs.head_body_use_atoms_filter(use_2) if ok : new = utils.form_new_clauses(use_head_body_map,incremental_kernel_search=True) for c in new: if not c.as_string in added: new_partial_rules.append(c) added.append(c.as_string) else: msg = 'Found a solution use(i,j) atom with no corresponding use(i,0) atom' raise excps.Use_2_HeadNotAbducedException(msg,gl.logger) partial_rules = new_partial_rules tried_kernel_clauses.extend(partial_rules) stop = 'stop' kernel_loop_counter += 1 if False: found_solution = True else: pass
def incremental_kernel_search(**kwargs): gl.use_dict = {} is_new_example = kwargs['is_new_example'] retained,new,refined = kwargs['retcl'],kwargs['newcl'],kwargs['refcl'] prior_theory = [x for y in [retained,new,refined] for x in y] positive_count = gl.current_example_object.positive_count if is_new_example: generate_kernel(**kwargs) # revise the clauses in the prior theory one by one first for c in prior_theory: # TODO pass # generate new clauses from the Kernel Set, using iterative deepening # on the subsets of the Kernel Set, until a solution is found. #------------------------------------------------------------------------------ # TODO: This strategy can be modified to return approximate solution. This # may be useful in cases where large effort is required to obtain a correct # hypothesis, or in cases where a correct hypothesis does not exist (noise). # A straightforward strategy towards this is to keep the best hypothesis found # within a max_iterations bound. #------------------------------------------------------------------------------- var_kernel = gl.current_var_kernel found_solution = False already_found = [] for i in range(1,len(var_kernel)+1): i_subsets = itertools.combinations(var_kernel, i) for subset_ in i_subsets: subset = list(subset_) analyze_use_try(subset,[]) out = asp.ind(kernel_generalization=True) #============================================================================== # Test code. Try to add what you get from each subset to the bk in an effort # to further reduce solving time. (use_2,use_3) = functs.split_use_2_3(out) (ok,use_head_body_map) = functs.head_body_use_atoms_filter(use_2) if ok : new = form_new_clauses(use_head_body_map) if new != []: already_found.extend(new) n = '\n\n'.join(map(lambda x: x.as_string_with_var_types ,already_found)) write_to_file(gl.helper, n) else: msg = 'Found a solution use(i,j) atom with no corresponding use(i,0) atom' raise excps.Use_2_HeadNotAbducedException(msg,gl.logger) #=============================================================================== pos,negs,score = get_score(out) print(str(i)+'-subsets',pos,negs,score) if score == positive_count: #------------------ print('Found it') sys.exit() #------------------ found_solution = True (use_2,use_3) = functs.split_use_2_3(out) (ok,use_head_body_map) = functs.head_body_use_atoms_filter(use_2) if ok : new = form_new_clauses(use_head_body_map) else: msg = 'Found a solution use(i,j) atom with no corresponding use(i,0) atom' raise excps.Use_2_HeadNotAbducedException(msg,gl.logger) (retained,revisable) = functs.filter_retained_(use_3,gl.use_dict) update_support(retained) break # stop searchin i-level subsets if found_solution: break # stop searching subsets if not found_solution: print('unsat at kernel generalization') sys.exit() else: # TODO pass
def revise(**kwargs): hs,scs,cls = kwargs['heuristic_search'],kwargs['set_cover_search'],kwargs['clause_level_search'] special_search = scs or cls if not special_search: gl.use_dict = {} is_new_example = kwargs['is_new_example'] debug_mode = kwargs['debug'] if 'debug' in kwargs else False # keep this optional retained,new,refined = kwargs['retcl'],kwargs['newcl'],kwargs['refcl'] if debug_mode: import debug_utils prior_theory = debug_utils.py_load_from_file() else: prior_theory = [retained,new,refined] prior_theory_ = [x for y in prior_theory for x in y] if is_new_example: #retained.extend(refined) # that's a patch because due to a bug previously refined clauses are left out generate_kernel(**kwargs) if gl.runargs["kernel-set-only"]: # We only need to generate a kernel set sys.exit() var_kernel = gl.current_var_kernel if 'search_subsets' in kwargs: search_kernel_by_subsets(prior_theory_) elif 'incremental_solve' in kwargs and kwargs['incremental_solve']: (solution,use_2,use_3) = incremental_solve(var_kernel,prior_theory_) else: analyze_use_try(gl.current_var_kernel,prior_theory_) out = asp.ind(kernel_generalization=True) (use_2,use_3) = functs.split_use_2_3(out) (ok,use_head_body_map) = functs.head_body_use_atoms_filter(use_2) if ok : new = form_new_clauses(use_head_body_map) else: msg = 'Found a solution use(i,j) atom with no corresponding use(i,0) atom' raise excps.Use_2_HeadNotAbducedException(msg,gl.logger) (retained,revisable) = functs.filter_retained_(use_3,gl.use_dict) update_support(retained) else: # re-seeing past example #prior_theory = [retained,refined] # Only analyze the new clauses generated last. # Update This is messy...Analyze it all, there is no serious overhead #analyze_use_try([],new,preserve=[x for y in prior_theory for x in y]) analyze_use_try([],prior_theory_) out = asp.ind(recheck_hist_memory=True) (_,use_3) = functs.split_use_2_3(out) # no use/2 here (retained,revisable) = functs.filter_retained_(use_3,gl.use_dict) revisable_ = [] for (clause_index,clause) in revisable: #import debug_utils #debug_utils.check_on_previous() revcl = functs.form_revised_(clause_index,clause,gl.use_dict,use_3) revcl = structs.Clause(revcl,gl) revisable_.append((clause,revcl)) incorrects = [inx[0] for inx in revisable_] specialized = [] for (incorrect,one_solution) in revisable_ : # need to find optimal refinement optimal_ref = one_solution check_prior = [z for z in prior_theory_ if not z in incorrects] if search_more(initsol=one_solution,parent=incorrect,prior=check_prior): other_init_solutions = [x[1] for x in revisable_ if x[1] != one_solution] check_prior.extend(other_init_solutions) optimal_ref = get_optimal_refinement(initsol=one_solution, parent=incorrect, prior=check_prior) if isinstance(optimal_ref,list): # then it went through get_optimal_refinement because that returns a list specialized.extend(optimal_ref) else: if isinstance(optimal_ref,structs.Clause): # then it's the initial solution, no support update yet update_support((incorrect,optimal_ref))# this must be passed as a (parent,child) tuple specialized.append(optimal_ref) #d = dict(zip(incorrects,specialized)) if is_new_example: (n,r,s) = (new,retained,specialized) else: (n,r,s) = ([],retained,specialized) updateprior(n,r,s) return (n,r,s) else: if kwargs['set_cover_search']: return set_cover_search(**kwargs) elif kwargs['heuristic_search']: return heuristic_search(**kwargs) else: #return incremental_kernel_search.incremental_search(**kwargs) return incremental_kernel_search(**kwargs)