Ejemplo n.º 1
0
def main():
    domain, formula, name = checker_problem()
    thresholds = {v: 0.1 for v in domain.real_vars}
    data = uniform(domain, 1000)
    labels = evaluate(domain, formula, data)
    data = data[labels == 1]
    labels = labels[labels == 1]

    def learn_inc(_data, _labels, _i, _k, _h):
        strategy = OneClassStrategy(RandomViolationsStrategy(10), thresholds)
        learner = KCnfSmtLearner(_k, _h, strategy, "mvn")
        initial_indices = LearnOptions.initial_random(20)(list(
            range(len(_data))))
        # learner.add_observer(LoggingObserver(None, _k, _h, None, True))
        learner.add_observer(
            PlottingObserver(domain, "test_output/checker",
                             "run_{}_{}_{}".format(_i, _k,
                                                   _h), domain.real_vars[0],
                             domain.real_vars[1], None, False))
        return learner.learn(domain, _data, _labels, initial_indices)

    (new_data, new_labels,
     formula), k, h = learn_bottom_up(data, labels, learn_inc, 1, 1, 1, 1,
                                      None, None)
    print("Learned CNF(k={}, h={}) formula {}".format(k, h,
                                                      pretty_print(formula)))
    print("Data-set grew from {} to {} entries".format(len(labels),
                                                       len(new_labels)))
Ejemplo n.º 2
0
    def observe_iteration(self, data, labels, formula, new_active_indices,
                          solving_time, selection_time):
        flat = {
            "type": "update",
            "theory": smt_to_nested(formula),
            "indices": [int(v) for v in new_active_indices],
            "solving_time": solving_time,
            "selection_time": selection_time,
            "k": self.k,
            "h": self.h,
        }
        if self.violation_counter is not None:
            flat["violations"] = [
                int(v) for v in self.violation_counter.last_violations
            ]

        if self.verbose:
            print("Found model after {:.2f}s".format(solving_time))
            print(pretty_print(formula))
            if self.violation_counter is not None:
                violation_count = len(self.violation_counter.last_violations)
                selected_count = len(new_active_indices)
                print("Selected {} of {} violations in {:.2f}s".format(
                    selected_count, violation_count, selection_time))
        self.log(flat)
Ejemplo n.º 3
0
def background_knowledge_example():
    domain = Domain.make(["a", "b"], ["x", "y"], [(0, 1), (0, 1)])
    a, b, x, y = domain.get_symbols(domain.variables)
    formula = (a | b) & (~a | ~b) & (x >= 0) & (x <= y) & (y <= 1)
    thresholds = {v: 0.1 for v in domain.real_vars}
    data = uniform(domain, 10000)
    labels = evaluate(domain, formula, data)
    data = data[labels == 1]
    labels = labels[labels == 1]

    def learn_inc(_data, _labels, _i, _k, _h):
        strategy = OneClassStrategy(
            RandomViolationsStrategy(10),
            thresholds)  #, background_knowledge=(a | b) & (~a | ~b))
        learner = KCnfSmtLearner(_k, _h, strategy, "mvn")
        initial_indices = LearnOptions.initial_random(20)(list(
            range(len(_data))))
        # learner.add_observer(LoggingObserver(None, _k, _h, None, True))
        learner.add_observer(
            PlottingObserver(domain, "test_output/bg",
                             "run_{}_{}_{}".format(_i, _k,
                                                   _h), domain.real_vars[0],
                             domain.real_vars[1], None, False))
        return learner.learn(domain, _data, _labels, initial_indices)

    (new_data, new_labels,
     formula), k, h = learn_bottom_up(data, labels, learn_inc, 1, 1, 1, 1,
                                      None, None)
    print("Learned CNF(k={}, h={}) formula {}".format(k, h,
                                                      pretty_print(formula)))
    print("Data-set grew from {} to {} entries".format(len(labels),
                                                       len(new_labels)))
Ejemplo n.º 4
0
def negative_samples_example(background_knowledge):
    domain = Domain.make(["a", "b"], ["x", "y"], [(0, 1), (0, 1)])
    a, b, x, y = domain.get_symbols(domain.variables)
    formula = (a | b) & (~a | ~b) & (x <= y) & domain.get_bounds()
    background_knowledge = (a | b) & (~a
                                      | ~b) if background_knowledge else None
    thresholds = {"x": 0.1, "y": 0.2}
    data = uniform(domain, 10000)
    labels = evaluate(domain, formula, data)
    data = data[labels == 1]
    labels = labels[labels == 1]
    original_sample_count = len(labels)

    start_time = time.time()

    data, labels = OneClassStrategy.add_negatives(domain, data, labels,
                                                  thresholds, 100,
                                                  background_knowledge)
    print("Created {} negative examples".format(
        len(labels) - original_sample_count))

    directory = "test_output{}bg_sampled{}{}".format(
        os.path.sep, os.path.sep, time.strftime("%Y-%m-%d %Hh%Mm%Ss"))

    def learn_inc(_data, _labels, _i, _k, _h):
        strategy = OneClassStrategy(RandomViolationsStrategy(10),
                                    thresholds,
                                    background_knowledge=background_knowledge)
        learner = KCnfSmtLearner(_k, _h, strategy, "mvn")
        initial_indices = LearnOptions.initial_random(20)(list(
            range(len(_data))))
        learner.add_observer(
            PlottingObserver(domain, directory,
                             "run_{}_{}_{}".format(_i, _k,
                                                   _h), domain.real_vars[0],
                             domain.real_vars[1], None, False))
        return learner.learn(domain, _data, _labels, initial_indices)

    (new_data, new_labels,
     learned_formula), k, h = learn_bottom_up(data, labels, learn_inc, 1, 1, 1,
                                              1, None, None)
    if background_knowledge:
        learned_formula = learned_formula & background_knowledge

    duration = time.time() - start_time

    print("{}".format(smt_to_nested(learned_formula)))
    print("Learned CNF(k={}, h={}) formula {}".format(
        k, h, pretty_print(learned_formula)))
    print("Data-set grew from {} to {} entries".format(len(labels),
                                                       len(new_labels)))
    print("Learning took {:.2f}s".format(duration))

    test_data, labels = OneClassStrategy.add_negatives(domain, data, labels,
                                                       thresholds, 1000,
                                                       background_knowledge)
    assert all(evaluate(domain, learned_formula, test_data) == labels)
Ejemplo n.º 5
0
from inspect import signature

import numpy as np

from smtlearn.examples import ice_cream_problem
from pywmi.plot import plot_data, plot_formula
from pywmi.sample import uniform
from pywmi.smt_check import evaluate
import random
from smtlearn.violations.core import RandomViolationsStrategy
from smtlearn.k_cnf_smt_learner import KCnfSmtLearner
from pywmi.smt_print import pretty_print

random.seed(666)
np.random.seed(666)

domain, formula, name = ice_cream_problem()
# plot_formula(None, domain, formula)

data = uniform(domain, 100)
labels = evaluate(domain, formula, data)

learner = KCnfSmtLearner(3, 3, RandomViolationsStrategy(10))
initial_indices = random.sample(range(data.shape[0]), 20)

learned_theory = learner.learn(domain, data, labels, initial_indices)
print(pretty_print(learned_theory))
Ejemplo n.º 6
0
 def __str__(self):
     return pretty_print(self.test)
Ejemplo n.º 7
0
def main():
    formula, k, h = LearnOptions().execute_from_command_line("Learn SMT(LRA) theories from data")
    print("Learned formula (k={k}, h={h}): {f}".format(f=pretty_print(formula), k=k, h=h))
Ejemplo n.º 8
0
def main():
    smt_lib_name = "smt-lib-benchmark"
    synthetic_name = "synthetic"
    parser = argparse.ArgumentParser(
        description="Interface with benchmark or synthetic data for experiments"
    )

    parser.add_argument("source")
    parser.add_argument("--sample_size", type=int, default=None)
    parser.add_argument("--runs", type=int, default=None)
    parser.add_argument("--input_dir", type=str, default=None)
    parser.add_argument("--output_dir", type=str, default=None)
    parser.add_argument("--processes", type=int, default=None)
    parser.add_argument("--time_out", type=int, default=None)

    task_parsers = parser.add_subparsers(dest="task")
    prepare_parser = task_parsers.add_parser("prepare")
    prepare_parser.add_argument("--reset_samples", type=bool, default=False)
    learn_parser = task_parsers.add_parser("learn")
    analyze_parser = task_parsers.add_parser("analyze")
    analyze_parser.add_argument("--dirs", nargs="+", type=str)
    analyze_parser.add_argument("--res_path", type=str, default=None)

    show_parsers = analyze_parser.add_subparsers()
    show_parser = show_parsers.add_parser("show")
    show.add_arguments(show_parser)

    learn_options = LearnOptions()
    learn_options.add_arguments(learn_parser)

    args = parser.parse_args()
    if args.task == "prepare":
        if args.source == smt_lib_name:
            prepare_smt_lib_benchmark()
            prepare_ratios()
            prepare_samples(args.runs, args.sample_size, args.reset_samples)
        elif args.source == synthetic_name:
            prepare_synthetic(args.input_dir, args.output_dir, args.runs,
                              args.sample_size)
    elif args.task == "learn":
        learn_options.parse_arguments(args)
        if args.source == smt_lib_name:
            learn_benchmark(args.runs, args.sample_size, args.processes,
                            args.time_out, learn_options)
        elif args.source == synthetic_name:
            learn_synthetic(args.input_dir, args.output_dir, args.runs,
                            args.sample_size, args.processes, args.time_out,
                            learn_options)
        elif args.source.startswith("ex"):
            example_name = args.source.split(":", 1)[1]
            domain, formula = examples.get_by_name(example_name)
            np.random.seed(1)
            from pywmi.sample import uniform
            samples = uniform(domain, args.sample_size)
            from pywmi import evaluate
            labels = evaluate(domain, formula, samples)
            learn_options.set_value("domain", domain, False)
            learn_options.set_value("data", samples, False)
            learn_options.set_value("labels", labels, False)
            (formula, k, h), duration = learn_options.call(True)
            print("[{:.2f}s] Learned formula (k={}, h={}): {}".format(
                duration, k, h, pretty_print(formula)))
    elif args.task == "analyze":
        analyze(args.dirs, args.res_path, show.parse_args(args))
Ejemplo n.º 9
0
def parse():
    parser = argparse.ArgumentParser()
    parser.add_argument("file")
    task_parsers = parser.add_subparsers(dest="task", help="Which task to run")

    vp = task_parsers.add_parser("volume")
    vp.add_argument(
        "engines",
        help="One or more engines (later engines are used if earlier engines fail)",
        nargs="+",
    )
    vp.add_argument("-s", "--status", help="Print current status", action="store_true")

    pp = task_parsers.add_parser("prob")
    pp.add_argument(
        "engines",
        help="One or more engines (later engines are used if earlier engines fail)",
        nargs="+",
    )
    pp.add_argument("-s", "--status", help="Print current status", action="store_true")

    cp = task_parsers.add_parser("convert")
    cp.add_argument(
        "-o", "--json_file", help="The output path for the json file", default=None
    )

    compare_p = task_parsers.add_parser("compare")
    compare_p.add_argument(
        "engines", help="The engines to compare (see engine input format)", nargs="+"
    )
    compare_p.add_argument(
        "-q", "--query_index", help="The query index to check", default=None, type=int
    )

    normalize_p = task_parsers.add_parser("normalize")
    normalize_p.add_argument("new_support", type=str, help="The new support")
    normalize_p.add_argument(
        "output_path", type=str, help="Output path for normalized xadd"
    )
    normalize_p.add_argument(
        "-t", "--total", action="store_true", help="Dump total model instead of paths"
    )

    plot_p = task_parsers.add_parser("plot")
    plot_p.add_argument("-o", "--output", type=str, help="Output path", default=None)
    plot_p.add_argument("-x", "--feat_x", type=str, help="Feature x", default=None)
    plot_p.add_argument("-y", "--feat_y", type=str, help="Feature y", default=None)
    plot_p.add_argument(
        "-d",
        "--difference",
        type=str,
        help="Path to density to compute difference for",
        default=None,
    )

    print_p = task_parsers.add_parser("print")

    parser.add_argument(
        "-d", "--dialect", default=None, type=str, help="The dialect to use for import"
    )
    parser.add_argument(
        "-v", "--verbose", action="store_true", help="Enable verbose output"
    )
    args = parser.parse_args()

    density = Import.import_density(args.file, args.dialect)
    domain, support, weight, queries = (
        density.domain,
        density.support,
        density.weight,
        density.queries,
    )

    if args.verbose:
        logging.basicConfig(level=logging.INFO)

    if args.task == "convert":
        json_file = args.json_file
        if json_file is None:
            json_file = args.file + ".converted.json"

        density.to_file(json_file)

    elif args.task == "volume":
        print(
            get_volume(
                [get_engine(d, domain, support, weight) for d in args.engines],
                print_status=args.status,
            )
        )

    elif args.task == "prob":
        print(
            get_volume(
                [get_engine(d, domain, support, weight) for d in args.engines],
                queries,
                args.status,
            )
        )

    elif args.task == "compare":
        query = None
        if args.query_index is not None and args.query_index < len(queries):
            query = queries[args.query_index]
        compare([get_engine(d, domain, support, weight) for d in args.engines], query)

    elif args.task == "normalize":
        engine = XaddEngine(domain, support, weight, "original")
        new_density = Density.from_file(args.new_support)  # type: Density
        engine.normalize(new_density.support, not args.total)

    elif args.task == "plot":
        if args.output is not None and args.output == "*":
            output_file = args.file + ".png"
        else:
            output_file = args.output
        if args.difference:
            other = Density.from_file(args.difference)  # type: Density
            difference = support & ~other.support | ~support & other.support
            plot.plot_formula(
                output_file, domain, difference, (args.feat_x, args.feat_y)
            )
        else:
            plot.plot_formula(output_file, domain, support, (args.feat_x, args.feat_y))

    elif args.task == "print":
        print("-- Domain ---")
        print(density.domain)
        print("--- Support ---")
        print(pretty_print(density.support))
        print("--- Weight ---")
        print(pretty_print(density.weight))
        if len(density.queries) > 0:
            print("--- Queries ---")
            for query in density.queries:
                print("\t", pretty_print(query))