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()
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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()
Ejemplo n.º 6
0
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()