Beispiel #1
0
        public void testInfluenceSemantics()
        {
            Assert.AreEqual(1, a.getChildren().Count);
            Assert.IsTrue(a.getChildren().Contains(c));
            Assert.AreEqual(0, a.getParents().Count);

            Assert.AreEqual(1, b.getChildren().Count);
            Assert.IsTrue(b.getChildren().Contains(c));
            Assert.AreEqual(0, b.getParents().Count);

            Assert.AreEqual(2, c.getChildren().Count);
            Assert.IsTrue(c.getChildren().Contains(d));
            Assert.IsTrue(c.getChildren().Contains(e));
            Assert.AreEqual(2, c.getParents().Count);
            Assert.IsTrue(c.getParents().Contains(a));
            Assert.IsTrue(c.getParents().Contains(b));

            Assert.AreEqual(0, d.getChildren().Count);
            Assert.AreEqual(1, d.getParents().Count);
            Assert.IsTrue(d.getParents().Contains(c));

            Assert.AreEqual(0, e.getChildren().Count);
            Assert.AreEqual(1, e.getParents().Count);
            Assert.IsTrue(e.getParents().Contains(c));
        }
Beispiel #2
0
        public double probabilityOf(String Y, bool value,
                                    Dictionary <String, bool> evidence)
        {
            BayesNetNode y = getNodeOf(Y);

            if (y == null)
            {
                throw new ApplicationException("Unable to find a node with variable "
                                               + Y);
            }
            else
            {
                List <BayesNetNode> parentNodes = y.getParents();
                if (parentNodes.Count == 0)
                {// root nodes
                    Dictionary <String, bool> YTable = new Dictionary <String, bool>();
                    YTable.Add(Y, value);

                    double prob = y.probabilityOf(YTable);
                    return(prob);
                }
                else
                {// non rootnodes
                    Dictionary <String, bool> parentValues = new Dictionary <String, bool>();
                    foreach (BayesNetNode parent in parentNodes)
                    {
                        parentValues.Add(parent.getVariable(), evidence[parent
                                                                        .getVariable()]);
                    }
                    double prob = y.probabilityOf(parentValues);
                    if (value.Equals(true))
                    {
                        return(prob);
                    }
                    else
                    {
                        return(1.0 - prob);
                    }
                }
            }
        }
Beispiel #3
0
        private List <BayesNetNode> markovBlanket(BayesNetNode node,
                                                  List <BayesNetNode> soFar)
        {
            // parents
            List <BayesNetNode> parents = node.getParents();

            foreach (BayesNetNode parent in parents)
            {
                if (!soFar.Contains(parent))
                {
                    soFar.Add(parent);
                }
            }
            // children
            List <BayesNetNode> children = node.getChildren();

            foreach (BayesNetNode child in children)
            {
                if (!soFar.Contains(child))
                {
                    soFar.Add(child);
                    List <BayesNetNode> childsParents = child.getParents();
                    foreach (BayesNetNode childsParent in childsParents)
                    {
                        ;
                        if ((!soFar.Contains(childsParent)) &&
                            (!(childsParent.Equals(node))))
                        {
                            soFar.Add(childsParent);
                        }
                    } // childsParents
                }     // end contains child
            }         // end child

            return(soFar);
        }
Beispiel #4
0
        private List<BayesNetNode> markovBlanket(BayesNetNode node,
                List<BayesNetNode> soFar)
        {
            // parents
            List<BayesNetNode> parents = node.getParents();
            foreach (BayesNetNode parent in parents)
            {
                if (!soFar.Contains(parent))
                {
                    soFar.Add(parent);
                }
            }
            // children
            List<BayesNetNode> children = node.getChildren();
            foreach (BayesNetNode child in children)
            {
                if (!soFar.Contains(child))
                {
                    soFar.Add(child);
                    List<BayesNetNode> childsParents = child.getParents();
                    foreach (BayesNetNode childsParent in childsParents)
                    {
                        ;
                        if ((!soFar.Contains(childsParent))
                                && (!(childsParent.Equals(node))))
                        {
                            soFar.Add(childsParent);
                        }
                    }// childsParents
                }// end contains child

            }// end child

            return soFar;
        }