예제 #1
0
  /**
   * Pointwise multiplication of this Factor by a given multiplier, creating a
   * new Factor representing the product of the two.<br>
   * <br>
   * see: AIMA3e Figure 14.10 page 527.<br>
   * <br>
   * Note: Default Factor multiplication is not commutative. The reason is
   * because the order of the variables comprising a Factor dictate the
   * ordering of the values for that factor. The default order of the
   * variables of the Factor returned is the order of the variables as they
   * are seen, as read from the left to right term, for e.g.: <br>
   * <br>
   * f<sub>1</sub>(Y)f<sub>2</sub>(X, Y)<br>
   * <br>
   * would give a Factor of the following form: <br>
   * Y, X<br>
   * <br>
   * i.e. an ordered union of the variables from the two factors. <br>
   * To override the default order of the product use pointwiseProductPOS().
   * 
   * @param multiplier
   * 
   * @return a new Factor representing the pointwise product of this and the
   *         passed in multiplier. The order of the variables comprising the
   *         product factor is the ordered union of the left term (this) and
   *         the right term (multiplier).
   * 
   * @see Factor#pointwiseProductPOS(Factor, RandomVariable...)
   */
 abstract public Factor pointwiseProduct(Factor multiplier);
예제 #2
0
        /**
         * Pointwise Multiplication - Product Order Specified (POS).<br>
         * <br>
         * see: AIMA3e Figure 14.10 page 527.<br>
         * <br>
         * Pointwise multiplication of this Factor by a given multiplier, creating a
         * new Factor representing the product of the two. The order of the
         * variables comprising the product will match those specified.
         * 
         * @param multiplier
         * @param prodVarOrder
         *            the order the variables comprising the product are to be in.
         * 
         * @return a new Factor representing the pointwise product of this and the
         *         passed in multiplier. The order of the variables comprising the
         *         product distribution is the order specified.
         * 
         * @see Factor#pointwiseProduct(Factor)
         */

       abstract public Factor pointwiseProductPOS(Factor multiplier,
                                   params RandomVariable[] prodVarOrder);
예제 #3
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(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());
            }
        }
예제 #4
0
 public FactorIteratorAdapter(Factor.Iterator fi)
 {
     this.fi = fi;
 }
예제 #5
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(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());
        }
예제 #6
0
 public void iterateOver(Factor.Iterator fi,
                         params AssignmentProposition[] fixedValues)
 {
     iterateOverTable(new FactorIteratorAdapter(fi), fixedValues);
 }
예제 #7
0
 public void iterateOver(Factor.Iterator fi)
 {
     iterateOverTable(new FactorIteratorAdapter(fi));
 }
예제 #8
0
 public Factor pointwiseProductPOS(Factor multiplier,
                                   params RandomVariable[] prodVarOrder)
 {
     return pointwiseProductPOS((ProbabilityTable) multiplier, prodVarOrder);
 }
예제 #9
0
 public Factor pointwiseProduct(Factor multiplier)
 {
     return pointwiseProduct((ProbabilityTable) multiplier);
 }