Java Code Examples for kodkod.ast.Relation#unary()

The following examples show how to use kodkod.ast.Relation#unary() . 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: 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 2
Source File: ReductionAndProofTest.java    From kodkod with MIT License 6 votes vote down vote up
public ReductionAndProofTest() {
	this.solver = new Solver();
	solver.options().setLogTranslation(2);
	solver.options().setSolver(SATFactory.MiniSatProver);
	List<String> atoms = new ArrayList<String>(USIZE);
	for (int i = 0; i < USIZE; i++) {
		atoms.add(""+i);
	}
	final Universe universe = new Universe(atoms);
	this.factory = universe.factory();
	this.a = Relation.unary("a");
	this.b = Relation.unary("b");
	this.a2b = Relation.binary("a2b");
	this.b2a = Relation.binary("b2a");
	this.first = Relation.unary("first");
	this.last = Relation.unary("last");
	this.ordered = Relation.unary("ordered");
	this.total = Relation.binary("total");
	this.bounds = new Bounds(universe);
}
 
Example 3
Source File: EnumerationTest.java    From kodkod with MIT License 6 votes vote down vote up
@Test
public final void testTrivial() {
	final Relation r = Relation.unary("r");
	final Universe u  = new Universe(Arrays.asList("a","b","c"));
	final TupleFactory f = u.factory();
	final Bounds b = new Bounds(u);
	b.bound(r, f.setOf("a"), f.allOf(1));
	final Formula someR = r.some();
	
	Iterator<Solution> sol = solver.solveAll(someR, b);
	// has a trivial instance, followed by 2 non-trivial instances
	assertEquals(Solution.Outcome.TRIVIALLY_SATISFIABLE, sol.next().outcome());
	assertEquals(Solution.Outcome.SATISFIABLE, sol.next().outcome());
	assertEquals(Solution.Outcome.SATISFIABLE, sol.next().outcome());
	assertEquals(Solution.Outcome.UNSATISFIABLE, sol.next().outcome());
	assertFalse(sol.hasNext());

}
 
Example 4
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
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 5
Source File: EvaluatorTest.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Constructor for EvaluatorTest.
 * @param arg0
 */
public EvaluatorTest() {
	univ = Relation.unary("univ");
	hilary = Relation.unary("hilary");
	jocelyn = Relation.unary("jocelyn");
	person = Relation.unary("person");
	spouse = Relation.binary("spouse");
	shaken = Relation.binary("shaken");
}
 
Example 6
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 7
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 8
Source File: SET967.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new instance of SET967.
 */
public SET967() {
    empty = Relation.unary("empty");
    subset = Relation.binary("subset");
    in = Relation.binary("in");
    disjoint = Relation.binary("disjoint");
    union = Relation.binary("union");
    singleton = Relation.binary("singleton");
    intersect2 = Relation.ternary("set_intersection2");
    union2 = Relation.ternary("set_union2");
    cartesian2 = Relation.ternary("cartesian_product2");
    ordered = Relation.ternary("ordered_pair");
    unordered = Relation.ternary("unordered_pair");
}
 
Example 9
Source File: ALG195_1.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new instance of Quasigroups7.
 */
ALG195_1() {
    op1 = Relation.ternary("op1");
    op2 = Relation.ternary("op2");
    s1 = Relation.unary("s1");
    s2 = Relation.unary("s2");
    e1 = new Relation[7];
    e2 = new Relation[7];
    h = new Relation[7];
    for (int i = 0; i < 7; i++) {
        e1[i] = Relation.unary("e1" + i);
        e2[i] = Relation.unary("e2" + i);
        h[i] = Relation.binary("h" + (i + 1));
    }
}
 
Example 10
Source File: Netconfig.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance of the Netconfig problem.
 */
public Netconfig() {
    Time = Relation.unary("Time");
    start = Relation.unary("start");
    end = Relation.unary("end");
    tick = Relation.binary("tick");
    Router = Relation.unary("Router");
    Site = Relation.unary("Site");
    HQ = Relation.unary("HQ");
    Sub = Relation.unary("Sub");
    site = Relation.binary("site");
    satellite = Relation.ternary("satellite");
    lineOfSight = Relation.ternary("lineOfSight");
}
 
Example 11
Source File: CeilingsAndFloors.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Creates an instance of the ceilings and floors class.
 */
public CeilingsAndFloors() {
	Platform = Relation.unary("Platform");
	Man = Relation.unary("Man");
	ceiling = Relation.binary("ceiling");
	floor = Relation.binary("floor");
}
 
Example 12
Source File: BlockedNQueens2.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Constructs an instance of BlockedNQueens from the file
 * with the given name.
 */
public BlockedNQueens2(final String file) {
	this.file = file;
	this.queen = Relation.binary("Queen");
	this.num = Relation.unary("num");
	this.ord = Relation.binary("ord");
	
}
 
Example 13
Source File: RegressionTests.java    From kodkod with MIT License 5 votes vote down vote up
@Test
public final void testFelix_05072008() { 
	Relation A=Relation.unary("A"), first=Relation.unary("OrdFirst"), last=Relation.unary("OrdLast"), next=Relation.nary("OrdNext", 2);

	List<String> atomlist = Arrays.asList("A1", "A2", "A3");
	Universe universe = new Universe(atomlist);
	TupleFactory factory = universe.factory();
	Bounds bounds = new Bounds(universe);

	TupleSet all = factory.setOf("A1","A2","A3");
	bounds.boundExactly(A, all);
	bounds.bound(first, all);
	bounds.bound(last, all);
	bounds.bound(next, all.product(all));

	Formula form = next.totalOrder(A,first,last);

	Solver solver = new Solver();
	solver.options().setSolver(SATFactory.MiniSat);
	solver.options().setBitwidth(4);
	solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
	solver.options().setSymmetryBreaking(0);
	solver.options().setSkolemDepth(0);

	Iterator<Solution> sol = solver.solveAll(form, bounds);
	assertTrue(sol.hasNext());
	assertEquals(sol.next().outcome(), Solution.Outcome.TRIVIALLY_SATISFIABLE);
	assertTrue(sol.hasNext());
	assertEquals(sol.next().outcome(), Solution.Outcome.TRIVIALLY_UNSATISFIABLE);
	assertFalse(sol.hasNext());

	//		int i=1;
	//		
	//		while (sol.hasNext()) {
	//			System.out.println("================================== "+i+" ===================================");
	//		  System.out.println(sol.next());
	//		  System.out.flush();
	//		  i++;
	//		}
}
 
Example 14
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public final void testFelix_03062008_2() {
    Relation x5 = Relation.unary("Role");
    Relation x6 = Relation.unary("Session");

    List<String> atomlist = Arrays.asList("Role$0", "Session$0", "Session$1");

    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("Role$0"));
    bounds.bound(x5, x5_upper);

    TupleSet x6_upper = factory.noneOf(1);
    x6_upper.add(factory.tuple("Session$0"));
    x6_upper.add(factory.tuple("Session$1"));
    bounds.bound(x6, x6_upper);

    Variable x11 = Variable.unary("x_a");
    Decls x10 = x11.oneOf(x6);
    Variable x15 = Variable.unary("x_b");
    Decls x14 = x15.oneOf(x5);
    Variable x17 = Variable.unary("x_c");
    Decls x16 = x17.oneOf(x5);
    Decls x13 = x14.and(x16);
    Expression x20 = x15.product(x17);
    Expression x19 = x11.product(x20);
    Formula x18 = x19.some();
    Formula x12 = x18.forSome(x13);
    Formula x9 = x12.forAll(x10);
    Formula x24 = x5.some();
    Formula x23 = x24.not();
    Formula x28 = x5.eq(x5);
    Formula x29 = x6.eq(x6);
    Formula x25 = x28.and(x29);
    Formula x22 = x23.and(x25);
    Formula x8 = x9.and(x22).and(x5.no()).and(x6.no());

    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(2);

    System.out.flush();

    Solution sol = solver.solve(x8, bounds);
    Instance inst = sol.instance();
    assertNotNull(inst);

    for (Relation rel : inst.relations()) {
        if (rel != x5 && rel != x6) {
            final TupleSet range = inst.tuples(x6).product(inst.tuples(x5));
            assertTrue(range.containsAll(inst.tuples(rel)));
        }
    }
}
 
Example 15
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public final void testBGP_03172011() {

        Relation x5 = Relation.unary("s012");
        Relation x8 = Relation.unary("zero");
        Relation x9 = Relation.unary("one");
        Relation x12 = Relation.nary("next", 2);

        Universe universe = new Universe(Arrays.asList("0", "1", "2", "3"));
        TupleFactory factory = universe.factory();
        Bounds bounds = new Bounds(universe);

        bounds.boundExactly(x5, factory.setOf("0", "1", "2"));
        bounds.boundExactly(x8, factory.setOf("0"));
        bounds.bound(x9, factory.setOf("1"), factory.setOf("1", "2"));

        TupleSet x12_upper = factory.noneOf(2);
        x12_upper.add(factory.tuple("1", "2"));
        x12_upper.add(factory.tuple("2", "3"));
        bounds.boundExactly(x12, x12_upper);

        Variable x714 = Variable.unary("x714");
        Decls x713 = x714.oneOf(x8.union(x9));

        Variable x720 = Variable.unary("x720");
        Expression x723 = x8.union(x9);
        Expression x724 = x9.join(x12);
        Expression x722 = x723.union(x724);
        Expression x721 = x722.difference(x714);
        Decls x719 = x720.oneOf(x721);

        Variable x727 = Variable.unary("x727");
        Expression x732 = x714.union(x720);
        Expression x728 = x5.difference(x732);
        Decls x726 = x727.oneOf(x728);

        Variable x735 = Variable.unary("x735");
        Decls x734 = x735.oneOf(x8);

        Variable x893 = Variable.unary("x893");
        Decls x892 = x893.oneOf(x727);
        Formula x894 = x720.no();
        Formula x891 = x894.forAll(x892);

        Formula x712 = x891.forSome(x713.and(x719).and(x726).and(x734));
        Formula x267 = Formula.FALSE.or(x712);

        Solver solver = new Solver();
        solver.options().setSolver(SATFactory.MiniSat);
        solver.options().setBitwidth(4);
        // solver.options().setFlatten(false);
        solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
        solver.options().setSymmetryBreaking(20);
        solver.options().setSkolemDepth(0);

        final Solution sol = solver.solve(x267, bounds);
        assertEquals(sol.outcome(), Solution.Outcome.TRIVIALLY_UNSATISFIABLE);
    }
 
Example 16
Source File: HamiltonianCycle2.java    From kodkod with MIT License 4 votes vote down vote up
/**
 * Returns a log encoded instance of HamiltonianCycle2.
 * @return  a log encoded instance of HamiltonianCycle2.
 */
public static HamiltonianCycle2 logEncoding(String file, Graph.Format format ) {
	final Graph<?> graph = format.parse(file);
	final Relation edges = Relation.binary("edges");
	final Relation enc = Relation.binary("enc");
	final Relation vertex = Relation.unary("vertex");
	final Relation[] pts = new Relation[graph.nodes().size()];
	for(int i = 0; i < pts.length; i++) { pts[i] = Relation.unary("p"+i); }
	
	final int bits =  33 - Integer.numberOfLeadingZeros(pts.length-1);
	final List<Object> atoms = new ArrayList<Object>(pts.length + bits);
	atoms.addAll(graph.nodes());
	for(int i = 0; i < bits; i++) { atoms.add(new Bit(i)); }
	
	final Bounds bounds = new Bounds(new Universe(atoms));
	
	final TupleFactory f = bounds.universe().factory();
	final TupleSet edgeBound = f.noneOf(2);
	for(Object from : graph.nodes()) {
		for (Object to : graph.edges(from))
			edgeBound.add(f.tuple(from, to));
	}
	
	bounds.boundExactly(edges, edgeBound);
	
	bounds.boundExactly(pts[0], f.setOf(atoms.get(pts.length)));
	for(int i = 1; i < pts.length; i++) { 
		bounds.bound(pts[i], f.range(f.tuple(atoms.get(pts.length)), f.tuple(atoms.get(atoms.size()-1))));
	}
	bounds.boundExactly(vertex, f.range(f.tuple(atoms.get(0)), f.tuple(atoms.get(pts.length-1))));
	
	final TupleSet encBound = f.noneOf(2);
	for(int i = 1; i <= pts.length; i++) { 
		final Object iatom = atoms.get(i-1);
		for(int j = 0; j < bits; j++) { 
			if ((i & (1<<j)) != 0) { 
				encBound.add(f.tuple(iatom, atoms.get(pts.length+j)));
			}
		}
	}
	bounds.boundExactly(enc, encBound);
	
	final Expression[] exprs = new Expression[pts.length];
	final Variable v = Variable.unary("v");
	final Decls d = v.oneOf(vertex);
	for(int i = 0; i < exprs.length; i++) { 
		exprs[i] = pts[i].eq(v.join(enc)).comprehension(d);
	}
	
	return new HamiltonianCycle2(bounds, exprs, Multiplicity.SOME, vertex, edges);
}
 
Example 17
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 18
Source File: Toughnut.java    From kodkod with MIT License 4 votes vote down vote up
/**
 * Creates an instance of Toughnut.
 */
public Toughnut() {
	this.Cell = Relation.unary("Cell");
	this.covered = Relation.nary("covered", 4);
	this.ord = Relation.binary("ord");
}
 
Example 19
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public final void testFelix_06192008() {
    Relation x5 = Relation.unary("R");

    List<String> atomlist = Arrays.asList("X");

    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("X"));
    bounds.bound(x5, x5_upper);

    Variable x10 = Variable.unary("a");
    Expression x11 = x5.difference(x5);
    Decls x9 = x10.oneOf(x11);
    Variable x14 = Variable.nary("b", 2);
    Expression x15 = x5.product(x5);
    Decls x13 = x14.setOf(x15);
    Expression x19 = x5.product(x5);
    Formula x17 = x14.in(x19);
    Expression x22 = x10.product(x10);
    Formula x21 = x22.eq(x14);
    Formula x16 = x17.and(x21);
    Formula x12 = x16.forSome(x13);
    Formula x7 = x12.forAll(x9);

    // System.out.println(x7);

    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("Depth=0..."); System.out.flush();
    solver.options().setSkolemDepth(0);
    assertEquals(Solution.Outcome.TRIVIALLY_SATISFIABLE, solver.solve(x7, bounds).outcome());

    // System.out.println("Depth=1..."); System.out.flush();
    solver.options().setSkolemDepth(1);
    final Solution sol = solver.solve(x7, bounds);
    assertEquals(Solution.Outcome.SATISFIABLE, sol.outcome());
    assertEquals(2, sol.instance().relations().size());
    for (Relation r : sol.instance().relations()) {
        assertTrue(sol.instance().tuples(r).isEmpty());
    }
}
 
Example 20
Source File: Trees.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs a new instance of the trees problem
 */
public Trees() {
    V = Relation.unary("V");
    E = Relation.binary("E");
}