Java Code Examples for kodkod.instance.TupleFactory#setOf()

The following examples show how to use kodkod.instance.TupleFactory#setOf() . 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: RegressionTests.java    From kodkod with MIT License 6 votes vote down vote up
@Test
public final void testGreg_02192006() {
	Relation r1 = Relation.unary("r1");
	Relation r2 = Relation.unary("r2");
	Relation r3 = Relation.unary("r3");
	Expression e = r1.in(r2).thenElse(r2, r1);
	Formula f = e.eq(r2).and(e.in(r3));
	Object o1 = "o1";
	Object o2 = "o2";	
	Universe univ = new Universe(Arrays.asList(o1, o2));
	TupleFactory factory = univ.factory();
	TupleSet set1 = factory.setOf(o1);
	TupleSet set2 = factory.setOf(o2);
	Bounds bounds = new Bounds(univ);
	bounds.bound(r1, set1);
	bounds.boundExactly(r2, set2);
	bounds.bound(r3, set1);

	assertEquals(Solution.Outcome.TRIVIALLY_UNSATISFIABLE, solver.solve(f, bounds).outcome());


}
 
Example 3
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
public final void testGreg_02192006() {
    Relation r1 = Relation.unary("r1");
    Relation r2 = Relation.unary("r2");
    Relation r3 = Relation.unary("r3");
    Expression e = r1.in(r2).thenElse(r2, r1);
    Formula f = e.eq(r2).and(e.in(r3));
    Object o1 = "o1";
    Object o2 = "o2";
    Universe univ = new Universe(Arrays.asList(o1, o2));
    TupleFactory factory = univ.factory();
    TupleSet set1 = factory.setOf(o1);
    TupleSet set2 = factory.setOf(o2);
    Bounds bounds = new Bounds(univ);
    bounds.bound(r1, set1);
    bounds.boundExactly(r2, set2);
    bounds.bound(r3, set1);

    assertEquals(Solution.Outcome.TRIVIALLY_UNSATISFIABLE, solver.solve(f, bounds).outcome());

}
 
Example 4
Source File: Viktor.java    From kodkod with MIT License 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 5
Source File: ALG197.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the bounds the problem (axioms 1, 4, 9-11, last formula of 14-15, and
 * first formula of 16-22).
 *
 * @return the bounds for the problem
 */
@Override
public final Bounds bounds() {
    final Bounds b = super.bounds();
    final TupleFactory f = b.universe().factory();

    final TupleSet op1h = b.upperBound(op1).clone();
    final TupleSet op2h = b.upperBound(op2).clone();

    final TupleSet op1l = f.setOf(f.tuple("e16", "e16", "e15")); // axiom
                                                                // 14,
                                                                // line
                                                                // 6
    final TupleSet op2l = f.setOf(f.tuple("e26", "e26", "e25")); // axiom
                                                                // 15,
                                                                // line
                                                                // 6

    op1h.removeAll(f.area(f.tuple("e16", "e16", "e10"), f.tuple("e16", "e16", "e16")));
    op1h.addAll(op1l);

    op2h.removeAll(f.area(f.tuple("e26", "e26", "e20"), f.tuple("e26", "e26", "e26")));
    op2h.addAll(op2l);

    b.bound(op1, op1l, op1h);
    b.bound(op2, op2l, op2h);

    final TupleSet high = f.area(f.tuple("e10", "e20"), f.tuple("e15", "e26"));

    // first line of axioms 16-22
    for (int i = 0; i < 7; i++) {
        Tuple t = f.tuple("e16", "e2" + i);
        high.add(t);
        b.bound(h[i], f.setOf(t), high);
        high.remove(t);
    }

    return b;
}
 
Example 6
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 7
Source File: ALG197.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the bounds the problem (axioms 1, 4, 9-11, last formula of 14-15, and first formula of 16-22).
 * @return the bounds for the problem
 */
public final Bounds bounds() {
	final Bounds b = super.bounds();
	final TupleFactory f = b.universe().factory();
	
	final TupleSet op1h = b.upperBound(op1).clone();
	final TupleSet op2h = b.upperBound(op2).clone();
	
	final TupleSet op1l = f.setOf(f.tuple("e16", "e16", "e15")); // axiom 14, line 6
	final TupleSet op2l = f.setOf(f.tuple("e26", "e26", "e25")); // axiom 15, line 6
	
	op1h.removeAll(f.area(f.tuple("e16", "e16", "e10"), f.tuple("e16", "e16", "e16")));
	op1h.addAll(op1l);
	
	op2h.removeAll(f.area(f.tuple("e26", "e26", "e20"), f.tuple("e26", "e26", "e26")));
	op2h.addAll(op2l);
	
	b.bound(op1, op1l, op1h);
	b.bound(op2, op2l, op2h);
	
	final TupleSet high = f.area(f.tuple("e10", "e20"), f.tuple("e15", "e26"));
	
	// first line of axioms 16-22
	for(int i = 0; i < 7; i++) {
		Tuple t = f.tuple("e16", "e2"+i);
		high.add(t);
		b.bound(h[i], f.setOf(t), high);
		high.remove(t);
	}
	
	return b;
}
 
Example 8
Source File: ALG195.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the bounds the problem (axioms 1, 4, 9-13, second formula of 14-15, and first formula of 16-22).
 * @return the bounds for the problem
 */
public final Bounds bounds() {
	final Bounds b = super.bounds();
	final TupleFactory f = b.universe().factory();
	
	final TupleSet op1h = b.upperBound(op1).clone();
	final TupleSet op2h = b.upperBound(op2).clone();
	
	for(int i = 0; i < 7; i++) {
		op1h.remove(f.tuple("e1"+i, "e1"+i, "e1"+i)); // axiom 12
		op2h.remove(f.tuple("e2"+i, "e2"+i, "e2"+i)); // axiom 13
	}
	
	final TupleSet op1l = f.setOf(f.tuple("e15", "e15", "e11")); // axiom 14, line 2
	final TupleSet op2l = f.setOf(f.tuple("e25", "e25", "e21")); // axiom 15, line 2
	
	op1h.removeAll(f.area(f.tuple("e15", "e15", "e10"), f.tuple("e15", "e15", "e16")));
	op1h.addAll(op1l);
	
	op2h.removeAll(f.area(f.tuple("e25", "e25", "e20"), f.tuple("e25", "e25", "e26")));
	op2h.addAll(op2l);
	
	b.bound(op1, op1l, op1h);
	b.bound(op2, op2l, op2h);
	
	final TupleSet high = f.area(f.tuple("e10", "e20"), f.tuple("e14", "e26"));
	high.addAll(f.area(f.tuple("e16", "e20"), f.tuple("e16", "e26")));
	
	// first line of axioms 16-22
	for(int i = 0; i < 7; i++) {
		Tuple t = f.tuple("e15", "e2"+i);
		high.add(t);
		b.bound(h[i], f.setOf(t), high);
		high.remove(t);
	}
	
	return b;
}
 
Example 9
Source File: Transpose4x4UnaryL.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns relation bounds over a universe of interpretation consisting of integers 0 - 15, inclusive.
 * @return relation bounds over a universe of interpretation consisting of integers 0 - 15, inclusive.
 */
final Bounds bounds() {
	final Universe u = new Universe(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
	final TupleFactory f = u.factory();
	
	final Bounds b = new Bounds(u);
	
	// tell the solver to interpret integer objects as their corresponding integer values
	for (int i = 0; i < 16; i++)
		b.boundExactly(i, f.setOf(i));
	
	final TupleSet s3 = f.setOf(0, 1, 2, 3);        										// { 0, 1, 2, 3 }
	final TupleSet s12 = f.setOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); 	// { 0, ..., 12 }
	for(int i = 0; i < 4; i++) {
		b.bound(mx1[i], s12);
		b.bound(mx2[i], s12);		
		b.bound(sx1[i], s12);
		b.bound(sx2[i], s12);
		for(int j = 0; j < 4; j++) {
			b.bound(mi[i][j], s3);
			b.bound(si[i][j], s3);
		}
	}
	
	final TupleSet ord = f.noneOf(2);  				// { [0, 1], [1, 2], [2, 3], ..., [14, 15] }
	for(int i = 0; i < 15; i++)
		ord.add(f.tuple((Object)i, i+1));
	b.boundExactly(succ, ord);
	
	return b;
}
 
Example 10
Source File: Transpose4x4UnaryLR.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns relation bounds over a universe of interpretation consisting of integers 0 - 16, inclusive.
 * @return relation bounds over a universe of interpretation consisting of integers 0 - 16, inclusive.
 */
final Bounds bounds() {
	final Universe u = new Universe(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
	final TupleFactory f = u.factory();
	
	final Bounds b = new Bounds(u);
	
	// tell the solver to interpret integer objects as their corresponding integer values
	for (int i = 0; i <= 16; i++)
		b.boundExactly(i, f.setOf(i));
	
	final TupleSet s3 = f.setOf(0, 1, 2, 3);        							// { 0, 1, 2, 3 }
	final TupleSet s12 = f.setOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); 	// { 0, ..., 12 }
	for(int i = 0; i < 4; i++) {
		b.bound(sl[i], s12);
		b.bound(mx1[i], s12);
		b.bound(mx2[i], s12);	
		b.bound(tl[i], s12);
		b.bound(sx1[i], s12);
		b.bound(sx2[i], s12);
		for(int j = 0; j < 4; j++) {
			b.bound(mi[i][j], s3);
			b.bound(si[i][j], s3);
		}
	}
	
	b.boundExactly(idx, f.setOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15));
	final TupleSet ord = f.noneOf(2);  				// { [0, 1], [1, 2], [2, 3], ..., [14, 15], [15, 16] }
	for(int i = 0; i < 16; i++)
		ord.add(f.tuple((Object)i, i+1));
	b.boundExactly(succ, ord);
	
	return b;
}
 
Example 11
Source File: RegressionTests.java    From kodkod with MIT License 4 votes vote down vote up
@Test
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 12
Source File: RegressionTests.java    From kodkod with MIT License 4 votes vote down vote up
@Test
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 13
Source File: RegressionTests.java    From kodkod with MIT License 4 votes vote down vote up
@Test
public final void testEmina_05072008() {
	Relation A=Relation.unary("A"), first=Relation.unary("OrdFirst"), last=Relation.unary("OrdLast"), next=Relation.nary("OrdNext", 2);
	Relation B=Relation.unary("B"), acyclic = Relation.binary("acyclic");

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

	TupleSet allA = factory.setOf("A1","A2","A3");
	TupleSet allB = factory.setOf("B1","B2","B3");
	TupleSet allC = factory.setOf("C1", "C2");
	bounds.boundExactly(A, allA);
	bounds.bound(first, allA);
	bounds.bound(last, allA);
	bounds.bound(next, allA.product(allA));
	bounds.boundExactly(B, allB);
	bounds.bound(acyclic, allC.product(allC));

	Variable v = Variable.unary("v");
	Formula f0 = Formula.TRUE.forSome(v.setOf(B));
	Formula f1 = next.totalOrder(A, first, last);
	Formula f2 = acyclic.acyclic();
	Formula form = f0.and(f1).and(f2);

	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);
	int i=1;

	while (sol.hasNext()) {
		assertTrue(i <= 17);
		sol.next();
		i++;
	}
}
 
Example 14
Source File: RegressionTests.java    From kodkod with MIT License 4 votes vote down vote up
@Test
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().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);
}
 
Example 15
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 16
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 17
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public final void testEmina_05072008() {
    Relation A = Relation.unary("A"), first = Relation.unary("OrdFirst"), last = Relation.unary("OrdLast"),
                    next = Relation.nary("OrdNext", 2);
    Relation B = Relation.unary("B"), acyclic = Relation.binary("acyclic");

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

    TupleSet allA = factory.setOf("A1", "A2", "A3");
    TupleSet allB = factory.setOf("B1", "B2", "B3");
    TupleSet allC = factory.setOf("C1", "C2");
    bounds.boundExactly(A, allA);
    bounds.bound(first, allA);
    bounds.bound(last, allA);
    bounds.bound(next, allA.product(allA));
    bounds.boundExactly(B, allB);
    bounds.bound(acyclic, allC.product(allC));

    Variable v = Variable.unary("v");
    Formula f0 = Formula.TRUE.forSome(v.setOf(B));
    Formula f1 = next.totalOrder(A, first, last);
    Formula f2 = acyclic.acyclic();
    Formula form = f0.and(f1).and(f2);

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

    Iterator<Solution> sol = solver.solveAll(form, bounds);
    int i = 1;

    while (sol.hasNext()) {
        assertTrue(i <= 17);
        sol.next();
        i++;
    }
}
 
Example 18
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
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().setFlatten(false);
    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 19
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);
}
 
Example 20
Source File: ALG195.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the bounds the problem (axioms 1, 4, 9-13, second formula of 14-15,
 * and first formula of 16-22).
 *
 * @return the bounds for the problem
 */
@Override
public final Bounds bounds() {
    final Bounds b = super.bounds();
    final TupleFactory f = b.universe().factory();

    final TupleSet op1h = b.upperBound(op1).clone();
    final TupleSet op2h = b.upperBound(op2).clone();

    for (int i = 0; i < 7; i++) {
        op1h.remove(f.tuple("e1" + i, "e1" + i, "e1" + i)); // axiom 12
        op2h.remove(f.tuple("e2" + i, "e2" + i, "e2" + i)); // axiom 13
    }

    final TupleSet op1l = f.setOf(f.tuple("e15", "e15", "e11")); // axiom
                                                                // 14,
                                                                // line
                                                                // 2
    final TupleSet op2l = f.setOf(f.tuple("e25", "e25", "e21")); // axiom
                                                                // 15,
                                                                // line
                                                                // 2

    op1h.removeAll(f.area(f.tuple("e15", "e15", "e10"), f.tuple("e15", "e15", "e16")));
    op1h.addAll(op1l);

    op2h.removeAll(f.area(f.tuple("e25", "e25", "e20"), f.tuple("e25", "e25", "e26")));
    op2h.addAll(op2l);

    b.bound(op1, op1l, op1h);
    b.bound(op2, op2l, op2h);

    final TupleSet high = f.area(f.tuple("e10", "e20"), f.tuple("e14", "e26"));
    high.addAll(f.area(f.tuple("e16", "e20"), f.tuple("e16", "e26")));

    // first line of axioms 16-22
    for (int i = 0; i < 7; i++) {
        Tuple t = f.tuple("e15", "e2" + i);
        high.add(t);
        b.bound(h[i], f.setOf(t), high);
        high.remove(t);
    }

    return b;
}