예제 #1
0
def test_normalization_negative():
    def get_normalization_file(filename):
        return path.join(path.dirname(__file__), "res", "bug_z_negative",
                         filename)

    domain = Domain.from_file(get_normalization_file("domain"))
    support = domain.get_bounds() & read_smtlib(
        get_normalization_file("vanilla.support"))
    weight = read_smtlib(get_normalization_file("vanilla.weight"))
    new_support = read_smtlib(get_normalization_file("renorm.support"))
    pa_engine = RejectionEngine(
        domain, Iff(new_support, ~normalize_formula(new_support)), Real(1),
        1000000)
    difference_volume = pa_engine.compute_volume()
    assert difference_volume == pytest.approx(0, EXACT_REL_ERROR**2)

    support = normalize_formula(support)
    new_support = normalize_formula(new_support)
    weight = normalize_formula(weight)

    engine = XaddEngine(domain, support, weight)
    new_weight = engine.normalize(new_support, paths=False)

    computed_volume = engine.copy_with(weight=new_weight).compute_volume()
    # print(pa_engine.copy_with(support=domain.get_bounds(), weight=new_weight).compute_volume())
    # print(pa_engine.copy_with(support=new_support, weight=new_weight).compute_volume())
    illegal_volume = engine.copy_with(support=~new_support,
                                      weight=new_weight).compute_volume()
    print(computed_volume, illegal_volume)

    # new_new_weight = engine.copy_with(support=domain.get_bounds(), weight=new_weight).normalize(new_support, paths=False)
    # print(pa_engine.copy_with(support=domain.get_bounds(), weight=new_new_weight).compute_volume())

    assert computed_volume == pytest.approx(1, rel=0.1)
    assert illegal_volume == pytest.approx(0, rel=EXACT_REL_ERROR)
예제 #2
0
파일: parse_all.py 프로젝트: mpreiner/pysmt
def execute_script_fname(smtfile):
    """Read and call a Solver to solve the instance"""
    print(smtfile)
    reset_env()
    assert os.path.exists(smtfile)
    start = time.clock()
    read_smtlib(smtfile)
    end = time.clock()
    return ( (end - start), smtfile)
예제 #3
0
def import_wmi_generate_100(filename):
    # type: (str) -> Density
    queries = [smt.read_smtlib(filename + ".query")]
    support = smt.read_smtlib(filename + ".support")
    weights = smt.read_smtlib(filename + ".weights")
    variables = queries[0].get_free_variables() | support.get_free_variables() | weights.get_free_variables()
    domain = Domain.make(real_bounds=(-100, 100),
                         boolean_variables=[v.symbol_name() for v in variables if v.symbol_type() == smt.BOOL])
    return Density(domain, support, weights, queries)
예제 #4
0
def execute_script_fname(smtfile):
    """Read and call a Solver to solve the instance"""
    print(smtfile)
    reset_env()
    assert os.path.exists(smtfile)
    start = time.clock()
    read_smtlib(smtfile)
    end = time.clock()
    return ((end - start), smtfile)
예제 #5
0
 def _read_experiment(path):
     input_file = open(path, 'r')
     problem_instances = []
     for support_name, weights_name, query_name in  pickle.load(input_file):
         support = read_smtlib(support_name)
         weights = read_smtlib(weights_name)
         query = read_smtlib(query_name)
         problem_instances.append((support, weights, query))
     
     input_file.close()
     return problem_instances
예제 #6
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)))
예제 #7
0
    def read(path):
        with open(path, 'rb') as f:
            index = pickle.load(f)

        folder = abspath(dirname(path))
        model = Model.read(join(folder, index['model_path']))

        datasets = {}
        for dataset_name, rel_path in index['dataset_paths'].items():
            datasets[dataset_name] = Dataset.read(join(folder, rel_path))

        bounds = bounds = index['bounds'] if 'bounds' in index else None
        learned_supports = []
        metadata = None

        if 'support_paths' in index:
            for support_path in index['support_paths']:
                try:
                    learned_supports.append(
                        read_smtlib(join(folder, support_path)))
                except FileNotFoundError as e:
                    logger.warning("Couldn't read: {}".format(path))

        if 'metadata' in index:
            metadata = index['metadata']

        problem = Problem(model,
                          datasets,
                          bounds=bounds,
                          learned_supports=learned_supports,
                          metadata=metadata)

        problem.original_path = path
        return problem
예제 #8
0
def import_wmi_mspn(filename):
    # type: (str) -> Density
    q_file, s_file, w_file = ("{}.{}".format(filename, ext) for ext in ["query", "support", "weight"])
    queries = [smt.TRUE()] if not os.path.exists(q_file) else [smt.read_smtlib(q_file)]
    support = smt.read_smtlib(s_file)
    if os.path.exists(w_file):
        weights = smt.read_smtlib(w_file)
    else:
        weights = smt.Real(1)
    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)})

    return Density(domain, support, weights, queries)
예제 #9
0
    def read(path):
        with open(path, 'rb') as f:
            index = pickle.load(f)

        folder = abspath(dirname(path))
        support = read_smtlib(join(folder, index['support_path']))
        weightfun = read_smtlib(join(folder, index['weightf_path']))
        variables = [
            get_env().formula_manager.get_or_create_symbol(varname, vartype)
            for varname, vartype in index['variables']
        ]
        bounds = index['bounds']

        metadata = None
        if 'metadata' in index:
            metadata = index['metadata']

        return Model(support, weightfun, variables, bounds, metadata=metadata)
예제 #10
0
def import_problem(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): (None, None)
                   for v in variables}  # TODO This is a hack

    return (Domain(var_names, var_types, var_domains)), target_formula
예제 #11
0
    def from_file(cls: 'FileDensity', filename: str) -> 'FileDensity':
        def wrap(f):
            return os.path.join(filename, f)

        if not os.path.exists(filename):
            os.makedirs(filename)

        domain = Domain.from_file(wrap(cls.get_domain_file()))
        support = smt.read_smtlib(wrap(cls.get_support_file()))
        weight_file = wrap(cls.get_weight_file())
        weight = None
        if os.path.exists(weight_file):
            weight = smt.read_smtlib(weight_file)

        queries = []
        query_files = sorted(glob(os.path.join(filename, "query_*.smt2")),
                             key=lambda f: int(f.split("_")[-1][:-5]))
        for query_file in query_files:
            queries.append(smt.read_smtlib(query_file))
        return FileDensity(domain, support, weight, queries)
예제 #12
0
    def test_read_and_write_shortcuts(self):
        fs = get_example_formulae()

        fdi, tmp_fname = mkstemp()
        os.close(fdi)  # Close initial file descriptor
        for (f_out, _, _, _) in fs:
            write_smtlib(f_out, tmp_fname)
            f_in = read_smtlib(tmp_fname)
            self.assertEqual(f_out.simplify(), f_in.simplify())
        # Clean-up
        os.remove(tmp_fname)
예제 #13
0
    def test_read_and_write_shortcuts(self):
        fs = get_example_formulae()

        fdi, tmp_fname = mkstemp()
        os.close(fdi) # Close initial file descriptor
        for (f_out, _, _, _) in fs:
            write_smtlib(f_out, tmp_fname)
            f_in = read_smtlib(tmp_fname)
            self.assertEqual(f_out.simplify(), f_in.simplify())
        # Clean-up
        os.remove(tmp_fname)
예제 #14
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
예제 #15
0
def execute_script_fname(smtfile):
    """Read and print the formula in HR Format."""
    reset_env()
    assert os.path.exists(smtfile)
    smtlib_start = time.clock()
    f = read_smtlib(smtfile)
    smtlib_end = time.clock()
    start = time.clock()
    s = f.serialize()
    end = time.clock()
    assert s is not None
    #print(smtfile, (smtlib_end-smtlib_start), (end-start))
    return (smtfile, (end - start))
예제 #16
0
    def read(path):
        with open(path, 'rb') as f:
            index = pickle.load(f)

        folder = abspath(dirname(path))

        feats, constr = Dataset.read_feats(join(folder, index['feats_path']))
        data = Dataset.read_data(join(folder, index['data_path']), feats)

        if 'constr_path' in index:
            constr2 = read_smtlib(index['constr_path'])
            if constr is None:
                constr = constr2
            else:
                constr = And(constr, constr2)

        return Dataset(feats, data, constr)
예제 #17
0
def test_normalization():
    def get_normalization_file(filename):
        return path.join(path.dirname(__file__), "res", "renorm_bug", filename)

    for i in range(5):
        domain = Domain.from_file(get_normalization_file("domain.json"))
        support = read_smtlib(get_normalization_file("vanilla.support"))
        weight = read_smtlib(get_normalization_file("vanilla.weight"))
        new_support = read_smtlib(
            get_normalization_file("renorm_chi_{}.support".format(i)))

        # print(smt_to_nested(support))

        clean_support = normalize_formula(support)
        clean_new_support = normalize_formula(new_support)
        clean_weight = normalize_formula(weight)

        print(smt_to_nested(clean_weight))

        assert (RejectionEngine(domain, ~Iff(support, clean_support),
                                Real(1.0), 1000000).compute_volume() == 0)
        assert (RejectionEngine(domain, ~Iff(new_support, clean_new_support),
                                Real(1.0), 1000000).compute_volume() == 0)

        # plot_formula("new_support", domain, new_support, ["r0", "r1"])
        # plot_formula("clean_new_support", domain, clean_new_support, ["r0", "r1"])

        support = clean_support
        new_support = clean_new_support
        weight = clean_weight

        # print(RejectionEngine(domain, Iff(weight, ~clean_weight), Real(1.0), 1000000).compute_volume())

        # print(smt_to_nested(support))

        print("Problem", i)
        engine = XaddEngine(domain, support, weight, "original")
        print("Volume before starting", engine.compute_volume())
        new_weight = engine.normalize(new_support, paths=False)

        Density(domain, new_support, new_weight).to_file("normalized.json")

        illegal_volume = XaddEngine(domain, ~new_support, new_weight,
                                    "mass").compute_volume()
        assert illegal_volume == pytest.approx(0, rel=EXACT_REL_ERROR)

        computed_volume = XaddEngine(domain, TRUE(), new_weight,
                                     "mass").compute_volume()
        computed_volume_within = XaddEngine(domain, new_support, new_weight,
                                            "mass").compute_volume()
        computed_volume_within2 = XaddEngine(domain, new_support,
                                             new_weight).compute_volume()
        computed_volume_within3 = RejectionEngine(domain, new_support,
                                                  new_weight,
                                                  1000000).compute_volume()
        print(
            "pa new_support new_weight",
            computed_volume_within,
            "xadd new_support new_weight:",
            computed_volume_within2,
            "rej new_support new_weight:",
            computed_volume_within3,
        )
        assert computed_volume_within == pytest.approx(computed_volume_within2,
                                                       EXACT_REL_ERROR)
        print(
            "pa true new_weight:",
            computed_volume,
            "pa new_support new_weight",
            computed_volume_within,
            "pa outside new_support new_weight",
            illegal_volume,
        )
        assert computed_volume == pytest.approx(1, rel=EXACT_REL_ERROR)
        assert computed_volume_within == pytest.approx(1, rel=EXACT_REL_ERROR)

        illegal_volume = engine.copy_with(support=~new_support,
                                          weight=new_weight).compute_volume()
        assert illegal_volume == pytest.approx(0, rel=EXACT_REL_ERROR)
예제 #18
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("]")
예제 #19
0
    def renormalize(self,
                    training_data,
                    seed,
                    mode=RENORM_OFF,
                    support=None,
                    timeout=None,
                    global_norm=False):

        if timeout is None:
            timeout = DEF_RENORM_TIMEOUT

        detcopy = self.det.copy()

        model_support = detcopy.tree_to_WMI_support()
        model_weight = detcopy.tree_to_WMI_weightfun()

        bounds = {
            v.symbol_name(): b
            for v, b in detcopy.root.bounds.items() if v.symbol_type() == REAL
        }

        renorm_support = None
        if mode == RENORM_BG_ONLY and training_data.constraints is not None:
            renorm_support = training_data.constraints
        elif mode == RENORM_FULL:
            if training_data.constraints is not None and support is not None:
                renorm_support = training_data.constraints & support
            elif training_data.constraints is not None:
                renorm_support = training_data.constraints
            elif support is not None:
                renorm_support = support

        renormalized = False
        if renorm_support is not None:

            if global_norm:
                logger.debug("Global renormalization")
                model_support = model_support & renorm_support
                renormalized = True
            else:
                logger.debug("Local renormalization")

                def renorm_wrap(inst, support, support_path, weight_path):
                    try:
                        inst.renormalize(support)
                        support = inst.tree_to_WMI_support()
                        weight = inst.tree_to_WMI_weightfun()
                        msg = "Writing result to files:\n{}\n{}"
                        logger.debug(msg.format(support_path, weight_path))
                        write_smtlib(support, support_path)
                        write_smtlib(weight, weight_path)
                        logger.debug("Done.")

                    except ModelException as e:
                        logger.error(
                            "Couldn't renormalize the DET: {}".format(e))

                # communication with wrapper process through file
                # NEVER use multiprocessing.Queue with huge pysmt formulas
                rndstr = ''.join(choice(TMP_CHARS) for _ in range(TMP_LEN))
                support_path = "{}.support".format(rndstr)
                weight_path = "{}.weight".format(rndstr)
                timed_proc = Process(target=renorm_wrap,
                                     args=(detcopy, renorm_support,
                                           support_path, weight_path))

                logger.debug(
                    "Starting renormalization with timeout: {}".format(
                        timeout))
                timed_proc.start()
                logger.debug("Timed proc started")
                timed_proc.join(timeout)
                logger.debug("Timed proc joined")

                if timed_proc.is_alive():
                    logger.warning("Renormalization timed out")
                    pid = timed_proc.pid
                    logger.warning(
                        "Killing process {} and its children".format(pid))
                    kill_recursive(pid)

                else:
                    try:
                        model_support = read_smtlib(support_path)
                        remove(support_path)
                    except FileNotFoundError:
                        model_support = None
                    try:
                        model_weight = read_smtlib(weight_path)
                        remove(weight_path)
                    except FileNotFoundError:
                        model_weight = None

                    if model_support is None or model_weight is None:
                        raise ModelException("Couldn't renormalize the DET")

                    logger.debug("Renormalization done")
                    renormalized = True

        model = Model(model_support,
                      model_weight,
                      list(map(lambda x: x[0], training_data.features)),
                      bounds,
                      metadata=self.learner_args)

        # is Z = 1?
        if renormalized:
            check_Z_normalize(model, seed, TEST_AND_NORM_SAMPLES)

        elif not global_norm:
            # fallback strategy for local: to global
            model, renormalized = self.renormalize(training_data,
                                                   seed,
                                                   mode=mode,
                                                   support=support,
                                                   timeout=timeout,
                                                   global_norm=True)

        return model, renormalized
예제 #20
0
s.from_file(smtlib_problems[0])

# In[7]:

s.check()

# In[8]:

s.model()

# ## Can we solve with cln?

# In[9]:

root = read_smtlib(smtlib_problems[0])

# In[10]:

root.serialize()

# Generate a model for the smt:

# In[11]:
'''
(! (((1.0 + (skoX * -1.0)) + (skoY * -1.0)) <= skoZ)) & 
(
    (! (skoZ <= 0.0)) & 
    (
        (! (skoY <= 0.0)) & 
        (! (skoX <= 0.0))