kodkod.instance.TupleFactory Java Examples

The following examples show how to use kodkod.instance.TupleFactory. 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: FloatTestBase.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
private Instance instance(Formula f, Set<Relation> vars) {
	int msb = bitWidth - 1;

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

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

	Set<Tuple> digits = HashSetFactory.make();
	for(Object o : atoms) {
		digits.add(tf.tuple(o));
	}
	for(Relation r : vars) {
		b.bound(r, tf.setOf(digits));
	}
	
	Solver solver = new Solver(options);
	
	return solver.solve(f, b).instance();
}
 
Example #2
Source File: NUM378.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Returns bounds for the problem.
 * @return bounds for the problem.
 */
public final Bounds bounds() {
	final int n = 21;
	final List<String> atoms = new ArrayList<String>(n);
	atoms.add("goal");
	for(int i = 0; i < n; i++)
		atoms.add("n"+i);
	final Universe u = new Universe(atoms);
	final Bounds bound = new Bounds(u);
	final TupleFactory f = u.factory();
	
	final TupleSet succBound = f.noneOf(2);
	for(int i = 0; i < n; i++) {
		succBound.add(f.tuple("n"+i,"n"+((i+1)%n)));
	}
	bound.boundExactly(succ, succBound);
	
	final TupleSet sumBound = f.noneOf(3);
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			sumBound.add(f.tuple("n"+i, "n"+j, "n"+((i+j)%n)));
		}
	}
	bound.boundExactly(sum, sumBound);
	return bound;
}
 
Example #3
Source File: EnumerationTest.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
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: NUM378.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Returns bounds for the problem.
 *
 * @return bounds for the problem.
 */
public final Bounds bounds() {
    final int n = 21;
    final List<String> atoms = new ArrayList<String>(n);
    atoms.add("goal");
    for (int i = 0; i < n; i++)
        atoms.add("n" + i);
    final Universe u = new Universe(atoms);
    final Bounds bound = new Bounds(u);
    final TupleFactory f = u.factory();

    final TupleSet succBound = f.noneOf(2);
    for (int i = 0; i < n; i++) {
        succBound.add(f.tuple("n" + i, "n" + ((i + 1) % n)));
    }
    bound.boundExactly(succ, succBound);

    final TupleSet sumBound = f.noneOf(3);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            sumBound.add(f.tuple("n" + i, "n" + j, "n" + ((i + j) % n)));
        }
    }
    bound.boundExactly(sum, sumBound);
    return bound;
}
 
Example #5
Source File: OverflowTheoremTest.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
protected void setupBounds() {
    Relation ret = Relation.unary("ret");
    int min = min(bw);
    int max = max(bw);
    List<String> atoms = new ArrayList<String>(max - min + 1);
    for (int i = min; i <= max; i++) {
        atoms.add(String.valueOf(i));
    }
    final Universe universe = new Universe(atoms);
    TupleFactory factory = universe.factory();
    this.bounds = new Bounds(factory.universe());
    for (int i = min; i <= max; i++) {
        bounds.boundExactly(i, factory.setOf(String.valueOf(i)));
    }
    bounds.bound(ret, factory.noneOf(1), factory.allOf(1));
}
 
Example #6
Source File: NQueens.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Returns a bounds for the explicit integer encoding.
 * @requires n > 0
 * @return bounds for the explicit integer encoding.
 */
public Bounds bounds() { 
	
	final List<Integer> atoms = new ArrayList<Integer>(n);
	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();
	
	b.boundExactly(queen, f.allOf(1));
	b.bound(x, f.allOf(2));
	b.bound(y, f.allOf(2));
	
	for(int i = 0; i < n; i++) { 
		b.boundExactly(i, f.setOf(Integer.valueOf(i)));
	}
	
	return b;
}
 
Example #7
Source File: IncrementalOverflowNumTest.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasic() {
    Options opt = new Options();
    opt.setNoOverflow(true);
    opt.setBitwidth(2);
    IncrementalSolver solver = IncrementalSolver.solver(opt);
    Universe univ = new Universe("-2", "-1", "0", "1");
    Bounds b = new Bounds(univ);
    TupleFactory factory = univ.factory();
    b.boundExactly(-2, factory.range(factory.tuple("-2"), factory.tuple("-2")));
    b.boundExactly(-1, factory.range(factory.tuple("-1"), factory.tuple("-1")));
    b.boundExactly(0, factory.range(factory.tuple("0"), factory.tuple("0")));
    b.boundExactly(1, factory.range(factory.tuple("1"), factory.tuple("1")));
    Variable n = Variable.unary("n");
    Formula f = n.sum().plus(IntConstant.constant(1)).lte(n.sum()).forSome(n.oneOf(Expression.INTS));
    Solution sol = solver.solve(f, b);
    assertNoInstance(sol);
}
 
Example #8
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 #9
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 #10
Source File: SocialGolfer.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Returns the bounds for the scheduling problem with the given number of players, groups and weeks, using
 * the specified group size.
 * @return bounds for the scheduling problem with the given number of players, groups and weeks, using
 * the specified group size.
 */
public final Bounds bounds(int players, int groups, int weeks, int size) { 
	if (players<1 || groups<1 || weeks<1 || size<1) throw new IllegalArgumentException("invalid schedule parameters");
	final List<Object> atoms = new ArrayList<Object>(players+groups+weeks+1);
	for(int i = 0; i < players; i++) { 
		atoms.add("p" + i);
	}
	for(int i = 0; i < groups; i++) { 
		atoms.add("g" + i);
	}
	for(int i = 0; i < weeks; i++) { 
		atoms.add("w" + i);
	}
	atoms.add(size);
	final Universe u = new Universe(atoms);
	final TupleFactory f = u.factory();
	final Bounds b = new Bounds(u);
	
	b.boundExactly(size, f.setOf(size));
	b.boundExactly(this.size, f.setOf(size));
	b.boundExactly(this.player, f.range(f.tuple("p0"), f.tuple("p"+(players-1))));
	b.boundExactly(this.group, f.range(f.tuple("g0"), f.tuple("g"+(groups-1))));
	b.boundExactly(this.week, f.range(f.tuple("w0"), f.tuple("w"+(weeks-1))));
	b.bound(this.plays, b.upperBound(week).product(b.upperBound(group)).product(b.upperBound(player)));
	return b;
}
 
Example #11
Source File: Handshake.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a bounds for the given number of persons.
 *
 * @return a bounds for the given number of persons.
 */
public Bounds bounds(int persons) {
    final List<String> atoms = new ArrayList<String>(persons);
    atoms.add("Hilary");
    atoms.add("Jocelyn");
    for (int i = 2; i < persons; i++) {
        atoms.add("Person" + i);
    }
    final Universe u = new Universe(atoms);
    final TupleFactory f = u.factory();
    final Bounds b = new Bounds(u);
    b.boundExactly(Person, f.allOf(1));
    b.boundExactly(Hilary, f.setOf("Hilary"));
    b.boundExactly(Jocelyn, f.setOf("Jocelyn"));
    b.bound(spouse, f.allOf(2));
    b.bound(shaken, f.allOf(2));
    return b;
}
 
Example #12
Source File: GRA013_026.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Returns the bounds.
 * @return the bounds
 */
public final Bounds bounds() {
	final List<String> atoms = new ArrayList<String>(graphSize);
	for(int i = 1; i <= graphSize; i++)
		atoms.add("n"+i);
	atoms.add("goal");
	final Universe u = new Universe(atoms);
	final TupleFactory f = u.factory();
	final Bounds b = new Bounds(u);
	b.bound(goal, f.setOf("goal"));
	final TupleSet ns = f.range(f.tuple("n1"), f.tuple("n"+graphSize));
	b.boundExactly(node, ns);
	
	final TupleSet s = f.noneOf(2);
	for(int i = 1; i < graphSize; i++) {
		for(int j = i+1; j < graphSize; j++)
			s.add(f.tuple("n"+i, "n"+j));
	}
	b.boundExactly(lessThan, s);
	b.bound(red, s);
	b.bound(green, s);
	return b;
}
 
Example #13
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 #14
Source File: Pigeonhole.java    From kodkod with MIT License 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 #15
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 #16
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 #17
Source File: Handshake.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Returns a bounds for the given number of persons.
 * @return a bounds for the given number of persons.
 */
public Bounds bounds(int persons) {
	final List<String> atoms = new ArrayList<String>(persons);
	atoms.add("Hilary"); 
	atoms.add("Jocelyn");
	for(int i = 2; i < persons; i ++) {
		atoms.add("Person" + i);
	}
	final Universe u = new Universe(atoms);
	final TupleFactory f = u.factory();
	final Bounds b = new Bounds(u);
	b.boundExactly(Person, f.allOf(1));
	b.boundExactly(Hilary, f.setOf("Hilary"));
	b.boundExactly(Jocelyn, f.setOf("Jocelyn"));
	b.bound(spouse, f.allOf(2));
	b.bound(shaken, f.allOf(2));
	return b;
}
 
Example #18
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 #19
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private final void doTestAleks_03102013() {
    Relation r = Relation.unary("R");
    Relation s = Relation.binary("f");
    Variable v = Variable.unary("e");
    Decl decl = v.oneOf(r);
    Expression shared = v.join(s);
    Formula expr = (shared.difference(shared)).one().forAll(decl);

    Formula fin = expr.and(expr.not());

    List<Object> atomlist = new LinkedList<Object>();
    atomlist.add("R$0");
    atomlist.add("R$1");
    atomlist.add("R$2");

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

    bounds.bound(r, factory.allOf(1));
    bounds.bound(s, factory.allOf(2));

    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(4);
    solver.options().setSkolemDepth(0);
    solver.options().setLogTranslation(0);
    Solution sol = solver.solve(fin, bounds);
    assertNull(sol.instance());
}
 
Example #20
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
public final void testFelix_05152007_2() {
    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.eq(x5).not();

    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(x7, bounds);
    Set<Formula> core = Nodes.minRoots(x7, sol.proof().highLevelCore().values());
    assertEquals(1, core.size());
    assertTrue(core.contains(x7));
}
 
Example #21
Source File: SudokuParser.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the representation of the clues encoded in the given array as 
 * an NxNxN tuple set drawn from the given universe.  N is assumed
 * to be a perfect square, and the universe is assumed to consist of Integer objects in 
 * the range [1..N]. The  array is assumed to consist of N*N numbers, drawn 
 * from the set 0..N, inclusive, where a consecutive sequence of N
 * numbers represents one row of an NxN Sudoku grid.
 * Zeros stand for blank entries.  
 * @requires some r: int | puzzle.length = r * r * r * r
 * @requires universe.atoms[int] = {i: Integer | 1 <= i.intValue() <= puzzle.length} 
 * @return a tupleset representation of the clues in the puzzle specified by the given array.
 */
public static final TupleSet parse(String[] puzzle, Universe univ) { 
	final int n = (int)StrictMath.sqrt(puzzle.length);
	final int r = (int)StrictMath.sqrt(n);
	if (puzzle.length!=r*r*r*r)  throw new IllegalArgumentException("Not a valid puzzle spec: expected " + (r*r*r*r) + " numbers but found " + puzzle.length);
			
	final TupleFactory f = univ.factory();
	
	final TupleSet givens = f.noneOf(3);
	
	for(int i = 0; i < n; i++) { 
		for(int j = 0; j < n; j++) { 
			try {
				final int digit = Integer.parseInt(puzzle[i*n+j]);
				if (digit>0 && digit<=n) {
					final Tuple t = f.tuple(i+1, j+1, digit);
					givens.add(t);
				} else if (digit>n) { 
					throw new IllegalArgumentException("Not a valid puzzle spec: expected numbers from [0, " + n+"] but found "+digit);
				} 
			} catch (NumberFormatException nfe) { 
				throw new IllegalArgumentException("Not a valid puzzle spec: expected numbers from [0, " + n+"] but found "+puzzle[i*n+j]);
			}
		}
	}
	
	return givens;
}
 
Example #22
Source File: IntTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private IntExpression[] nonConstants() {
    final Options options = solver.options();

    final IntRange range = options.integers();
    final int min = range.min(), max = range.max();
    final int size = range.size();

    final Relation[] r = new Relation[size];

    final TupleFactory f = bounds.universe().factory();
    for (int i = 0; i < size; i++) {
        int arity = i % 3 + 1;
        r[i] = Relation.nary("r" + i, arity);

        TupleSet b = f.noneOf(arity);
        for (int j = (i / 3) * ((int) Math.pow(SIZE, arity - 1)), jmax = j + size; j < jmax; j++) {
            b.add(f.tuple(arity, j % b.capacity()));
        }

        bounds.bound(r[i], b);
    }

    final IntExpression[] vals = new IntExpression[max - min + 1];
    for (int i = 0; i < size; i++) {
        vals[i] = i + min < 0 ? r[i].count().negate() : r[i].count();
    }

    return vals;
}
 
Example #23
Source File: BookExamples.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * This constructs a Kodkod Tuple from the list of atoms, and returns null if no
 * such Tuple can be constructed.
 */
private static Tuple t_tuple(TupleFactory factory, Object... atoms) {
    try {
        if (atoms.length <= 0)
            return null;
        return factory.tuple(atoms);
    } catch (Throwable ex) {
        return null;
    }
}
 
Example #24
Source File: RingElection.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns a bounds object that scopes Process, Time, and their
 * fields according to given values.
 * @return bounds
 */
public Bounds bounds(int processes, int times) {
	final List<String> atoms = new ArrayList<String>(processes + times);
	for(int i = 0; i < processes; i++) {
		atoms.add("Process"+i);
	}
	for(int i = 0; i < times; i++) {
		atoms.add("Time"+i);
	}
	final Universe u = new Universe(atoms);
	final TupleFactory f = u.factory();
	final Bounds b = new Bounds(u);
	
	final TupleSet pb = f.range(f.tuple("Process0"), f.tuple("Process"+ (processes-1)));
	final TupleSet tb = f.range(f.tuple("Time0"), f.tuple("Time"+(times-1)));
	
	b.bound(Process, pb);
	b.bound(succ, pb.product(pb));
	b.bound(toSend, pb.product(pb).product(tb));
	b.bound(elected, pb.product(tb));
	b.bound(pord, pb.product(pb));
	b.bound(pfirst, pb);
	b.bound(plast, pb);
	
	b.bound(Time, tb);
	b.bound(tord, tb.product(tb));
	b.bound(tfirst, tb);
	b.bound(tlast, tb);
	
	return b;
}
 
Example #25
Source File: IncrementalSolverTest.java    From kodkod with MIT License 5 votes vote down vote up
@Test
public void testIncrementalPartialSymmetryBreaking() {
	final Bounds b = new Bounds(new Universe("A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9"));
	final TupleFactory t = b.universe().factory();
	final Relation ord = Relation.binary("ord"), univ03 = Relation.unary("univ[0..3]"), first = Relation.unary("first"), last = Relation.unary("last");
	final Relation fun = Relation.binary("fun"), univ47 = Relation.unary("univ[4..7]"), univ89 = Relation.unary("univ[8..9]");

	b.bound(univ47, t.setOf("A4", "A5", "A6", "A7"));
	b.bound(univ89, t.setOf("A8", "A9"));
	b.bound(fun, b.upperBound(univ47).product(b.upperBound(univ89)));

	final Formula[] f = { fun.function(univ47, univ89), 
			ord.totalOrder(univ03, first, last), 
			ord.acyclic(), 
			last.product(first).in(ord) };

	final int vars0 =  4 + 2 + 4*2; // univ47, univ89, fun
	checkOutcomeAndStats(checkModel(solver.solve(f[0], b), f[0]), SATISFIABLE, vars0);

	b.relations().clear();
	b.bound(univ03, t.setOf("A0", "A1", "A2", "A3"));
	b.bound(first, b.upperBound(univ03));
	b.bound(last, b.upperBound(univ03));
	b.bound(ord, b.upperBound(univ03).product(b.upperBound(univ03)));
	final int vars1 = vars0 + 4*3 + 4*4; // univ03, first, last, ord
	checkOutcomeAndStats(checkModel(solver.solve(f[1], b), f[0], f[1]), SATISFIABLE, vars1);

	b.relations().clear();

	checkOutcomeAndStats(checkModel(solver.solve(f[2], b), f[0], f[1], f[2]), SATISFIABLE, vars1);

	checkOutcomeAndStats(solver.solve(f[3], b), UNSATISFIABLE, vars1);
	
	//final Solver whole = new Solver(solver.options());
	//System.out.println(whole.solve(Formula.and(f[0], f[1], f[2]), b));
}
 
Example #26
Source File: RegressionTests.java    From kodkod with MIT License 5 votes vote down vote up
@Test
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 #27
Source File: Dijkstra.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the bounds corresponding to the given scopes.
 * @return bounds
 */
public Bounds bounds(int states, int processes, int mutexes) {
	final List<String> atoms = new ArrayList<String>(states + processes + mutexes);
	for(int i = 0; i < states; i++) {
		atoms.add("State"+i);
	}
	for(int i = 0; i < processes; i++) {
		atoms.add("Process"+i);
	}
	for(int i = 0; i < mutexes; i++) {
		atoms.add("Mutex"+i);
	}
	final Universe u = new Universe(atoms);
	final TupleFactory f = u.factory();
	final Bounds b = new Bounds(u);
	
	final TupleSet sb = f.range(f.tuple("State0"), f.tuple("State"+(states-1)));
	final TupleSet pb = f.range(f.tuple("Process0"), f.tuple("Process"+(processes-1)));
	final TupleSet mb = f.range(f.tuple("Mutex0"), f.tuple("Mutex"+(mutexes-1)));
	
	b.bound(State, sb);
	b.bound(holds, sb.product(pb).product(mb));
	b.bound(waits, sb.product(pb).product(mb));
	
	b.bound(sfirst, sb);
	b.bound(slast, sb);
	b.bound(sord, sb.product(sb));
	
	b.bound(Process, pb);
	
	b.bound(Mutex, mb);
	b.bound(mfirst, mb);
	b.bound(mlast, mb);
	b.bound(mord, mb.product(mb));
		
	return b;
}
 
Example #28
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 #29
Source File: SolutionRelation.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public void bound(Bounds b, TupleFactory tf) {
	try {
		SortedSet<Variable> vars = variables();
		TupleSet bound = tf.noneOf(vars.size());
		rows: for(Iterator<Row> rows = solution.rows(); rows.hasNext(); ) {
			Row row = rows.next();
			
			for(Iterator<Variable> vs = solution.variables(); vs.hasNext(); ) {
				Variable v = vs.next();
				Object o = QueryTripleTermUtil.toAtom(row.get(v));
				if (bindings.containsKey(v.getName())) {
					if (! bindings.get(v.getName()).equals(o)) {
						continue rows;
					}
				}
			}
			
			int i = 0;
			Object[] atoms = new Object[ usedVarNames.size()];
			for(Iterator<Variable> vs = vars.iterator(); vs.hasNext(); ) {
				Variable v = vs.next();
				Object o = QueryTripleTermUtil.toAtom(row.get(v));
				if (usedVarNames.contains(v.getName())) {
					atoms[i++] = o;
				}
			}
			bound.add(tf.tuple(atoms));
		}

		System.err.println("bound " + bound);
		
		b.boundExactly(solutionRelation, bound);
		
	} catch (URISyntaxException e) {
		assert false : "bad solution element: " + e;
	}
}
 
Example #30
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;
}