Пример #1
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;
        }
Пример #2
0
        private static double enumerateAll(BayesNet net, List <string> unprocessedVariables,
                                           Dictionary <String, bool> evidenceVariables)
        {
            if (unprocessedVariables.Count == 0)
            {
                return(1.0);
            }
            else
            {
                String Y = (String)unprocessedVariables[0];

                if (evidenceVariables.ContainsKey(Y))
                {
                    double probYGivenParents = net.probabilityOf(Y,
                                                                 evidenceVariables[Y], evidenceVariables);

                    double secondTerm = enumerateAll(net, Util
                                                     .rest(unprocessedVariables), evidenceVariables);

                    return(probYGivenParents * secondTerm);
                }
                else
                {
                    double sigma = 0.0;
                    Dictionary <String, bool> clone1 = cloneEvidenceVariables(evidenceVariables);
                    clone1.Add(Y, true);
                    double probYTrueGivenParents = net.probabilityOf(Y,
                                                                     true, clone1);

                    double secondTerm = enumerateAll(net, Util
                                                     .rest(unprocessedVariables), clone1);

                    double trueProbabilityY = probYTrueGivenParents * secondTerm;

                    Dictionary <String, bool> clone2 = cloneEvidenceVariables(evidenceVariables);
                    clone2.Add(Y, false);
                    double probYFalseGivenParents = net.probabilityOf(Y,
                                                                      false, clone2);

                    secondTerm = enumerateAll(net, Util.rest(unprocessedVariables),
                                              clone2);
                    double falseProbabilityY = probYFalseGivenParents * secondTerm;
                    // System.Console.Write(secondTerm + " ) )");
                    sigma = trueProbabilityY + falseProbabilityY;
                    return(sigma);
                }
            }
        }
Пример #3
0
        public static double[] ask(Query q, BayesNet net)
        {
            Dictionary <String, bool> evidenceVariables = q.getEvidenceVariables();

            double[] probDist = new double[2];
            // true probability
            evidenceVariables[q.getQueryVariable()] = true;
            probDist[0] = enumerateAll(net, net.getVariables(), evidenceVariables);
            // false probability
            evidenceVariables[q.getQueryVariable()] = false;
            probDist[1] = enumerateAll(net, net.getVariables(), evidenceVariables);
            // System.Console.WriteLine( probDist[0] + " " + probDist[1]);
            // return probDist;
            double[] normalized = Util.normalize(probDist);
            // System.Console.WriteLine( normalized[0] + " " + normalized[1]);
            return(normalized);
        }
        //
        // 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;
        }
Пример #5
0
	public void setUp() {
		net = createBurglaryNetwork();
	}