예제 #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);
     }
 }
예제 #2
0
        public void testCurrentNumberalValue()
        {
            MixedRadixNumber mrn;

            //
            mrn = new MixedRadixNumber(0, new int[] { 3, 3, 2, 2 });
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(0));
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(1));
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(2));
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(3));
            //
            mrn = new MixedRadixNumber(35, new int[] { 3, 3, 2, 2 });
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0));
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1));
            Assert.AreEqual(1, mrn.GetCurrentNumeralValue(2));
            Assert.AreEqual(1, mrn.GetCurrentNumeralValue(3));
            //
            mrn = new MixedRadixNumber(25, new int[] { 3, 3, 2, 2 });
            Assert.AreEqual(1, mrn.GetCurrentNumeralValue(0));
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1));
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(2));
            Assert.AreEqual(1, mrn.GetCurrentNumeralValue(3));
            //
            mrn = new MixedRadixNumber(17, new int[] { 3, 3, 2, 2 });
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0));
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1));
            Assert.AreEqual(1, mrn.GetCurrentNumeralValue(2));
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(3));
            //
            mrn = new MixedRadixNumber(8, new int[] { 3, 3, 2, 2 });
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0));
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1));
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(2));
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(3));
            //
            mrn = new MixedRadixNumber(359, new int[] { 3, 4, 5, 6 });
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0));
            Assert.AreEqual(3, mrn.GetCurrentNumeralValue(1));
            Assert.AreEqual(4, mrn.GetCurrentNumeralValue(2));
            Assert.AreEqual(5, mrn.GetCurrentNumeralValue(3));
            //
            mrn = new MixedRadixNumber(359, new int[] { 6, 5, 4, 3 });
            Assert.AreEqual(5, mrn.GetCurrentNumeralValue(0));
            Assert.AreEqual(4, mrn.GetCurrentNumeralValue(1));
            Assert.AreEqual(3, mrn.GetCurrentNumeralValue(2));
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(3));
        }
예제 #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());
        }
예제 #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());
            }
        }
예제 #5
0
        public void testSetCurrentValueFor()
        {
            MixedRadixNumber mrn;

            //
            mrn = new MixedRadixNumber(0, new int[] { 3, 3, 2, 2 });
            mrn.SetCurrentValueFor(new int[] { 0, 0, 0, 0 });
            Assert.AreEqual(0, mrn.IntValue());
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(0));
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(1));
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(2));
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(3));
            //
            mrn = new MixedRadixNumber(0, new int[] { 3, 3, 2, 2 });
            mrn.SetCurrentValueFor(new int[] { 2, 2, 1, 1 });
            Assert.AreEqual(35, mrn.IntValue());
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0));
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1));
            Assert.AreEqual(1, mrn.GetCurrentNumeralValue(2));
            Assert.AreEqual(1, mrn.GetCurrentNumeralValue(3));
            //
            mrn = new MixedRadixNumber(0, new int[] { 3, 3, 2, 2 });
            mrn.SetCurrentValueFor(new int[] { 1, 2, 0, 1 });
            Assert.AreEqual(25, mrn.IntValue());
            Assert.AreEqual(1, mrn.GetCurrentNumeralValue(0));
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1));
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(2));
            Assert.AreEqual(1, mrn.GetCurrentNumeralValue(3));
            //
            mrn = new MixedRadixNumber(0, new int[] { 3, 3, 2, 2 });
            mrn.SetCurrentValueFor(new int[] { 2, 2, 1, 0 });
            Assert.AreEqual(17, mrn.IntValue());
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0));
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1));
            Assert.AreEqual(1, mrn.GetCurrentNumeralValue(2));
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(3));
            //
            mrn = new MixedRadixNumber(0, new int[] { 3, 3, 2, 2 });
            mrn.SetCurrentValueFor(new int[] { 2, 2, 0, 0 });
            Assert.AreEqual(8, mrn.IntValue());
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0));
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(1));
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(2));
            Assert.AreEqual(0, mrn.GetCurrentNumeralValue(3));
            //
            mrn = new MixedRadixNumber(0, new int[] { 3, 4, 5, 6 });
            mrn.SetCurrentValueFor(new int[] { 2, 3, 4, 5 });
            Assert.AreEqual(359, mrn.IntValue());
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(0));
            Assert.AreEqual(3, mrn.GetCurrentNumeralValue(1));
            Assert.AreEqual(4, mrn.GetCurrentNumeralValue(2));
            Assert.AreEqual(5, mrn.GetCurrentNumeralValue(3));
            //
            mrn = new MixedRadixNumber(0, new int[] { 6, 5, 4, 3 });
            mrn.SetCurrentValueFor(new int[] { 5, 4, 3, 2 });
            Assert.AreEqual(359, mrn.IntValue());
            Assert.AreEqual(5, mrn.GetCurrentNumeralValue(0));
            Assert.AreEqual(4, mrn.GetCurrentNumeralValue(1));
            Assert.AreEqual(3, mrn.GetCurrentNumeralValue(2));
            Assert.AreEqual(2, mrn.GetCurrentNumeralValue(3));
        }