def __init__(self, print_results=None, config={ "debug": False, "verbose_grounding_solving": True, "negation_counter": 1 }): self.config = config if print_results is not None: self.config['print_results'] = print_results self.asp_engine = ASPEngine(self.config)
def main(): config = {"debug":False} solver = Solver() engine = ASPEngine(config) problem_name = "sudoku" folder = "benchmark/" base_sketch = folder + problem_name + "_base_sketch.sasp" pos_generator = folder + problem_name + "_positive_examples_generator.asp" print(pos_generator) neg_generator = folder + problem_name + "_negative_examples_generator.asp" new_sketch_filename = folder + problem_name + "tmp_sketch_to_run.sasp" log_file = open("log_"+problem_name+"_time_vs_numberofexamples.csv","w") print("number_of_examples,time",file=log_file) print('running the experiment for', problem_name) for k in [1] + [x for x in range(5,505,5)]: text_to_append = [] print('learning for k =', 2*k) engine.run(pos_generator, number_of_solutions=k) examples = engine.get_set_of_atoms() for i,example in enumerate(examples): text_to_append.append(make_example_str("+",example)) engine.run(neg_generator, number_of_solutions=k) examples = engine.get_set_of_atoms() for i,example in enumerate(examples): text_to_append.append(make_example_str("-",example)) create_new_sketch(new_sketch_filename, base_sketch, text_to_append) start = time.time() solver.learn_ASP_constraints(new_sketch_filename,config) end = time.time() print(len(text_to_append),",",round(end-start,2),sep="",file=log_file) log_file.flush() log_file.close()
def main(): config = {"debug": False} engine = ASPEngine(config) problem_name = "latin_square" folder = "benchmark/" pos_generator_template = folder + problem_name + "_positive_examples_generator.asp" neg_generator_template = folder + problem_name + "_negative_examples_generator.asp" solver = Solver() pos_generator = folder + "pos_generator_latin_square_rewritten.sasp" neg_generator = folder + "neg_generator_latin_square_rewritten.sasp" rewrite_into_file(pos_generator_template, 3, pos_generator) rewrite_into_file(neg_generator_template, 3, neg_generator) engine.run(pos_generator, number_of_solutions=0) pos_examples = engine.get_set_of_atoms() engine.run(neg_generator, number_of_solutions=0) neg_examples = engine.get_set_of_atoms() # examples = list(map(lambda x: ("pos",x), pos_examples)) + pos = list(map(lambda x: ("pos", x), pos_examples)) neg = list(map(lambda x: ("neg", x), neg_examples)) max_examples = 12 for v in range(4, 0, -1): base_sketch = folder + "latin_square_sketch_3_by_3_vars_{v}.sasp".format( v=v) log_file = open( "current_test/log_number_of_solutions_latin_square_preferences_vars_{v}.csv" .format(v=v), "w") print( "iteration_index,number_of_examples,solutions,without_preferences", file=log_file) for n in range(1, 100): new_sketch_filename = folder + problem_name + "tmp_sketch_to_run.sasp" pos_sample = random.sample(pos, max_examples) neg_sample = random.sample(neg, max_examples) for k in [x for x in range(1, max_examples + 1, 1)]: sample = pos_sample[:k] + neg_sample[:k] text_to_append = [] for i, (sign, example) in enumerate(sample): if sign == "pos": text_to_append.append(make_example_str("+", example)) elif sign == "neg": text_to_append.append(make_example_str("-", example)) else: raise Exception('wtf') create_new_sketch(new_sketch_filename, base_sketch, text_to_append) sols, before_pref = solver.learn_ASP_constraints( new_sketch_filename, config) print(n, len(text_to_append), len(sols), before_pref, sep=",", file=log_file) log_file.flush()
def generate_sudoku_positive_negative_examples(k): config = {"debug": False} engine = ASPEngine(config) max_solution_number = 5000 problem_name = "sudoku" folder = "benchmark/" pos_generator = folder + problem_name + "_positive_examples_generator.asp" neg_generator = folder + problem_name + "_negative_examples_generator.asp" new_sketch_filename = folder + problem_name + "tmp_sketch_to_run.sasp" engine.run(pos_generator, number_of_solutions=max_solution_number) pos_examples = sample(engine.get_set_of_atoms(), k) engine.run(neg_generator, number_of_solutions=max_solution_number) neg_examples = sample(engine.get_set_of_atoms(), k) return pos_examples, neg_examples
def main(): config = {"debug": False} engine = ASPEngine(config) problem_name = "latin_square" folder = "benchmark/" base_sketch_template = folder + problem_name + "_base_sketch.sasp" pos_generator_template = folder + problem_name + "_positive_examples_generator.asp" neg_generator_template = folder + problem_name + "_negative_examples_generator.asp" base_sketch = folder + "base_sketch_latin_square_rewritten.sasp" pos_generator = folder + "pos_generator_latin_square_rewritten.sasp" neg_generator = folder + "neg_generator_latin_square_rewritten.sasp" for n in range(13, 15): rewrite_into_file(base_sketch_template, n, base_sketch) rewrite_into_file(pos_generator_template, n, pos_generator) rewrite_into_file(neg_generator_template, n, neg_generator) new_sketch_filename = folder + problem_name + "tmp_sketch_to_run.sasp" log_file = open( "log_" + problem_name + "_k_" + str(n) + "_time_vs_numberofexamples.csv", "w") print("number_of_examples,time", file=log_file) print('running the experiment for', problem_name, "with n=", n) for k in [1] + [x for x in range(5, 251, 5)]: text_to_append = [] print('learning for k =', 2 * k) engine.run(pos_generator, number_of_solutions=k) examples = engine.get_set_of_atoms() for i, example in enumerate(examples): text_to_append.append(make_example_str("+", example)) engine.run(neg_generator, number_of_solutions=k) examples = engine.get_set_of_atoms() for i, example in enumerate(examples): text_to_append.append(make_example_str("-", example)) create_new_sketch(new_sketch_filename, base_sketch, text_to_append) start = time.time() learn_ASP_constraints(new_sketch_filename, config) end = time.time() print(len(text_to_append), ",", round(end - start, 2), sep="", file=log_file) log_file.flush() log_file.close()
def run_experiment_and_record(filenames, n, solutions_log, time_log): config = {"debug": False} engine = ASPEngine(config) pos_generator = "benchmark/regular_queens_positive.asp" neg_generator = "benchmark/regular_queens_negative.asp" output_filename = "tmp_queens/tmp_script.asp" max_examples = 50 engine.run(pos_generator) pos_examples = sample(engine.get_set_of_atoms(), max_examples) engine.run(neg_generator, number_of_solutions=50000) neg_examples = sample(engine.get_set_of_atoms(), max_examples) for filename in filenames: print('running on', filename) for k in [x for x in range(1, 11) ] + [x for x in range(10, max_examples + 1, 5)]: text_to_append = [] print('learning for k =', 2 * k) for i, example in enumerate(pos_examples[:k]): text_to_append.append(make_example_str("+", example)) for i, example in enumerate(neg_examples[:k]): text_to_append.append(make_example_str("-", example)) create_new_sketch(output_filename, filename, text_to_append) start = time.time() programs = solver.learn_programs(output_filename) print(get_clean_name(filename), 2 * k, n, len(solutions), before, sep=",", file=solutions_log) end = time.time() print(get_clean_name(filename), 2 * k, n, round(end - start, 2), sep=",", file=time_log) solutions_log.flush() time_log.flush()
class Solver(): def __init__(self, print_results=None, config={ "debug": False, "verbose_grounding_solving": True, "negation_counter": 1 }): self.config = config if print_results is not None: self.config['print_results'] = print_results self.asp_engine = ASPEngine(self.config) def run_dull_grounder(self, filename, ground_output): asp_output_program_name = "tmp_asp_script.asp" program = Program(filename, config=self.config) generator = Generator(program, asp_output_program_name) generator.include_original_sketch() generator.generate_program() command = 'gringo {} > {}'.format(asp_output_program_name, ground_output) os.system(command) def run_dull_clasp(self, grounded_file): command = 'clasp {} 0 > tmp/solution'.format(grounded_file) os.system(command) def create_set_of_solutions(self, solutions): for solution_atoms in solutions: solution = Solution(solution_atoms) yield solution def create_new_asp_program(self, rules): program_str = "\n".join(map(lambda x: str(x), rules)) return program_str def learn_programs(self, script_name): print_results = self.config.get("print_results", True) solutions, _ = self.learn_ASP_constraints(script_name, print_results=print_results) solutions = self.create_set_of_solutions(solutions) for solution in solutions: self.config['negation_counter'] = 1 program = Program(script_name, config=self.config) new_program = self.create_new_asp_program( solution.apply_to_program(program)) yield new_program def learn_ASP_constraints(self, script_name, config=None, print_results=False): if self.config["debug"]: print("DEBUGGING MODE IS ON") asp_output_program_name = "tmp_asp_script.asp" program = Program(script_name, config=self.config) generator = Generator(program, asp_output_program_name) generator.include_original_sketch() generator.generate_program() self.asp_engine.run(asp_output_program_name) before_dominance_check = self.asp_engine.get_number_of_solutions() print("before dominance models:", before_dominance_check) # bp() self.asp_engine.remove_dominated(program) if print_results: self.asp_engine.print_result() solutions = self.asp_engine.get_set_of_atoms() print('after dominance models:', len(solutions)) if self.config.get("debug", False): print("DEBUGGING MODE: END OF THE OUTPUT") return solutions, before_dominance_check def read_input_script(self): parser = argparse.ArgumentParser(prog='sketcher') parser.add_argument('sketch', nargs=1, help='Sketched ASP program') args = parser.parse_args() script_name = args.sketch[0] return script_name def get_examples_with_indices(self, filename): with open(filename, "r") as sketch: data = sketch.read().splitlines() indices = [] for index, line in enumerate(data): if "[EXAMPLES]" in line: section_header_index = index if re.search("positive *: *", line) or re.search( "negative *: *", line): indices.append(index) if not section_header_index: raise Exception("no examples given?") return data, section_header_index, indices def construct_program_with_n_examples(self, program, indices, n): selected = random.sample(indices, n) new_program = program[:] for i in selected: new_program[i] = "%" + program[i] # comment out the example return "\n".join(new_program) def construct_program_without_last_n_examples(self, program, indices, n, shuffle=False): if not n: indices_to_cut = [] else: if shuffle: random.shuffle(indices) indices_to_cut = indices[-n:] new_program = [] for indx, line in enumerate(program): if indx not in indices_to_cut: new_program.append(line) return "\n".join(new_program) def classify(self, program, example): tmp_script = "tmp/classify_asp_script.asp" with open(tmp_script, "w") as tmp: pass #just cleaning with open(tmp_script, "a") as tmp: print(program, file=tmp) print(example, file=tmp) self.asp_engine.run(tmp_script) if self.asp_engine.get_number_of_solutions() != 0: return "positive" else: return "negative" def run_with_background_knowledge(self, program, background_knowledge): tmp_script = "tmp/precision_asp_script.asp" with open(tmp_script, "w") as tmp: pass #just cleaning with open(tmp_script, "a") as tmp: print(program, file=tmp) print(background_knowledge, file=tmp) self.asp_engine.run(tmp_script) number_of_solutions = self.asp_engine.get_number_of_solutions() return number_of_solutions