コード例 #1
0
ファイル: EnumerationAsk.cs プロジェクト: tvn-cosine/aima.net
        // END-BayesInference
        //

        //
        // PROTECTED METHODS
        //
        // function ENUMERATE-ALL(vars, e) returns a real number
        protected double enumerateAll(ICollection <IRandomVariable> vars, ObservedEvidence e)
        {
            // if EMPTY?(vars) then return 1.0
            if (0 == vars.Size())
            {
                return(1);
            }
            // Y <- FIRST(vars)
            IRandomVariable Y = Util.first(vars);

            // if Y has value y in e
            if (e.containsValue(Y))
            {
                // then return P(y | parents(Y)) * ENUMERATE-ALL(REST(vars), e)
                return(e.posteriorForParents(Y) * enumerateAll(Util.rest(vars), e));
            }

            /**
             * <pre>
             *  else return &sum;<sub>y</sub> P(y | parents(Y)) * ENUMERATE-ALL(REST(vars), e<sub>y</sub>)
             *       where e<sub>y</sub> is e extended with Y = y
             * </pre>
             */
            double sum = 0;

            foreach (object y in ((IFiniteDomain)Y.getDomain()).GetPossibleValues())
            {
                e.setExtendedValue(Y, y);
                sum += e.posteriorForParents(Y) * enumerateAll(Util.rest(vars), e);
            }

            return(sum);
        }
コード例 #2
0
ファイル: EnumerationAsk.cs プロジェクト: tvn-cosine/aima.net
 public ProbabilityTableIteratorImpl(IBayesianNetwork bn, ProbabilityTable q, ObservedEvidence e, IRandomVariable[] x, EnumerationAsk enumerationAsk)
 {
     this.bn             = bn;
     this.q              = q;
     this.e              = e;
     this.x              = x;
     this.enumerationAsk = enumerationAsk;
 }
コード例 #3
0
ファイル: EnumerationAsk.cs プロジェクト: tvn-cosine/aima.net
        // function ENUMERATION-ASK(X, e, bn) returns a distribution over X

        /**
         * The ENUMERATION-ASK algorithm in Figure 14.9 evaluates expression trees
         * (Figure 14.8) using depth-first recursion.
         *
         * @param X
         *            the query variables.
         * @param observedEvidence
         *            observed values for variables E.
         * @param bn
         *            a Bayes net with variables {X} &cup; E &cup; Y /* Y = hidden
         *            variables //
         * @return a distribution over the query variables.
         */
        public ICategoricalDistribution enumerationAsk(IRandomVariable[] X,
                                                       AssignmentProposition[] observedEvidence,
                                                       IBayesianNetwork bn)
        {
            // Q(X) <- a distribution over X, initially empty
            ProbabilityTable Q = new ProbabilityTable(X);
            ObservedEvidence e = new ObservedEvidence(X, observedEvidence, bn);

            // for each value x<sub>i</sub> of X do
            ProbabilityTable.ProbabilityTableIterator di = new ProbabilityTableIteratorImpl(bn, Q, e, X, this);
            Q.iterateOverTable(di);

            // return NORMALIZE(Q(X))
            return(Q.normalize());
        }
コード例 #4
0
        public CategoricalDistribution enumerationAsk(RandomVariable[] X,
                                                      AssignmentProposition[] observedEvidence,
                                                      BayesianNetwork bn)
        {
            // Q(X) <- a distribution over X, initially empty
            ProbabilityTable Q = new ProbabilityTable(X);
            ObservedEvidence e = new ObservedEvidence(X, observedEvidence, bn);

            // for each value x<sub>i</sub> of X do
            //    ProbabilityTable.Iterator di = new ProbabilityTable.Iterator()
            //                                       {
            //                                           int cnt = 0;

            //                                                   /**
            //     * <pre>
            //     * Q(x<sub>i</sub>) <- ENUMERATE-ALL(bn.VARS, e<sub>x<sub>i</sub></sub>)
            //     *   where e<sub>x<sub>i</sub></sub> is e extended with X = x<sub>i</sub>
            //     * </pre>
            //     */
            //                                                   public void iterate(Map<RandomVariable,
            //                                           Object > possibleWorld,
            //                                           double probability) {
            //                                                      for (int i = 0; i < X.length; i++) {
            //                                                      e.setExtendedValue(X[i],
            //                                           possibleWorld.get(X[i]));
            //                                       }
            //    Q.setValue(cnt,
            //               enumerateAll(bn.getVariablesInTopologicalOrder(), e));
            //    cnt++;
            //}
            //}
            //    ;
            //    Q.iterateOverTable(di);

            //    // return NORMALIZE(Q(X))
            //    return Q.normalize();
            // TODO: need to clean this up, fail for now
            return(null);
        }