예제 #1
0
        /// <summary>
        /// Scoring with Tree Model
        /// </summary>
        /// <param name="dict">Values</param>
        /// <returns></returns>
        public override ScoreResult Score(Dictionary <string, object> dict)
        {
            ScoreResult resStart = new ScoreResult("", null);
            Node        root     = this.node;

            resStart.Nodes.Add(root);
            resStart.Value = root.Score;

            return(Node.Evaluate(root, missingValueStrategy, noTrueChildStrategy, dict, resStart));
        }
예제 #2
0
        /// <summary>
        /// Scoring with Tree Model
        /// </summary>
        /// <param name="dict">Values</param>
        /// <returns></returns>
        public override ScoreResult Score(Dictionary <string, object> dict)
        {
            ScoreResult resStart = new ScoreResult("", null);
            RuleSet     ruleset  = this.ruleSet;

            /*resStart.Nodes.Add(root);
             * resStart.Value = root.Score;
             *
             * return Node.Evaluate(root, missingValueStrategy, noTrueChildStrategy, dict, resStart);*/
            //throw new NotImplementedException();

            // Check if there are only one SelectionMethod
            return(RuleSet.Evaluate(ruleset, "", dict, resStart));
        }
예제 #3
0
        /// <summary>
        /// Scoring with Tree Model
        /// </summary>
        /// <param name="root">Parent node</param>
        /// <param name="missingvalueStr">Missing value strategy to evaluate this node.</param>
        /// <param name="noTrueChildStr">Strategy to evaluate this node if no child are true</param>
        /// <param name="dict">Values</param>
        /// <param name="res" >Result to return</param>
        /// <returns></returns>
        public static ScoreResult Evaluate(Node root, MissingValueStrategy missingvalueStr, NoTrueChildStrategy noTrueChildStr,
                                           Dictionary <string, object> dict, ScoreResult res)
        {
            // Test childs
            foreach (Node child in root.Nodes)
            {
                PredicateResult childPredicate = child.Predicate.Evaluate(dict);
                if (childPredicate == PredicateResult.True)
                {
                    res.Nodes.Add(child);
                    res.Value = child.Score;
                    foreach (ScoreDistribution sco in child.ScoreDistributions)
                    {
                        if (sco.Value.Equals(child.Score))
                        {
                            if (sco.Confidence != null)
                            {
                                res.Confidence = Decimal.Parse(sco.Confidence, NumberStyles.Any, CultureInfo.InvariantCulture);
                            }
                        }
                    }

                    return(Evaluate(child, missingvalueStr, noTrueChildStr, dict, res));
                }
                else if (childPredicate == PredicateResult.Unknown)
                {
                    // Unknow value lead to act with missingvalueStr
                    switch (missingvalueStr)
                    {
                    case MissingValueStrategy.LastPrediction:
                        return(res);

                    case MissingValueStrategy.NullPrediction:
                        res.Value = null;
                        return(res);

                    case MissingValueStrategy.WeightedConfidence:
                        Dictionary <string, decimal> conf = CalculateConfidence(root, dict);
                        string max_conf = null;
                        foreach (string key in conf.Keys)
                        {
                            if (max_conf == null)
                            {
                                max_conf = key;
                            }

                            if (conf[key] > conf[max_conf])
                            {
                                max_conf = key;
                            }
                        }
                        res.Value      = max_conf;
                        res.Confidence = conf[max_conf];
                        return(res);

                    case MissingValueStrategy.AggregateNodes:
                        return(res);

                    default:
                        throw new NotImplementedException();
                    }
                }
            }

            // All child nodes are false
            if (root.Nodes.Count > 0)
            {
                if (noTrueChildStr == NoTrueChildStrategy.ReturnNullPrediction)
                {
                    res.Value = null;
                }
            }
            return(res);
        }
예제 #4
0
        /// <summary>
        /// Scoring with rule set model
        /// </summary>
        /// <param name="root">Parent node</param>
        /// <param name="criterionStr">Criterion</param>
        /// <param name="dict">Values</param>
        /// <param name="res" >Result to return</param>
        /// <returns></returns>
        public static ScoreResult Evaluate(RuleSet root, String criterionStr,
                                           Dictionary <string, object> dict, ScoreResult res)
        {
            // HACK
            res.Value      = root.fdefaultscore;
            res.Confidence = 1.0M;


            // Both the ordering of keys and values is significant
            OrderedDictionary /*<String, SimpleRule>*/ firedRules = new OrderedDictionary();           //<String, SimpleRule>

            // Test childs
            foreach (Rule rule in root.Rules)
            {
                collectFiredRules(firedRules, rule, dict);

                if (firedRules.Count > 0)
                {
                    foreach (String key in firedRules.Keys)
                    {
                        res.Value      = key;
                        res.Confidence = 1.0M;                         //((SimpleRule)firedRules(key))..
                        return(res);
                    }
                }
            }

            // For now we return the first

            /*foreach (String key in firedRules.Keys) {
             *      res.Value = key;
             *      res.Confidence = 1.0M; //((SimpleRule)firedRules(key))..
             *      return res;
             * }*/

            // Test childs

            /*foreach(Rule rule in root.Rules)
             * {
             *      PredicateResult resRule = rule.Evaluate(dict);
             *      if (resRule == PredicateResult.True)
             *      {
             *              //res.Nodes.Add(child);
             *              res.Value = rule.Score;
             *              foreach(ScoreDistribution sco in rule.ScoreDistributions)
             *              {
             *                      if (sco.Value.Equals(rule.Score))
             *                      {
             *                              if (sco.Confidence != null)
             *                                      res.Confidence = Decimal.Parse(sco.Confidence, NumberStyles.Any, CultureInfo.InvariantCulture);
             *                      }
             *              }
             *
             *              return Evaluate(child, missingvalueStr, noTrueChildStr, dict, res);
             *      }
             *      if (resRule == PredicateResult.Unknown)
             *              throw new NotImplementedException();*/

            /*else if (childPredicate == PredicateResult.Unknown)
             * {
             *      // Unknow value lead to act with missingvalueStr
             *      switch(missingvalueStr)
             *      {
             *              case MissingValueStrategy.LastPrediction:
             *                      return res;
             *
             *              case MissingValueStrategy.NullPrediction:
             *                      res.Value = null;
             *                      return res;
             *
             *              case MissingValueStrategy.WeightedConfidence:
             *                      Dictionary<string, decimal> conf = CalculateConfidence(root, dict);
             *                      string max_conf = null;
             *                      foreach(string key in conf.Keys)
             *                      {
             *                              if (max_conf == null)
             *                                      max_conf = key;
             *
             *                              if (conf[key] > conf[max_conf])
             *                                      max_conf = key;
             *                      }
             *                      res.Value = max_conf;
             *                      res.Confidence = conf[max_conf];
             *                      return res;
             *
             *              case MissingValueStrategy.AggregateNodes:
             *                      return res;
             *
             *              default:
             *                      throw new NotImplementedException();
             *      }
             * }
             * }*/

            // All child nodes are false

            /*if (root.Nodes.Count > 0)
             *      if (noTrueChildStr == NoTrueChildStrategy.ReturnNullPrediction)
             *      {
             *              res.Value = null;
             *      }*/
            return(res);
        }
예제 #5
0
        /// <summary>
        /// Scoring with Tree Model
        /// </summary>
        /// <param name="root">Parent node</param>
        /// <param name="missingvalueStr">Missing value strategy to evaluate this node.</param>
        /// <param name="noTrueChildStr">Strategy to evaluate this node if no child are true</param>
        /// <param name="dict">Values</param>
        /// <param name="res" >Result to return</param>
        /// <returns></returns>
        public static ScoreResult Evaluate(Node root, MissingValueStrategy missingvalueStr, NoTrueChildStrategy noTrueChildStr, 
		                            Dictionary<string, object> dict, ScoreResult res)
        {
            // Test childs
            foreach(Node child in root.Nodes)
            {
                PredicateResult childPredicate = child.Predicate.Evaluate(dict);
                if (childPredicate == PredicateResult.True)
                {
                    res.Nodes.Add(child);
                    res.Value = child.Score;
                    foreach(ScoreDistribution sco in child.ScoreDistributions)
                    {
                        if (sco.Value.Equals(child.Score))
                        {
                            if (sco.Confidence != null)
                                res.Confidence = Decimal.Parse(sco.Confidence, NumberStyles.Any, CultureInfo.InvariantCulture);
                        }
                    }

                    return Evaluate(child, missingvalueStr, noTrueChildStr, dict, res);
                }
                else if (childPredicate == PredicateResult.Unknown)
                {
                    // Unknow value lead to act with missingvalueStr
                    switch(missingvalueStr)
                    {
                        case MissingValueStrategy.LastPrediction:
                            return res;

                        case MissingValueStrategy.NullPrediction:
                            res.Value = null;
                            return res;

                        case MissingValueStrategy.WeightedConfidence:
                            Dictionary<string, decimal> conf = CalculateConfidence(root, dict);
                            string max_conf = null;
                            foreach(string key in conf.Keys)
                            {
                                if (max_conf == null)
                                    max_conf = key;

                                if (conf[key] > conf[max_conf])
                                    max_conf = key;
                            }
                            res.Value = max_conf;
                            res.Confidence = conf[max_conf];
                            return res;

                        case MissingValueStrategy.AggregateNodes:
                            return res;

                        default:
                            throw new NotImplementedException();
                    }
                }
            }

            // All child nodes are false
            if (root.Nodes.Count > 0)
                if (noTrueChildStr == NoTrueChildStrategy.ReturnNullPrediction)
                {
                    res.Value = null;
                }
            return res;
        }
예제 #6
0
        /// <summary>
        /// Scoring with Tree Model
        /// </summary>
        /// <param name="dict">Values</param>
        /// <returns></returns>
        public override ScoreResult Score(Dictionary<string, object> dict)
        {
            ScoreResult resStart = new ScoreResult("", null);
            RuleSet ruleset = this.ruleSet;
            /*resStart.Nodes.Add(root);
            resStart.Value = root.Score;

            return Node.Evaluate(root, missingValueStrategy, noTrueChildStrategy, dict, resStart);*/
            //throw new NotImplementedException();

            // Check if there are only one SelectionMethod
            return RuleSet.Evaluate(ruleset, "", dict, resStart);
        }
예제 #7
0
        /// <summary>
        /// Scoring with Tree Model
        /// </summary>
        /// <param name="dict">Values</param>
        /// <returns></returns>
        public override ScoreResult Score(Dictionary<string, object> dict)
        {
            ScoreResult resStart = new ScoreResult("", null);
            Node root = this.node;
            resStart.Nodes.Add(root);
            resStart.Value = root.Score;

            return Node.Evaluate(root, missingValueStrategy, noTrueChildStrategy, dict, resStart);
        }
예제 #8
0
파일: RuleSet.cs 프로젝트: mindis/pmml4net
        /// <summary>
        /// Scoring with rule set model
        /// </summary>
        /// <param name="root">Parent node</param>
        /// <param name="criterionStr">Criterion</param>
        /// <param name="dict">Values</param>
        /// <param name="res" >Result to return</param>
        /// <returns></returns>
        public static ScoreResult Evaluate(RuleSet root, String criterionStr, 
		                            Dictionary<string, object> dict, ScoreResult res)
        {
            // HACK
            res.Value = root.fdefaultscore;
            res.Confidence = 1.0M;

            // Both the ordering of keys and values is significant
            OrderedDictionary/*<String, SimpleRule>*/ firedRules = new OrderedDictionary();//<String, SimpleRule>

            // Test childs
            foreach(Rule rule in root.Rules)
            {
                collectFiredRules(firedRules, rule, dict);

                if (firedRules.Count > 0)
                    foreach (String key in firedRules.Keys) {
                        res.Value = key;
                        res.Confidence = 1.0M; //((SimpleRule)firedRules(key))..
                        return res;
                    }
            }

            // For now we return the first
            /*foreach (String key in firedRules.Keys) {
                res.Value = key;
                res.Confidence = 1.0M; //((SimpleRule)firedRules(key))..
                return res;
            }*/

            // Test childs
            /*foreach(Rule rule in root.Rules)
            {
                PredicateResult resRule = rule.Evaluate(dict);
                if (resRule == PredicateResult.True)
                {
                    //res.Nodes.Add(child);
                    res.Value = rule.Score;
                    foreach(ScoreDistribution sco in rule.ScoreDistributions)
                    {
                        if (sco.Value.Equals(rule.Score))
                        {
                            if (sco.Confidence != null)
                                res.Confidence = Decimal.Parse(sco.Confidence, NumberStyles.Any, CultureInfo.InvariantCulture);
                        }
                    }

                    return Evaluate(child, missingvalueStr, noTrueChildStr, dict, res);
                }
                if (resRule == PredicateResult.Unknown)
                    throw new NotImplementedException();*/

                /*else if (childPredicate == PredicateResult.Unknown)
                {
                    // Unknow value lead to act with missingvalueStr
                    switch(missingvalueStr)
                    {
                        case MissingValueStrategy.LastPrediction:
                            return res;

                        case MissingValueStrategy.NullPrediction:
                            res.Value = null;
                            return res;

                        case MissingValueStrategy.WeightedConfidence:
                            Dictionary<string, decimal> conf = CalculateConfidence(root, dict);
                            string max_conf = null;
                            foreach(string key in conf.Keys)
                            {
                                if (max_conf == null)
                                    max_conf = key;

                                if (conf[key] > conf[max_conf])
                                    max_conf = key;
                            }
                            res.Value = max_conf;
                            res.Confidence = conf[max_conf];
                            return res;

                        case MissingValueStrategy.AggregateNodes:
                            return res;

                        default:
                            throw new NotImplementedException();
                    }
                }
            }*/

            // All child nodes are false
            /*if (root.Nodes.Count > 0)
                if (noTrueChildStr == NoTrueChildStrategy.ReturnNullPrediction)
                {
                    res.Value = null;
                }*/
            return res;
        }