Beispiel #1
0
int process_bch(config const & config) {
    FILE * fin = nullptr;
    string filename = config.get_filename();
    // Make sure file exists
    if ((fin = fopen(filename.c_str(), "rt")) == nullptr) {
        opensmt_error2("can't open file", filename.c_str());
    }
    ::bchset_in(fin);
    ::bch_init_parser();
    ::bchparse();
    OpenSMTContext & ctx = *bch_ctx;
    if (config.get_precision() > 0) {
        ctx.setPrecision(config.get_precision());
    }
    ctx.setLocalOpt(config.get_local_opt());
    ctx.setDebug(config.get_debug());
    ctx.setPolytope(config.get_polytope());
    ctx.setShrinkForDop(config.get_sync());
    unordered_map<string, Enode *> var_map = bch_var_map;
    vector<Enode *> & costs = bch_costs;
    vector<Enode *> & ctrs_X = bch_ctrs;
    int const ret = process_main(ctx, config, costs, var_map, ctrs_X);
    ::bchlex_destroy();
    fclose(fin);
    ::bch_cleanup_parser();
    return ret;
}
Beispiel #2
0
int process_inv(config const & config) {
    FILE * fin = nullptr;
    string filename = config.get_filename();
    // Make sure file exists
    if ((fin = fopen(filename.c_str(), "rt")) == nullptr) {
        opensmt_error2("can't open file", filename.c_str());
    }
    ::invset_in(fin);
    ::inv_init_parser();
    ::invparse();
    OpenSMTContext & ctx = *inv_ctx;
    if (inv_prec > 0) {
        ctx.setPrecision(inv_prec);
    }
    if (config.get_precision() > 0) {
        ctx.setPrecision(config.get_precision());
    }
    int const ret = process_main(ctx, config);
    ::invlex_destroy();
    fclose(fin);
    ::inv_cleanup_parser();
    return ret;
}
Beispiel #3
0
int process_main(OpenSMTContext & ctx,
                 config const & config,
                 vector<Enode *> const & costs,
                 unordered_map<string, Enode*> var_map,
                 vector<Enode *> const & ctrs_X) {
    // minimize cost_i(x)
    // satisfying ctr_j(x)
    //
    // exists x. ctr(x) /\ forall y. [ctr(y) -> (cost(x) <= cost(y))]
    // exists x. ctr(x) /\ forall y. [!ctr(y) \/ (cost(x) <= cost(y))]
    // exists x, min_1, ..., min_n.
    //              /\ cost_i(x) = min_i
    //               i
    //
    //              /\ ctr_j(x)
    //               j
    //
    //              /\ forall y. [\/ !ctr_j(y) \/ min_i <= cost_i(y))]
    //                             j            i
    vector<Enode *> or_ctrs;
    for (Enode * ctr_X : ctrs_X) {
        Enode * ctr_not_Y = ctx.mkNot(ctx.mkCons(subst_exist_vars_to_univerally_quantified(ctx, var_map, ctr_X)));  // ctr(y)
        or_ctrs.push_back(ctr_not_Y);
    }

    vector<Enode*> eq_costs;
    for (unsigned i = 0; i < costs.size(); ++i) {
        Enode * min_var_i = make_min_var(ctx, var_map, i);                      // min
        Enode * eq_cost  = make_eq_cost(ctx, costs[i], min_var_i);              // cost_i(x) = min_i
        Enode * leq_cost = make_leq_cost(ctx, var_map, costs[i], min_var_i);    // min <= costs[0](y)
        eq_costs.push_back(eq_cost);
        or_ctrs.push_back(leq_cost);
    }

    // !ctr_1(y) \/ ... \/ !ctr_m(y) \/ (min_1 <= costs_1(y) ... \/ (min_n <= costs_n(y)
    Enode * or_term = ctx.mkOr(make_vec_to_list(ctx, or_ctrs));
    vector<pair<string, Snode *>> sorted_var_list;
    for (Enode * e : or_term->get_forall_vars()) {
        pair<string, Snode *> p = make_pair(e->getCar()->getName(), e->getSort());
        sorted_var_list.push_back(p);
    }
    Enode * quantified = ctx.mkForall(sorted_var_list, or_term);
    cout << "Precision  : " << ctx.getPrecision() << endl;
    for (auto var : var_map) {
        cout << "Variable   : " << var.first
             << " in [" << var.second->getDomainLowerBound() << ", "
             << var.second->getDomainUpperBound() << "]" << endl;
    }
    for (Enode * cost : costs) {
        cout << "Minimize   : " << cost << endl;
    }
    for (Enode * ctr_X : ctrs_X) {
        cout << "Constraint : " << ctr_X << endl;
    }
    for (Enode * eq_cost : eq_costs) {
        ctx.Assert(eq_cost);
    }
    for (Enode * ctr_X : ctrs_X) {
        ctx.Assert(ctr_X);
    }
    ctx.Assert(quantified);
    auto result = ctx.CheckSAT();
    cout << "Result     : ";
    if (result == l_True) {
        cout << "delta-sat" << endl;
        print_result(var_map);
        if (config.get_save_visualization()) {
            string vis_filename = config.get_filename() + ".py";
            ofstream of(vis_filename);
            // TODO(soonhok): generalize for a multi-obj case
            save_visualization_code(of, costs[0], var_map, config.get_vis_cell(), "min_0");
            cout << "Visualization Code is saved at " << vis_filename << endl;
        }
        if (config.get_run_visualization()) {
            // TODO(soonhok): generalize for a multi-obj case
            run_visualization(costs[0], var_map, config.get_vis_cell(), "min_0");
        }
    } else {
        cout << "unsat" << endl;
    }
    return 0;
}