Beispiel #1
0
 public OrNode(Fact v)
 {
     fact = v;
 }
Beispiel #2
0
        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);
        }