Search in sources :

Example 61 with Relation

use of kodkod.ast.Relation in project org.alloytools.alloy by AlloyTools.

the class SolutionIterator method nextTrivialSolution.

/**
 * Returns the trivial solution corresponding to the trivial translation stored
 * in {@code this.translation}, and if {@code this.translation.cnf.solve()} is
 * true, sets {@code this.translation} to a new translation that eliminates the
 * current trivial solution from the set of possible solutions. The latter has
 * the effect of forcing either the translator or the solver to come up with the
 * next solution or return UNSAT. If {@code this.translation.cnf.solve()} is
 * false, sets {@code this.translation} to null.
 *
 * @requires this.translation != null
 * @ensures this.translation is modified to eliminate the current trivial
 *          solution from the set of possible solutions
 * @return current solution
 */
private Solution nextTrivialSolution() {
    final Translation.Whole transl = this.translation;
    // this also
    final Solution sol = Solver.trivial(transl, translTime);
    if (sol.instance() == null) {
        // unsat, no more solutions
        translation = null;
    } else {
        trivial++;
        final Bounds bounds = transl.bounds();
        final Bounds newBounds = bounds.clone();
        final List<Formula> changes = new ArrayList<Formula>();
        for (Relation r : bounds.relations()) {
            final TupleSet lower = bounds.lowerBound(r);
            if (lower != bounds.upperBound(r)) {
                // r may change
                if (lower.isEmpty()) {
                    changes.add(r.some());
                } else {
                    final Relation rmodel = Relation.nary(r.name() + "_" + trivial, r.arity());
                    newBounds.boundExactly(rmodel, lower);
                    changes.add(r.eq(rmodel).not());
                }
            }
        }
        // nothing can change => there can be no more solutions (besides the
        // current trivial one).
        // note that transl.formula simplifies to the constant true with
        // respect to
        // transl.bounds, and that newBounds is a superset of transl.bounds.
        // as a result, finding the next instance, if any, for
        // transl.formula.and(Formula.or(changes))
        // with respect to newBounds is equivalent to finding the next
        // instance of Formula.or(changes) alone.
        final Formula formula = changes.isEmpty() ? Formula.FALSE : Formula.or(changes);
        final long startTransl = System.currentTimeMillis();
        translation = Translator.translate(formula, newBounds, transl.options());
        translTime += System.currentTimeMillis() - startTransl;
    }
    return sol;
}
Also used : Formula(kodkod.ast.Formula) TupleSet(kodkod.instance.TupleSet) Translation(kodkod.engine.fol2sat.Translation) Relation(kodkod.ast.Relation) Bounds(kodkod.instance.Bounds) ArrayList(java.util.ArrayList)

Example 62 with Relation

use of kodkod.ast.Relation in project org.alloytools.alloy by AlloyTools.

the class BugTests method testMana_01312007.

public final void testMana_01312007() {
    final Relation A = Relation.unary("A");
    final Relation first1 = Relation.unary("first1");
    final Relation first2 = Relation.unary("first2");
    final Relation last1 = Relation.unary("last1");
    final Relation last2 = Relation.unary("last2");
    final Relation next1 = Relation.binary("next1");
    final Relation next2 = Relation.binary("next2");
    final Formula f0 = next1.totalOrder(A, first1, last1);
    final Formula f1 = next2.totalOrder(A, first2, last2);
    final Formula f2 = first1.eq(last2);
    final Formula f3 = f0.and(f1).and(f2);
    final Universe u = new Universe(Arrays.asList("a0", "a1", "a2"));
    final TupleFactory f = u.factory();
    final Bounds b = new Bounds(u);
    b.bound(A, f.allOf(1));
    b.bound(first1, f.allOf(1));
    b.bound(first2, f.allOf(1));
    b.bound(last1, f.allOf(1));
    b.bound(last2, f.allOf(1));
    b.bound(next1, f.allOf(2));
    b.bound(next2, f.allOf(2));
    final Solver solver = new Solver();
    final Solution sol = solver.solve(f3, b);
    assertEquals(Solution.Outcome.SATISFIABLE, sol.outcome());
}
Also used : 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)

Example 63 with Relation

use of kodkod.ast.Relation in project org.alloytools.alloy by AlloyTools.

the class BugTests method testFelix_02222008.

public final void testFelix_02222008() {
    List<String> atomlist = Arrays.asList("X1", "X2", "X3");
    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);
    Relation x = Relation.unary("X");
    TupleSet x_upper = factory.noneOf(1);
    x_upper.add(factory.tuple("X1"));
    x_upper.add(factory.tuple("X2"));
    x_upper.add(factory.tuple("X3"));
    bounds.bound(x, x_upper);
    Variable a = Variable.unary("a");
    Variable b = Variable.unary("b");
    Variable c = Variable.unary("c");
    Formula goal = x.lone().not().and(b.union(c).eq(a).forSome(c.oneOf(x)).forAll(b.oneOf(x)).forSome(a.setOf(x)));
    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(4);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
    solver.options().setSymmetryBreaking(0);
    solver.options().setSkolemDepth(2);
    Iterator<Solution> itr = solver.solveAll(goal, bounds);
    int sols = 0;
    while (itr.hasNext()) {
        Solution sol = itr.next();
        Instance inst = sol.instance();
        if (inst == null)
            break;
        sols++;
        for (Relation rel : inst.relations()) {
            if (rel != x) {
                if (rel.arity() == 1) {
                    // rel = a
                    assertEquals(inst.tuples(x), inst.tuples(rel));
                } else {
                    // rel = c
                    final TupleSet dom = factory.noneOf(1);
                    for (Tuple t : inst.tuples(rel)) {
                        dom.add(factory.tuple(t.atom(0)));
                    }
                    assertEquals(inst.tuples(x), dom);
                }
            }
        }
    }
    assertEquals(3, sols);
}
Also used : TupleSet(kodkod.instance.TupleSet) Solver(kodkod.engine.Solver) Variable(kodkod.ast.Variable) Instance(kodkod.instance.Instance) Bounds(kodkod.instance.Bounds) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) Solution(kodkod.engine.Solution) Tuple(kodkod.instance.Tuple)

Example 64 with Relation

use of kodkod.ast.Relation in project org.alloytools.alloy by AlloyTools.

the class BugTests method testFelix_08022006.

public final void testFelix_08022006() {
    Relation x = Relation.nary("P", 1);
    String[] atoms = { "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5", "6", "7" };
    java.util.ArrayList<String> atomlist = new java.util.ArrayList<String>();
    for (String a : atoms) atomlist.add(a);
    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);
    for (int i = -8; i <= 7; i++) bounds.boundExactly(i, factory.setOf(String.valueOf(i)));
    bounds.bound(x, factory.allOf(1));
    Formula f = x.in(Expression.INTS).and(x.some());
    // System.out.println(bounds);
    Solver solver = new Solver();
    // solver.options().setSolver(SATFactory.ZChaff);
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(4);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
    Solution sol;
    try {
        sol = solver.solve(f, bounds);
        assertNotNull(sol.instance());
    } catch (HigherOrderDeclException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnboundLeafException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : Solver(kodkod.engine.Solver) UnboundLeafException(kodkod.engine.fol2sat.UnboundLeafException) Bounds(kodkod.instance.Bounds) ArrayList(java.util.ArrayList) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) HigherOrderDeclException(kodkod.engine.fol2sat.HigherOrderDeclException) Solution(kodkod.engine.Solution)

Example 65 with Relation

use of kodkod.ast.Relation in project org.alloytools.alloy by AlloyTools.

the class BugTests method testFelix_05152007_1.

public final void testFelix_05152007_1() {
    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.some();
    Formula x8 = x5.no();
    Formula x6 = x7.and(x8);
    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(x6, bounds);
    // System.out.println("Sol="+sol);
    Set<Formula> core = Nodes.minRoots(x6, sol.proof().highLevelCore().values());
    assertEquals(2, core.size());
    assertTrue(core.contains(x7));
    assertTrue(core.contains(x8));
}
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

Relation (kodkod.ast.Relation)87 Bounds (kodkod.instance.Bounds)49 Formula (kodkod.ast.Formula)45 TupleFactory (kodkod.instance.TupleFactory)45 Universe (kodkod.instance.Universe)45 TupleSet (kodkod.instance.TupleSet)43 Solution (kodkod.engine.Solution)30 Expression (kodkod.ast.Expression)26 IntExpression (kodkod.ast.IntExpression)24 Solver (kodkod.engine.Solver)24 ArrayList (java.util.ArrayList)23 Variable (kodkod.ast.Variable)23 Instance (kodkod.instance.Instance)15 Decls (kodkod.ast.Decls)13 Evaluator (kodkod.engine.Evaluator)9 Tuple (kodkod.instance.Tuple)8 IntSet (kodkod.util.ints.IntSet)7 BinaryExpression (kodkod.ast.BinaryExpression)6 Decl (kodkod.ast.Decl)6 LinkedHashSet (java.util.LinkedHashSet)4