Search in sources :

Example 61 with Solver

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

the class BugTests method testFelix_02212008.

public final void testFelix_02212008() {
    Relation x0 = Relation.unary("Int/min");
    Relation x1 = Relation.unary("Int/zero");
    Relation x2 = Relation.unary("Int/max");
    Relation x3 = Relation.nary("Int/next", 2);
    Relation x4 = Relation.unary("seq/Int");
    List<String> atomlist = Arrays.asList("-1", "-2", "-3", "-4", "-5", "-6", "-7", "-8", "0", "1", "2", "3", "4", "5", "6", "7");
    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);
    TupleSet x0_upper = factory.noneOf(1);
    x0_upper.add(factory.tuple("-8"));
    bounds.boundExactly(x0, x0_upper);
    TupleSet x1_upper = factory.noneOf(1);
    x1_upper.add(factory.tuple("0"));
    bounds.boundExactly(x1, x1_upper);
    TupleSet x2_upper = factory.noneOf(1);
    x2_upper.add(factory.tuple("7"));
    bounds.boundExactly(x2, x2_upper);
    TupleSet x3_upper = factory.noneOf(2);
    x3_upper.add(factory.tuple("-8").product(factory.tuple("-7")));
    x3_upper.add(factory.tuple("-7").product(factory.tuple("-6")));
    x3_upper.add(factory.tuple("-6").product(factory.tuple("-5")));
    x3_upper.add(factory.tuple("-5").product(factory.tuple("-4")));
    x3_upper.add(factory.tuple("-4").product(factory.tuple("-3")));
    x3_upper.add(factory.tuple("-3").product(factory.tuple("-2")));
    x3_upper.add(factory.tuple("-2").product(factory.tuple("-1")));
    x3_upper.add(factory.tuple("-1").product(factory.tuple("0")));
    x3_upper.add(factory.tuple("0").product(factory.tuple("1")));
    x3_upper.add(factory.tuple("1").product(factory.tuple("2")));
    x3_upper.add(factory.tuple("2").product(factory.tuple("3")));
    x3_upper.add(factory.tuple("3").product(factory.tuple("4")));
    x3_upper.add(factory.tuple("4").product(factory.tuple("5")));
    x3_upper.add(factory.tuple("5").product(factory.tuple("6")));
    x3_upper.add(factory.tuple("6").product(factory.tuple("7")));
    bounds.boundExactly(x3, x3_upper);
    TupleSet x4_upper = factory.noneOf(1);
    x4_upper.add(factory.tuple("0"));
    x4_upper.add(factory.tuple("1"));
    x4_upper.add(factory.tuple("2"));
    x4_upper.add(factory.tuple("3"));
    bounds.boundExactly(x4, x4_upper);
    bounds.boundExactly(-8, factory.range(factory.tuple("-8"), factory.tuple("-8")));
    bounds.boundExactly(-7, factory.range(factory.tuple("-7"), factory.tuple("-7")));
    bounds.boundExactly(-6, factory.range(factory.tuple("-6"), factory.tuple("-6")));
    bounds.boundExactly(-5, factory.range(factory.tuple("-5"), factory.tuple("-5")));
    bounds.boundExactly(-4, factory.range(factory.tuple("-4"), factory.tuple("-4")));
    bounds.boundExactly(-3, factory.range(factory.tuple("-3"), factory.tuple("-3")));
    bounds.boundExactly(-2, factory.range(factory.tuple("-2"), factory.tuple("-2")));
    bounds.boundExactly(-1, factory.range(factory.tuple("-1"), factory.tuple("-1")));
    bounds.boundExactly(0, factory.range(factory.tuple("0"), factory.tuple("0")));
    bounds.boundExactly(1, factory.range(factory.tuple("1"), factory.tuple("1")));
    bounds.boundExactly(2, factory.range(factory.tuple("2"), factory.tuple("2")));
    bounds.boundExactly(3, factory.range(factory.tuple("3"), factory.tuple("3")));
    bounds.boundExactly(4, factory.range(factory.tuple("4"), factory.tuple("4")));
    bounds.boundExactly(5, factory.range(factory.tuple("5"), factory.tuple("5")));
    bounds.boundExactly(6, factory.range(factory.tuple("6"), factory.tuple("6")));
    bounds.boundExactly(7, factory.range(factory.tuple("7"), factory.tuple("7")));
    Variable x9 = Variable.nary("isTree_r", 2);
    Expression x10 = Expression.INTS.product(Expression.INTS);
    Decls x8 = x9.setOf(x10);
    Expression x15 = Expression.INTS.product(Expression.INTS);
    Formula x14 = x9.in(x15);
    Formula x13 = x14.and(Formula.TRUE);
    Formula x12 = x13.and(Formula.TRUE);
    Formula x7 = x12.forSome(x8);
    Formula x19 = x0.eq(x0);
    Formula x20 = x2.eq(x2);
    Formula x18 = x19.and(x20);
    Formula x21 = x3.eq(x3);
    Formula x17 = x18.and(x21);
    Formula x6 = x7.and(x17);
    Formula x23 = x1.eq(x1);
    Formula x24 = x4.eq(x4);
    Formula x22 = x23.and(x24);
    Formula x5 = x6.and(x22);
    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(4);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
    solver.options().setSymmetryBreaking(20);
    solver.options().setSkolemDepth(0);
    Iterator<Solution> sols = solver.solveAll(x5, bounds);
    assertTrue(sols.hasNext());
    Solution a = sols.next();
    assertEquals(Solution.Outcome.TRIVIALLY_SATISFIABLE, a.outcome());
    assertTrue(sols.hasNext());
    a = sols.next();
    assertEquals(Solution.Outcome.SATISFIABLE, a.outcome());
}
Also used : TupleSet(kodkod.instance.TupleSet) Solver(kodkod.engine.Solver) Variable(kodkod.ast.Variable) Decls(kodkod.ast.Decls) Bounds(kodkod.instance.Bounds) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression) Solution(kodkod.engine.Solution)

Example 62 with Solver

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

the class BugTests method testFelix_11122006.

public final void testFelix_11122006() {
    Relation x0 = Relation.nary("Q", 1);
    Relation x1 = Relation.nary("B", 1);
    Relation x2 = Relation.nary("A", 1);
    Relation x3 = Relation.nary("QQ", 3);
    List<String> atomlist = Arrays.asList("A", "B", "Q");
    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);
    TupleSet x0_upper = factory.noneOf(1);
    x0_upper.add(factory.tuple("Q"));
    bounds.boundExactly(x0, x0_upper);
    TupleSet x1_upper = factory.noneOf(1);
    x1_upper.add(factory.tuple("B"));
    bounds.boundExactly(x1, x1_upper);
    TupleSet x2_upper = factory.noneOf(1);
    x2_upper.add(factory.tuple("A"));
    bounds.boundExactly(x2, x2_upper);
    TupleSet x3_upper = factory.noneOf(3);
    x3_upper.add(factory.tuple("Q").product(factory.tuple("A")).product(factory.tuple("A")));
    x3_upper.add(factory.tuple("Q").product(factory.tuple("B")).product(factory.tuple("B")));
    bounds.bound(x3, x3_upper);
    Expression x7 = x2.product(x2);
    Expression x8 = x0.join(x3);
    Formula x6 = x7.in(x8);
    Formula x5 = x6.not();
    Expression x18 = x1.product(x1);
    Expression x17 = x7.union(x18);
    Expression x16 = x0.product(x17);
    Formula x15 = x3.in(x16);
    Formula x4 = x5.and(x15);
    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(4);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
    // System.out.println(bounds);
    // System.out.println(x4);
    Solution sol = solver.solve(x4, bounds);
    assertEquals(sol.outcome(), Solution.Outcome.SATISFIABLE);
// System.out.println(sol.toString());
}
Also used : TupleSet(kodkod.instance.TupleSet) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) Solver(kodkod.engine.Solver) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression) Bounds(kodkod.instance.Bounds) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Solution(kodkod.engine.Solution)

Example 63 with Solver

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

the class BugTests method testFelix_03062008.

public final void testFelix_03062008() {
    Relation x0 = Relation.unary("Int/min");
    Relation x1 = Relation.unary("Int/zero");
    Relation x2 = Relation.unary("Int/max");
    Relation x3 = Relation.nary("Int/next", 2);
    Relation x4 = Relation.unary("seq/Int");
    List<String> atomlist = Arrays.asList("-1", "-2", "-3", "-4", "-5", "-6", "-7", "-8", "0", "1", "2", "3", "4", "5", "6", "7");
    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);
    TupleSet x0_upper = factory.noneOf(1);
    x0_upper.add(factory.tuple("-8"));
    bounds.boundExactly(x0, x0_upper);
    TupleSet x1_upper = factory.noneOf(1);
    x1_upper.add(factory.tuple("0"));
    bounds.boundExactly(x1, x1_upper);
    TupleSet x2_upper = factory.noneOf(1);
    x2_upper.add(factory.tuple("7"));
    bounds.boundExactly(x2, x2_upper);
    TupleSet x3_upper = factory.noneOf(2);
    x3_upper.add(factory.tuple("-8").product(factory.tuple("-7")));
    x3_upper.add(factory.tuple("-7").product(factory.tuple("-6")));
    x3_upper.add(factory.tuple("-6").product(factory.tuple("-5")));
    x3_upper.add(factory.tuple("-5").product(factory.tuple("-4")));
    x3_upper.add(factory.tuple("-4").product(factory.tuple("-3")));
    x3_upper.add(factory.tuple("-3").product(factory.tuple("-2")));
    x3_upper.add(factory.tuple("-2").product(factory.tuple("-1")));
    x3_upper.add(factory.tuple("-1").product(factory.tuple("0")));
    x3_upper.add(factory.tuple("0").product(factory.tuple("1")));
    x3_upper.add(factory.tuple("1").product(factory.tuple("2")));
    x3_upper.add(factory.tuple("2").product(factory.tuple("3")));
    x3_upper.add(factory.tuple("3").product(factory.tuple("4")));
    x3_upper.add(factory.tuple("4").product(factory.tuple("5")));
    x3_upper.add(factory.tuple("5").product(factory.tuple("6")));
    x3_upper.add(factory.tuple("6").product(factory.tuple("7")));
    bounds.boundExactly(x3, x3_upper);
    TupleSet x4_upper = factory.noneOf(1);
    x4_upper.add(factory.tuple("0"));
    bounds.boundExactly(x4, x4_upper);
    bounds.boundExactly(-8, factory.range(factory.tuple("-8"), factory.tuple("-8")));
    bounds.boundExactly(-7, factory.range(factory.tuple("-7"), factory.tuple("-7")));
    bounds.boundExactly(-6, factory.range(factory.tuple("-6"), factory.tuple("-6")));
    bounds.boundExactly(-5, factory.range(factory.tuple("-5"), factory.tuple("-5")));
    bounds.boundExactly(-4, factory.range(factory.tuple("-4"), factory.tuple("-4")));
    bounds.boundExactly(-3, factory.range(factory.tuple("-3"), factory.tuple("-3")));
    bounds.boundExactly(-2, factory.range(factory.tuple("-2"), factory.tuple("-2")));
    bounds.boundExactly(-1, factory.range(factory.tuple("-1"), factory.tuple("-1")));
    bounds.boundExactly(0, factory.range(factory.tuple("0"), factory.tuple("0")));
    bounds.boundExactly(1, factory.range(factory.tuple("1"), factory.tuple("1")));
    bounds.boundExactly(2, factory.range(factory.tuple("2"), factory.tuple("2")));
    bounds.boundExactly(3, factory.range(factory.tuple("3"), factory.tuple("3")));
    bounds.boundExactly(4, factory.range(factory.tuple("4"), factory.tuple("4")));
    bounds.boundExactly(5, factory.range(factory.tuple("5"), factory.tuple("5")));
    bounds.boundExactly(6, factory.range(factory.tuple("6"), factory.tuple("6")));
    bounds.boundExactly(7, factory.range(factory.tuple("7"), factory.tuple("7")));
    Variable x9 = Variable.unary("x");
    Decls x8 = x9.oneOf(Expression.INTS);
    Variable x14 = Variable.unary("p_t'");
    Expression x15 = Expression.INTS.difference(x9);
    Decls x13 = x14.oneOf(x15);
    Variable x20 = Variable.unary("p_v");
    Decls x19 = x20.oneOf(Expression.INTS);
    Formula x21 = x14.in(x9);
    Expression x18 = x21.comprehension(x19);
    IntExpression x17 = x18.count();
    IntExpression x22 = IntConstant.constant(0);
    Formula x16 = x17.gt(x22);
    Formula x12 = x16.forAll(x13);
    Formula x11 = x12.not();
    Formula x7 = x11.forAll(x8);
    Formula x25 = x0.eq(x0);
    Formula x26 = x2.eq(x2);
    Formula x24 = x25.and(x26);
    Formula x27 = x3.eq(x3);
    Formula x23 = x24.and(x27);
    Formula x6 = x7.and(x23);
    Formula x29 = x1.eq(x1);
    Formula x30 = x4.eq(x4);
    Formula x28 = x29.and(x30);
    Formula x5 = x6.and(x28);
    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(4);
    // solver.options().setFlatten(false);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
    solver.options().setSymmetryBreaking(20);
    // System.out.println(PrettyPrinter.print(x5, 2));
    solver.options().setSkolemDepth(0);
    Solution sol1 = solver.solve(x5, bounds);
    assertNotNull(sol1.instance());
    solver.options().setSkolemDepth(1);
    Solution sol2 = solver.solve(x5, bounds);
    assertNotNull(sol2.instance());
}
Also used : TupleSet(kodkod.instance.TupleSet) Solver(kodkod.engine.Solver) Variable(kodkod.ast.Variable) Decls(kodkod.ast.Decls) Bounds(kodkod.instance.Bounds) IntExpression(kodkod.ast.IntExpression) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression) Solution(kodkod.engine.Solution)

Example 64 with Solver

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

the class UCoreStats method checkCorrect.

/**
 * Checks that the given core is unsatisfiable with respect to the given bounds.
 *
 * @return true if the core is correct; false otherwise
 */
static boolean checkCorrect(Set<Formula> core, Bounds bounds) {
    System.out.print("checking correctness ... ");
    final long start = System.currentTimeMillis();
    Solver solver = solver();
    solver.options().setSolver(SATFactory.MiniSat);
    final Solution sol = solver.solve(Formula.and(core), bounds);
    final long end = System.currentTimeMillis();
    if (sol.instance() == null) {
        System.out.println("correct (" + (end - start) + " ms).");
        return true;
    } else {
        System.out.println("incorrect! (" + (end - start) + " ms). The core is satisfiable:");
        System.out.println(sol);
        return false;
    }
}
Also used : Solver(kodkod.engine.Solver) Solution(kodkod.engine.Solution)

Example 65 with Solver

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

the class UCoreStats method findMinCore.

/**
 * Runs the minimal core finder using the specified strategy (with the given
 * resolution depth, if applicable) on the given methods, bounds, and prints out
 * results using the given results printer.
 */
private static void findMinCore(final Class<? extends ReductionStrategy> strategy, final int depth, final Map<Method, Formula> checks, final Bounds bounds, final ResultPrinter out) {
    try {
        final Solver solver = solver();
        final ThreadMXBean bean = ManagementFactory.getThreadMXBean();
        bean.setThreadCpuTimeEnabled(true);
        for (Map.Entry<Method, Formula> check : checks.entrySet()) {
            // System.out.println(PrettyPrinter.print(check.getValue(), 1));
            Solution sol = solver.solve(check.getValue(), bounds);
            if (sol.outcome() == Solution.Outcome.UNSATISFIABLE) {
                final long start = bean.getCurrentThreadUserTime() / 1000000;
                final Set<Formula> initialCore = Nodes.minRoots(check.getValue(), sol.proof().highLevelCore().values());
                final Set<Formula> minCore;
                if (strategy == null) {
                    // no strategy -- one-step
                    minCore = initialCore;
                } else {
                    if (strategy.getSimpleName().startsWith("RCE")) {
                        sol.proof().minimize(strategy(strategy, sol.proof().log(), depth));
                    } else {
                        sol.proof().minimize(strategy(strategy, sol.proof().log()));
                    }
                    minCore = Nodes.minRoots(check.getValue(), sol.proof().highLevelCore().values());
                }
                final long end = bean.getCurrentThreadUserTime() / 1000000;
                out.printUnsat(check.getKey().getName(), check.getValue(), bounds, sol.stats(), initialCore, minCore, end - start);
                assert checkCorrect(minCore, bounds);
                assert checkMinimal(minCore, bounds);
            } else if (sol.outcome() == Solution.Outcome.TRIVIALLY_UNSATISFIABLE) {
                out.printFalse(check.getKey().getName(), check.getValue(), bounds, sol);
            } else {
                out.printSat(check.getKey().getName(), check.getValue(), bounds, sol);
            }
        }
    } catch (IllegalArgumentException e) {
        System.out.println(e.getMessage());
        usage();
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) Formula(kodkod.ast.Formula) Solver(kodkod.engine.Solver) Method(java.lang.reflect.Method) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Solution(kodkod.engine.Solution)

Aggregations

Solver (kodkod.engine.Solver)76 Solution (kodkod.engine.Solution)72 Formula (kodkod.ast.Formula)67 Bounds (kodkod.instance.Bounds)67 Universe (kodkod.instance.Universe)26 TupleFactory (kodkod.instance.TupleFactory)25 Relation (kodkod.ast.Relation)24 TupleSet (kodkod.instance.TupleSet)23 Expression (kodkod.ast.Expression)14 IntExpression (kodkod.ast.IntExpression)14 Variable (kodkod.ast.Variable)14 Decls (kodkod.ast.Decls)11 HigherOrderDeclException (kodkod.engine.fol2sat.HigherOrderDeclException)7 UnboundLeafException (kodkod.engine.fol2sat.UnboundLeafException)7 ConsoleReporter (kodkod.engine.config.ConsoleReporter)6 LinkedHashSet (java.util.LinkedHashSet)5 Evaluator (kodkod.engine.Evaluator)3 AbstractReporter (kodkod.engine.config.AbstractReporter)3 Instance (kodkod.instance.Instance)3 Method (java.lang.reflect.Method)2