kodkod.util.ints.Ints Java Examples

The following examples show how to use kodkod.util.ints.Ints. 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: LeafInterpreter.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Returns a {@link kodkod.engine.bool.BooleanMatrix matrix} m of 
 * {@link kodkod.engine.bool.BooleanValue boolean formulas} representing
 * the specified constant expression.    
 * @return { m: BooleanMatrix | let dset = [0..this.universe.size()^c.arity) | 
 *           m.dimensions.dimensions = [0..c.arity) ->one this.universe.size() && 
 *           c = UNIV => m.elements[dset] = TRUE, c = NONE => m.elements[dset] = FALSE,
 *           c = IDEN => (all i: dset | (some j: int | i = j*(1+this.universe.size())) => m.elements[i] = TRUE, m.elements[i] = FALSE),
 *           c = INT => (all i: dset | (some j: int | this.interpret(j)=i) => m.elements[i] = TRUE, m.elements[i] = FALSE }
 */
public final BooleanMatrix interpret(ConstantExpression c) {
	final int univSize = universe().size();
	if (c==Expression.UNIV) {
		final IntSet all =  Ints.rangeSet(Ints.range(0, univSize-1));
		return factory().matrix(Dimensions.square(univSize, 1), all, all);
	} else if (c==Expression.IDEN) {
		final Dimensions dim2 = Dimensions.square(univSize, 2);
		final IntSet iden = Ints.bestSet(dim2.capacity());
		for(int i = 0; i < univSize; i++) {
			iden.add(i*univSize + i);
		}			
		return factory().matrix(dim2, iden, iden);
	} else if (c==Expression.NONE) {
		return factory().matrix(Dimensions.square(univSize, 1), Ints.EMPTY_SET, Ints.EMPTY_SET);
	} else if (c==Expression.INTS) {
		final IntSet ints = Ints.bestSet(univSize);
		for(IntIterator iter = ints().iterator(); iter.hasNext(); ) {
			ints.add(interpret(iter.next()));
		}
		return factory().matrix(Dimensions.square(univSize, 1), ints, ints);
	} else {
		throw new IllegalArgumentException("unknown constant expression: " + c);
	}
}
 
Example #2
Source File: BooleanMatrixTest.java    From kodkod with MIT License 6 votes vote down vote up
@Test
public final void testSetAndGet() {
	// set regions [4..8], [11..14], [16..23], [9..10] to variables
	fill(mF324, mCells, mR[1]); fill(mF324, mCells, mR[3]); 
	fill(mF324, mCells, mR[5]); fill(mF324, mCells, mR[2]);
	assertTrue(equivalent(mF324, mCells));

	// check that the dense regions in the matrix are [4..14] and [16..23]
	Iterator<IndexedEntry<BooleanValue>> indeces = mF324.iterator();
	checkEquals(Ints.merge(Ints.merge(mR[1], mR[2]), mR[3]), indeces);
	checkEquals(mR[5], indeces);

	// wipe out 4, 14, 23, and 10
	blank(mF324, mCells,mR[1].min()); blank(mF324, mCells, mR[3].max()); 
	blank(mF324, mCells, mR[5].max()); blank(mF324, mCells, mR[2].max());
	assertTrue(equivalent(mF324, mCells));
	// add 4 again
	fill(mF324, mCells, range(mR[1].min(),mR[1].min()));
	indeces = mF324.iterator();
	checkEquals(Ints.merge(range(mR[1].min(), mR[1].max()), range(mR[2].min(), mR[2].max()-1)), indeces);
	checkEquals(range(mR[2].max()+1, mR[3].max()-1), indeces);
	checkEquals(range(mR[5].min(), mR[5].max()-1), indeces);

	//System.out.println(mF324);
}
 
Example #3
Source File: NativeSolverTest.java    From kodkod with MIT License 6 votes vote down vote up
@Test
public void testProofOfLastEmptyClauseCNF() {
	for(SATFactory factory : solvers) {
		if (!factory.prover()) continue;
		final SATProver solver = (SATProver) factory.instance();
		solver.addVariables(1);
		solver.addClause(new int[]{1});
		solver.addVariables(1);
		solver.addClause(new int[]{-2});
		solver.addVariables(2);
		solver.addClause(new int[]{2, 3, 4});
		solver.addClause(new int[0]);
		assertFalse(solver.solve());
		final ResolutionTrace proof = solver.proof();
		assertEquals(4, proof.size());
		assertEquals(Ints.singleton(3), proof.core());
		assertEquals(Ints.EMPTY_SET, proof.resolvents());
		assertEquals(0, proof.get(3).size());
	}
}
 
Example #4
Source File: NativeSolverTest.java    From kodkod with MIT License 6 votes vote down vote up
@Test
public void testProofOfNthEmptyClauseCNF() {
	for(SATFactory factory : solvers) {
		if (!factory.prover()) continue;
		final SATProver solver = (SATProver) factory.instance();
		solver.addVariables(1);
		solver.addClause(new int[]{1});
		solver.addVariables(1);
		solver.addClause(new int[]{-2});
		solver.addVariables(2);
		solver.addClause(new int[]{2, 3, 4});
		solver.addClause(new int[0]);
		solver.addClause(new int[]{4});
		solver.addClause(new int[]{3});
		assertFalse(solver.solve());
		final ResolutionTrace proof = solver.proof();
		assertEquals(4, proof.size());
		assertEquals(Ints.singleton(3), proof.core());
		assertEquals(Ints.EMPTY_SET, proof.resolvents());
		assertEquals(0, proof.get(3).size());
	}
}
 
Example #5
Source File: LeafInterpreter.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a {@link kodkod.engine.bool.BooleanMatrix matrix} m of
 * {@link kodkod.engine.bool.BooleanValue boolean formulas} representing the
 * specified constant expression.
 *
 * @return { m: BooleanMatrix | let dset = [0..this.universe.size()^c.arity) |
 *         m.dimensions.dimensions = [0..c.arity) ->one this.universe.size() &&
 *         c = UNIV => m.elements[dset] = TRUE, c = NONE => m.elements[dset] =
 *         FALSE, c = IDEN => (all i: dset | (some j: int | i =
 *         j*(1+this.universe.size())) => m.elements[i] = TRUE, m.elements[i] =
 *         FALSE), c = INT => (all i: dset | (some j: int | this.interpret(j)=i)
 *         => m.elements[i] = TRUE, m.elements[i] = FALSE }
 */
public final BooleanMatrix interpret(ConstantExpression c) {
    final int univSize = universe().size();
    if (c == Expression.UNIV) {
        final IntSet all = Ints.rangeSet(Ints.range(0, univSize - 1));
        return factory().matrix(Dimensions.square(univSize, 1), all, all);
    } else if (c == Expression.IDEN) {
        final Dimensions dim2 = Dimensions.square(univSize, 2);
        final IntSet iden = Ints.bestSet(dim2.capacity());
        for (int i = 0; i < univSize; i++) {
            iden.add(i * univSize + i);
        }
        return factory().matrix(dim2, iden, iden);
    } else if (c == Expression.NONE) {
        return factory().matrix(Dimensions.square(univSize, 1), Ints.EMPTY_SET, Ints.EMPTY_SET);
    } else if (c == Expression.INTS) {
        final IntSet ints = Ints.bestSet(univSize);
        for (IntIterator iter = ints().iterator(); iter.hasNext();) {
            ints.add(interpret(iter.next()));
        }
        return factory().matrix(Dimensions.square(univSize, 1), ints, ints);
    } else {
        throw new IllegalArgumentException("unknown constant expression: " + c);
    }
}
 
Example #6
Source File: StrategyUtils.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the set of all variables in the core of the given trace that form
 * unit clauses.
 *
 * @return { v: [1..) | some c: trace.core | c.size() = 1 and c.maxVariable() =
 *         v }
 */
public static IntSet coreUnits(ResolutionTrace trace) {
    final IntSet units = new IntTreeSet();

    for (Iterator<Clause> itr = trace.reverseIterator(trace.core()); itr.hasNext();) {
        Clause c = itr.next();
        if (c.size() == 1) {
            units.add(c.maxVariable());
        }
    }

    if (units.isEmpty())
        return Ints.EMPTY_SET;

    return Ints.asSet(units.toArray());
}
 
Example #7
Source File: LazyTrace.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 * @see kodkod.engine.satlab.ResolutionTrace#get(int)
 */
public Clause get(final int index) {
	if (index>=0 && index<trace.length) {
		if (axiom(index)) { // return a self-contained clause
			return new Clause() {
				final int[] literals = trace[index];
				final int hashCode = Ints.superFastHash(literals);
				public Iterator<Clause> antecedents() { return Containers.emptyIterator(); }
				public IntIterator literals() { return new IntArrayIterator(literals,0,literals.length); }
				public int maxVariable() { return StrictMath.abs(literals[literals.length-1]); }
				public int numberOfAntecedents() { return 0; }
				public int size() { return literals.length;	}
				public int[] toArray(int[] array) {
					if (array.length<literals.length) { array = new int[literals.length]; }
					System.arraycopy(literals, 0, array, 0, literals.length);
					return array;
				}
				public int hashCode() { return hashCode; }
			};
		} else {
			return new ClauseView(index);
		}
	}
	throw new IndexOutOfBoundsException("invalid index: " + index);
}
 
Example #8
Source File: LazyTrace.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 * @see kodkod.engine.satlab.ResolutionTrace#implicants(kodkod.util.ints.IntSet)
 */
public IntSet reachable(IntSet indices) {
	if (indices.isEmpty()) return Ints.EMPTY_SET;
	else if (valid(indices)) {
		final IntSet ret = new IntBitSet(trace.length);
		ret.addAll(indices);
		for(int i = indices.max(); i >= axioms; i--) {
			if (ret.contains(i)) {
				int[] resolvent = trace[i];
				if (resolved(i)) { 
					for(int j = 1, antes = resolvent[0]; j <= antes; j++) {
						ret.add(resolvent[j]);
					}
				} else {
					for(int j = 0; j < resolvent.length; j++) {
						ret.add(resolvent[j]);
					}
				}
			}
		}
		return ret;
	}
	else throw new IndexOutOfBoundsException("invalid indices: " + indices);
}
 
Example #9
Source File: LazyTrace.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see kodkod.engine.satlab.ResolutionTrace#implicants(kodkod.util.ints.IntSet)
 */
@Override
public IntSet reachable(IntSet indices) {
    if (indices.isEmpty())
        return Ints.EMPTY_SET;
    else if (valid(indices)) {
        final IntSet ret = new IntBitSet(trace.length);
        ret.addAll(indices);
        for (int i = indices.max(); i >= axioms; i--) {
            if (ret.contains(i)) {
                int[] resolvent = trace[i];
                if (resolved(i)) {
                    for (int j = 1, antes = resolvent[0]; j <= antes; j++) {
                        ret.add(resolvent[j]);
                    }
                } else {
                    for (int j = 0; j < resolvent.length; j++) {
                        ret.add(resolvent[j]);
                    }
                }
            }
        }
        return ret;
    } else
        throw new IndexOutOfBoundsException("invalid indices: " + indices);
}
 
Example #10
Source File: ECFPStrategy.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 * @see kodkod.engine.satlab.ReductionStrategy#next(kodkod.engine.satlab.ResolutionTrace)
 */
public IntSet next(final ResolutionTrace trace) {
	final IntSet core = trace.core();
	if (lastCore > core.size()) {
		lastCore = core.size();
		return core;
	} else {
		lastCore = Integer.MIN_VALUE;                  
		return Ints.EMPTY_SET;
	}
}
 
Example #11
Source File: SymmetryDetector.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Refines the atomic partitions this.parts based on the contents of the given set.
 * @requires all disj s, q: this.parts[int] | 
 *            some s.ints && some q.ints && (no s.ints & q.ints) &&
 *            this.parts[int].ints = [0..this.bounds.universe.size())
 * @ensures  all disj s, q: this.parts'[int] | 
 *            some s.ints && some q.ints && (no s.ints & q.ints) &&
 *            this.parts'[int].ints = [0..this.bounds.universe.size()) &&
 *            (all i: [0..this.parts'.size()) | 
 *             this.parts'[i].ints in set.ints || no this.parts'[i].ints & set.ints)
 */
private void refinePartitions(IntSet set) {
	for(ListIterator<IntSet> partsIter = parts.listIterator(); partsIter.hasNext(); ) {
		IntSet part = partsIter.next();
		IntSet intersection = Ints.bestSet(part.min(), part.max());
		intersection.addAll(part);
		intersection.retainAll(set);
		if (!intersection.isEmpty() && intersection.size() < part.size()) {
			part.removeAll(intersection);
			partsIter.add(intersection);
		}
	}
}
 
Example #12
Source File: IntTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private void testIfIntExpr(Options.IntEncoding encoding) {
    solver.options().setIntEncoding(encoding);
    bounds.bound(r1, factory.setOf("15"), factory.setOf("15"));
    Formula f = (r1.some().thenElse(r1.count(), IntConstant.constant(5))).eq(IntConstant.constant(1));
    Solution s = solve(f);
    assertNotNull(s.instance());
    assertEquals(Ints.singleton(15), s.instance().tuples(r1).indexView());

    f = (r1.some().thenElse(r1.sum(), IntConstant.constant(5))).eq(IntConstant.constant(1));
    s = solve(f);
    assertNull(s.instance());

    bounds.bound(r1, factory.setOf("3"), factory.allOf(1));
    bounds.boundExactly(3, factory.setOf("3"));
    bounds.boundExactly(1, factory.setOf("1"));
    f = ((r1.count().eq(IntConstant.constant(2))).thenElse(r1.sum(), IntConstant.constant(5))).eq(IntConstant.constant(4));
    s = solve(f);
    assertNotNull(s.instance());
    assertTrue(s.instance().tuples(r1).indexView().contains(1));
    assertTrue(s.instance().tuples(r1).indexView().contains(3));
    assertEquals(2, s.instance().tuples(r1).size());

    f = Formula.TRUE.thenElse(IntConstant.constant(2), IntConstant.constant(3)).eq(IntConstant.constant(4));
    s = solve(f);
    assertEquals(Solution.Outcome.TRIVIALLY_UNSATISFIABLE, s.outcome());

    f = Formula.FALSE.thenElse(IntConstant.constant(2), IntConstant.constant(3)).eq(IntConstant.constant(3));
    s = solve(f);
    assertEquals(Solution.Outcome.TRIVIALLY_SATISFIABLE, s.outcome());
}
 
Example #13
Source File: BooleanMatrixTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
public final void testSetAndGet() {
    // set regions [4..8], [11..14], [16..23], [9..10] to variables
    fill(mF324, mCells, mR[1]);
    fill(mF324, mCells, mR[3]);
    fill(mF324, mCells, mR[5]);
    fill(mF324, mCells, mR[2]);
    assertTrue(equivalent(mF324, mCells));

    // check that the dense regions in the matrix are [4..14] and [16..23]
    Iterator<IndexedEntry<BooleanValue>> indeces = mF324.iterator();
    assertEquals(Ints.merge(Ints.merge(mR[1], mR[2]), mR[3]), indeces);
    assertEquals(mR[5], indeces);

    // wipe out 4, 14, 23, and 10
    blank(mF324, mCells, mR[1].min());
    blank(mF324, mCells, mR[3].max());
    blank(mF324, mCells, mR[5].max());
    blank(mF324, mCells, mR[2].max());
    assertTrue(equivalent(mF324, mCells));
    // add 4 again
    fill(mF324, mCells, range(mR[1].min(), mR[1].min()));
    indeces = mF324.iterator();
    assertEquals(Ints.merge(range(mR[1].min(), mR[1].max()), range(mR[2].min(), mR[2].max() - 1)), indeces);
    assertEquals(range(mR[2].max() + 1, mR[3].max() - 1), indeces);
    assertEquals(range(mR[5].min(), mR[5].max() - 1), indeces);

    // System.out.println(mF324);
}
 
Example #14
Source File: BenchmarkSymmStats2.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
SymmInfo(int size) {
    this.parts = new LinkedHashSet<IntSet>();
    for (int i = 0, max = size; i < max; i++) {
        parts.add(Ints.singleton(i));
    }
    this.time = "t\\o";
    this.symms = "t\\o";
}
 
Example #15
Source File: LazyTrace.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see kodkod.engine.satlab.ResolutionTrace#backwardReachable(kodkod.util.ints.IntSet)
 */
@Override
public IntSet backwardReachable(IntSet indices) {
    if (indices.isEmpty())
        return Ints.EMPTY_SET;
    else if (valid(indices)) {
        final IntSet ret = new IntBitSet(trace.length);
        ret.addAll(indices);
        for (int i = axioms, length = trace.length; i < length; i++) {
            int[] resolvent = trace[i];
            if (resolved(i)) {
                for (int j = 1, antes = resolvent[0]; j <= antes; j++) {
                    if (ret.contains(resolvent[j])) {
                        ret.add(i);
                        break;
                    }
                }
            } else {
                for (int j = 0; j < resolvent.length; j++) {
                    if (ret.contains(resolvent[j])) {
                        ret.add(i);
                        break;
                    }
                }
            }
        }
        return ret;
    } else
        throw new IndexOutOfBoundsException("invalid indices: " + indices);
}
 
Example #16
Source File: NativeSolverTest.java    From kodkod with MIT License 5 votes vote down vote up
@Test
public void testProofOfEmptyClauseCNF() {
	for(SATFactory factory : solvers) {
		if (!factory.prover()) continue;
		final SATProver solver = (SATProver) factory.instance();
		solver.addClause(new int[0]);
		assertFalse(solver.solve());
		final ResolutionTrace proof = solver.proof();
		assertEquals(1, proof.size());
		assertEquals(Ints.singleton(0), proof.core());
		assertEquals(Ints.EMPTY_SET, proof.resolvents());
		assertEquals(0, proof.get(0).size());
	}
}
 
Example #17
Source File: LeafInterpreter.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns this.vars.
 *
 * @return this.vars.
 */
public final Map<Relation,IntSet> vars() {
    final Map<Relation,IntSet> ret = new LinkedHashMap<Relation,IntSet>((vars.size() * 4) / 3);
    for (Map.Entry<Relation,IntRange> e : vars.entrySet()) {
        ret.put(e.getKey(), Ints.rangeSet(e.getValue()));
    }
    return ret;
}
 
Example #18
Source File: LazyTrace.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see kodkod.engine.satlab.ResolutionTrace#resolvents()
 */
@Override
public IntSet resolvents() {
    if (trace.length > axioms)
        return Ints.rangeSet(Ints.range(axioms, trace.length - 1));
    else
        return Ints.EMPTY_SET;
}
 
Example #19
Source File: LazyTrace.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see kodkod.engine.satlab.ResolutionTrace#learnable(kodkod.util.ints.IntSet)
 */
@Override
public IntSet learnable(IntSet indices) {
    if (indices.isEmpty())
        return Ints.EMPTY_SET;
    else if (valid(indices)) {
        final IntSet ret = new IntBitSet(trace.length);
        ret.addAll(indices);
        TOP: for (int i = axioms, length = trace.length; i < length; i++) {
            int[] resolvent = trace[i];
            if (resolved(i)) {
                for (int j = 1, antes = resolvent[0]; j <= antes; j++) {
                    if (!ret.contains(resolvent[j])) {
                        continue TOP;
                    }
                }
            } else {
                for (int j = 0; j < resolvent.length; j++) {
                    if (!ret.contains(resolvent[j])) {
                        continue TOP;
                    }
                }
            }
            ret.add(i);
        }
        return ret;
    } else
        throw new IndexOutOfBoundsException("invalid indices: " + indices);
}
 
Example #20
Source File: StrategyUtils.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the set of all variables in the core of the given trace
 * that form unit clauses.
 * @return { v: [1..) | some c: trace.core | c.size() = 1 and c.maxVariable() = v }
 */
public static IntSet coreUnits(ResolutionTrace trace) { 
	final IntSet units = new IntTreeSet();
	
	for(Iterator<Clause> itr = trace.reverseIterator(trace.core()); itr.hasNext(); ) { 	
		Clause c = itr.next();
		if (c.size()==1) { 
			units.add(c.maxVariable());
		}
	}
	
	if (units.isEmpty()) return Ints.EMPTY_SET;
	
	return Ints.asSet(units.toArray());
}
 
Example #21
Source File: TupleSet.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Projects this TupleSet onto the given dimension.
 * @return {s: TupleSet | s.arity = 1 && s.universe = this.universe && 
 *                        s.tuples = { t: Tuple | some q: this.tuples | q.atoms[dimension] = t.atoms[int] } }
 * @throws IllegalArgumentException  dimension < 0 || dimension >= this.arity
 */
public TupleSet project(int dimension) {
	if (dimension < 0 || dimension >= arity) {
		throw new IllegalArgumentException("dimension < 0 || dimension >= this.arity");
	}
	final IntSet projection = Ints.bestSet(universe.size());
	final TupleFactory factory = universe.factory();
	for(IntIterator indexIter = tuples.iterator(); indexIter.hasNext();) {
		projection.add(factory.project(indexIter.next(), arity, dimension));
	}
	return new TupleSet(universe,1,projection);
}
 
Example #22
Source File: ECFPStrategy.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see kodkod.engine.satlab.ReductionStrategy#next(kodkod.engine.satlab.ResolutionTrace)
 */
@Override
public IntSet next(final ResolutionTrace trace) {
    final IntSet core = trace.core();
    if (lastCore > core.size()) {
        lastCore = core.size();
        return core;
    } else {
        lastCore = Integer.MIN_VALUE;
        return Ints.EMPTY_SET;
    }
}
 
Example #23
Source File: LazyTrace.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 * @see kodkod.engine.satlab.ResolutionTrace#resolvents()
 */
public IntSet resolvents() { 
	if (trace.length > axioms)
		return Ints.rangeSet(Ints.range(axioms, trace.length-1)); 
	else
		return Ints.EMPTY_SET;
}
 
Example #24
Source File: LazyTrace.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 * @see kodkod.engine.satlab.ResolutionTrace#backwardReachable(kodkod.util.ints.IntSet)
 */
public IntSet backwardReachable(IntSet indices) {
	if (indices.isEmpty()) return Ints.EMPTY_SET;
	else if (valid(indices)) {
		final IntSet ret = new IntBitSet(trace.length);
		ret.addAll(indices);
		for(int i = axioms, length = trace.length; i < length; i++) {
			int[] resolvent = trace[i];
			if (resolved(i)) { 
				for(int j = 1, antes = resolvent[0]; j <= antes; j++) {
					if (ret.contains(resolvent[j])) {
						ret.add(i);
						break;
					}
				}
			} else {
				for(int j = 0; j < resolvent.length; j++) {
					if (ret.contains(resolvent[j])) {
						ret.add(i);
						break;
					}
				}
			}
		}
		return ret;
	}
	else throw new IndexOutOfBoundsException("invalid indices: " + indices);
}
 
Example #25
Source File: LazyTrace.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 * @see kodkod.engine.satlab.ResolutionTrace#learnable(kodkod.util.ints.IntSet)
 */
public IntSet learnable(IntSet indices) {
	if (indices.isEmpty()) return Ints.EMPTY_SET;
	else if (valid(indices)) {
		final IntSet ret = new IntBitSet(trace.length);
		ret.addAll(indices);
		TOP: for(int i = axioms, length = trace.length; i < length; i++) {
			int[] resolvent = trace[i];
			if (resolved(i)) { 
				for(int j = 1, antes = resolvent[0]; j <= antes; j++) {
					if (!ret.contains(resolvent[j])) {
						continue TOP;
					}
				}
			} else {
				for(int j = 0; j < resolvent.length; j++) {
					if (!ret.contains(resolvent[j])) {
						continue TOP;
					}
				}
			}
			ret.add(i);
		}
		return ret;
	}
	else throw new IndexOutOfBoundsException("invalid indices: " + indices);
}
 
Example #26
Source File: LazyTrace.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 * @see kodkod.engine.satlab.ResolutionTrace#directlyLearnable(kodkod.util.ints.IntSet)
 */
public IntSet directlyLearnable(IntSet indices) { 
	if (indices.isEmpty()) return Ints.EMPTY_SET;
	else if (valid(indices)) {
		final IntSet ret = new IntBitSet(trace.length);
		ret.addAll(indices);
		TOP: for(int i = axioms, length = trace.length; i < length; i++) {
			int[] resolvent = trace[i];
			if (resolved(i)) { 
				for(int j = 1, antes = resolvent[0]; j <= antes; j++) {
					if (!indices.contains(resolvent[j])) {
						continue TOP;
					}
				}
			} else {
				for(int j = 0; j < resolvent.length; j++) {
					if (!indices.contains(resolvent[j])) {
						continue TOP;
					}
				}
			}
			ret.add(i);
		}
		return ret;
	}
	
	else throw new IndexOutOfBoundsException("invalid indices: " + indices);
}
 
Example #27
Source File: Clause.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the hashcode for this clause.  The hashcode for a clause is equivalent
 * to Ints.superFastHash(x) where x is an array such that its first this.size() 
 * elements are the literals of this clause (as returned by this.literals()) 
 * and its remaining this.numberOfAntecedents() elements are the hashCodes of 
 * this.antecedents (as returned by this.antecedents()).
 * @return hashcode for this clause
 */
public int hashCode() {
	int hash = size() + numberOfAntecedents();
	for(IntIterator iter = literals(); iter.hasNext(); ) {
		hash = Ints.superFastHashIncremental(iter.next(), hash);
	}
	for(Iterator<Clause> iter = antecedents(); iter.hasNext(); ) {
		hash = Ints.superFastHashIncremental(iter.next().hashCode(), hash);
	}
	return Ints.superFastHashAvalanche(hash);
}
 
Example #28
Source File: LeafInterpreter.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Populates the {@code vars} map with bindings from each relation in {@code rels} to an integer range,
 * which specifies the identifiers of the variables used to encode the contents of that relation.  The 
 * resulting integer ranges put together form a complete range that starts at {@code minVar}.
 * @requires lowers.universe = uppers.universe 
 * @requires all r: rels | lowers.get(r).tuples in uppers.get(r).tuples 
 * @ensures vars.map' = vars.map ++ 
 *          { r: rels, v: IntRange | v.size() = uppers.get(r).size() - lowers.get(r).size() && v.size() > 0 }
 * @ensures min(vars.map'[rels]) = minVar && max(vars.map'[rels]) = minVar + (sum r: rels | vars.map'[r].size()) - 1
 * @return sum r: rels | vars.map'[r].size()
 */
private static int allocateVars(int minVar, Map<Relation, IntRange> vars, Set<Relation> rels, Map<Relation, TupleSet> lowers, Map<Relation, TupleSet> uppers) {
	int maxLit = minVar;
	for(Relation r : rels) {
		int rLits = uppers.get(r).size() - lowers.get(r).size();
		if (rLits > 0) {
			vars.put(r, Ints.range(maxLit, maxLit + rLits - 1));
			maxLit += rLits;
		}
	}
	return maxLit - minVar;
}
 
Example #29
Source File: LeafInterpreter.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns this.vars.
 * @return this.vars.
 */
public final Map<Relation, IntSet> vars() {
	final Map<Relation, IntSet> ret = new LinkedHashMap<Relation,IntSet>((vars.size() * 4)/3);
	for(Map.Entry<Relation, IntRange> e: vars.entrySet()) {
		ret.put(e.getKey(), Ints.rangeSet(e.getValue()));
	}
	return ret;
}
 
Example #30
Source File: SymmetryDetector.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Constructs a new SymmetryDetector for the given bounds.
 * @ensures this.bounds' = bounds
 */
private SymmetryDetector(Bounds bounds) {
	this.bounds = bounds;
	this.usize = bounds.universe().size();
	
       //	start with the maximum partition -- the whole universe.
	this.parts = new LinkedList<IntSet>();
	final IntSet set = Ints.bestSet(usize);
	for(int i = 0; i < usize; i++) { set.add(i); }
	this.parts.add(set);
}