def test_plot_boolean_or():
    nested_string = "(| (var bool a) (var bool b))"
    domain = Domain.make(["a", "b"], ["x", "y"], [(0, 1), (0, 1)])
    formula = nested_to_smt(nested_string)
    with TemporaryFile(suffix=".png") as filename:
        plot_formula(filename, domain, formula)
        image = Image.open(filename)
        assert image.getpixel((900, 900)) == image.getpixel((300, 900))
Ejemplo n.º 2
0
def import_smt_synthetic(filename):
    # type: (str) -> Density

    with open(filename) as f:
        flat = json.load(f)

    domain = Domain.from_state(flat["synthetic_problem"]["problem"]["domain"])
    queries = [smt.TRUE()]
    support = nested_to_smt(flat["synthetic_problem"]["problem"]["theory"]) & domain.get_bounds()
    weights = smt.Real(1)

    return Density(domain, support, weights, queries)
Ejemplo n.º 3
0
 def accuracy_approx(experiment):
     key = "accuracy_approx:{}".format(experiment.imported_from_file)
     if Properties.db.exists(key):
         return Properties.db.get(key)
     else:
         pysmt.environment.push_env()
         pysmt.environment.get_env().enable_infix_notation = True
         if os.path.basename(experiment.imported_from_file).startswith("synthetic"):
             db = Properties.get_db_synthetic(experiment)
             name = Properties.to_synthetic_name(experiment.imported_from_file)
             entry = db.get(name)
             domain = import_domain(json.loads(entry["domain"]))
             true_formula = nested_to_smt(entry["formula"])
         else:
             density = Density.import_from(experiment.parameters.original_values["domain"])
             domain = Domain(density.domain.variables, density.domain.var_types, Properties.get_bound(experiment))
             true_formula = density.support
         learned_formula = nested_to_smt(experiment.results.formula)
         engine = RejectionEngine(domain, smt.TRUE(), smt.Real(1.0), 100000)
         accuracy = engine.compute_probability(smt.Iff(true_formula, learned_formula))
         pysmt.environment.pop_env()
         print(accuracy)
         Properties.db.set(key, accuracy)
     return accuracy
Ejemplo n.º 4
0
def import_xadd_mspn(filename):
    # type: (str) -> Density
    name = os.path.basename(filename)
    parts = name.split("_")
    real_vars = int(parts[1])
    bool_vars = int(parts[2])

    domain = Domain.make(["A_{}".format(i) for i in range(bool_vars)],
                         {"x_{}".format(i): [0, 1] for i in range(real_vars)})

    support = smt.TRUE()
    with open(filename) as f:
        weight = nested_to_smt(f.readlines()[0])
    queries = [smt.TRUE()]

    return Density(domain, support, weight, queries)
Ejemplo n.º 5
0
def prepare_synthetic(input_directory, output_directory, runs, sample_size):
    seeds = [random.randint(0, 2**32 - 1) for _ in range(runs)]

    db = get_synthetic_db(output_directory, True)
    os.makedirs(output_directory)
    for filename in glob.glob("{}/**/synthetics*.txt".format(input_directory),
                              recursive=True):
        pysmt.environment.push_env()
        pysmt.environment.get_env().enable_infix_notation = True
        with open(filename) as file_reference:
            flat = json.load(file_reference)

        name = flat["synthetic_problem"]["problem"]["name"]
        print(name)

        if not db.exists(name):
            domain = import_domain(
                flat["synthetic_problem"]["problem"]["domain"])
            formula = nested_to_smt(
                flat["synthetic_problem"]["problem"]["theory"])
            Density(domain, formula, smt.Real(1.0)).export_to(
                os.path.join(output_directory, "{}.density".format(name)))
            entry = {
                "domain": export_domain(domain),
                "generation": {
                    "h": flat["synthetic_problem"]["half_space_count"],
                    "k": flat["synthetic_problem"]["formula_count"],
                    "l": flat["synthetic_problem"]["terms_per_formula"],
                    "structure": flat["synthetic_problem"]["cnf_or_dnf"],
                },
                "formula": smt_to_nested(formula),
                "samples": []
            }
        else:
            entry = dict(db.get(name))
            domain = import_domain(entry["domain"])
            formula = import_domain(entry["domain"])

        samples = entry.get("samples", [])
        matching_samples = []
        for sample in samples:
            if sample["sample_size"] == sample_size:
                matching_samples.append(sample)

        for i in range(runs - len(matching_samples)):
            seed = seeds[len(matching_samples) + i]
            samples_file = "{}.{}.{}.samples.npy".format(
                name, sample_size, seed)
            labels_file = "{}.{}.{}.labels.npy".format(name, sample_size, seed)
            np.random.seed(seed)
            data = uniform(domain, sample_size)
            np.save(os.path.join(output_directory, samples_file), data)
            labels = evaluate(domain, formula, data)
            np.save(os.path.join(output_directory, labels_file), labels)
            samples.append({
                "sample_size": sample_size,
                "seed": seed,
                "samples_file": samples_file,
                "labels_file": labels_file
            })

        entry["samples"] = samples
        db.set(name, entry)

        pysmt.environment.pop_env()
Ejemplo n.º 6
0
    def import_normalized(filename):
        from pywmi import nested_to_smt

        with open(filename) as f:
            return nested_to_smt(f.readlines()[0])