Search in sources :

Example 1 with Model

use of org.sosy_lab.java_smt.api.Model in project java-smt by sosy-lab.

the class OptimizationFormulaWeights method optimizeWithWeights.

/**
 * add some constraints and then solve them with optimization according to weights.
 */
private static void optimizeWithWeights(OptimizationProverEnvironment prover, BooleanFormulaManager bmgr, IntegerFormulaManager imgr, LogManager logger) throws InterruptedException, SolverException {
    // create some symbols and formulas
    IntegerFormula x = imgr.makeVariable("x");
    IntegerFormula y = imgr.makeVariable("y");
    IntegerFormula z = imgr.makeVariable("z");
    IntegerFormula zero = imgr.makeNumber(0);
    IntegerFormula four = imgr.makeNumber(4);
    IntegerFormula ten = imgr.makeNumber(10);
    IntegerFormula scoreBasic = imgr.makeNumber(0);
    IntegerFormula scoreLow = imgr.makeNumber(2);
    IntegerFormula scoreMedium = imgr.makeNumber(4);
    IntegerFormula scoreHigh = imgr.makeNumber(10);
    // add some very important constraints: x<10, y<10, z<10, 10=x+y+z
    prover.addConstraint(bmgr.and(// very important -> direct constraint
    imgr.lessOrEquals(x, ten), // very important -> direct constraint
    imgr.lessOrEquals(y, ten), // very important -> direct constraint
    imgr.lessOrEquals(z, ten), imgr.equal(ten, imgr.add(x, imgr.add(y, z)))));
    // generate weighted formulas: if a formula should be satisfied,
    // use higher weight for the positive instance than for its negated instance.
    List<IntegerFormula> weights = ImmutableList.of(// important
    bmgr.ifThenElse(imgr.lessOrEquals(x, zero), scoreHigh, scoreBasic), // important
    bmgr.ifThenElse(imgr.lessOrEquals(x, four), scoreHigh, scoreBasic), // less important
    bmgr.ifThenElse(imgr.lessOrEquals(y, zero), scoreMedium, scoreBasic), // less important
    bmgr.ifThenElse(imgr.lessOrEquals(y, four), scoreMedium, scoreBasic), // not important
    bmgr.ifThenElse(imgr.lessOrEquals(z, zero), scoreLow, scoreBasic), // important
    bmgr.ifThenElse(imgr.lessOrEquals(z, four), scoreHigh, scoreBasic));
    // Maximize sum of weights
    int handle = prover.maximize(imgr.sum(weights));
    OptStatus response = prover.check();
    assert response == OptStatus.OPT;
    // ideal solution: sum=32 with e.g. x=0,y=6,z=4  or  x=0,y=7,z=3  or  x=0,y=8,z=2 ...
    try (Model model = prover.getModel()) {
        logger.log(Level.INFO, "maximal sum ", prover.upper(handle, Rational.ZERO).orElseThrow(), "with model", model.asList());
    }
}
Also used : OptStatus(org.sosy_lab.java_smt.api.OptimizationProverEnvironment.OptStatus) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) Model(org.sosy_lab.java_smt.api.Model)

Example 2 with Model

use of org.sosy_lab.java_smt.api.Model in project java-smt by sosy-lab.

the class AllSatExample method allSatIntegers.

/**
 * For integer formulas, we can implement the allsat-loop and collect all models when iterating.
 */
private List<List<ValueAssignment>> allSatIntegers() throws InterruptedException, SolverException {
    bfmgr = context.getFormulaManager().getBooleanFormulaManager();
    ifmgr = context.getFormulaManager().getIntegerFormulaManager();
    // formula ((1 <= a <= 10) && (1 <= b <= 10) && (a >= 2 * b)) with 25 models
    IntegerFormula a = ifmgr.makeVariable("a");
    IntegerFormula b = ifmgr.makeVariable("b");
    prover.addConstraint(ifmgr.lessOrEquals(num(1), a));
    prover.addConstraint(ifmgr.lessOrEquals(a, num(10)));
    prover.addConstraint(ifmgr.lessOrEquals(num(1), b));
    prover.addConstraint(ifmgr.lessOrEquals(b, num(10)));
    prover.addConstraint(ifmgr.greaterOrEquals(a, ifmgr.multiply(num(2), b)));
    List<List<ValueAssignment>> models = new ArrayList<>();
    // loop over all possible models for "1<=a<=10 AND 1<=b<=10 AND a==2*b"
    while (!prover.isUnsat()) {
        models.add(prover.getModelAssignments());
        try (Model model = prover.getModel()) {
            // convert model into formula, assuming we know all symbols and know they are integers
            BooleanFormula modelAsFormula = bfmgr.and(ifmgr.equal(a, num(model.evaluate(a))), ifmgr.equal(b, num(model.evaluate(b))));
            // prevent next model from using the same assignment as a previous model
            prover.addConstraint(bfmgr.not(modelAsFormula));
        }
    }
    return models;
}
Also used : IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) ArrayList(java.util.ArrayList) Model(org.sosy_lab.java_smt.api.Model) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula)

Example 3 with Model

use of org.sosy_lab.java_smt.api.Model in project java-smt by sosy-lab.

the class LoggingBasicProverEnvironment method getModel.

@Override
public Model getModel() throws SolverException {
    Model m = wrapped.getModel();
    logger.log(Level.FINE, "model", m);
    return m;
}
Also used : Model(org.sosy_lab.java_smt.api.Model)

Example 4 with Model

use of org.sosy_lab.java_smt.api.Model in project java-smt by sosy-lab.

the class Yices2NativeApiTest method modelTest.

@SuppressWarnings("resource")
@Test
public void modelTest() {
    int varx = yices_named_variable(yices_real_type(), "x");
    int eq = yices_arith_eq_atom(varx, yices_int32(10));
    int query = yices_named_variable(yices_real_type(), "x");
    Yices2FormulaCreator creator = new Yices2FormulaCreator();
    yices_push(env);
    yices_assert_formula(env, eq);
    System.out.println("varx: " + varx);
    System.out.println("query: " + query);
    if (yices_check_context(env, 0) == YICES_STATUS_SAT) {
        Model m = new Yices2Model(yices_get_model(env, 1), null, creator);
        Object val = m.evaluate(creator.encapsulateWithTypeOf(varx));
        System.out.println(val);
        m.close();
    }
}
Also used : Model(org.sosy_lab.java_smt.api.Model) Test(org.junit.Test)

Example 5 with Model

use of org.sosy_lab.java_smt.api.Model in project java-smt by sosy-lab.

the class Yices2NativeApiTest method modelExplorationTest.

@SuppressWarnings("resource")
@Test
public void modelExplorationTest() {
    int x = yices_int32(5);
    int y = yices_int32(7);
    int z = yices_named_variable(yices_int_type(), "z");
    int gt = yices_arith_gt_atom(z, x);
    int lt = yices_arith_lt_atom(z, y);
    int x2 = yices_int32(333);
    int y2 = yices_int32(335);
    int z2 = yices_named_variable(yices_int_type(), "z2");
    int gt2 = yices_arith_gt_atom(z2, x2);
    int lt2 = yices_arith_lt_atom(z2, y2);
    int sub = yices_sub(z2, z);
    int eq = yices_arith_eq_atom(sub, yices_int32(328));
    Yices2FormulaCreator creator = new Yices2FormulaCreator();
    yices_push(env);
    yices_assert_formula(env, gt);
    yices_assert_formula(env, lt);
    yices_assert_formula(env, gt2);
    yices_assert_formula(env, lt2);
    yices_assert_formula(env, eq);
    if (yices_check_context(env, 0) == YICES_STATUS_SAT) {
        long model = yices_get_model(env, 1);
        Model m = new Yices2Model(model, null, creator);
        System.out.println(yices_model_to_string(model));
        Object val = m.evaluate(creator.encapsulateWithTypeOf(eq));
        System.out.println(val);
        int addT = yices_add(z, z2);
        Object val2 = m.evaluate(creator.encapsulateWithTypeOf(addT));
        System.out.println(val2);
        System.out.println("DEFINED TERMS");
        int[] terms = yices_def_terms(model);
        for (int term : terms) {
            System.out.println(yices_term_to_string(term));
            System.out.println("Term id is: " + term);
            int[] yval = yices_get_value(model, term);
            System.out.println("Node id is: " + yval[0]);
            System.out.println("Node tag is: " + yval[1]);
            if (yval[1] == YVAL_RATIONAL) {
                System.out.println("Value is: " + yices_val_get_mpq(model, yval[0], yval[1]));
            }
        }
        m.close();
    } else {
        throw new IllegalArgumentException("The environment is not solvable!");
    }
}
Also used : Model(org.sosy_lab.java_smt.api.Model) Test(org.junit.Test)

Aggregations

Model (org.sosy_lab.java_smt.api.Model)55 Test (org.junit.Test)38 ProverEnvironment (org.sosy_lab.java_smt.api.ProverEnvironment)37 BasicProverEnvironment (org.sosy_lab.java_smt.api.BasicProverEnvironment)29 BooleanFormula (org.sosy_lab.java_smt.api.BooleanFormula)23 IntegerFormula (org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula)23 ValueAssignment (org.sosy_lab.java_smt.api.Model.ValueAssignment)18 BigInteger (java.math.BigInteger)8 ArrayList (java.util.ArrayList)8 BitvectorFormula (org.sosy_lab.java_smt.api.BitvectorFormula)8 ImmutableList (com.google.common.collect.ImmutableList)4 SolverException (org.sosy_lab.java_smt.api.SolverException)4 Formula (org.sosy_lab.java_smt.api.Formula)3 Program (com.dat3m.dartagnan.program.Program)2 Thread (com.dat3m.dartagnan.program.Thread)2 Event (com.dat3m.dartagnan.program.event.core.Event)2 Load (com.dat3m.dartagnan.program.event.core.Load)2 MemEvent (com.dat3m.dartagnan.program.event.core.MemEvent)2 Store (com.dat3m.dartagnan.program.event.core.Store)2 RegWriter (com.dat3m.dartagnan.program.event.core.utils.RegWriter)2