public void testIncrement() { MixedRadixNumber mrn = new MixedRadixNumber(0, new int[] { 3, 2 }); int i = 0; while (mrn.increment()) { i++; } Assert.AreEqual(i, mrn.getMaxAllowedValue()); }
// END-Factor // /** * Iterate over all the possible value assignments for the Random Variables * comprising this ProbabilityTable. * * @param pti * the ProbabilityTable Iterator to iterate. */ public void iterateOverTable(Factor.Iterator pti) { Map <RandomVariable, Object> possibleWorld = new LinkedHashMap <RandomVariable, Object>(); MixedRadixNumber mrn = new MixedRadixNumber(0, radices); do { foreach (RVInfo rvInfo in randomVarInfo.Values) { possibleWorld.put(rvInfo.getVariable(), rvInfo .getDomainValueAt(mrn.getCurrentNumeralValue(rvInfo . getRadixIdx ()))); } pti.iterate(possibleWorld, values[mrn.intValue()]); } while (mrn.increment()); }
/** * Iterate over all possible values assignments for the Random Variables * comprising this ProbabilityTable that are not in the fixed set of values. * This allows you to iterate over a subset of possible combinations. * * @param pti * the ProbabilityTable Iterator to iterate * @param fixedValues * Fixed values for a subset of the Random Variables comprising * this Probability Table. */ public void iterateOverTable(Factor.Iterator pti, params AssignmentProposition[] fixedValues) { Map <RandomVariable, Object> possibleWorld = new LinkedHashMap <RandomVariable, Object>(); MixedRadixNumber tableMRN = new MixedRadixNumber(0, radices); int[] tableRadixValues = new int[radices.Length]; // Assert that the Random Variables for the fixed values // are part of this probability table and assign // all the fixed values to the possible world. foreach (AssignmentProposition ap in fixedValues) { if (!randomVarInfo.ContainsKey(ap.getTermVariable())) { throw new ArgumentException("Assignment proposition [" + ap + "] does not belong to this probability table."); } possibleWorld.Add(ap.getTermVariable(), ap.getValue()); RVInfo fixedRVI = randomVarInfo.get(ap.getTermVariable()); tableRadixValues[fixedRVI.getRadixIdx()] = fixedRVI .getIdxForDomain(ap.getValue()); } // If have assignments for all the random variables // in this probability table if (fixedValues.Length == randomVarInfo.Count) { // Then only 1 iteration call is required. pti.iterate(possibleWorld, getValue(fixedValues)); } else { // Else iterate over the non-fixed values List <RandomVariable> freeVariables = SetOps.difference( new List <RandomVariable>(randomVarInfo.Keys), new List <RandomVariable>(possibleWorld.Keys)); Map <RandomVariable, RVInfo> freeVarInfo = new LinkedHashMap <RandomVariable, RVInfo>(); // Remove the fixed Variables foreach (RandomVariable fv in freeVariables) { freeVarInfo.put(fv, new RVInfo(fv)); } int[] freeRadixValues = createRadixs(freeVarInfo); MixedRadixNumber freeMRN = new MixedRadixNumber(0, freeRadixValues); Object fval = null; // Iterate through all combinations of the free variables do { // Put the current assignments for the free variables // into the possible world and update // the current index in the table MRN foreach (RVInfo freeRVI in freeVarInfo.values()) { fval = freeRVI.getDomainValueAt(freeMRN .getCurrentNumeralValue(freeRVI.getRadixIdx())); possibleWorld.put(freeRVI.getVariable(), fval); tableRadixValues[randomVarInfo.get(freeRVI.getVariable()) .getRadixIdx()] = freeRVI.getIdxForDomain(fval); } pti.iterate(possibleWorld, values[(int)tableMRN .getCurrentValueFor(tableRadixValues)]); } while (freeMRN.increment()); } }