Search in sources :

Example 11 with Instance

use of kodkod.instance.Instance in project org.alloytools.alloy by AlloyTools.

the class EvaluatorTest method testEvalTransitiveClosure.

public final void testEvalTransitiveClosure() {
    // // ^(Hilary->Jocelyn) = Hilary->Jocelyn
    // assertEquals(eval(hilary.product(jocelyn).closure()),
    // eval(hilary.product(jocelyn)));
    // // ^spouse = spouse + spouse.spouse
    // assertEquals(eval(spouse.closure()),
    // eval(spouse.union(spouse.join(spouse))));
    // // Hilary.^shaken = univ - (univ - Person.shaken)
    // assertEquals(eval(hilary.join(shaken.closure())),
    // eval(univ.difference(univ.difference(person.join(shaken)))));
    // try {
    // eval(person.closure());
    // fail("Expected IllegalArgumentException");
    // } catch (IllegalArgumentException iae) {}
    // ^r = value(^r)
    final Relation r = Relation.binary("r");
    final Universe u = evaluator.instance().universe();
    final TupleFactory f = u.factory();
    final Instance instance = new Instance(u);
    // value(r) = u[0]->u[1] + u[1]->u[2] + u[2]->u[3] + u[3]->u[4]
    TupleSet s = f.noneOf(r.arity());
    for (int i = 0; i < 4; i++) s.add(f.tuple(u.atom(i), u.atom(i + 1)));
    instance.add(r, s);
    // value(^r) = value(r) + u[0]->u[2] + u[0]->u[3] + u[0]->u[4] +
    // u[1]->u[3] u[1]->u[4] + u[2]->u[4]
    Set<Tuple> result = new HashSet<Tuple>();
    for (int i = 0; i < 4; i++) {
        for (int j = i + 1; j < 5; j++) {
            result.add(f.tuple(u.atom(i), u.atom(j)));
        }
    }
    assertEquals((new Evaluator(instance)).evaluate(r.closure()), result);
    // value(*r) = value(^r) + iden
    for (int i = 0; i < 10; i++) {
        result.add(f.tuple(u.atom(i), u.atom(i)));
    }
    assertEquals((new Evaluator(instance)).evaluate(r.reflexiveClosure()), result);
}
Also used : TupleSet(kodkod.instance.TupleSet) Relation(kodkod.ast.Relation) Instance(kodkod.instance.Instance) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Evaluator(kodkod.engine.Evaluator) Tuple(kodkod.instance.Tuple) HashSet(java.util.HashSet)

Example 12 with Instance

use of kodkod.instance.Instance in project org.alloytools.alloy by AlloyTools.

the class HOLSome4AllTest method evalS.

protected int evalS(Solution sol) {
    Instance inst = sol.instance();
    TupleSet x = inst.tuples(inst.skolems().iterator().next());
    return (Integer) x.iterator().next().atom(0);
}
Also used : TupleSet(kodkod.instance.TupleSet) Instance(kodkod.instance.Instance)

Example 13 with Instance

use of kodkod.instance.Instance in project org.alloytools.alloy by AlloyTools.

the class A4Solution method toString.

/**
 * Dumps the Kodkod solution into String.
 */
@Override
public String toString() {
    if (!solved)
        return "---OUTCOME---\nUnknown.\n";
    if (eval == null)
        return "---OUTCOME---\nUnsatisfiable.\n";
    String answer = toStringCache;
    if (answer != null)
        return answer;
    Instance sol = eval.instance();
    StringBuilder sb = new StringBuilder();
    sb.append("---INSTANCE---\n" + "integers={");
    boolean firstTuple = true;
    for (IndexedEntry<TupleSet> e : sol.intTuples()) {
        if (firstTuple)
            firstTuple = false;
        else
            sb.append(", ");
        // No need to print e.index() since we've ensured the Int atom's
        // String representation is always equal to ""+e.index()
        Object atom = e.value().iterator().next().atom(0);
        sb.append(atom2name(atom));
    }
    sb.append("}\n");
    try {
        for (Sig s : sigs) {
            sb.append(s.label).append("=").append(eval(s)).append("\n");
            for (Field f : s.getFields()) sb.append(s.label).append("<:").append(f.label).append("=").append(eval(f)).append("\n");
        }
        for (ExprVar v : skolems) {
            sb.append("skolem ").append(v.label).append("=").append(eval(v)).append("\n");
        }
        return toStringCache = sb.toString();
    } catch (Err er) {
        return toStringCache = ("<Evaluator error occurred: " + er + ">");
    }
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) ExprVar(edu.mit.csail.sdg.ast.ExprVar) TupleSet(kodkod.instance.TupleSet) Field(edu.mit.csail.sdg.ast.Sig.Field) Err(edu.mit.csail.sdg.alloy4.Err) Instance(kodkod.instance.Instance)

Example 14 with Instance

use of kodkod.instance.Instance in project org.alloytools.alloy by AlloyTools.

the class KK method main.

public static void main(String[] args) throws Exception {
    Relation x6 = Relation.unary("R");
    int[] ints = new int[] { 0, 1, 2 };
    List<Object> atomlist = new LinkedList<Object>();
    atomlist.add("R$0");
    atomlist.add("R$1");
    atomlist.add("R$2");
    for (int i : ints) atomlist.add(i);
    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);
    TupleSet x6_upper = factory.noneOf(1);
    x6_upper.add(factory.tuple("R$0"));
    x6_upper.add(factory.tuple("R$1"));
    x6_upper.add(factory.tuple("R$2"));
    bounds.bound(x6, x6_upper);
    for (int i : ints) {
        bounds.boundExactly(i, factory.setOf(i));
    }
    Formula x11 = x6.some();
    IntExpression x5 = x6.count();
    Formula x9 = x11.implies(x5.gt(IntConstant.constant(0)));
    Formula x7 = x9.not();
    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(2);
    // solver.options().setFlatten(false);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
    solver.options().setSymmetryBreaking(20);
    solver.options().setSkolemDepth(0);
    System.out.println("Solving...");
    System.out.println(PrettyPrinter.print(x7, 0));
    System.out.println(bounds);
    Solution sol = solver.solve(x7, bounds);
    System.out.println(sol.toString());
    Instance inst = sol.instance();
    Evaluator ev = new Evaluator(inst);
    System.out.println(ev.evaluate(x6.some()));
    System.out.println(ev.evaluate(x5));
    System.out.println(ev.evaluate(x5.gt(IntConstant.constant(0))));
    System.out.println(ev.evaluate(x6.some().implies(x5.gt(IntConstant.constant(0))).not()));
    System.out.println(ev.evaluate(x7));
}
Also used : TupleSet(kodkod.instance.TupleSet) Solver(kodkod.engine.Solver) Instance(kodkod.instance.Instance) Bounds(kodkod.instance.Bounds) IntExpression(kodkod.ast.IntExpression) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Evaluator(kodkod.engine.Evaluator) LinkedList(java.util.LinkedList) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) Solution(kodkod.engine.Solution)

Example 15 with Instance

use of kodkod.instance.Instance 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)

Aggregations

Instance (kodkod.instance.Instance)25 Formula (kodkod.ast.Formula)17 Relation (kodkod.ast.Relation)15 TupleFactory (kodkod.instance.TupleFactory)15 Universe (kodkod.instance.Universe)14 TupleSet (kodkod.instance.TupleSet)13 Bounds (kodkod.instance.Bounds)12 ArrayList (java.util.ArrayList)8 Variable (kodkod.ast.Variable)8 Evaluator (kodkod.engine.Evaluator)8 IntExpression (kodkod.ast.IntExpression)6 Solution (kodkod.engine.Solution)6 Expression (kodkod.ast.Expression)5 Tuple (kodkod.instance.Tuple)5 Decl (kodkod.ast.Decl)4 HashSet (java.util.HashSet)3 Solver (kodkod.engine.Solver)3 A4Reporter (edu.mit.csail.sdg.alloy4.A4Reporter)1 Err (edu.mit.csail.sdg.alloy4.Err)1 UniqueNameGenerator (edu.mit.csail.sdg.alloy4.UniqueNameGenerator)1