private void Run_Clicked(object sender, EventArgs e) { if (ForwardReasoningButton.Checked) { ThoughtLinePanel.Controls.Clear(); selected_userfacts = all_userfacts.Where(f => f.cntrl.FactValueControl.Value >= 50).ToList(); List <TerminalFact> res = forward(); if (res.Count != 0) { TerminalFact best = res.First(); label1.Text = best.text; pictureBox1.ImageLocation = best.img; } else { List <Tuple <TerminalFact, double> > guesses = new List <Tuple <TerminalFact, double> >(); foreach (TerminalFact t in terminals) { double val; var r = Rules.Where(cr => cr.result.Contains(t) && cr.condition.Any(cf => work_area.Contains(cf))) .Select(cr => (double)cr.condition.Intersect(work_area).Count() / cr.condition.Count()); if (r.Count() == 0) { val = 0; } else { val = r.Max(); } guesses.Add(new Tuple <TerminalFact, double>(t, val)); } guesses = guesses.OrderByDescending(t => t.Item2).ToList(); if (guesses.Count() == 0 || guesses.First().Item2 == 0) { label1.Text = "UNKNOWN"; pictureBox1.ImageLocation = "https://cdn-images-1.medium.com/max/800/1*Km98PgzRp9yRYfVZeSzwzQ.png"; } else { guesses = guesses.Where(g => g.Item2 != 0).ToList(); label1.Text = guesses.First().Item1.text + " with probability " + guesses.First().Item2.ToString("N2"); pictureBox1.ImageLocation = guesses.First().Item1.img; var p = panel_factory(); foreach (var g in guesses) { p.Controls.Add(label_factory(g.Item1.ToString() + " prb:%" + (g.Item2 * 100).ToString("N1"))); } ThoughtLinePanel.Controls.Add(p); } } } else if (FCbutton.Checked) { ThoughtLinePanel.Controls.Clear(); //selected_userfacts = all_userfacts.Where(f => f.cntrl.FactValueControl.Value == 1).ToList(); List <TerminalFact> res = confidence_forward(); if (res.Count != 0) { res = res.OrderByDescending(f => f.weight).ToList(); TerminalFact best = res.First(); label1.Text = best.text + "%" + (best.weight * 100).ToString("N1"); pictureBox1.ImageLocation = best.img; var panel = panel_factory(); panel.Controls.Add(label_factory("Guesses:")); foreach (var f in res) { panel.Controls.Add(label_factory(f.text + "%" + (f.weight * 100).ToString("N1"))); } ThoughtLinePanel.Controls.Add(panel); } else { label1.Text = "UNKNOWN"; pictureBox1.ImageLocation = "https://cdn-images-1.medium.com/max/800/1*Km98PgzRp9yRYfVZeSzwzQ.png"; } } else if (BackwardReasoningButton.Checked) { selected_userfacts = all_userfacts.Where(f => f.cntrl.FactValueControl.Value >= 50).ToList(); Dictionary <TerminalFact, Tuple <int, double> > res = backward(); if (res.Count != 0) { TerminalFact best_guess = null; TerminalFact best_result = null; var guess = panel_factory(); guess.Controls.Add(label_factory("Guesses: ", Color.Red)); var guessed_terminals = res.Where(r => r.Value.Item2 != 1.0 && r.Value.Item2 > 0.0).OrderByDescending(r => r.Value.Item2).ThenByDescending(r => r.Value.Item1).ToList(); foreach (var g in guessed_terminals) { guess.Controls.Add(label_factory(g.Key.text + " prb:%" + (g.Value.Item2 * 100).ToString("N1"))); } if (guessed_terminals.Count > 0) { ThoughtLinePanel.Controls.Add(guess); best_guess = guessed_terminals.First().Key as TerminalFact; } var result = panel_factory(); result.Controls.Add(label_factory("Results: ", Color.Green)); var resolved = res.Where(r => r.Value.Item2 == 1.0).OrderByDescending(r => r.Value.Item1).ToList(); foreach (var r in resolved) { result.Controls.Add(label_factory(r.Key.text)); } if (resolved.Count > 0) { ThoughtLinePanel.Controls.Add(result); best_result = resolved.First().Key as TerminalFact; } if (best_result != null) { pictureBox1.ImageLocation = best_result.img; label1.Text = best_result.text; } else if (best_guess != null) { pictureBox1.ImageLocation = best_guess.img; label1.Text = best_guess.text; } else { label1.Text = "UNKNOWN"; pictureBox1.ImageLocation = "https://cdn-images-1.medium.com/max/800/1*Km98PgzRp9yRYfVZeSzwzQ.png"; } } else { label1.Text = "UNKNOWN"; pictureBox1.ImageLocation = "https://cdn-images-1.medium.com/max/800/1*Km98PgzRp9yRYfVZeSzwzQ.png"; } } else { return; } pictureBox1.Refresh(); flowLayoutPanel1.Refresh(); }
public TerminalFact(TerminalFact f) : base(f as Fact) { img = f.img; }
private List <TerminalFact> confidence_forward() { double THRESHOLD = 0.05; selected_userfacts = all_userfacts.Select(f => { Fact r = new Fact(f) { weight = (double)f.cntrl.FactValueControl.Value / 100 }; return(r); }).ToList(); Dictionary <Rule, double> applyable = Rules.ToDictionary(r => r, r => 0.0); HashSet <Rule> used = new HashSet <Rule>(); List <TerminalFact> res = new List <TerminalFact>(); HashSet <Fact> all_facts = new HashSet <Fact>(support_area.Union(terminals).Select(f => new Fact(f) { weight = 0 })); all_facts.UnionWith(selected_userfacts); double RuleConditionWeight(Rule r) { var new_cond = all_facts.Where(ft => r.condition.Select(f => f.id).Contains(ft.id)); double res_weight = 0; if (new_cond.Count() > 0) { res_weight = r.weight * new_cond.Min(f => f.weight); } return(res_weight); } applyable = applyable.ToDictionary(item => item.Key, item => RuleConditionWeight(item.Key)); while (applyable.Count(item => item.Value >= THRESHOLD) > 0) { if (applyable.Count <= 0) { break; } applyable = applyable.ToDictionary(item => item.Key, item => RuleConditionWeight(item.Key)); Rule rule_toapply = applyable.Keys.OrderByDescending(key => applyable[key]).ThenByDescending(key => key.weight).First(); if (applyable[rule_toapply] < THRESHOLD) { break; } var new_result = all_facts.Where(ft => rule_toapply.result.Select(f => f.id).Contains(ft.id)); double res_conf = applyable[rule_toapply]; // var panel = panel_factory(); panel.Controls.Add(label_factory("Applied Rule:")); panel.Controls.Add(label_factory(String.Format("{0} confidence: {1}", rule_toapply, res_conf))); ThoughtLinePanel.Controls.Add(panel); // panel = panel_factory(); panel.Controls.Add(label_factory("New Facts:")); foreach (Fact f in new_result) { f.weight = f.weight + res_conf - res_conf * f.weight; string text = f.text + "conf: " + f.weight; Color c = Color.Black; TerminalFact old_term = terminals.Where(t => t.id == f.id).FirstOrDefault(); if (old_term != null) { TerminalFact tf = new TerminalFact(old_term) { weight = f.weight }; res.Add(tf); c = Color.Green; } panel.Controls.Add(label_factory(text, c)); } ThoughtLinePanel.Controls.Add(panel); applyable.Remove(rule_toapply); used.Add(rule_toapply); } return(res); }