Search in sources :

Example 51 with Solution

use of kodkod.engine.Solution in project org.alloytools.alloy by AlloyTools.

the class IntTest method testBitsetCast.

public void testBitsetCast() {
    final int width = 4, msb = width - 1;
    solver.options().setBitwidth(width);
    final List<Integer> atoms = new ArrayList<Integer>(width);
    for (int i = 0; i < msb; i++) {
        atoms.add(Integer.valueOf(1 << i));
    }
    atoms.add(Integer.valueOf(-1 << msb));
    final Bounds b = new Bounds(new Universe(atoms));
    final TupleFactory f = b.universe().factory();
    for (Integer i : atoms) {
        b.boundExactly(i, f.setOf(i));
    }
    b.bound(r1, f.allOf(1));
    for (int i = -1 << msb, max = 1 << msb; i < max; i++) {
        Formula test = r1.sum().toBitset().eq(IntConstant.constant(i).toBitset());
        Solution sol = solver.solve(test, b);
        Instance inst = sol.instance();
        assertNotNull(inst);
        Evaluator eval = new Evaluator(inst, solver.options());
        assertEquals(i, eval.evaluate(r1.sum()));
    }
}
Also used : Formula(kodkod.ast.Formula) Instance(kodkod.instance.Instance) Bounds(kodkod.instance.Bounds) ArrayList(java.util.ArrayList) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Evaluator(kodkod.engine.Evaluator) Solution(kodkod.engine.Solution)

Example 52 with Solution

use of kodkod.engine.Solution in project org.alloytools.alloy by AlloyTools.

the class IntTest method testUnOp.

private final void testUnOp(IntOperator op, IntExpression ei, int i, int result, int mask) {
    final IntExpression e = ei.apply(op);
    final Formula f = ei.eq(constant(i)).and(e.eq(constant(result)));
    final Solution s = solve(f);
    if (overflows(ei, i, result)) {
        assertNull(f.toString(), s.instance());
    } else {
        assertNotNull(f.toString(), s.instance());
        final Evaluator eval = new Evaluator(s.instance(), solver.options());
        assertEquals(result & mask, eval.evaluate(e) & mask);
    }
}
Also used : Formula(kodkod.ast.Formula) IntExpression(kodkod.ast.IntExpression) Evaluator(kodkod.engine.Evaluator) Solution(kodkod.engine.Solution)

Example 53 with Solution

use of kodkod.engine.Solution in project org.alloytools.alloy by AlloyTools.

the class IntTest method testIfIntExpr.

private void testIfIntExpr(Options.IntEncoding encoding) {
    solver.options().setIntEncoding(encoding);
    bounds.bound(r1, factory.setOf("15"), factory.setOf("15"));
    Formula f = (r1.some().thenElse(r1.count(), IntConstant.constant(5))).eq(IntConstant.constant(1));
    Solution s = solve(f);
    assertNotNull(s.instance());
    assertEquals(Ints.singleton(15), s.instance().tuples(r1).indexView());
    f = (r1.some().thenElse(r1.sum(), IntConstant.constant(5))).eq(IntConstant.constant(1));
    s = solve(f);
    assertNull(s.instance());
    bounds.bound(r1, factory.setOf("3"), factory.allOf(1));
    bounds.boundExactly(3, factory.setOf("3"));
    bounds.boundExactly(1, factory.setOf("1"));
    f = ((r1.count().eq(IntConstant.constant(2))).thenElse(r1.sum(), IntConstant.constant(5))).eq(IntConstant.constant(4));
    s = solve(f);
    assertNotNull(s.instance());
    assertTrue(s.instance().tuples(r1).indexView().contains(1));
    assertTrue(s.instance().tuples(r1).indexView().contains(3));
    assertEquals(2, s.instance().tuples(r1).size());
    f = Formula.TRUE.thenElse(IntConstant.constant(2), IntConstant.constant(3)).eq(IntConstant.constant(4));
    s = solve(f);
    assertEquals(Solution.Outcome.TRIVIALLY_UNSATISFIABLE, s.outcome());
    f = Formula.FALSE.thenElse(IntConstant.constant(2), IntConstant.constant(3)).eq(IntConstant.constant(3));
    s = solve(f);
    assertEquals(Solution.Outcome.TRIVIALLY_SATISFIABLE, s.outcome());
}
Also used : Formula(kodkod.ast.Formula) Solution(kodkod.engine.Solution)

Example 54 with Solution

use of kodkod.engine.Solution in project org.alloytools.alloy by AlloyTools.

the class IntTest method testIntSum.

private void testIntSum(Options.IntEncoding encoding) {
    solver.options().setIntEncoding(encoding);
    final Variable x = Variable.unary("x");
    bounds.bound(r1, factory.setOf("13", "14", "15"), factory.setOf("13", "14", "15"));
    Formula f = IntConstant.constant(3).eq(IntConstant.constant(1).sum(x.oneOf(r1)));
    Solution s = solve(f);
    assertNotNull(s.instance());
    bounds.bound(r1, factory.noneOf(1), factory.setOf("1", "3", "5"));
    bounds.boundExactly(1, factory.setOf("1"));
    bounds.boundExactly(3, factory.setOf("3"));
    bounds.boundExactly(5, factory.setOf("5"));
    f = IntConstant.constant(9).eq(x.sum().sum(x.oneOf(r1)));
    s = solve(f);
    assertNotNull(s.instance());
    assertEquals(s.instance().tuples(r1), factory.setOf("1", "3", "5"));
}
Also used : Formula(kodkod.ast.Formula) Variable(kodkod.ast.Variable) Solution(kodkod.engine.Solution)

Example 55 with Solution

use of kodkod.engine.Solution in project org.alloytools.alloy by AlloyTools.

the class BugTests method testFelix_05152007_2.

public final void testFelix_05152007_2() {
    Relation x5 = Relation.nary("A", 1);
    List<String> atomlist = Arrays.asList("A0", "A1", "A2");
    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);
    TupleSet x5_upper = factory.noneOf(1);
    x5_upper.add(factory.tuple("A2"));
    x5_upper.add(factory.tuple("A1"));
    x5_upper.add(factory.tuple("A0"));
    bounds.bound(x5, x5_upper);
    Formula x7 = x5.eq(x5).not();
    Solver solver = new Solver();
    solver.options().setLogTranslation(1);
    solver.options().setSolver(SATFactory.MiniSatProver);
    solver.options().setBitwidth(4);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
    Solution sol = solver.solve(x7, bounds);
    Set<Formula> core = Nodes.minRoots(x7, sol.proof().highLevelCore().values());
    assertEquals(1, core.size());
    assertTrue(core.contains(x7));
}
Also used : TupleSet(kodkod.instance.TupleSet) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) Solver(kodkod.engine.Solver) Bounds(kodkod.instance.Bounds) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Solution(kodkod.engine.Solution)

Aggregations

Solution (kodkod.engine.Solution)153 Formula (kodkod.ast.Formula)101 Bounds (kodkod.instance.Bounds)75 Solver (kodkod.engine.Solver)72 Universe (kodkod.instance.Universe)32 Relation (kodkod.ast.Relation)30 TupleFactory (kodkod.instance.TupleFactory)30 TupleSet (kodkod.instance.TupleSet)25 Variable (kodkod.ast.Variable)24 IntExpression (kodkod.ast.IntExpression)19 Expression (kodkod.ast.Expression)17 Test (org.junit.Test)16 QuantifiedFormula (kodkod.ast.QuantifiedFormula)15 Decls (kodkod.ast.Decls)11 Evaluator (kodkod.engine.Evaluator)10 HigherOrderDeclException (kodkod.engine.fol2sat.HigherOrderDeclException)7 UnboundLeafException (kodkod.engine.fol2sat.UnboundLeafException)7 ConsoleReporter (kodkod.engine.config.ConsoleReporter)6 Instance (kodkod.instance.Instance)6 ArrayList (java.util.ArrayList)5