Esempio n. 1
0
def main(filename, sample_count):
    seed = time.time()
    random.seed(seed)

    target_formula = smt.read_smtlib(filename)

    variables = target_formula.get_free_variables()
    var_names = [str(v) for v in variables]
    var_types = {str(v): v.symbol_type() for v in variables}
    var_domains = {str(v): (0, 200) for v in variables}  # TODO This is a hack

    domain = problem.Domain(var_names, var_types, var_domains)
    name = basename(filename).split(".")[0]
    target_problem = problem.Problem(domain, target_formula, name)

    # compute_difference(domain, target_formula, target_formula)

    samples = generator.get_problem_samples(target_problem, sample_count, 1)

    initial_indices = random.sample(list(range(sample_count)), 20)
    learner = KCnfSmtLearner(3, 3, RandomViolationsStrategy(5))

    dir_name = "../output/{}".format(name)
    img_name = "{}_{}_{}".format(learner.name, sample_count, seed)
    # learner.add_observer(plotting.PlottingObserver(data_set.samples, dir_name, img_name, "r0", "r1"))
    with open("log.txt", "w") as f:
        learner.add_observer(inc_logging.LoggingObserver(f))

        print(parse.smt_to_nested(learner.learn(domain, samples, initial_indices)))
Esempio n. 2
0
def adapt_domain(target_problem, lb, ub):
    domain = target_problem.domain
    var_domains = {}
    for v in domain.variables:
        var_domains[v] = (lb, ub)
    adapted_domain = problem.Domain(domain.variables, domain.var_types, var_domains)
    return problem.Problem(adapted_domain, target_problem.theory, target_problem.name)
Esempio n. 3
0
def generate_domain(bool_count, real_count):
    variables = ["b{}".format(i) for i in range(bool_count)] + ["r{}".format(i) for i in range(real_count)]
    var_types = dict()
    var_domains = dict()
    for i, v in enumerate(variables):
        if i < bool_count:
            var_types[v] = smt.BOOL
        else:
            var_types[v] = smt.REAL
            var_domains[v] = (0, 1)

    return problem.Domain(variables, var_types, var_domains)
Esempio n. 4
0
def import_problem(name, filename):
    target_formula = smt.read_smtlib(filename)

    variables = target_formula.get_free_variables()
    var_names = [str(v) for v in variables]
    var_types = {str(v): v.symbol_type() for v in variables}
    var_domains = {str(v): (0, 200) for v in variables}  # TODO This is a hack

    domain = problem.Domain(var_names, var_types, var_domains)
    target_problem = problem.Problem(domain, target_formula, name)

    return target_problem
Esempio n. 5
0
def main(filename, sample_count, seed):
    random.seed(seed)

    target_formula = smt.read_smtlib(filename)

    variables = target_formula.get_free_variables()
    var_names = [str(v) for v in variables]
    var_types = {str(v): v.symbol_type() for v in variables}
    var_domains = {
        str(v): (0, 1)
        for v in variables
    }  #FIXME: This is a hack. Adjust depending on your benchmarks

    domain = problem.Domain(var_names, var_types, var_domains)
    name = basename(filename).split(".")[0]
    target_problem = problem.Problem(domain, target_formula, name)

    samples = generator.get_problem_samples(target_problem, sample_count, 1)
    test_samples = generator.get_problem_samples(target_problem, 1000, 1)

    kwargs = {"problem": target_problem, "data": samples}
    t1 = time.time()
    res1 = timeout(search_cnf, kwargs=kwargs, duration=600)
    t2 = time.time()
    res2 = timeout(search_clause, kwargs=kwargs, duration=600)
    t3 = time.time()
    res3 = timeout(search_lit, kwargs=kwargs, duration=600)
    t4 = time.time()

    dur1 = "TO" if res1 is None else t2 - t1
    dur2 = "TO" if res2 is None else t3 - t2
    dur3 = "TO" if res3 is None else t4 - t3

    formula1 = "None" if res1 is None else pretty_print(res1)
    formula2 = "None" if res2 is None else pretty_print(res2)
    formula3 = "None" if res3 is None else pretty_print(res3)

    #FIXME: this will show 1 for timeouts
    cl1 = formula1.count("&") + 1
    cl2 = formula2.count("&") + 1
    cl3 = formula3.count("&") + 1

    hl1 = formula1.count("<")
    hl2 = formula2.count("<")
    hl3 = formula3.count("<")

    strat = AllViolationsStrategy()

    train_acc1 = "N/A" if res1 is None else (1000 - len(
        list(strat.select_active(domain, samples, res1, [])))) / 1000
    train_acc2 = "N/A" if res2 is None else (1000 - len(
        list(strat.select_active(domain, samples, res2, [])))) / 1000
    train_acc3 = "N/A" if res3 is None else (1000 - len(
        list(strat.select_active(domain, samples, res3, [])))) / 1000

    acc1 = "N/A" if res1 is None else (1000 - len(
        list(strat.select_active(domain, test_samples, res1, [])))) / 1000
    acc2 = "N/A" if res2 is None else (1000 - len(
        list(strat.select_active(domain, test_samples, res2, [])))) / 1000
    acc3 = "N/A" if res3 is None else (1000 - len(
        list(strat.select_active(domain, test_samples, res3, [])))) / 1000

    print("[")
    print("file = {}".format(filename))
    print("samples= {}".format(sample_count))
    print("INCAL_result = {}".format(formula1))
    print("SHREC1_result = {}".format(formula2))
    print("SHREC2_result = {}".format(formula3))
    print("INCAL_time = {}".format(dur1))
    print("SHREC1_time = {}".format(dur2))
    print("SHREC2_time = {}".format(dur3))
    print("INCAL_clauses = {}".format(cl1))
    print("SHREC1_clauses = {}".format(cl2))
    print("SHREC2_clauses = {}".format(cl3))
    print("INCAL_halflines = {}".format(hl1))
    print("SHREC1_halflines = {}".format(hl2))
    print("SHREC2_halflines = {}".format(hl3))
    print("INCAL_cost = {}".format(cl1 + hl1))
    print("SHREC1_cost = {}".format(cl2 + hl2))
    print("SHREC2_cost = {}".format(cl3 + hl3))
    print("INCAL_train_acc = {}".format(train_acc1))
    print("SHREC1_train_acc = {}".format(train_acc2))
    print("SHREC2_train_acc = {}".format(train_acc3))
    print("INCAL_acc = {}".format(acc1))
    print("SHREC1_acc = {}".format(acc2))
    print("SHREC2_acc = {}".format(acc3))
    print("]")
Esempio n. 6
0
def adapt_domain_multiple(target_problem, new_bounds):
    domain = target_problem.domain
    adapted_domain = problem.Domain(domain.variables, domain.var_types, new_bounds)
    return problem.Problem(adapted_domain, target_problem.theory, target_problem.name)
Esempio n. 7
0
        seed = hash(time.time())
        random.seed(seed)
        samples = [generator.get_sample(domain) for _ in range(ratio_sample_size)]

        ratio = calculate_ratio(domain, formula) if ratio_sample_size is None else calculate_ratio_approx(formula, samples)

        for sample_size in results_flat[problem_id]:
            config = results_flat[problem_id][sample_size]

            if ratio_sample_size is None:
                if recompute or "exact_ratio" not in config:
                    config["exact_ratio"] = ratio
            else:
                if recompute or "approx_ratio" not in config:
                    config["approx_ratio"] = dict()
                ratio_dict = config["approx_ratio"]
                if ratio_sample_size not in ratio_dict:
                    ratio_dict[ratio_sample_size] = []
                if len(ratio_dict[ratio_sample_size]) < 1:
                    ratio_dict[ratio_sample_size].append({
                        "ratio": ratio,
                        "seed": seed,
                    })

    dump_results(results_flat, results_dir)


if __name__ == "__main__":
    x = smt.Symbol("x", smt.REAL)
    calculate_accuracy(problem.Domain(["x"], {"x": smt.REAL}, {"x": (0, 1)}), x <= smt.Real(0.5), x <= smt.Real(0.4))