kodkod.engine.Evaluator Java Examples

The following examples show how to use kodkod.engine.Evaluator. 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: BlockedNQueens2.java    From kodkod with MIT License 6 votes vote down vote up
/**
	 * Prints the given solution using the given options to the console
	 */
	void print(Instance instance, Options options) {
		final Evaluator eval = new Evaluator(instance, options);
		final int n = instance.universe().size();
		for(int i = 0; i < n; i++) { 
			Expression x = IntConstant.constant(i).toExpression();
			for(int j = 0; j < n; j++) { 
				Expression y = IntConstant.constant(j).toExpression();
				if (eval.evaluate(x.product(y).in(queen))) { 
					System.out.print(" Q");
				} else {
					System.out.print(" .");
				}
			}
			System.out.println();
		}
//		System.out.println(instance); 
	}
 
Example #2
Source File: MagicSeries.java    From kodkod with MIT License 6 votes vote down vote up
private void print(Solution sol, Solver s) { 
	if (sol.instance()==null)
		System.out.println(sol);
	else {
		System.out.println(sol.outcome());
		System.out.println(sol.stats());
		final Evaluator eval = new Evaluator(sol.instance(), s.options());
		final Relation r = Relation.unary("r");
		final TupleFactory f = sol.instance().universe().factory();
		for(Object atom : f.universe()) { 
			eval.instance().add(r,  f.setOf(atom));
			System.out.print(atom + "->" + eval.evaluate(r.join(el).sum()) + "; ");
		}
		System.out.println();
	}
}
 
Example #3
Source File: EvaluatorTest.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();

    URL resource = getClass().getResource(SOLUTION);

    evaluator = new Evaluator(InstanceCreator.getInstance(resource.openStream()));
    Map<String,Relation> nameToRelation = new HashMap<String,Relation>();
    for (Relation r : evaluator.instance().relations()) {
        nameToRelation.put(r.name(), r);
    }
    univ = relation(nameToRelation, UNIV_PATH, "univ");
    hilary = relation(nameToRelation, PATH, "Hilary");
    jocelyn = relation(nameToRelation, PATH, "Jocelyn");
    person = relation(nameToRelation, PATH, "Person");
    spouse = relation(nameToRelation, PATH, "spouse");
    shaken = relation(nameToRelation, PATH, "shaken");
}
 
Example #4
Source File: BlockedNQueens.java    From kodkod with MIT License 6 votes vote down vote up
/**
	 * Prints the given solution using the given options to the console
	 */
	void print(Instance instance, Options options) {
		final Evaluator eval = new Evaluator(instance, options);
		final int n = instance.tuples(queen).size();
		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 #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++) { 
		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 #7
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 #8
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 #9
Source File: Transpose4x4UnaryL.java    From kodkod with MIT License 5 votes vote down vote up
/** Converts the given array of singleton integer relations to an array of ints. */
private final static int[][] toArray2D(Evaluator eval, Expression[][] r) {
	final int[][] ret = new int[r.length][];
	for(int i = 0; i < r.length; i++) {
		ret[i] = toArray(eval, r[i]);
	}
	return ret;
}
 
Example #10
Source File: IntConstraints.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Prints the solution to the screen.
 */
private final void print(Solution sol, Options options) {
    System.out.println(sol.stats());
    final Evaluator eval = new Evaluator(sol.instance(), options);
    final long mask = -1L >>> 32;
    for (int i = 0; i < 1000; i++) {
        long min = (low + 10 * i) & mask, max = (min + 10) & mask;
        long result = eval.evaluate(var[i].sum()) & mask;
        System.out.println(min + " <= [var_" + (i + 1) + "=" + result + "] <= " + max);
    }
}
 
Example #11
Source File: Transpose4x4UnaryLR.java    From kodkod with MIT License 5 votes vote down vote up
/** Converts the given array of singleton integer relations to an array of ints. */
private final static int[] toArray(Evaluator eval, Expression... r) {
	final int[] ret = new int[r.length];
	for(int i = 0; i < r.length; i++) {
		final TupleSet ts = eval.evaluate(r[i]);
		assert ts.arity() == 1 && ts.size() == 1;
		ret[i] = (Integer) ts.iterator().next().atom(0);
	}
	return ret;
}
 
Example #12
Source File: Transpose4x4UnaryLR.java    From kodkod with MIT License 5 votes vote down vote up
/** Converts the given array of singleton integer relations to an array of ints. */
private final static int[][] toArray2D(Evaluator eval, Expression[][] r) {
	final int[][] ret = new int[r.length][];
	for(int i = 0; i < r.length; i++) {
		ret[i] = toArray(eval, r[i]);
	}
	return ret;
}
 
Example #13
Source File: HamiltonianCycle2.java    From kodkod with MIT License 5 votes vote down vote up
/**
	 * Usage: examples.classicnp.HamiltonianCycle2 <graph file> <DIMACS | ASP> <EXT | LOG>
	 */
	public static void main(String[] args) {
		if (args.length!=3)
			usage();
		final HamiltonianCycle2 model;
		if ("LOG".equals(args[2].toUpperCase())) {
		  model = logEncoding(args[0], Enum.valueOf(Graph.Format.class, args[1].toUpperCase()));
		} else if ("EXT".equals(args[2].toUpperCase())) {
		  model = extEncoding(args[0], Enum.valueOf(Graph.Format.class, args[1].toUpperCase()));
		} else {
			usage();
			model = null;
		}
		final Formula f = model.cycleDefinition();
		final Bounds b = model.bounds();
		System.out.println(f);
		System.out.println(b);
		final Solver solver = new Solver();
		solver.options().setSolver(SATFactory.MiniSat);
		solver.options().setReporter(new ConsoleReporter());
//		solver.options().setFlatten(false);
		final Solution s = solver.solve(f,b);
		System.out.println(s.outcome());
		System.out.println(s.stats());
		if (s.instance()!=null) {
			final Evaluator eval = new Evaluator(s.instance(), solver.options());
			final Expression[] dec = model.pts;
			System.out.print(eval.evaluate(dec[0]));
			for(int i = 1; i < dec.length; i++) { 
				System.out.print(" -> " + eval.evaluate(dec[i]));
			}
			System.out.println();
		}
	}
 
Example #14
Source File: RegressionTests.java    From kodkod with MIT License 5 votes vote down vote up
@Test
public final void testFelix_01032007() {
	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);

	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")));

	Expression set=IntConstant.constant(8).toExpression();

	Solver solver = new Solver();
	solver.options().setSolver(SATFactory.DefaultSAT4J);
	solver.options().setBitwidth(4);
	solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
	Solution sol = solver.solve(set.some(), bounds);

	Evaluator eval = new Evaluator(sol.instance(), solver.options());
	TupleSet ts = eval.evaluate(set);
	assertFalse(ts.size()==0);

}
 
Example #15
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 #16
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 #17
Source File: IntTest.java    From kodkod with MIT License 5 votes vote down vote up
private final void testBinOp(IntOperator op, IntExpression ei, IntExpression ej, int i, int j, int result, int mask) {
	final IntExpression e = ei.compose(op, ej);
	final Formula f = ei.eq(constant(i)).and(ej.eq(constant(j))).and(e.eq(constant(result)));
	final Solution s = solve(f);
	//if (s.instance()==null)
	//	System.out.println(f + " no solution!");
	assertNotNull(s.instance());
	final Evaluator eval = new Evaluator(s.instance(), solver.options());
	//System.out.println(f + ", expected: " + (result & mask) + ", actual: " + (eval.evaluate(e) & mask));
	assertEquals(result & mask, eval.evaluate(e) & mask);
	
}
 
Example #18
Source File: IntTest.java    From kodkod with MIT License 5 votes vote down vote up
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);

	assertNotNull(s.instance());
	final Evaluator eval = new Evaluator(s.instance(), solver.options());
	assertEquals(result & mask, eval.evaluate(e) & mask);
	
}
 
Example #19
Source File: IntTest.java    From kodkod with MIT License 5 votes vote down vote up
private final void testCompOp(IntCompOperator op, IntExpression ei, IntExpression ej, int i, int j, boolean result) {
	final Formula e = ei.compare(op, ej);
	final Formula f = ei.eq(constant(i)).and(ej.eq(constant(j))).and(result ? e : e.not());
	final Solution s = solve(f);
	assertNotNull(s.instance());
	final Evaluator eval = new Evaluator(s.instance(), solver.options());
	assertFalse(result ^ eval.evaluate(e));
	
}
 
Example #20
Source File: IncrementalSolverTest.java    From kodkod with MIT License 5 votes vote down vote up
private Solution checkModel(Solution s, Formula...formulas) {
	final Instance i = s.instance();
	assertNotNull(i);
	final Evaluator eval = new Evaluator(i, solver.options());
	assertTrue(eval.evaluate(Formula.and(formulas)));
	return s;
}
 
Example #21
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 #22
Source File: IntTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private final void testCompOp(IntCompOperator op, IntExpression ei, IntExpression ej, int i, int j, boolean result) {
    final Formula e = ei.compare(op, ej);
    final Formula f = ei.eq(constant(i)).and(ej.eq(constant(j))).and(result ? e : e.not());
    final Solution s = solve(f);
    if (overflows(ei, i, 0) || overflows(ej, j, 0)) {
        assertNull(f.toString(), s.instance());
    } else {
        assertNotNull(f.toString(), s.instance());
        final Evaluator eval = new Evaluator(s.instance(), solver.options());
        assertFalse(result ^ eval.evaluate(e));
    }
}
 
Example #23
Source File: Transpose4x4UnaryL.java    From kodkod with MIT License 5 votes vote down vote up
/** Converts the given array of singleton integer relations to an array of ints. */
private final static int[] toArray(Evaluator eval, Expression... r) {
	final int[] ret = new int[r.length];
	for(int i = 0; i < r.length; i++) {
		final TupleSet ts = eval.evaluate(r[i]);
		assert ts.arity() == 1 && ts.size() == 1;
		ret[i] = (Integer) ts.iterator().next().atom(0);
	}
	return ret;
}
 
Example #24
Source File: IntTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #25
Source File: IntTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private final void testBinOp(IntOperator op, IntExpression ei, IntExpression ej, int i, int j, int result, int realResult, int mask) {
    final IntExpression e = ei.compose(op, ej);
    final Formula f = ei.eq(constant(i)).and(ej.eq(constant(j))).and(e.eq(constant(result)));
    final Solution s = solve(f);
    Instance inst = s.instance();
    if (overflows(op, ei, ej, i, j, realResult)) {
        assertNull(f.toString(), inst);
    } else {
        assertNotNull(f.toString(), inst);
        final Evaluator eval = new Evaluator(inst, solver.options());
        assertEquals(f.toString(), result & mask, eval.evaluate(e) & mask);
    }
}
 
Example #26
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 #27
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
public final void testFelix_01032007() {
    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);

    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")));

    Expression set = IntConstant.constant(8).toExpression();

    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(4);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
    Solution sol = solver.solve(set.some(), bounds);

    assertNotNull("expected SATISFIABLE but was " + sol.outcome(), sol.instance());

    Evaluator eval = new Evaluator(sol.instance(), solver.options());
    TupleSet ts = eval.evaluate(set);
    assertFalse(ts.size() == 0);

}
 
Example #28
Source File: ConfigAssure.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Displays an instance obtained with the given options.
 *
 * @requires inst != null and opt != null
 */
private final void display(Instance inst, Options opt) {
    final Universe univ = inst.universe();
    final Evaluator eval = new Evaluator(inst, opt);
    final TupleFactory factory = univ.factory();
    final List<TupleSet> subnets = new ArrayList<TupleSet>();

    System.out.println("address\t\tnetwork id\tmask\tdevice-interface");
    for (int i = 0, ports = univ.size() - 32; i < ports; i++) {
        final Object atom = univ.atom(i);
        final Relation p = Relation.unary(atom.toString());
        inst.add(p, factory.setOf(atom));

        System.out.print(toQuad(eval.evaluate(addr(p))) + "\t");
        System.out.print(toQuad(eval.evaluate(netid(p))) + "\t");
        System.out.print(eval.evaluate(implicitMask(p)) + "\t");
        System.out.println(p);

        final TupleSet members = eval.evaluate(subnet(p));
        if (!members.isEmpty())
            subnets.add(members);
    }

    System.out.println("\nsubnets:");
    for (TupleSet sub : subnets) {
        System.out.println(sub);
    }

}
 
Example #29
Source File: Viktor.java    From org.alloytools.alloy with Apache License 2.0 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 #30
Source File: IncrementalOverflowEnumTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Override
protected Iterator<Solution> solveAll(final Formula formula) {
    final IncrementalSolver solver = IncrementalSolver.solver(options);
    return new Iterator<Solution>() {

        Solution sol;

        @Override
        public boolean hasNext() {
            return sol == null || sol.sat();
        }

        @Override
        public Solution next() {
            if (sol == null) {
                sol = solver.solve(formula, bounds);
            } else {
                Evaluator ev = new Evaluator(sol.instance());
                int a = evalInt(ev, op1);
                int b = evalInt(ev, op2);
                int r = evalInt(ev, ret);
                Formula f = op1.eq(IntConstant.constant(a).toExpression()).and(op2.eq(IntConstant.constant(b).toExpression())).and(ret.eq(IntConstant.constant(r).toExpression())).not();
                sol = solver.solve(f, new Bounds(factory.universe()));
            }
            return sol;
        }

        @Override
        public void remove() {
            throw new RuntimeException("Not supported");
        }

    };
}