Exemplo n.º 1
0
 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);
     }
 }
Exemplo n.º 2
0
        public void testIncrement()
        {
            MixedRadixNumber mrn = new MixedRadixNumber(0, new int[] { 3, 2 });
            int i = 0;

            while (mrn.Increment())
            {
                ++i;
            }
            Assert.AreEqual(i, mrn.GetMaxAllowedValue());
        }
Exemplo n.º 3
0
        // 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(IIterator pti)
        {
            IMap <IRandomVariable, object> possibleWorld = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, object>();
            MixedRadixNumber mrn = new MixedRadixNumber(0, radices);

            do
            {
                foreach (RVInfo rvInfo in randomVarInfo.GetValues())
                {
                    possibleWorld.Put(rvInfo.getVariable(), rvInfo
                                      .getDomainValueAt(mrn.GetCurrentNumeralValue(rvInfo
                                                                                   .getRadixIdx())));
                }
                pti.iterate(possibleWorld, values[mrn.IntValue()]);
            } while (mrn.Increment());
        }
Exemplo n.º 4
0
        /**
         * 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());
            }
        }