public void Execute(IExampleInterface app)
        {
            // build the bayesian network structure
            BayesianNetwork network = new BayesianNetwork();
            BayesianEvent BlueTaxi = network.CreateEvent("blue_taxi");
            BayesianEvent WitnessSawBlue = network.CreateEvent("saw_blue");
            network.CreateDependency(BlueTaxi, WitnessSawBlue);
            network.FinalizeStructure();
            // build the truth tales
            BlueTaxi.Table.AddLine(0.85, true);
            WitnessSawBlue.Table.AddLine(0.80, true, true);
            WitnessSawBlue.Table.AddLine(0.20, true, false);

            // validate the network
            network.Validate();
            // display basic stats
            Console.WriteLine(network.ToString());
            Console.WriteLine("Parameter count: " + network.CalculateParameterCount());
            EnumerationQuery query = new EnumerationQuery(network);
            //SamplingQuery query = new SamplingQuery(network);
            query.DefineEventType(WitnessSawBlue, EventType.Evidence);
            query.DefineEventType(BlueTaxi, EventType.Outcome);
            query.SetEventValue(WitnessSawBlue, false);
            query.SetEventValue(BlueTaxi, false);
            query.Execute();
            Console.WriteLine(query.ToString());
        }
        public void TestSampling2()
        {
            BayesianNetwork network = new BayesianNetwork();
            BayesianEvent a = network.CreateEvent("a");
            BayesianEvent x1 = network.CreateEvent("x1");
            BayesianEvent x2 = network.CreateEvent("x2");
            BayesianEvent x3 = network.CreateEvent("x3");

            network.CreateDependency(a, x1, x2, x3);
            network.FinalizeStructure();

            a.Table.AddLine(0.5, true); // P(A) = 0.5
            x1.Table.AddLine(0.2, true, true); // p(x1|a) = 0.2
            x1.Table.AddLine(0.6, true, false);// p(x1|~a) = 0.6
            x2.Table.AddLine(0.2, true, true); // p(x2|a) = 0.2
            x2.Table.AddLine(0.6, true, false);// p(x2|~a) = 0.6
            x3.Table.AddLine(0.2, true, true); // p(x3|a) = 0.2
            x3.Table.AddLine(0.6, true, false);// p(x3|~a) = 0.6
            network.Validate();

            SamplingQuery query = new SamplingQuery(network);
            query.DefineEventType(x1, EventType.Evidence);
            query.DefineEventType(x2, EventType.Evidence);
            query.DefineEventType(x3, EventType.Evidence);
            query.DefineEventType(a, EventType.Outcome);
            query.SetEventValue(a, true);
            query.SetEventValue(x1, true);
            query.SetEventValue(x2, true);
            query.SetEventValue(x3, false);
            query.Execute();
            TestPercent(query.Probability, 18);
        }
        public BayesianNetwork Create()
        {
            BayesianNetwork network = new BayesianNetwork();
            BayesianEvent a = network.CreateEvent("a");
            BayesianEvent b = network.CreateEvent("b");

            network.CreateDependency(a, b);
            network.FinalizeStructure();
            a.Table.AddLine(0.5, true); // P(A) = 0.5
            b.Table.AddLine(0.2, true, true); // p(b|a) = 0.2
            b.Table.AddLine(0.8, true, false);// p(b|~a) = 0.8
            network.Validate();
            return network;
        }
 public void TestCount()
 {
     BayesianNetwork network = new BayesianNetwork();
     BayesianEvent a = network.CreateEvent("a");
     BayesianEvent b = network.CreateEvent("b");
     BayesianEvent c = network.CreateEvent("c");
     BayesianEvent d = network.CreateEvent("d");
     BayesianEvent e = network.CreateEvent("e");
     network.CreateDependency(a, b, d, e);
     network.CreateDependency(c, d);
     network.CreateDependency(b, e);
     network.CreateDependency(d, e);
     network.FinalizeStructure();
     Assert.AreEqual(16, network.CalculateParameterCount());
 }
        public void TestIndependant2()
        {
            BayesianNetwork network = new BayesianNetwork();
            BayesianEvent a = network.CreateEvent("a");
            BayesianEvent b = network.CreateEvent("b");
            BayesianEvent c = network.CreateEvent("c");
            BayesianEvent d = network.CreateEvent("d");
            network.CreateDependency(a, b, c);
            network.CreateDependency(b, d);
            network.CreateDependency(c, d);
            network.FinalizeStructure();

            Assert.IsFalse(network.IsCondIndependent(b, c));
            Assert.IsFalse(network.IsCondIndependent(b, c, d));
            Assert.IsTrue(network.IsCondIndependent(a, c, a));
            Assert.IsFalse(network.IsCondIndependent(a, c, a, d));
        }
        public void TestSampling1()
        {
            BayesianNetwork network = new BayesianNetwork();
            BayesianEvent a = network.CreateEvent("a");
            BayesianEvent b = network.CreateEvent("b");

            network.CreateDependency(a, b);
            network.FinalizeStructure();
            a.Table.AddLine(0.5, true); // P(A) = 0.5
            b.Table.AddLine(0.2, true, true); // p(b|a) = 0.2
            b.Table.AddLine(0.8, true, false);// p(b|~a) = 0.8
            network.Validate();

            SamplingQuery query = new SamplingQuery(network);
            query.DefineEventType(a, EventType.Evidence);
            query.DefineEventType(b, EventType.Outcome);
            query.SetEventValue(b, true);
            query.SetEventValue(a, true);
            query.Execute();
            TestPercent(query.Probability, 20);
        }
Exemple #7
0
        public void TestK2Structure()
        {
            String[] labels = { "available", "not" };

            IMLDataSet data = new BasicMLDataSet(DATA, null);
            BayesianNetwork network = new BayesianNetwork();
            BayesianEvent x1 = network.CreateEvent("x1", labels);
            BayesianEvent x2 = network.CreateEvent("x2", labels);
            BayesianEvent x3 = network.CreateEvent("x3", labels);
            network.FinalizeStructure();
            TrainBayesian train = new TrainBayesian(network, data, 10);
            train.InitNetwork = BayesianInit.InitEmpty;
            while (!train.TrainingDone)
            {
                train.Iteration();
            }
            train.Iteration();
            Assert.IsTrue(x1.Parents.Count == 0);
            Assert.IsTrue(x2.Parents.Count == 1);
            Assert.IsTrue(x3.Parents.Count == 1);
            Assert.IsTrue(x2.Parents.Contains(x1));
            Assert.IsTrue(x3.Parents.Contains(x2));
            Assert.AreEqual(0.714, network.GetEvent("x2").Table.FindLine(1, new int[] { 1 }).Probability, 0.001);
        }
 public void Validate(BayesianNetwork network)
 {
     Assert.AreEqual(3, network.CalculateParameterCount());
 }
        /// <inheritdoc/>
        public Object Read(Stream istream)
        {
            BayesianNetwork result = new BayesianNetwork();
            EncogReadHelper input = new EncogReadHelper(istream);
            EncogFileSection section;
            String queryType = "";
            String queryStr = "";
            String contentsStr = "";

            while ((section = input.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("BAYES-NETWORK")
                        && section.SubSectionName.Equals("BAYES-PARAM"))
                {
                    IDictionary<String, String> p = section.ParseParams();
                    queryType = p["queryType"];
                    queryStr = p["query"];
                    contentsStr = p["contents"];
                }
                if (section.SectionName.Equals("BAYES-NETWORK")
                        && section.SubSectionName.Equals("BAYES-TABLE"))
                {

                    result.Contents = contentsStr;

                    // first, define relationships (1st pass)
                    foreach (String line in section.Lines)
                    {
                        result.DefineRelationship(line);
                    }

                    result.FinalizeStructure();

                    // now define the probabilities (2nd pass)
                    foreach (String line in section.Lines)
                    {
                        result.DefineProbability(line);
                    }
                }
                if (section.SectionName.Equals("BAYES-NETWORK")
                        && section.SubSectionName.Equals("BAYES-PROPERTIES"))
                {
                    IDictionary<String, String> paras = section.ParseParams();
                    EngineArray.PutAll(paras, result.Properties);
                }
            }

            // define query, if it exists
            if (queryType.Length > 0)
            {
                IBayesianQuery query = null;
                if (queryType.Equals("EnumerationQuery"))
                {
                    query = new EnumerationQuery(result);
                }
                else
                {
                    query = new SamplingQuery(result);
                }

                if (query != null && queryStr.Length > 0)
                {
                    result.Query = query;
                    result.DefineClassificationStructure(queryStr);
                }
            }

            return result;
        }
 /// <summary>
 /// Create a bayesian network.
 /// </summary>
 /// <param name="architecture">The architecture to use.</param>
 /// <param name="input">The input neuron count.</param>
 /// <param name="output">The output neuron count.</param>
 /// <returns>The new bayesian network.</returns>
 public IMLMethod Create(String architecture, int input,
                         int output)
 {
     var method = new BayesianNetwork {Contents = architecture};
     return method;
 }
        /// <inheritdoc/>
        public Object Read(Stream istream)
        {
            BayesianNetwork  result = new BayesianNetwork();
            EncogReadHelper  input  = new EncogReadHelper(istream);
            EncogFileSection section;
            String           queryType   = "";
            String           queryStr    = "";
            String           contentsStr = "";

            while ((section = input.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("BAYES-NETWORK") &&
                    section.SubSectionName.Equals("BAYES-PARAM"))
                {
                    IDictionary <String, String> p = section.ParseParams();
                    queryType   = p["queryType"];
                    queryStr    = p["query"];
                    contentsStr = p["contents"];
                }
                if (section.SectionName.Equals("BAYES-NETWORK") &&
                    section.SubSectionName.Equals("BAYES-TABLE"))
                {
                    result.Contents = contentsStr;

                    // first, define relationships (1st pass)
                    foreach (String line in section.Lines)
                    {
                        result.DefineRelationship(line);
                    }

                    result.FinalizeStructure();

                    // now define the probabilities (2nd pass)
                    foreach (String line in section.Lines)
                    {
                        result.DefineProbability(line);
                    }
                }
                if (section.SectionName.Equals("BAYES-NETWORK") &&
                    section.SubSectionName.Equals("BAYES-PROPERTIES"))
                {
                    IDictionary <String, String> paras = section.ParseParams();
                    EngineArray.PutAll(paras, result.Properties);
                }
            }

            // define query, if it exists
            if (queryType.Length > 0)
            {
                IBayesianQuery query = null;
                if (queryType.Equals("EnumerationQuery"))
                {
                    query = new EnumerationQuery(result);
                }
                else
                {
                    query = new SamplingQuery(result);
                }

                if (query != null && queryStr.Length > 0)
                {
                    result.Query = query;
                    result.DefineClassificationStructure(queryStr);
                }
            }

            return(result);
        }
        /// <inheritdoc/>
        public void Save(Stream os, Object obj)
        {
            EncogWriteHelper o = new EncogWriteHelper(os);
            BayesianNetwork  b = (BayesianNetwork)obj;

            o.AddSection("BAYES-NETWORK");
            o.AddSubSection("BAYES-PARAM");
            String queryType = "";
            String queryStr  = b.ClassificationStructure;

            if (b.Query != null)
            {
                queryType = b.Query.GetType().Name;
            }

            o.WriteProperty("queryType", queryType);
            o.WriteProperty("query", queryStr);
            o.WriteProperty("contents", b.Contents);
            o.AddSubSection("BAYES-PROPERTIES");
            o.AddProperties(b.Properties);

            o.AddSubSection("BAYES-TABLE");
            foreach (BayesianEvent e in b.Events)
            {
                foreach (TableLine line in e.Table.Lines)
                {
                    if (line == null)
                    {
                        continue;
                    }
                    StringBuilder str = new StringBuilder();
                    str.Append("P(");

                    str.Append(BayesianEvent.FormatEventName(e, line.Result));

                    if (e.Parents.Count > 0)
                    {
                        str.Append("|");
                    }

                    int  index = 0;
                    bool first = true;
                    foreach (BayesianEvent parentEvent in e.Parents)
                    {
                        if (!first)
                        {
                            str.Append(",");
                        }
                        first = false;
                        int arg = line.Arguments[index++];
                        if (parentEvent.IsBoolean)
                        {
                            if (arg == 0)
                            {
                                str.Append("+");
                            }
                            else
                            {
                                str.Append("-");
                            }
                        }
                        str.Append(parentEvent.Label);
                        if (!parentEvent.IsBoolean)
                        {
                            str.Append("=");
                            if (arg >= parentEvent.Choices.Count)
                            {
                                throw new BayesianError("Argument value " + arg + " is out of range for event " + parentEvent.ToString());
                            }
                            str.Append(parentEvent.GetChoice(arg));
                        }
                    }
                    str.Append(")=");
                    str.Append(line.Probability);
                    str.Append("\n");
                    o.Write(str.ToString());
                }
            }

            o.Flush();
        }