public void iterate(IMap <IRandomVariable, object> possibleWorld, double probability) { foreach (IRandomVariable rv in possibleWorld.GetKeys()) { RVInfo rvInfo = quotient.randomVarInfo.Get(rv); qRVs[rvInfo.getRadixIdx()] = rvInfo .getIdxForDomain(possibleWorld.Get(rv)); } if (null != diff) { // Start from 0 off the diff dMRN.SetCurrentValueFor(new int[diff.Size()]); do { foreach (IRandomVariable rv in diff.GetKeys()) { RVInfo drvInfo = diff.Get(rv); RVInfo qrvInfo = quotient.randomVarInfo.Get(rv); qRVs[qrvInfo.getRadixIdx()] = dMRN .GetCurrentNumeralValue(drvInfo .getRadixIdx()); } updateQuotient(probability); } while (dMRN.Increment()); } else { updateQuotient(probability); } }
public double getValue(params AssignmentProposition[] assignments) { if (assignments.Length != randomVarInfo.Size()) { throw new IllegalArgumentException( "Assignments passed in is not the same size as variables making up probability table."); } int[] radixValues = new int[assignments.Length]; foreach (AssignmentProposition ap in assignments) { RVInfo rvInfo = randomVarInfo.Get(ap.getTermVariable()); if (null == rvInfo) { throw new IllegalArgumentException( "Assignment passed for a variable that is not part of this probability table:" + ap.getTermVariable()); } radixValues[rvInfo.getRadixIdx()] = rvInfo.getIdxForDomain(ap .getValue()); } return(values[(int)queryMRN.GetCurrentValueFor(radixValues)]); }
/** * 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(IIterator pti, params AssignmentProposition[] fixedValues) { IMap <IRandomVariable, object> possibleWorld = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, 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 IllegalArgumentException("Assignment proposition [" + ap + "] does not belong to this probability table."); } possibleWorld.Put(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.Size()) { // Then only 1 iteration call is required. pti.iterate(possibleWorld, getValue(fixedValues)); } else { // Else iterate over the non-fixed values ISet <IRandomVariable> freeVariables = SetOps.difference( CollectionFactory.CreateSet <IRandomVariable>(this.randomVarInfo.GetKeys()), CollectionFactory.CreateSet <IRandomVariable>(possibleWorld.GetKeys())); IMap <IRandomVariable, RVInfo> freeVarInfo = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, RVInfo>(); // Remove the fixed Variables foreach (IRandomVariable 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.GetValues()) { 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()); } }