Beispiel #1
0
        public override ChartElement Build()
        {
            BayesianNode node = new BayesianNode();

            node.Name      = Name;
            node.PinSize   = new Size(5, 5);
            node.BackColor = Color.LightBlue;


            node.Display        = new Rectangle(Center.X - 100, Center.Y - 25, 130, 30);
            node.ShowOutputPins = false;
            node.ShowInputPins  = false;
            return(node);
        }
        public void Marginalize_X(BayesianNode Xj)
        {
            BayesianClique Ci  = null;
            int            min = int.MaxValue;

            foreach (var c in TreeCluster)
            {
                if (c.Nodes.Contains(Xj) && c.Nodes.Count < min)
                {
                    Ci = c;
                }
            }
            if (Ci != null)
            {
                Marginalize_Xj(Xj, Ci);
            }
        }
        private void Marginalize_Xj(BayesianNode Xj, BayesianClique Ci)
        {
            if (TreeCluster == null)
            {
                ;
            }
            //var clique = TreeCluster.Aggregate(default(BayesianClique), (x, y) => (x == null || y.Nodes.Contains(Xj) && y.Nodes.Count < x.Nodes.Count) ? y : x);

            //if (clique == default(BayesianClique)) throw new Exception("El nodo no pertenece al arbol de union");
            //var index = clique.Nodes.IndexOf(Xj);
            var index = Ci.Nodes.IndexOf(Xj);

            for (int i = 0; i < Xj.ConditionalProbability.Length; i++)
            {
                Xj.ConditionalProbability[i] = Math.Round(Ci.ConditionalProbability.Where(x => StringToList(x.Key)[index] == i).Select(y => y.Value).Sum(), 3);
            }
        }
 public override ChartElement Build()
 {
     BayesianNode node = new BayesianNode();
     node.Name = Name;
     node.PinSize = new Size(5, 5);
     node.BackColor = Color.LightBlue;
     
     
     node.Display = new Rectangle(Center.X - 100, Center.Y - 25, 130, 30);
     node.ShowOutputPins = false;
     node.ShowInputPins = false;
     return node;
 }
        private void computarProbabilidadesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl1.SelectTab(0);
            try
            {
                if (CheckConsistency())
                {
                    //obtener la evidencia
                    if (flowChartViewer1.Charts.Count > 0 && Evidence.Count > 0)
                        GetEvidence();
                    //transformar el grafo del visor a un grafo de nodos bayesianos

                    var graph = new BayesianGraph();
                    //
                     var dict = new Dictionary<BayesianNode, BayesianNodeChartElement>(flowChartViewer1.Charts.Count);
                    BayesianNode node;
                    var dict2 = new Dictionary<BayesianNodeChartElement, BayesianNode>(flowChartViewer1.Charts.Count);
                    
                    graph.Clear();
                   
                    foreach (var item in flowChartViewer1.Charts.Cast<BayesianNodeChartElement>())
                    {

                        node = new BayesianNode(item.Parents.Count, item.Childs.Count(), item.States.Count,
                                                item.Parents.Select(x => x.States.Count).ToArray()) { Probabilities = item.Condicional_Probabilities, Name = item.Name };

                        dict.Add(node, item);
                        dict2.Add(item,node);
                        item.CreateConditionalProbabilities();


                        graph.AddVertex(node);
                    }
                    foreach (var bnodechart in flowChartViewer1.Charts.Cast<BayesianNodeChartElement>())
                    {
                        foreach (var child in bnodechart.Childs)
                        {
                            graph.AddEdge(dict2[bnodechart], dict2[child]);
                        }
                    }
                    //ejecutar el traspaso de mensajes en un poliarbol o en una red multiplemente conexa
                    progressBar1.Value = 5;
                    //if(MessageTransfer.ParalellStartNumber>graph.Count)
                        MessageTransfer.Increment += UpdateProgress;
                   

                    if (graph.Arbol())
                    {
                        if (Evidence.Count > 0)
                            MessageTransfer.Message_Tranfer_Poliarbol(graph, Evidence[EvidenceIndex]);
                        else
                            MessageTransfer.Message_Tranfer_Poliarbol(graph);
                        foreach (var nodebayesian in graph)
                            dict[nodebayesian].SetConditionalProbabilities(nodebayesian.ConditionalProbability);
                    }
                    else
                    {
                        var markovnet = new MarkovNet(graph);
                        if (Evidence.Count > 0)
                            MessageTransfer.Message_Tranfer_Arbol_Union(markovnet, Evidence[EvidenceIndex]);
                        else
                            MessageTransfer.Message_Tranfer_Arbol_Union(markovnet);
                        foreach (var nodebayesian in markovnet.BayesianNet)

                            dict[nodebayesian].SetConditionalProbabilities(nodebayesian.ConditionalProbability);

                    }

                    timer1.Stop();
                    progressBar1.Value=100;
                   
                    Tests.Add(new BayesianTest()
                        {
                            Evidence = Evidence[EvidenceIndex].Clone(),
                            Probabilities =
                                graph.Select(x => x.ConditionalProbability.Clone() as double[]).ToArray()
                        });
                    TestsIndex = Tests.Count - 1;
                    ShowConditionalProbabilities();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Hubo algun problema al calcular las probabilidades condicionales. " + ex.Message);
            }
        }