public static void main(String[] args) { BDDFactory bdd = init(1000000, 100000); System.out.println("One: " + CUDDFactory.one); System.out.println("Zero: " + CUDDFactory.zero); BDDDomain[] doms = bdd.extDomain(new int[] {50, 10, 15, 20, 15}); BDD b = bdd.one(); for (int i = 0; i < doms.length - 1; ++i) { b.andWith(doms[i].ithVar(i)); } for (int i = 0; i < bdd.numberOfDomains(); ++i) { BDDDomain d = bdd.getDomain(i); int[] ivar = d.vars(); System.out.print("Domain #" + i + ":"); for (int j = 0; j < ivar.length; ++j) { System.out.print(' '); System.out.print(j); System.out.print(':'); System.out.print(ivar[j]); } System.out.println(); } BDDPairing p = bdd.makePair(doms[2], doms[doms.length - 1]); System.out.println("Pairing: " + p); System.out.println("Before replace(): " + b); BDD c = b.replace(p); System.out.println("After replace(): " + c); c.printDot(); }
/* (non-Javadoc) * @see com.epochx.core.scorer.SemanticScorer#doScore(com.epochx.representation.CandidateProgram, com.epochx.representation.CandidateProgram) */ public double doScore(GPCandidateProgram program1, GPCandidateProgram program2) { double score; // TODO figure out a better way of doing this if (super.getSemanticModule() instanceof BooleanSemanticModule) { ((BooleanSemanticModule) super.getSemanticModule()).start(); } BooleanRepresentation program1Representation = (BooleanRepresentation) getSemanticModule().codeToBehaviour(program1); BooleanRepresentation program2Representation = (BooleanRepresentation) getSemanticModule().codeToBehaviour(program2); BDD rep1 = program1Representation.getBDD(); BDD rep2 = program2Representation.getBDD(); BDD diffRep1 = rep1.and(rep2.not()); BDD diffRep2 = rep2.and(rep1.not()); BDD finalDiff = diffRep1.or(diffRep2); score = finalDiff.satCount() * 100; // TODO figure out a better way of doing this if (super.getSemanticModule() instanceof BooleanSemanticModule) { ((BooleanSemanticModule) super.getSemanticModule()).stop(); } return score; }
public BDD varRange(BigInteger lo, BigInteger hi) { if (lo.signum() < 0 || hi.compareTo(size()) >= 0 || lo.compareTo(hi) > 0) { throw new BDDException("range <" + lo + ", " + hi + "> is invalid"); } BDDFactory factory = getFactory(); BDD result = factory.zero(); int[] ivar = this.vars(); while (lo.compareTo(hi) <= 0) { BDD v = factory.universe(); for (int n = ivar.length - 1; ; n--) { if (lo.testBit(n)) { v.andWith(factory.ithVar(ivar[n])); } else { v.andWith(factory.nithVar(ivar[n])); } BigInteger mask = BigInteger.ONE.shiftLeft(n).subtract(BigInteger.ONE); if (!lo.testBit(n) && lo.or(mask).compareTo(hi) <= 0) { lo = lo.or(mask).add(BigInteger.ONE); break; } } result.orWith(v); } return result; }
/** * Builds a BDD which is true for all the possible assignments to the variable blocks that makes * the blocks equal. * * <p>Compare to fdd_equals/fdd_equ. * * @param that * @return BDD */ public BDD buildEquals(BDDDomain that) { if (!this.size().equals(that.size())) { throw new BDDException( "Size of " + this + " != size of that " + that + "( " + this.size() + " vs " + that.size() + ")"); } BDDFactory factory = getFactory(); BDD e = factory.universe(); int[] this_ivar = this.vars(); int[] that_ivar = that.vars(); for (int n = 0; n < this.varNum(); n++) { BDD a = factory.ithVar(this_ivar[n]); BDD b = factory.ithVar(that_ivar[n]); a.biimpWith(b); e.andWith(a); } return e; }
/** * Convert a BDD that to a list of indices of this domain. Same as getVarIndices(BDD), except only * 'max' indices are extracted. * * @param bdd bdd that is the disjunction of domain indices * @param max maximum number of entries to be returned * @see #ithVar(long) */ public BigInteger[] getVarIndices(BDD bdd, int max) { BDDVarSet myvarset = set(); // can't use var here, must respect subclass a factory may provide int n = (int) bdd.satCount(myvarset); if (max != -1 && n > max) n = max; BigInteger[] res = new BigInteger[n]; BDDIterator it = bdd.iterator(myvarset); myvarset.free(); for (int i = 0; i < n; i++) { res[i] = it.nextValue(this); } return res; }
static Set<Integer> setOf(BDD bdd, BDDDomain dom) { if (bdd == null) return null; Set<Integer> set = new HashSet<Integer>(); if (bdd.isZero()) return set; BDDIterator itr = bdd.iterator(dom.set()); while (itr.hasNext()) { BDD a = itr.nextBDD(); set.add(a.scanVar(dom).intValue()); a.free(); } return set; }
/** * Returns what corresponds to a disjunction of all possible values of this domain. This is more * efficient than doing ithVar(0) OR ithVar(1) ... explicitly for all values in the domain. * * <p>Compare to fdd_domain. */ public BDD domain() { BDDFactory factory = getFactory(); /* Encode V<=X-1. V is the variables in 'var' and X is the domain size */ BigInteger val = size().subtract(BigInteger.ONE); BDD d = factory.universe(); int[] ivar = vars(); for (int n = 0; n < this.varNum(); n++) { if (val.testBit(0)) d.orWith(factory.nithVar(ivar[n])); else d.andWith(factory.nithVar(ivar[n])); val = val.shiftRight(1); } return d; }
@Test public void testOverlapDomain() { BDDFactory factory = BDDFactory.init("cudd", 10000, 10000); BDDDomain[] doms = factory.extDomain(new long[] {8, 8}); Assert.assertEquals(8, doms[0].size().longValue()); Assert.assertEquals(8, doms[1].size().longValue()); BDD bdd = doms[0].ithVar(5); Assert.assertEquals(5, bdd.scanVar(doms[0]).intValue()); doms[0] = factory.overlapDomain(doms[0], doms[1]); Assert.assertEquals(64, doms[0].size().longValue()); // Corrupted Assert.assertEquals(5, bdd.scanVar(doms[0]).intValue()); }
public BDD ithVar(BigInteger val) { if (val.signum() < 0 || val.compareTo(size()) >= 0) { throw new BDDException(val + " is out of range"); } BDDFactory factory = getFactory(); BDD v = factory.universe(); int[] ivar = this.vars(); for (int n = 0; n < ivar.length; n++) { if (val.testBit(0)) v.andWith(factory.ithVar(ivar[n])); else v.andWith(factory.nithVar(ivar[n])); val = val.shiftRight(1); } return v; }
/** Bug in JavaBDD. The method varRange does not work correctly. */ @Test public void testVarRange() { BDDFactory factory = BDDFactory.init("cudd", 10000, 10000); BDDDomain[] doms = factory.extDomain(new long[] {8}); BDD bdd = doms[0].varRange(2, 5); System.out.println(bdd.toStringWithDomains()); HashSet<Integer> set = new HashSet<Integer>(Arrays.asList(2, 3, 4, 5)); BDDIterator itr = bdd.iterator(doms[0].var); while (itr.hasNext()) { BDD tmp = itr.nextBDD(); Assert.assertTrue(set.contains(tmp.scanVar(doms[0]).longValue())); } bdd.free(); factory.done(); }
/* (non-Javadoc) * @see net.sf.javabdd.BDDFactory#nithVar(int) */ public BDD nithVar(int var) { BDD b = ithVar(var); BDD c = b.not(); b.free(); return c; }
public BDD buildAdd(BDDDomain that, int bits, long value) { if (bits > this.varNum() || bits > that.varNum()) throw new BDDException( "Number of bits requested (" + bits + ") is larger than domain sizes " + this.varNum() + "," + that.varNum()); BDDFactory bdd = getFactory(); if (value == 0L) { BDD result = bdd.universe(); int n; for (n = 0; n < bits; n++) { BDD b = bdd.ithVar(this.ivar[n]); b.biimpWith(bdd.ithVar(that.ivar[n])); result.andWith(b); } for (; n < Math.max(this.varNum(), that.varNum()); n++) { BDD b = (n < this.varNum()) ? bdd.nithVar(this.ivar[n]) : bdd.one(); b.andWith((n < that.varNum()) ? bdd.nithVar(that.ivar[n]) : bdd.one()); result.andWith(b); } return result; } int[] vars = new int[bits]; System.arraycopy(this.ivar, 0, vars, 0, vars.length); BDDBitVector y = bdd.buildVector(vars); BDDBitVector v = bdd.constantVector(bits, value); BDDBitVector z = y.add(v); int[] thatvars = new int[bits]; System.arraycopy(that.ivar, 0, thatvars, 0, thatvars.length); BDDBitVector x = bdd.buildVector(thatvars); BDD result = bdd.one(); int n; for (n = 0; n < x.size(); n++) { BDD b = x.bitvec[n].biimp(z.bitvec[n]); result.andWith(b); } for (; n < Math.max(this.varNum(), that.varNum()); n++) { BDD b = (n < this.varNum()) ? bdd.nithVar(this.ivar[n]) : bdd.one(); b.andWith((n < that.varNum()) ? bdd.nithVar(that.ivar[n]) : bdd.one()); result.andWith(b); } x.free(); y.free(); z.free(); v.free(); return result; }