/** * use the index of each slot, together with constraints only one queen in each row from different * angle e.g for var at position [0][0] has index 0, bdd && (NOT0 v NOT1) && (NOT0 v NOT2) && * (NOT0 v NOT3) && (NOT0 v NOT4) horizontal && (NOT0 v NOT5) && (NOT0 v NOT10) && (NOT0 v NOT15) * && (NOT0 v NOT20) vertical && (NOT0 v NOT6) && (NOT0 v NOT12) && (NOT0 v NOT18) && (NOT0 v * NOT24) diagonal && one queen per row rule oneQueen * * @return BDD */ public BDD build() { BDD bdd = factory.one(); for (int x = 0; x < size; x++) { BDD oneQueen = factory.zero(); for (int y = 0; y < size; y++) { oneQueen.orWith(factory.ithVar(idx(x, y))); BDD var = factory.nithVar(idx(x, y)); // get negated index value // horizontal for (int i = (y + 1); i < size; i++) { bdd.andWith(var.or(factory.nithVar(idx(x, i)))); } // vertical for (int i = (x + 1); i < size; i++) { bdd.andWith(var.or(factory.nithVar(idx(i, y)))); } // diagonal for (int i = 1; (y + i) < size; i++) { if ((x - i) >= 0) { bdd.andWith(var.or(factory.nithVar(idx(x - i, y + i)))); } if ((x + i) < size) { bdd.andWith(var.or(factory.nithVar(idx(x + i, y + i)))); } } } bdd.andWith(oneQueen); // one row has to/only can have one queen } return bdd; }
public BDD createPreBDD( BDDFactory factory, LinkedList<String> nAryVariables, LinkedList<BDD> nAryVariablesPreBDDs, boolean[] unusedVarIndices) { BDD ret; ret = pre.createBDD(factory, nAryVariables, nAryVariablesPreBDDs, null, true, unusedVarIndices); if (ret == null) return factory.one(); return ret; }
JavaBDDRegionManager(String bddPackage, Configuration config, LogManager pLogger) throws InvalidConfigurationException { config.inject(this); logger = pLogger; if (initTableRatio <= 0 || initTableRatio >= 1) { throw new InvalidConfigurationException( "Invalid value " + initTableRatio + " for option bdd.javabdd.initTableRatio, needs to be between 0 and 1."); } if (initTableSize == 0) { // JFactory uses 5 ints of 4 byte sizes for each entry in the BDD table double size = Runtime.getRuntime().maxMemory() * initTableRatio / 5 / 4; initTableSize = (size > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) size; logger.log(Level.CONFIG, "Setting value of bdd.javabdd.initTableSize to", initTableSize); } if (cacheRatio < 0) { throw new InvalidConfigurationException( "Invalid value " + cacheRatio + " for option bdd.javabdd.cacheRatio, cannot be negative."); } if (cacheSize == 0) { cacheSize = (int) (initTableSize * cacheRatio); } factory = BDDFactory.init(bddPackage.toLowerCase(), initTableSize, cacheSize); // register callbacks for logging try { Method gcCallback = JavaBDDRegionManager.class.getDeclaredMethod( "gcCallback", Integer.class, BDDFactory.GCStats.class); gcCallback.setAccessible(true); factory.registerGCCallback(this, gcCallback); Method resizeCallback = JavaBDDRegionManager.class.getDeclaredMethod( "resizeCallback", Integer.class, Integer.class); resizeCallback.setAccessible(true); factory.registerResizeCallback(this, resizeCallback); Method reorderCallback = JavaBDDRegionManager.class.getDeclaredMethod( "reorderCallback", Integer.class, BDDFactory.ReorderStats.class); reorderCallback.setAccessible(true); factory.registerReorderCallback(this, reorderCallback); // If we do not log, unregister the handlers to avoid the cost of // calling them with reflection. // Registering and immediately unregistering prevents the library // from printing stuff to stdout. if (!logger.wouldBeLogged(LOG_LEVEL)) { factory.unregisterGCCallback(this, gcCallback); factory.unregisterResizeCallback(this, resizeCallback); factory.unregisterReorderCallback(this, reorderCallback); } } catch (NoSuchMethodException e) { throw new AssertionError(e); } factory.setVarNum(varcount); factory.setCacheRatio(cacheRatio); trueFormula = new JavaBDDRegion(factory.one()); falseFormula = new JavaBDDRegion(factory.zero()); }