Пример #1
0
        //
        // PRIVATE METHODS
        //
        private BayesNet createWetGrassNetwork()
        {
            BayesNetNode cloudy    = new BayesNetNode("Cloudy");
            BayesNetNode sprinkler = new BayesNetNode("Sprinkler");
            BayesNetNode rain      = new BayesNetNode("Rain");
            BayesNetNode wetGrass  = new BayesNetNode("WetGrass");

            sprinkler.influencedBy(cloudy);
            rain.influencedBy(cloudy);
            wetGrass.influencedBy(rain, sprinkler);

            cloudy.setProbability(true, 0.5);
            sprinkler.setProbability(true, 0.10);
            sprinkler.setProbability(false, 0.50);

            rain.setProbability(true, 0.8);
            rain.setProbability(false, 0.2);

            wetGrass.setProbability(true, true, 0.99);
            wetGrass.setProbability(true, false, 0.90);
            wetGrass.setProbability(false, true, 0.90);
            wetGrass.setProbability(false, false, 0.00);

            BayesNet net = new BayesNet(cloudy);

            return(net);
        }
Пример #2
0
        public double[] mcmcAsk(String X, Dictionary <String, bool> evidence,
                                int numberOfVariables, Randomizer r)
        {
            double[]                  retval = new double[2];
            List <String>             _nonEvidenceVariables = nonEvidenceVariables(evidence, X);
            Dictionary <String, bool> evt = createRandomEvent(
                _nonEvidenceVariables, evidence, r);

            for (int j = 0; j < numberOfVariables; j++)
            {
                foreach (string variable in _nonEvidenceVariables)
                {
                    BayesNetNode              node    = getNodeOf(variable);
                    List <BayesNetNode>       blanket = markovBlanket(node);
                    Dictionary <string, bool> mb      = createMBValues(blanket, evt);
                    // event.put(node.getVariable(), node.isTrueFor(
                    // r.getProbability(), mb));
                    evt[node.getVariable()] = truthValue(rejectionSample(node
                                                                         .getVariable(), mb, 100, r), r);
                    bool queryValue = evt[X];
                    if (queryValue)
                    {
                        retval[0] += 1;
                    }
                    else
                    {
                        retval[1] += 1;
                    }
                }
            }
            return(Util.normalize(retval));
        }
Пример #3
0
        //
        // PRIVATE METHODS
        // 
        private BayesNet createBurglaryNetwork()
        {
            BayesNetNode burglary = new BayesNetNode("Burglary");
            BayesNetNode earthquake = new BayesNetNode("EarthQuake");
            BayesNetNode alarm = new BayesNetNode("Alarm");
            BayesNetNode johnCalls = new BayesNetNode("JohnCalls");
            BayesNetNode maryCalls = new BayesNetNode("MaryCalls");

            alarm.influencedBy(burglary, earthquake);
            johnCalls.influencedBy(alarm);
            maryCalls.influencedBy(alarm);

            burglary.setProbability(true, 0.001);// TODO behaviour changes if
            // root node
            earthquake.setProbability(true, 0.002);

            alarm.setProbability(true, true, 0.95);
            alarm.setProbability(true, false, 0.94);
            alarm.setProbability(false, true, 0.29);
            alarm.setProbability(false, false, 0.001);

            johnCalls.setProbability(true, 0.90);
            johnCalls.setProbability(false, 0.05);

            maryCalls.setProbability(true, 0.70);
            maryCalls.setProbability(false, 0.01);

            BayesNet net = new BayesNet(burglary, earthquake);
            return net;
        }
Пример #4
0
        //
        // PRIVATE METHODS
        //
        private BayesNet createBurglaryNetwork()
        {
            BayesNetNode burglary   = new BayesNetNode("Burglary");
            BayesNetNode earthquake = new BayesNetNode("EarthQuake");
            BayesNetNode alarm      = new BayesNetNode("Alarm");
            BayesNetNode johnCalls  = new BayesNetNode("JohnCalls");
            BayesNetNode maryCalls  = new BayesNetNode("MaryCalls");

            alarm.influencedBy(burglary, earthquake);
            johnCalls.influencedBy(alarm);
            maryCalls.influencedBy(alarm);

            burglary.setProbability(true, 0.001);
            earthquake.setProbability(true, 0.002);

            alarm.setProbability(true, true, 0.95);
            alarm.setProbability(true, false, 0.94);
            alarm.setProbability(false, true, 0.29);
            alarm.setProbability(false, false, 0.001);

            johnCalls.setProbability(true, 0.90);
            johnCalls.setProbability(false, 0.05);

            maryCalls.setProbability(true, 0.70);
            maryCalls.setProbability(false, 0.01);

            BayesNet net = new BayesNet(burglary, earthquake);

            return(net);
        }
Пример #5
0
        public void setUp()
        {
            a = new BayesNetNode("A");
            b = new BayesNetNode("B");
            c = new BayesNetNode("C");
            d = new BayesNetNode("D");
            e = new BayesNetNode("E");

            c.influencedBy(a, b);
            d.influencedBy(c);
            e.influencedBy(c);
        }
Пример #6
0
        public void setUp()
        {
            a = new BayesNetNode("A");
            b = new BayesNetNode("B");
            c = new BayesNetNode("C");
            d = new BayesNetNode("D");
            e = new BayesNetNode("E");

            c.influencedBy(a, b);
            d.influencedBy(c);
            e.influencedBy(c);
        }
Пример #7
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);
                    }
                }
            }
        }
Пример #8
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);
        }
Пример #9
0
 public BayesNet(BayesNetNode root1, BayesNetNode root2, BayesNetNode root3) : this(root1, root2)
 {
     roots.Add(root3);
 }
Пример #10
0
        //
        // PRIVATE METHODS
        //
        private BayesNet createWetGrassNetwork()
        {
            BayesNetNode cloudy = new BayesNetNode("Cloudy");
            BayesNetNode sprinkler = new BayesNetNode("Sprinkler");
            BayesNetNode rain = new BayesNetNode("Rain");
            BayesNetNode wetGrass = new BayesNetNode("WetGrass");

            sprinkler.influencedBy(cloudy);
            rain.influencedBy(cloudy);
            wetGrass.influencedBy(rain, sprinkler);

            cloudy.setProbability(true, 0.5);
            sprinkler.setProbability(true, 0.10);
            sprinkler.setProbability(false, 0.50);

            rain.setProbability(true, 0.8);
            rain.setProbability(false, 0.2);

            wetGrass.setProbability(true, true, 0.99);
            wetGrass.setProbability(true, false, 0.90);
            wetGrass.setProbability(false, true, 0.90);
            wetGrass.setProbability(false, false, 0.00);

            BayesNet net = new BayesNet(cloudy);
            return net;
        }
Пример #11
0
 public BayesNet(BayesNetNode root1, BayesNetNode root2) : this(root1)
 {
     roots.Add(root2);
 }
Пример #12
0
 public BayesNet(BayesNetNode root1, BayesNetNode root2, BayesNetNode root3) : this(root1, root2)
 {
     
     roots.Add(root3);
 }
Пример #13
0
 private List <BayesNetNode> markovBlanket(BayesNetNode node)
 {
     return(markovBlanket(node, new List <BayesNetNode>()));
 }
Пример #14
0
 public BayesNet(BayesNetNode root)
 {
     roots.Add(root);
 }
Пример #15
0
 public BayesNet(BayesNetNode root1, BayesNetNode root2) : this(root1)
 {
     roots.Add(root2);
 }
Пример #16
0
 public BayesNet(BayesNetNode root)
 {
     roots.Add(root);
 }
Пример #17
0
 private List<BayesNetNode> markovBlanket(BayesNetNode node)
 {
     return markovBlanket(node, new List<BayesNetNode>());
 }
Пример #18
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;
        }