kodkod.instance.Instance Java Examples

The following examples show how to use kodkod.instance.Instance. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NQueens.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Prints the given solution
 */
void print(Instance instance, Options options) { 
	final Evaluator eval = new Evaluator(instance, options);
	for(int i = 0; i < n; i++) { 
		Expression ci = IntConstant.constant(i).toExpression();
		for(int j = 0; j < n; j++) { 
			Expression cj = IntConstant.constant(j).toExpression();
			if (eval.evaluate(x.join(ci).intersection(y.join(cj)).some())) { 
				System.out.print(" Q");
			} else {
				System.out.print(" .");
			}
		}
		System.out.println();
	}
}
 
Example #2
Source File: FloatTestBase.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
private Instance instance(Formula f, Set<Relation> vars) {
	int msb = bitWidth - 1;

	Universe u = new Universe(atoms);
	TupleFactory tf = u.factory();
	Bounds b = new Bounds(u);

	for(int i = 0; i < msb; i++) {
		Integer v = Integer.valueOf(1<<i);
		b.boundExactly(v.intValue(), tf.setOf(v));
	}
	b.boundExactly(-(1<<msb), tf.setOf(Integer.valueOf(-(1<<msb))));

	Set<Tuple> digits = HashSetFactory.make();
	for(Object o : atoms) {
		digits.add(tf.tuple(o));
	}
	for(Relation r : vars) {
		b.bound(r, tf.setOf(digits));
	}
	
	Solver solver = new Solver(options);
	
	return solver.solve(f, b).instance();
}
 
Example #3
Source File: Drivers.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public static TupleSet check(UniverseFactory uf,
		Pair<Formula, Pair<Formula, Formula>> answer,
		String relation)
		throws URISyntaxException {
	Instance rels = check(uf, answer);
	if (rels != null) {
		for(Relation r : rels.relations()) {
			if (r.toString().equals(relation)) {
				return rels.tuples(r);
			}
		}
		return null;
	} else {
		return null;
	}
}
 
Example #4
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
public final void testEmina_01232006() {
    final List<String> atoms = new ArrayList<String>(5);
    for (int i = 0; i < 5; i++)
        atoms.add("a" + i);
    final Universe u = new Universe(atoms);
    final TupleFactory tf = u.factory();

    final Relation r1 = Relation.unary("r1"), r2 = Relation.binary("r2"), r3 = Relation.ternary("r3");
    final Bounds b = new Bounds(u);
    final TupleSet r2Bound = tf.noneOf(2);
    for (int i = 0; i < 4; i++)
        r2Bound.add(tf.tuple(atoms.get(i), atoms.get(i)));
    r2Bound.add(tf.tuple("a4", "a1"));
    r2Bound.add(tf.tuple("a4", "a2"));
    b.bound(r2, r2Bound);
    b.bound(r1, tf.setOf("a0", "a3"), tf.setOf("a0", "a3", "a4"));
    b.bound(r3, tf.setOf(tf.tuple("a0", "a0", "a0"), tf.tuple("a3", "a3", "a3")));
    final Formula f = r1.product(r2).in(r3);

    final Instance instance = solver.solve(f, b).instance();
    assertTrue((new Evaluator(instance)).evaluate(f));
    // System.out.println(instance);
    // System.out.println((new Evaluator(instance)).evaluate(f ));

}
 
Example #5
Source File: NQueens.java    From kodkod with MIT License 6 votes vote down vote up
/**
		 * Prints the given solution
		 */
		void print(Instance instance, Options options) {
			final Evaluator eval = new Evaluator(instance, options);
			for(int i = 0; i < n; i++) { 
				Expression ci = IntConstant.constant(i).toExpression();
				for(int j = 0; j < n; j++) { 
					Expression cj = IntConstant.constant(j).toExpression();
					if (eval.evaluate(x.join(ci).intersection(y.join(cj)).some())) { 
						System.out.print(" Q");
					} else {
						System.out.print(" .");
					}
				}
				System.out.println();
			}
//			System.out.println(instance); 
		}
 
Example #6
Source File: NQueens.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Prints the given solution
 */
void print(Instance instance, Options options) { 
	final Evaluator eval = new Evaluator(instance, options);
	for(int i = 0; i < n; i++) { 
		IntExpression ci = IntConstant.constant(i);
		for(int j = 0; j < n; j++) { 
			IntExpression cj = IntConstant.constant(j);
			Variable q = Variable.unary("q");
			if (eval.evaluate(q.join(x).sum().eq(ci).and(q.join(y).sum().eq(cj)).forSome(q.oneOf(queen)))) { 
				System.out.print(" Q");
			} else {
				System.out.print(" .");
			}
		}
		System.out.println();
	}
}
 
Example #7
Source File: TranslatorTest.java    From kodkod with MIT License 5 votes vote down vote up
private final void testIntersectionMultiplicity(Multiplicity mult, Relation p, Relation q, Tuple intersection) {
	final Instance m = solve(p.intersection(q).apply(mult));
	assertNotNull(m);
	final Set<Tuple> ps = m.tuples(p), qs = m.tuples(q); 
	assertFalse(ps.isEmpty());
	assertFalse(qs.isEmpty());
	assertTrue(ps.contains(intersection));
	assertTrue(qs.contains(intersection));
}
 
Example #8
Source File: JenaUtil.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static RDFNode fromAtom(Model m, Object o, BasicUniverse u, Instance t2) {
	if (o instanceof Pair<?,?>) {
		return fromLiteral(m, (Pair<String,Object>) o, u, t2);
	} else if (o.toString().startsWith("_:")) {
		return m.createResource(new AnonId(o.toString()));
	} else {
		return m.createResource(o.toString());
	}
}
 
Example #9
Source File: SymmetryBreakingTest.java    From kodkod with MIT License 5 votes vote down vote up
private Instance solve(Formula f, Bounds b) {
	
		final Solution sol = solver.solve(f, b);
		final Statistics stats = sol.stats();
		pVars = stats.primaryVariables();
		iVars = stats.variables() - pVars;
		clauses = stats.clauses();
		return sol.instance();
	
}
 
Example #10
Source File: RegressionTests.java    From kodkod with MIT License 5 votes vote down vote up
@Test
public final void testGreg_11232005() {
	final List<String> atoms = new ArrayList<String>(3);
	atoms.add("-1"); atoms.add("0"); atoms.add("1");
	final Universe u = new Universe(atoms);
	final TupleFactory t = u.factory();

	final Relation inc = Relation.binary("inc"), add = Relation.ternary("add"), 
	one = Relation.unary("1"), param0 = Relation.unary("param0"), 
	ints = Relation.unary("int");

	// (one param0 && ((1 . (param0 . add)) in (param0 . ^inc)))
	final Formula f = param0.one().and((one.join(param0.join(add))).in(param0.join(inc.closure())));

	final Bounds b = new Bounds(u);

	b.bound(param0, t.allOf(1));
	b.boundExactly(one, t.setOf(t.tuple("1")));
	b.boundExactly(ints, t.allOf(1));
	b.boundExactly(inc, t.setOf(t.tuple("-1","0"), t.tuple("0","1")));
	// [1, 1, -1], [1, -1, 0], [1, 0, 1], [-1, 1, 0], [-1, -1, 1],
	// [-1, 0, -1], [0, 1, 1], [0, -1, -1], [0, 0, 0]]
	b.boundExactly(add, t.setOf(t.tuple("1","1","-1"), t.tuple("1","-1","0"), t.tuple("1","0","1"), 
			t.tuple("-1","1","0"), t.tuple("-1","-1","1"), t.tuple("-1","0","-1"), 
			t.tuple("0","1","1"), t.tuple("0","-1","-1"), t.tuple("0","0","0")));

	//		System.out.println(f);
	//		System.out.println(b);


	final Instance instance = solver.solve(f, b).instance();
	assertTrue((new Evaluator(instance)).evaluate(f));
	//			System.out.println(instance);
	//			System.out.println((new Evaluator(instance)).evaluate(f  ));

}
 
Example #11
Source File: SkolemizationTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private final void testDeepSkolems(Multiplicity mult) {
    final Variable va = Variable.unary("va");
    final Variable vb = Variable.unary("vb");
    final Variable vc = Variable.unary("vc");
    final Variable vd = Variable.unary("vd");
    final Set<String> skolems = new HashSet<String>(4);

    Decl da1 = va.oneOf(r1a);
    Decl db = vb.declare(mult, r1b);
    Decl dc = vc.declare(mult, r1a);
    Decl dc1 = vc.oneOf(r1a);
    Decl dd = vd.declare(mult, r1b);
    Decl dd1 = vd.oneOf(r1b);

    skolems.add("$" + vb.name());

    Instance inst = solve(va.in(vb.join(r2b)).forSome(db).forAll(da1));
    assertSkolems(bounds, inst, skolems);

    skolems.add("$" + vc.name());
    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forSome(db).forAll(da1).forSome(dc).forAll(dd1));
    assertSkolems(bounds, inst, skolems);

    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forSome(db).forSome(dc).forAll(da1.and(dd1)));
    assertSkolems(bounds, inst, skolems);

    skolems.add("$" + vd.name());
    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forSome(db).forAll(da1).forSome(dc).not().forAll(dd).not());

    assertSkolems(bounds, inst, skolems);

    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forAll(dc).forAll(db).forSome(da1).not().forAll(dd1));
    skolems.remove("$" + vd.name());
    assertSkolems(bounds, inst, skolems);

    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forSome(db).forAll(dc1).forAll(da1).forAll(dd1));
    skolems.remove("$" + vc.name());
    assertSkolems(bounds, inst, skolems);

}
 
Example #12
Source File: SkolemizationTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private final void testNoSkolems(Decls d, Formula f) {
    Instance inst = solve(f.not());
    assertEquals(bounds.relations(), inst.relations());

    inst = solve(f.or(r2a.in(r2b)));
    assertEquals(bounds.relations(), inst.relations());

    inst = solve(f.iff(r2a.in(r2b)));
    assertEquals(bounds.relations(), inst.relations());

    inst = solve(r2a.in(r2b).implies(f));
    assertEquals(bounds.relations(), inst.relations());

}
 
Example #13
Source File: RegressionTests.java    From kodkod with MIT License 5 votes vote down vote up
@Test
public final void testVincent_02162006() {
	// set ups universe of atoms [1..257]
	final List<Integer> atoms = new ArrayList<Integer>();

	// change this to 256, and the program works
	for (int i=0; i<257; i++) {
		atoms.add(i+1);
	}

	final Universe universe = new Universe(atoms);
	final Bounds bounds = new Bounds(universe);
	final TupleFactory factory = universe.factory();

	// oneRel is bounded to the Integer 1
	final Relation oneRel = Relation.unary("oneRel");

	// rel can contain anything
	final Relation rel = Relation.unary("rel");

	bounds.bound(oneRel, factory.setOf(factory.tuple(atoms.get(0))),
			factory.setOf(factory.tuple(atoms.get(0))));
	bounds.bound(rel, factory.allOf(1));

	// constraint: oneRel in rel
	Formula formula = oneRel.in(rel);

	// solve      

	final Instance instance = solver.solve(formula, bounds).instance();
	assertNotNull(instance);
	//System.out.println(instance);



}
 
Example #14
Source File: ConsoleReporter.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Override
public void holFixpointIncrementingOutcome(HOLTranslation tr, Instance next) {
    if (next != null)
        System.out.println(String.format("  [%s]   climbed one step", tr));
    else
        System.out.println(String.format("  [%s]   fixpoint reached", tr));
}
 
Example #15
Source File: TranslatorTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private final void testIntersectionMultiplicity(Multiplicity mult, Relation p, Relation q, Tuple intersection) {
    final Instance m = solve(p.intersection(q).apply(mult));
    assertNotNull(m);
    final Set<Tuple> ps = m.tuples(p), qs = m.tuples(q);
    assertFalse(ps.isEmpty());
    assertFalse(qs.isEmpty());
    assertTrue(ps.contains(intersection));
    assertTrue(qs.contains(intersection));
}
 
Example #16
Source File: JenaUtil.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public static void addTupleSet(Dataset dataset, TupleSet tt, BasicUniverse u, Instance t2) {
	if (tt != null) {
		for(Tuple t : tt) {
			Object graph = t.atom(0);
			Model m = QuadTableRelations.defaultGraph.equals(graph)? dataset.getDefaultModel(): dataset.getNamedModel(graph.toString());
			m.add(fromTuple(m, t, u, t2));
		}
	}
}
 
Example #17
Source File: SkolemizationTest.java    From kodkod with MIT License 5 votes vote down vote up
private final void testNoSkolems(Decls d, Formula f) {
	Instance inst = solve(f.not());
	assertEquals(bounds.relations(), inst.relations());
	
	inst = solve(f.or(r2a.in(r2b)));
	assertEquals(bounds.relations(), inst.relations());
	
	inst = solve(f.iff(r2a.in(r2b)));
	assertEquals(bounds.relations(), inst.relations());
	
	inst = solve(r2a.in(r2b).implies(f));
	assertEquals(bounds.relations(), inst.relations());
	
}
 
Example #18
Source File: Quasigroups7.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private static void displayOp(Instance instance, Relation op) {
    System.out.println("\n" + op + ":");
    final Iterator<Tuple> iter = instance.tuples(op).iterator();
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 7; j++) {
            System.out.print(iter.next().atom(2));
            System.out.print("\t");
        }
        System.out.println();
    }
}
 
Example #19
Source File: Viktor.java    From kodkod with MIT License 5 votes vote down vote up
private final void display(Instance instance, Options options) {
		final Evaluator eval = new Evaluator(instance, options);
		for(int i = 0; i < 2; i++) {
			System.out.print("                      | ");
			System.out.print(instance.tuples(x[i]).indexView().min());
			System.out.println(" |");
		}
		
		for(int i = 0; i < rows; i++) {
			System.out.print("| ");
			for(int j = 0; j < cols; j++) {
				System.out.print(instance.tuples(a[i][j]).isEmpty() ? 0 : 1);
				System.out.print(" ");
			}
			System.out.print(i==1 ? "| * | " : "|   | ");
			System.out.print(instance.tuples(x[i+2]).indexView().min());
			System.out.print(i==1 ? " | = | " : " |   | ");
			System.out.print(eval.evaluate(b[i]));
			System.out.println(" |");
		}
		
		for(int i = 5; i < 8; i++) {
			System.out.print("                      | ");
			System.out.print(instance.tuples(x[i]).indexView().min());
			System.out.println(" |");
		}
		
//		for(int i = 0; i < 3; i++)
//			System.out.println(b[i]);
//		
//		for(int i = 0; i < rows; i++) {
//			for(int j = 0 ; j < 8; j++) {
//				IntExpression e0 = x[j].sum();
//				IntExpression e1 = a[i][j].some().thenElse(e0, IntConstant.constant(0));
//				System.out.println(e0 + " : " + eval.evaluate(e0));
//				System.out.println(e1 + " : " + eval.evaluate(e1));
//			}
//		}
	}
 
Example #20
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
public final void testGreg_11232005() {
    final List<String> atoms = new ArrayList<String>(3);
    atoms.add("-1");
    atoms.add("0");
    atoms.add("1");
    final Universe u = new Universe(atoms);
    final TupleFactory t = u.factory();

    final Relation inc = Relation.binary("inc"), add = Relation.ternary("add"), one = Relation.unary("1"),
                    param0 = Relation.unary("param0"), ints = Relation.unary("int");

    // (one param0 && ((1 . (param0 . add)) in (param0 . ^inc)))
    final Formula f = param0.one().and((one.join(param0.join(add))).in(param0.join(inc.closure())));

    final Bounds b = new Bounds(u);

    b.bound(param0, t.allOf(1));
    b.boundExactly(one, t.setOf(t.tuple("1")));
    b.boundExactly(ints, t.allOf(1));
    b.boundExactly(inc, t.setOf(t.tuple("-1", "0"), t.tuple("0", "1")));
    // [1, 1, -1], [1, -1, 0], [1, 0, 1], [-1, 1, 0], [-1, -1, 1],
    // [-1, 0, -1], [0, 1, 1], [0, -1, -1], [0, 0, 0]]
    b.boundExactly(add, t.setOf(t.tuple("1", "1", "-1"), t.tuple("1", "-1", "0"), t.tuple("1", "0", "1"), t.tuple("-1", "1", "0"), t.tuple("-1", "-1", "1"), t.tuple("-1", "0", "-1"), t.tuple("0", "1", "1"), t.tuple("0", "-1", "-1"), t.tuple("0", "0", "0")));

    // System.out.println(f);
    // System.out.println(b);

    final Instance instance = solver.solve(f, b).instance();
    assertTrue((new Evaluator(instance)).evaluate(f));
    // System.out.println(instance);
    // System.out.println((new Evaluator(instance)).evaluate(f ));

}
 
Example #21
Source File: RegressionTests.java    From kodkod with MIT License 5 votes vote down vote up
@Test
public final void testEmina_01232006() {
	final List<String> atoms = new ArrayList<String>(5);
	for(int i = 0; i < 5; i++) 
		atoms.add("a"+i);
	final Universe u = new Universe(atoms);
	final TupleFactory tf = u.factory();

	final Relation r1 = Relation.unary("r1"), 
	r2 = Relation.binary("r2"), 
	r3 = Relation.ternary("r3");
	final Bounds b = new Bounds(u);
	final TupleSet r2Bound = tf.noneOf(2);
	for(int i = 0; i < 4; i++)
		r2Bound.add(tf.tuple(atoms.get(i), atoms.get(i)));
	r2Bound.add(tf.tuple("a4", "a1"));
	r2Bound.add(tf.tuple("a4", "a2"));
	b.bound(r2, r2Bound);
	b.bound(r1, tf.setOf("a0", "a3"), tf.setOf("a0", "a3", "a4"));
	b.bound(r3, tf.setOf(tf.tuple("a0","a0","a0"), tf.tuple("a3","a3","a3")));
	final Formula f = r1.product(r2).in(r3);


	final Instance instance = solver.solve(f, b).instance();
	assertTrue((new Evaluator(instance)).evaluate(f));
	//			System.out.println(instance);
	//			System.out.println((new Evaluator(instance)).evaluate(f  ));

}
 
Example #22
Source File: ALG195_1.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Prints the values of the op1, op2, and h1-h7 relations
 * to standard out.
 */
void display(Instance instance) {
	displayOp(instance, op1);
	displayOp(instance, op2);
	for(int i = 0; i < 7; i++) {
		System.out.println("\n"+h[i]+":");
		System.out.println(instance.tuples(h[i]));
	}
}
 
Example #23
Source File: A4Solution.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/** 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 + ">");
    }
}
 
Example #24
Source File: ALG195_1.java    From kodkod with MIT License 5 votes vote down vote up
private static void displayOp(Instance instance, Relation op) {
	System.out.println("\n"+op+":");
	final Iterator<Tuple> iter = instance.tuples(op).iterator();
	for(int i = 0; i < 7; i++) {
		for(int j = 0; j < 7; j++) {
			System.out.print(iter.next().atom(2));
			System.out.print("\t");
		}
		System.out.println();
	}
}
 
Example #25
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public final void testGreg_01192006() {
    // circular linked list
    Relation Entry = Relation.unary("Entry");
    Relation head = Relation.unary("head");
    Relation next = Relation.binary("next");
    Formula nextFun = next.function(Entry, Entry);

    // bijection between indices and entries in linked list
    Relation Index = Relation.unary("Index");
    Relation index2Entry = Relation.binary("index2Entry");
    Expression entries = head.join(next.closure());
    Variable e = Variable.unary("e");
    Expression preImage = index2Entry.join(e);
    Formula index2EntryBij = e.in(entries).implies(preImage.one()).and(e.in(entries).not().implies(preImage.no())).forAll(e.oneOf(Entry));

    // try to force list to have three distinct entries
    Variable e1 = Variable.unary("e1");
    Variable e2 = Variable.unary("e2");
    Variable e3 = Variable.unary("e3");
    Formula threeEntries = e1.eq(e2).not().and(e1.eq(e3).not()).and(e2.eq(e3).not()).forSome(e1.oneOf(entries).and(e2.oneOf(entries).and(e3.oneOf(entries))));
    Formula simulate = head.one().and(nextFun).and(index2EntryBij).and(threeEntries);

    Object Entry0 = "Entry0";
    Object Entry1 = "Entry1";
    Object Entry2 = "Entry2";
    Object Entry3 = "Entry3";
    Object Index0 = "Index0";
    Object Index1 = "Index1";
    Object Index2 = "Index2";
    Object Index3 = "Index3";

    Universe univ = new Universe(Arrays.asList(Entry0, Entry1, Entry2, Entry3, Index0, Index1, Index2, Index3));
    TupleFactory factory = univ.factory();
    TupleSet entryTuples = factory.setOf(Entry0, Entry1, Entry2, Entry3);
    TupleSet indexTuples = factory.setOf(Index0, Index1, Index2, Index3);

    Instance instance = new Instance(univ);
    instance.add(Entry, entryTuples);
    instance.add(head, factory.setOf(Entry0));
    instance.add(Index, indexTuples);

    Tuple next0 = factory.tuple(Entry0, Entry1);
    Tuple next1 = factory.tuple(Entry1, Entry2);
    Tuple next2 = factory.tuple(Entry2, Entry3);
    Tuple next3 = factory.tuple(Entry3, Entry0);
    instance.add(next, factory.setOf(next0, next1, next2, next3));

    Tuple i2e0 = factory.tuple(Index0, Entry0);
    Tuple i2e1 = factory.tuple(Index1, Entry1);
    Tuple i2e2 = factory.tuple(Index2, Entry2);
    Tuple i2e3 = factory.tuple(Index3, Entry3);
    instance.add(index2Entry, factory.setOf(i2e0, i2e1, i2e2, i2e3));

    Evaluator eval = new Evaluator(instance);
    assertTrue(eval.evaluate(simulate));

    Bounds bounds = new Bounds(univ);
    bounds.boundExactly(Entry, entryTuples);
    bounds.bound(head, entryTuples);
    bounds.bound(next, entryTuples.product(entryTuples));
    bounds.bound(Index, indexTuples);
    bounds.bound(index2Entry, indexTuples.product(entryTuples));
    // Solver solver = new Solver(SATSolverName.Default);

    // System.out.println(simulate);
    // System.out.println(bounds);
    // System.out.println(instance);
    instance = solver.solve(simulate, bounds).instance();
    // System.out.println(instance);
    assertNotNull(instance);

}
 
Example #26
Source File: RegressionTests.java    From kodkod with MIT License 4 votes vote down vote up
@Test
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);
}
 
Example #27
Source File: SymmetryBreakingTest.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
private Instance solve(Formula f) {
    return solve(f, bounds);
}
 
Example #28
Source File: TranslatorTest.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public final void testFlattening() {
    final List<String> atoms = new ArrayList<String>(9);
    atoms.add("-1");
    atoms.add("0");
    atoms.add("1");
    atoms.add("null");
    for (int i = 0; i < 5; i++) {
        atoms.add("m" + i);
    }
    final Universe u = new Universe(atoms);
    final TupleFactory t = u.factory();

    final Relation[] m = new Relation[5];
    for (int i = 0; i < 5; i++) {
        m[i] = Relation.unary("m" + i);
    }

    final Relation univ = Relation.unary("univ"), none = Relation.unary("none"), iden = Relation.binary("inden"),
                    mutant = Relation.unary("mutant"), inc = Relation.binary("inc"), add = Relation.ternary("add"),
                    i0 = Relation.unary("i0"), zero = Relation.unary("0"), one = Relation.unary("1"),
                    ints = Relation.unary("int");

    final Bounds b = new Bounds(u);

    b.boundExactly(univ, t.allOf(1));
    b.boundExactly(none, t.noneOf(1));
    TupleSet idenSet = t.noneOf(2);
    for (String atom : atoms) {
        idenSet.add(t.tuple(atom, atom));
    }
    b.boundExactly(iden, idenSet);

    b.bound(mutant, t.range(t.tuple("m0"), t.tuple("m4")));
    for (int i = 0; i < 5; i++) {
        b.boundExactly(m[i], t.setOf("m" + i));
    }

    b.bound(i0, t.range(t.tuple("-1"), t.tuple("1")));
    b.boundExactly(zero, t.setOf(t.tuple("0")));
    b.boundExactly(one, t.setOf(t.tuple("1")));
    b.boundExactly(ints, t.allOf(1));
    b.boundExactly(inc, t.setOf(t.tuple("-1", "0"), t.tuple("0", "1")));

    // [1, 1, -1], [1, -1, 0], [1, 0, 1], [-1, 1, 0], [-1, -1, 1],
    // [-1, 0, -1], [0, 1, 1], [0, -1, -1], [0, 0, 0]]
    b.boundExactly(add, t.setOf(t.tuple("1", "1", "-1"), t.tuple("1", "-1", "0"), t.tuple("1", "0", "1"), t.tuple("-1", "1", "0"), t.tuple("-1", "-1", "1"), t.tuple("-1", "0", "-1"), t.tuple("0", "1", "1"), t.tuple("0", "-1", "-1"), t.tuple("0", "0", "0")));

    /*
     * ((one i0 && one mutant) && !((if (i0 in (0 . ^~inc)) then ((add . 0) . i0)
     * else i0) = (if !((if (mutant in m4) then (if (i0 in 0) then 1 else 0) else
     * (if (mutant in m3) then (if ((i0 = 0) || (i0 in (0 . ^~inc))) then 1 else 0)
     * else (if (mutant in m2) then (if (i0 in (0 . ^inc)) then 1 else 0) else (if
     * (mutant in m1) then (if ((i0 = 0) || (i0 in (0 . ^inc))) then 1 else 0) else
     * (if (mutant in m0) then (if !(i0 in 0) then 1 else 0) else (if (i0 in (0 .
     * ^~inc)) then 1 else 0)))))) in 0) then ((add . 0) . i0) else i0)))
     */

    final Formula[] cm = new Formula[5];
    for (int i = 0; i < 5; i++) {
        cm[i] = mutant.in(m[i]);
    }

    // (i0 in (0 . ^~inc))
    final Formula fs0 = i0.in(zero.join(inc.transpose().closure()));
    // (i0 in (0 . ^inc))
    final Formula fs1 = i0.in(zero.join(inc.closure()));
    // (i0 = 0)
    final Formula fs2 = i0.eq(zero);

    final Expression em0 = cm[0].thenElse(i0.in(zero).not().thenElse(one, zero), fs0.thenElse(one, zero));
    final Expression em1 = cm[1].thenElse((fs2.or(fs1)).thenElse(one, zero), em0);
    final Expression em2 = cm[2].thenElse(fs1.thenElse(one, zero), em1);
    final Expression em3 = cm[3].thenElse(fs2.or(fs0).thenElse(one, zero), em2);
    final Expression em4 = cm[4].thenElse(i0.in(zero).thenElse(one, zero), em3);

    final Expression es1 = add.join(zero).join(i0);
    final Expression expr2 = em4.in(zero).not().thenElse(es1, i0);
    final Expression expr1 = fs0.thenElse(es1, i0);

    final Formula f = i0.one().and(mutant.one()).and(expr1.eq(expr2).not());

    final Instance instance = solver.solve(f, b).instance();
    assertNotNull(instance);

    // System.out.println(instance);

}
 
Example #29
Source File: Drivers.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
public static Instance check(UniverseFactory uf,
		Pair<Formula, Pair<Formula, Formula>> answer)
		throws URISyntaxException {
	return check(uf, SATFactory.MiniSat, answer);
}
 
Example #30
Source File: ListViz.java    From kodkod with MIT License 4 votes vote down vote up
static final void printInstance(ListEncoding enc, Instance inst) { 
	if (inst == null) return;
	final Evaluator eval = new Evaluator(inst);
	
	System.out.println("-----------invariant state-----------");
	for(Relation r : new Relation[]{ enc.list, enc.node, enc.string, enc.data, enc.thisList }) {
		System.out.println(r + " = "+ eval.evaluate(r));
	}

	
	System.out.println("\n-----------trace-----------");
	System.out.println("assume invariants(this, next, data, head) && this.head != null && this.head.next != null : " + 
			eval.evaluate(enc.pre()));
	System.out.println("next = "+ eval.evaluate(enc.next));
	System.out.println("head = "+ eval.evaluate(enc.head));
	
	System.out.println("\nnearNode0 = "+ eval.evaluate(enc.nearNode0()));
	System.out.println("midNode0 = "+ eval.evaluate(enc.midNode0()));
	System.out.println("farNode0 = "+ eval.evaluate(enc.farNode0()));
	
	System.out.println("\nnext0 = "+ eval.evaluate(enc.next0()));
	
	System.out.println("\nguard0 (farNode0 != null) = "+ eval.evaluate(enc.guard0()));
	System.out.println("next1 = "+ eval.evaluate(enc.next1()));
	System.out.println("nearNode1 = "+ eval.evaluate(enc.nearNode1()));
	System.out.println("midNode1 = "+ eval.evaluate(enc.midNode1()));
	System.out.println("farNode1 = "+ eval.evaluate(enc.farNode1()));
	
	System.out.println("\nnext2 = "+ eval.evaluate(enc.next2()));
	System.out.println("nearNode2 = "+ eval.evaluate(enc.nearNode2()));
	System.out.println("midNode2 = "+ eval.evaluate(enc.midNode2()));
	System.out.println("farNode2 = "+ eval.evaluate(enc.farNode2()));
	System.out.println("assume loopGuard (farNode2 = null) : " + eval.evaluate(enc.loopGuard()));
	
	System.out.println("\nnext3 = "+ eval.evaluate(enc.next3()));
	System.out.println("head0 = "+ eval.evaluate(enc.head0()));
	
	System.out.println("\nassert invariants(this, next3, data, head0) &&\n"+
			"(let nodes = this.head.*next, nodes' = this.head0.*next3, ns = nodes - nil |\n" +
			"  nodes' = nodes and next3 & (ns -> ns) = ~(next & (ns -> ns))) : " +  eval.evaluate(enc.post()));
}