public static void Message_Tranfer_Arbol_Union(MarkovNet arboldeunion, Evidence evidence ) { Generic(evidence,null,arboldeunion); }
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); } }
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); } }
static void Generic(Evidence evidence, BayesianGraph poliarbol, MarkovNet arboldeunion) { //tamanno del grafo sea poliarbol o de racimos var count = poliarbol == null ? arboldeunion.Count : poliarbol.Count; double realization_probalility = 1.0; var nodesConditionalProbabilities = new double[count][]; var bayesianGraph = poliarbol ?? arboldeunion.BayesianNet; bool traspasoEnPoliarbol = poliarbol != null; if (evidence == null) return; var deterministicEvidence = new KeyValuePair<BayesianNode, int>[evidence.EvidenceValues.Length]; //absorber evidencia //no hay evidencia for (int i = 0; i < count; i++) deterministicEvidence[i] = new KeyValuePair<BayesianNode, int>(bayesianGraph[i], -1); for (int j = 0; j < count; j++) nodesConditionalProbabilities[j] = new double[bayesianGraph[j].ConditionalProbability.Length]; bool stocasticrealizations = false; foreach (var item in EvidenceRealization(evidence, deterministicEvidence, 0)) { stocasticrealizations = true; if (traspasoEnPoliarbol) Message_Tranfer_Poliarbol(poliarbol, item.Item1); else Message_Tranfer_Arbol_Union(arboldeunion, item.Item1); realization_probalility = item.Item2; for (int i = 0; i < nodesConditionalProbabilities.Length; i++) for (int j = 0; j < nodesConditionalProbabilities[i].Length; j++) { nodesConditionalProbabilities[i][j] += bayesianGraph[i].ConditionalProbability[j] * realization_probalility; } } //si no hubo evidencia estocastica es que se esta solicitando el traspaso de //msges sin evidencia if (stocasticrealizations) { for (int i = 0; i < nodesConditionalProbabilities.Length; i++) for (int j = 0; j < nodesConditionalProbabilities[i].Length; j++) bayesianGraph[i].ConditionalProbability[j] = nodesConditionalProbabilities[i][j]; } else { if (traspasoEnPoliarbol) Message_Tranfer_Poliarbol(poliarbol, deterministicEvidence); else Message_Tranfer_Arbol_Union(arboldeunion, deterministicEvidence); } }
public static void Message_Tranfer_Arbol_Union(MarkovNet arboldeunion, KeyValuePair<BayesianNode, int>[] evidence = null) { //absorber evidence if (evidence != null && evidence.Length > 0) { arboldeunion.BayesianNet.Evidence = evidence.Select(x => x.Value).ToArray(); } if (arboldeunion.Count() > ParalellStartNumber) { #region ParalellAlgorithm arboldeunion.Debug = true; var taskarr = new Task[arboldeunion.Count]; for (int i = 0; i < arboldeunion.Count; i++) { taskarr[i] = new Task(o => { var node = o as BayesianNode; if (node != null) lock (node) { do { arboldeunion.Work(node); } while (!node.Finished); } }, arboldeunion[i]); taskarr[i].Start(); } for (int i = 0; i < arboldeunion.Count*8; i++) { System.Threading.Thread.Sleep(300); Increment(); } Task.WaitAll(taskarr); arboldeunion.Debug = false; #endregion } else #region DeterministicAlgorithm while (!arboldeunion.AllConditionalProbabilitiesComputed()) { foreach (var item in arboldeunion) { arboldeunion.Work(item); } Increment(); } #endregion foreach (var node in arboldeunion.BayesianNet) arboldeunion.Marginalize_X(node); }