use of org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula in project java-smt by sosy-lab.
the class Interpolation method interpolateProgramTrace.
/**
* Example for encoding a program path and computing interpolants along the path. Taken from <a
* href="http://satsmt2014.forsyte.at/files/2014/01/interpolation_philipp.pdf">slides</a> from
* Philipp Rümmer.
*
* <p>Example trace of a program:
*
* <pre>
* i=0
* k=j
* assume (i<50)
* i++
* k++
* assume (i>=50)
* assume (j==0)
* assume (k<50)
* </pre>
*/
private static <T> void interpolateProgramTrace(InterpolatingProverEnvironment<T> prover, IntegerFormulaManager imgr, LogManager logger) throws InterruptedException, SolverException {
// create some variables.
// primed variable needed for 'self-assignments', alternatively use SSA-indices.
IntegerFormula i = imgr.makeVariable("i");
IntegerFormula i1 = imgr.makeVariable("i'");
IntegerFormula j = imgr.makeVariable("j");
IntegerFormula k = imgr.makeVariable("k");
IntegerFormula k1 = imgr.makeVariable("k'");
IntegerFormula zero = imgr.makeNumber(0);
IntegerFormula one = imgr.makeNumber(1);
IntegerFormula fifty = imgr.makeNumber(50);
// create and assert some formulas.
List<BooleanFormula> programTrace = ImmutableList.of(imgr.equal(i, zero), imgr.equal(k, j), imgr.lessThan(i, fifty), imgr.equal(i1, imgr.add(i, one)), imgr.equal(k1, imgr.add(k, one)), imgr.greaterOrEquals(i1, fifty), imgr.equal(j, zero), imgr.lessThan(k1, fifty));
// assert all formulas in the prover
List<T> handles = new ArrayList<>();
for (BooleanFormula step : programTrace) {
handles.add(prover.addConstraint(step));
}
// check for satisfiability
boolean unsat = prover.isUnsat();
Preconditions.checkState(unsat, "the example for interpolation should be UNSAT");
// get a sequence of interpolants for the program trace.
List<BooleanFormula> itps = prover.getSeqInterpolants0(handles);
logger.log(Level.INFO, "Interpolants for the program trace are:", itps);
}
use of org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula 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());
}
}
use of org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula in project java-smt by sosy-lab.
the class OptimizationIntReal method optimizeWithIntegers.
/**
* Solve the problem with integer logic.
*/
private static void optimizeWithIntegers(Configuration config, LogManager logger, ShutdownNotifier notifier, Solvers solver) throws InterruptedException, SolverException {
// create solver context
try (SolverContext context = SolverContextFactory.createSolverContext(config, logger, notifier, solver);
OptimizationProverEnvironment prover = context.newOptimizationProverEnvironment(ProverOptions.GENERATE_MODELS)) {
logger.log(Level.WARNING, "Using solver " + solver + " in version " + context.getVersion());
BooleanFormulaManager bmgr = context.getFormulaManager().getBooleanFormulaManager();
IntegerFormulaManager nmgr = context.getFormulaManager().getIntegerFormulaManager();
IntegerFormula x = nmgr.makeVariable("x");
// assert 0 <= x < 10
prover.addConstraint(bmgr.and(nmgr.greaterOrEquals(x, nmgr.makeNumber(0)), nmgr.lessThan(x, nmgr.makeNumber(10))));
logger.log(Level.INFO, "optimizing with integer logic");
printUpperAndLowerBound(prover, x, logger);
} catch (InvalidConfigurationException | UnsatisfiedLinkError e) {
// on some machines we support only some solvers,
// thus we can ignore these errors.
logger.logUserException(Level.INFO, e, "Solver " + solver + " is not available.");
} catch (UnsupportedOperationException e) {
logger.logUserException(Level.INFO, e, e.getMessage());
}
}
use of org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula 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;
}
use of org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula in project java-smt by sosy-lab.
the class AllSatExample method allSatIntegers2.
/**
* For integer formulas, we can implement the allsat-loop and collect all models when iterating.
*/
private List<List<ValueAssignment>> allSatIntegers2() throws InterruptedException, SolverException {
bfmgr = context.getFormulaManager().getBooleanFormulaManager();
ifmgr = context.getFormulaManager().getIntegerFormulaManager();
// formula ((1 <= a <= 3) && (0 == b) && (p == q)) with 6 models
IntegerFormula a = ifmgr.makeVariable("a");
IntegerFormula b = ifmgr.makeVariable("b");
BooleanFormula p = bfmgr.makeVariable("p");
BooleanFormula q = bfmgr.makeVariable("q");
prover.addConstraint(ifmgr.lessOrEquals(num(1), a));
prover.addConstraint(ifmgr.equal(num(0), b));
prover.addConstraint(ifmgr.lessOrEquals(a, num(3)));
prover.addConstraint(bfmgr.equivalence(p, q));
List<List<ValueAssignment>> models = new ArrayList<>();
// loop over all possible models for "1<=a<=3 AND p=q"
while (!prover.isUnsat()) {
final ImmutableList<ValueAssignment> modelAssignments = prover.getModelAssignments();
models.add(modelAssignments);
final List<BooleanFormula> modelAssignmentsAsFormulas = new ArrayList<>();
for (ValueAssignment va : modelAssignments) {
modelAssignmentsAsFormulas.add(va.getAssignmentAsFormula());
}
// prevent next model from using the same assignment as a previous model
prover.addConstraint(bfmgr.not(bfmgr.and(modelAssignmentsAsFormulas)));
}
return models;
}
Aggregations