Java Code Examples for kodkod.instance.Universe#factory()

The following examples show how to use kodkod.instance.Universe#factory() . 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: Viktor.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the bounds for the problem.
 *
 * @return bounds
 */
public final Bounds bounds() {
    List<String> atoms = new ArrayList<String>(cols + 1);
    for (int i = 0; i < cols; i++) {
        atoms.add(String.valueOf(i));
    }
    atoms.add("a");
    final Universe u = new Universe(atoms);
    final TupleFactory f = u.factory();
    final Bounds b = new Bounds(u);

    final TupleSet abound = f.setOf("a");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            b.bound(a[i][j], abound);
        }
    }
    final TupleSet xbound = f.range(f.tuple(String.valueOf(0)), f.tuple(String.valueOf(cols - 1)));
    for (int j = 0; j < cols; j++) {
        b.bound(x[j], xbound);
        b.boundExactly(j, f.setOf(String.valueOf(j)));
    }

    return b;
}
 
Example 2
Source File: Pigeonhole.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the bounds for the given number of pigeons and holes.
 *
 * @return bounds
 */
public Bounds bounds(int pigeons, int holes) {
    final List<String> atoms = new ArrayList<String>(pigeons + holes);
    for (int i = 0; i < pigeons; i++) {
        atoms.add("Pigeon" + i);
    }
    for (int i = 0; i < holes; i++) {
        atoms.add("Hole" + i);
    }
    final Universe u = new Universe(atoms);
    final TupleFactory f = u.factory();

    final Bounds b = new Bounds(u);

    final TupleSet pbound = f.range(f.tuple("Pigeon0"), f.tuple("Pigeon" + (pigeons - 1)));
    final TupleSet hbound = f.range(f.tuple("Hole0"), f.tuple("Hole" + (holes - 1)));
    b.boundExactly(Pigeon, pbound);
    b.boundExactly(Hole, hbound);
    b.bound(hole, pbound.product(hbound));
    return b;
}
 
Example 3
Source File: SymmetryBreakingTest.java    From kodkod with MIT License 5 votes vote down vote up
public SymmetryBreakingTest() {
	this.solver = new Solver();
	
	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();
	
	to1 = Relation.binary("to1");
	ord1 = Relation.unary("ord1");
	first1 = Relation.unary("first1");
	last1 = Relation.unary("last1");
	
	to2 = Relation.binary("to2");
	ord2 = Relation.unary("ord2");
	first2 = Relation.unary("first2");
	last2 = Relation.unary("last2");
	
	to3 = Relation.binary("to3");
	ord3 = Relation.unary("ord3");
	first3 = Relation.unary("first3");
	last3 = Relation.unary("last3");

	ac1 = Relation.binary("ac1");
	ac2 = Relation.binary("ac2");
	ac3 = Relation.binary("ac3");
	
	r1 = Relation.unary("r1");
	r2 = Relation.binary("r2");
}
 
Example 4
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
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));

}
 
Example 5
Source File: SkolemizationTest.java    From kodkod with MIT License 5 votes vote down vote up
public SkolemizationTest() {
	this.solver = new Solver();
	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.r1a = Relation.unary("r1a");
	this.r1b = Relation.unary("r1b");
	this.r2a = Relation.binary("r2a");
	this.r2b = Relation.binary("r2b");
	this.bounds = new Bounds(universe);
}
 
Example 6
Source File: SymmetryBreakingTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
public SymmetryBreakingTest(String arg0) {
    super(arg0);
    this.solver = new Solver();

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

    to1 = Relation.binary("to1");
    ord1 = Relation.unary("ord1");
    first1 = Relation.unary("first1");
    last1 = Relation.unary("last1");

    to2 = Relation.binary("to2");
    ord2 = Relation.unary("ord2");
    first2 = Relation.unary("first2");
    last2 = Relation.unary("last2");

    to3 = Relation.binary("to3");
    ord3 = Relation.unary("ord3");
    first3 = Relation.unary("first3");
    last3 = Relation.unary("last3");

    ac1 = Relation.binary("ac1");
    ac2 = Relation.binary("ac2");
    ac3 = Relation.binary("ac3");

    r1 = Relation.unary("r1");
    r2 = Relation.binary("r2");
}
 
Example 7
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 8
Source File: RegressionTests.java    From kodkod with MIT License 5 votes vote down vote up
@Test
public final void testFelix_01062007() {
	Relation x1 = Relation.nary("A",1);
	List<String> atomlist = Arrays.asList("A");
	Universe universe = new Universe(atomlist);
	TupleFactory factory = universe.factory();
	Bounds bounds = new Bounds(universe);

	TupleSet x1_upper = factory.noneOf(1);
	x1_upper.add(factory.tuple("A"));
	bounds.bound(x1, x1_upper);

	Solver solver = new Solver();
	solver.options().setSolver(SATFactory.MiniSat);

	Iterator<Solution> sols = solver.solveAll(Formula.TRUE, bounds);
	assertNotNull(sols.next().instance());

	assertNotNull(sols.next().instance());

	assertNull(sols.next().instance());

	//		Solution sol1=sols.next();
	//		System.out.println("Solution1:"+sol1.instance());
	//
	//		Solution sol2=sols.next();
	//		System.out.println("Solution2:"+sol2.instance());
	//
	//		Solution sol3=sols.next();
	//		System.out.println("Solution3:"+sol3.instance());

}
 
Example 9
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
public final void testFelix_01062007() {
    Relation x1 = Relation.nary("A", 1);
    List<String> atomlist = Arrays.asList("A");
    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);

    TupleSet x1_upper = factory.noneOf(1);
    x1_upper.add(factory.tuple("A"));
    bounds.bound(x1, x1_upper);

    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.MiniSat);

    Iterator<Solution> sols = solver.solveAll(Formula.TRUE, bounds);
    assertNotNull(sols.next().instance());

    assertNotNull(sols.next().instance());

    assertNull(sols.next().instance());

    // Solution sol1=sols.next();
    // System.out.println("Solution1:"+sol1.instance());
    //
    // Solution sol2=sols.next();
    // System.out.println("Solution2:"+sol2.instance());
    //
    // Solution sol3=sols.next();
    // System.out.println("Solution3:"+sol3.instance());

}
 
Example 10
Source File: AbstractWorldDefinitions.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the bounds for the given scope.
 * @return bounds for the given scope
 */
public final Bounds bounds(int scope) {
	final List<String> atoms = new LinkedList<String>();
	
	final int max = scope-1;
	for(int i = 0; i < scope; i++) { atoms.add("Coin"+i); }
	for(int i = 0; i < scope; i++) { atoms.add("Purse"+i);}
	for(int i = 0; i < scope; i++) { atoms.add("TransferDetails"+i);}
	atoms.add("aNullIn");
	for(int i = 0; i < scope; i++) { atoms.add("AbWorld"+i);}
	for(int i = 0; i < max; i++) { atoms.add("AOUT"+i);}
	atoms.add("aNullOut");
	
	final Universe u = new Universe(atoms);
	final TupleFactory f = u.factory();
	final Bounds b = new Bounds(u);
	
	
	b.bound(Coin, f.range(f.tuple("Coin0"), f.tuple("Coin"+max)));
	b.bound(Purse, f.range(f.tuple("Purse0"), f.tuple("Purse"+max)));
	b.bound(TransferDetails, f.range(f.tuple("TransferDetails0"), f.tuple("TransferDetails"+max)));
	b.bound(AbWorld, f.range(f.tuple("AbWorld0"), f.tuple("AbWorld"+max)));
	b.bound(AbPurse, b.upperBound(Purse));
	b.bound(AOUT, f.range(f.tuple("AOUT0"), f.tuple("aNullOut")));
	b.bound(AIN, f.range(f.tuple("TransferDetails0"), f.tuple("aNullIn")));
	
	b.boundExactly(aNullIn, f.setOf("aNullIn"));
	b.boundExactly(aNullOut, f.setOf("aNullOut"));
				
	b.bound(from, b.upperBound(TransferDetails).product(b.upperBound(Purse)));
	b.bound(to, b.upperBound(TransferDetails).product(b.upperBound(Purse)));
	b.bound(value, b.upperBound(TransferDetails).product(b.upperBound(Coin)));
	b.bound(abAuthPurse, b.upperBound(AbWorld).product(b.upperBound(AbPurse)));
	
	b.bound(abBalance, b.upperBound(AbPurse).product(b.upperBound(Coin)).product(b.upperBound(AbWorld)));
	b.bound(abLost, b.upperBound(AbPurse).product(b.upperBound(Coin)).product(b.upperBound(AbWorld)));
	
	return b;
}
 
Example 11
Source File: NQueens.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns a bounds for the log integer encoding.
 * @return bounds for the log integer encoding.
 */
public Bounds bounds() { 
	final int bits = 32 - Integer.numberOfLeadingZeros(n - 1);
	final List<Object> atoms = new ArrayList<Object>(n + bits);
	for(int i =0; i < n; i++) { 
		atoms.add("Q"+i);
	}
	for(int i = 0; i < bits; i++) { 
		atoms.add(Integer.valueOf(1<<i));
	}
	
	final Universe u = new Universe(atoms);
	final Bounds b = new Bounds(u);
	final TupleFactory f = u.factory();
	
	final TupleSet queens = f.range(f.tuple("Q0"), f.tuple("Q"+(n-1)));
	final TupleSet ints = f.range(f.tuple(Integer.valueOf(1)), f.tuple(Integer.valueOf(1<<(bits-1))));
	
	b.boundExactly(queen, queens);
	b.bound(x, queens.product(ints));
	b.bound(y, queens.product(ints));
	
	for(int i = 0; i < bits; i++) { 
		b.boundExactly(1<<i, f.setOf(Integer.valueOf(1<<i)));
	}
	
	return b;
}
 
Example 12
Source File: RegressionTests.java    From kodkod with MIT License 5 votes vote down vote up
@Test
public final void testFelix_05152007_3() {
	Relation x5 = Relation.nary("A", 1);

	List<String> atomlist = Arrays.asList("A[0]", "A[1]", "A[2]");

	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("A[0]"));
	x5_upper.add(factory.tuple("A[1]"));
	x5_upper.add(factory.tuple("A[2]"));

	bounds.bound(x5, x5_upper);

	Formula a=x5.some();
	Formula a1 = x5.no();
	Formula b=a1.and(Formula.TRUE.and(Formula.TRUE));
	Formula c=a.and(b);

	Solver solver = new Solver();

	solver.options().setLogTranslation(1);
	solver.options().setSolver(SATFactory.DefaultSAT4J);
	solver.options().setBitwidth(4);
	solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);

	Solution sol = solver.solve(c,bounds);
	Set<Formula> core = Nodes.minRoots(c, sol.proof().highLevelCore().values());

	assertEquals(2, core.size());
	assertTrue(core.contains(a));
	assertTrue(core.contains(a1));


}
 
Example 13
Source File: NQueens.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the bounds for relational encoding of the problem.
 * @return the bounds for relational encoding of the problem.
 */
public Bounds bounds() {
	final List<Object> atoms = new ArrayList<Object>(n*2);
	for(int i =0; i < n; i++) { 
		atoms.add("Q"+i);
	}
	
	for(int i =0; i < n; i++) { 
		atoms.add(Integer.valueOf(i));
	}
	
	final Universe u = new Universe(atoms);
	final Bounds b = new Bounds(u);
	final TupleFactory f = u.factory();
	
	final TupleSet qbound = f.range(f.tuple("Q0"), f.tuple("Q"+(n-1)));
	final TupleSet nbound = f.range(f.tuple(Integer.valueOf(0)), f.tuple(Integer.valueOf(n-1)));
	
	b.boundExactly(queen, qbound);
	b.boundExactly(num, nbound);
	b.bound(x, qbound.product(nbound));
	b.bound(y, qbound.product(nbound));
	
	final TupleSet obound = f.noneOf(2);
	for(int i = 1; i < n; i++) { 
		obound.add(f.tuple((Object)Integer.valueOf(i-1), Integer.valueOf(i)));
	}
	
	b.boundExactly(ord, obound);
	
	for(int i = 0; i < n; i++) { 
		b.boundExactly(i, f.setOf(Integer.valueOf(i)));
	}
	
	return b;
}
 
Example 14
Source File: ALG195_1.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the partial bounds the problem (axioms 1, 4, 9-11).
 *
 * @return the partial bounds for the problem
 */
public Bounds bounds() {
    final List<String> atoms = new ArrayList<String>(14);
    for (int i = 0; i < 7; i++)
        atoms.add("e1" + i);
    for (int i = 0; i < 7; i++)
        atoms.add("e2" + i);

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

    final TupleSet s1bound = f.range(f.tuple("e10"), f.tuple("e16"));
    final TupleSet s2bound = f.range(f.tuple("e20"), f.tuple("e26"));

    b.boundExactly(s1, s1bound);
    b.boundExactly(s2, s2bound);

    // axioms 9, 10, 11
    for (int i = 0; i < 7; i++) {
        b.boundExactly(e1[i], f.setOf("e1" + i));
        b.boundExactly(e2[i], f.setOf("e2" + i));
    }

    // axom 1
    b.bound(op1, f.area(f.tuple("e10", "e10", "e10"), f.tuple("e16", "e16", "e16")));
    // axiom 4
    b.bound(op2, f.area(f.tuple("e20", "e20", "e20"), f.tuple("e26", "e26", "e26")));

    final TupleSet hbound = s1bound.product(s2bound);
    for (Relation r : h) {
        b.bound(r, hbound);
    }

    return b;
}
 
Example 15
Source File: Bigconfig.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a bounds with the given number of hqs and subs, constructed using the
 * given universe.
 *
 * @requires hqNum > 0 && subNum > 0
 * @requires u contains at least (hqNum+sub) Router atoms and as many Site atoms
 * @return bounds
 */
private Bounds bounds(int hqNum, int subNum, Universe u) {
    final TupleFactory f = u.factory();

    final Bounds b = new Bounds(u);
    final int siteMax = hqNum + subNum - 1;

    final String site0 = "Site0";
    final String siteN = "Site" + siteMax;
    final String siteHQ = "Site" + (hqNum - 1);
    final String siteSub = "Site" + hqNum;
    final String router0 = "Router0";
    final String routerN = "Router" + siteMax;

    final TupleSet sites = f.range(f.tuple(site0), f.tuple(siteN));
    b.boundExactly(Site, sites);
    b.boundExactly(HQ, f.range(f.tuple(site0), f.tuple(siteHQ)));
    b.boundExactly(Sub, f.range(f.tuple(siteSub), f.tuple(siteN)));

    final TupleSet routers = f.range(f.tuple(router0), f.tuple(routerN));
    b.boundExactly(Router, routers);
    b.bound(link, routers.product(routers));
    // b.bound(site, routers.product(sites));
    final TupleSet routerLocations = f.noneOf(2);
    for (int i = 0; i <= siteMax; i++) {
        routerLocations.add(f.tuple("Router" + i, "Site" + i));
    }
    b.boundExactly(site, routerLocations);

    return b;
}
 
Example 16
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 17
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
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: BugTests.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public final void testVincent_02182006() {
    // 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();

    final Relation oneRel = Relation.unary("oneRel");
    final Relation pCourses = Relation.binary("pCourses");
    final Relation prev = Relation.binary("prev");
    final Relation sCourses = Relation.binary("sCourses");
    final Relation prereqs = Relation.binary("prereqs");
    final Relation semester = Relation.unary("Semester");
    final Relation course = Relation.unary("Course");
    final Relation prereqset = Relation.unary("PrereqSet");

    final int courseIndex = 0;
    final int courseScope = 254;
    final int semesterIndex = 254;
    final int semesterScope = 2;
    final int prereqsetIndex = 256;
    final int prereqsetScope = 1;

    bounds.bound(course, factory.range(factory.tuple(atoms.get(courseIndex)), factory.tuple(atoms.get(courseIndex + courseScope - 1))));
    bounds.bound(semester, factory.range(factory.tuple(atoms.get(semesterIndex)), factory.tuple(atoms.get(semesterIndex + semesterScope - 1))));
    bounds.bound(prereqset, factory.range(factory.tuple(atoms.get(prereqsetIndex)), factory.tuple(atoms.get(prereqsetIndex + prereqsetScope - 1))));

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

    // list1 = [256, 2]
    // list2 = [256, 3]
    // pCoursesTS = [ [256, 2], [256, 3] ]
    List<Integer> list1 = new ArrayList<Integer>();
    list1.add(atoms.get(256));
    list1.add(atoms.get(1));
    List<Integer> list2 = new ArrayList<Integer>();
    list2.add(atoms.get(256));
    list2.add(atoms.get(2));
    TupleSet pCoursesTS = factory.setOf(factory.tuple(list1), factory.tuple(list2));
    bounds.bound(pCourses, pCoursesTS, pCoursesTS);

    // prevTS = [ [255, 254] ]
    TupleSet prevTS = factory.setOf(factory.tuple((Object) atoms.get(255), (Object) atoms.get(254)));
    bounds.bound(prev, prevTS, prevTS);

    // sCourses can be anything from Semester -> Course
    bounds.bound(sCourses, factory.area(factory.tuple((Object) atoms.get(semesterIndex), (Object) atoms.get(courseIndex)),

                                        factory.tuple((Object) atoms.get(semesterIndex + semesterScope - 1), (Object) atoms.get(courseIndex + courseScope - 1))));

    // pCoursesTS = [ [0, 256] ]
    TupleSet prereqsTS = factory.setOf(factory.tuple((Object) atoms.get(0), (Object) atoms.get(256)));
    bounds.bound(prereqs, prereqsTS, prereqsTS);

    // all s: futureSemesters | all c: s.courses | no c.prereqs or some p:
    // c.prereqs | p.courses in s.prev^.courses
    final Variable s = Variable.unary("s"), c = Variable.unary("c"), p = Variable.unary("p");
    Formula formula = (p.join(pCourses).in(s.join(prev.closure()).join(sCourses)).forAll(p.oneOf(c.join(prereqs)))).forAll(c.oneOf(s.join(sCourses))).forAll(s.oneOf(semester));

    // System.out.println(formula);

    final Instance instance = solver.solve(formula, bounds).instance();
    assertNotNull(instance);
}
 
Example 19
Source File: RegressionTests.java    From kodkod with MIT License 4 votes vote down vote up
@Test
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().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 20
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public final void testFelix_03162009() {
    Relation x = Relation.unary("X");
    Relation y = Relation.unary("Y");
    Relation q = Relation.unary("Q");
    Relation f = Relation.nary("f", 2);

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

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

    TupleSet x_upper = factory.noneOf(1);
    x_upper.add(factory.tuple("X"));
    bounds.boundExactly(x, x_upper);

    TupleSet y_upper = factory.noneOf(1);
    y_upper.add(factory.tuple("Y"));
    bounds.boundExactly(y, y_upper);

    TupleSet q_upper = factory.noneOf(1);
    q_upper.add(factory.tuple("X"));
    q_upper.add(factory.tuple("Y"));
    bounds.bound(q, q_upper);

    TupleSet f_upper = factory.noneOf(2);
    f_upper.add(factory.tuple("X").product(factory.tuple("X")));
    f_upper.add(factory.tuple("X").product(factory.tuple("Y")));
    f_upper.add(factory.tuple("Y").product(factory.tuple("X")));
    f_upper.add(factory.tuple("Y").product(factory.tuple("Y")));
    bounds.bound(f, f_upper);

    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);
    solver.options().setSkolemDepth(0);

    Expression test = f.override(q.product(y));
    TupleSet approx = factory.setOf(test.arity(), Translator.approximate(test, bounds, solver.options()).denseIndices());
    assertEquals(f_upper, approx);
}