コード例 #1
0
 public RuleSet(Transform spawnPoint, Course course)
 {
     boids      = new List <GameObject>();
     alignment  = new RuleParameters();
     separation = new RuleParameters {
         considerOtherGroups = true
     };
     cohesion           = new RuleParameters();
     collisionAvoidance = new RuleParameters();
     targetChasing      = new RuleParameters();
     this.spawnPoint    = spawnPoint;
     this.course        = course;
 }
コード例 #2
0
        public IEnumerable <Rule> FindNegativeRuleSecondApproach(RuleParameters initialParameters)
        {
            double minSupport    = initialParameters.MinSupport;
            double minConfidence = initialParameters.MinConfidence;

            List <Rule> rulesResult            = new List <Rule>();
            List <Item> initialFrequentItemset = new List <Item>();

            var transactionsSet = _minerHelper.GetTransactionSet(FilteredDataSet.Records, FilteredDataSet.Headers);
            var headersList     = FilteredDataSet.Headers;

            foreach (var header in headersList)
            {
                if (_minerHelper.CalculateSupport(transactionsSet, new List <string> {
                    header
                }, FilteredDataSet.Records.Count) >= minSupport)
                {
                    initialFrequentItemset.Add(
                        new Item {
                        Name = header
                    }
                        );
                }
            }

            int rulesCounter = 0;

            for (int k = initialParameters.MinLength; k < initialParameters.MaxLength; k++)
            {
                // we are getting the candidates from 1-frequent itemset
                // against the traditional convention - that set is always generated
                // from 1-frequent itemset
                //List<List<Item>> candidates = frequentItemsets.ElementAt(0);

                // next we need to join them with theirselves to create
                // possible combinations
                var kFrequentCandidates = _minerHelper.GetCombinations(initialFrequentItemset, k).ToList();
                var kFrequentItemset    = new List <List <Item> >();
                foreach (var subSet in kFrequentCandidates)
                {
                    if (_minerHelper.CalculateSupport(transactionsSet, subSet.Select(s => s.Name).ToList(), FilteredDataSet.Records.Count) >= minSupport)
                    {
                        for (int i = 1; i < subSet.Count(); i++)
                        {
                            // we get all possible combinations
                            var lhs = _minerHelper.GetCombinations(subSet, i);
                            foreach (var lhsEl in lhs)
                            {
                                var rhs           = subSet.Where(s => !lhsEl.Contains(s));
                                var candidateRule = new Rule
                                {
                                    LeftItemSet  = lhsEl,
                                    RightItemSet = rhs
                                };
                                // for every candidate rule, we are suppose to check some
                                // conditions such as : correlation coeff; support and confidence
                                // base on the results, we can easily classify single rule as a positive either negative
                                // We also use a 'conviction' parameter
                                double unionSupport = _minerHelper.CalculateSupport(transactionsSet, lhsEl
                                                                                    .Union(rhs)
                                                                                    .Select(l => l.Name)
                                                                                    .ToList(), FilteredDataSet.Records.Count);
                                double lhsSupport = _minerHelper.CalculateSupport(transactionsSet, lhsEl
                                                                                  .Select(l => l.Name)
                                                                                  .ToList(), FilteredDataSet.Records.Count);
                                double rhsSupport = _minerHelper.CalculateSupport(transactionsSet, rhs
                                                                                  .Select(l => l.Name)
                                                                                  .ToList(), FilteredDataSet.Records.Count);

                                double supportANotB    = lhsSupport - unionSupport;
                                double supportNotAB    = rhsSupport - unionSupport;
                                double supportNotANotB = 1 - lhsSupport - rhsSupport + unionSupport;

                                double aNotBConfidence    = supportANotB / lhsSupport;
                                double notABConfidence    = supportNotAB / (1 - lhsSupport);
                                double notANotBConfidence = supportNotANotB / (1 - lhsSupport);
                                double ABConfidence       = unionSupport / lhsSupport;

                                if (notANotBConfidence >= minConfidence && supportNotANotB >= minSupport)
                                {
                                    candidateRule.Support    = supportNotANotB;
                                    candidateRule.Confidence = notANotBConfidence;
                                    candidateRule.Type       = RuleType.BothNegative;
                                    //Logger.Log(string.Format("¬ {0} => ¬ {1} [{2}]",
                                    //   string.Join(" ,", lhsEl.Select(r => r.Name)),
                                    //   string.Join(" ,", rhs.Select(r => r.Name)),
                                    //   Math.Round(notANotBConfidence, 2)
                                    //   ));
                                    _setOfFoundRules.Add(candidateRule);
                                    rulesCounter++;
                                    rulesResult.Add(candidateRule);
                                }
                                else if (supportANotB >= minSupport && aNotBConfidence >= minConfidence)
                                {
                                    candidateRule.Support    = supportANotB;
                                    candidateRule.Confidence = aNotBConfidence;
                                    candidateRule.Type       = RuleType.RightNegative;
                                    //Logger.Log(string.Format("{0} => ¬ {1} [{2}]",
                                    //   string.Join(" ,", lhsEl.Select(r => r.Name)),
                                    //   string.Join(" ,", rhs.Select(r => r.Name)),
                                    //   Math.Round(aNotBConfidence, 2)));
                                    _setOfFoundRules.Add(candidateRule);
                                    rulesCounter++;
                                    rulesResult.Add(candidateRule);
                                }
                                else if (supportNotAB >= minSupport && notABConfidence >= minConfidence)
                                {
                                    candidateRule.Support    = supportNotAB;
                                    candidateRule.Confidence = aNotBConfidence;
                                    candidateRule.Type       = RuleType.LeftNegative;
                                    //Logger.Log(string.Format("¬ {0} => {1} [{2}]",
                                    //   string.Join(" ,", lhsEl.Select(r => r.Name)),
                                    //   string.Join(" ,", rhs.Select(r => r.Name)),
                                    //   Math.Round(notABConfidence, 2)
                                    //   ));
                                    _setOfFoundRules.Add(candidateRule);
                                    rulesCounter++;
                                    rulesResult.Add(candidateRule);
                                }
                            }
                        }
                    }
                }
            }
            return(rulesResult);
        }
        private void MineRules()
        {
            if (!sourceSelected)
            {
                Console.WriteLine("THe data source was not chosen.");
                Start();
                return;
            }

            try
            {
                Console.WriteLine("Please enter a minimal support (for example:0,4).");
                var supp = GetNumber(Console.ReadLine());
                if (supp < 0)
                {
                    throw new Exception("Wpisano nie poprawną wartość.");
                }

                Console.WriteLine("Please enter a minimal confidence (for example:0,2).");
                var conf = GetNumber(Console.ReadLine());
                if (conf < 0)
                {
                    throw new Exception("Wpisano nie poprawną wartość.");
                }

                Console.WriteLine("Please enter a minimal rule length (for example:2).");
                var minlen = GetOption(Console.ReadLine());
                if (minlen < 0)
                {
                    throw new Exception("Wpisano nie poprawną wartość.");
                }

                Console.WriteLine("Please enter a maximal rule length (for example:4).");
                var maxlen = GetOption(Console.ReadLine());
                if (maxlen < 0)
                {
                    throw new Exception("Wpisano nie poprawną wartość.");
                }

                RuleParameters param = new RuleParameters
                {
                    MinSupport    = supp,
                    MinConfidence = conf,
                    MinLength     = minlen,
                    MaxLength     = maxlen
                };

                Console.WriteLine("Which approach of mining do you want to use?(1 or 2)");
                var opt = GetOption(Console.ReadLine());
                if (opt < 1 || opt > 2)
                {
                    throw new Exception("Wpisano nie poprawną wartość.");
                }

                manager.GetObservableRulesCollection().CollectionChanged += MinerConsole_CollectionChanged;
                Console.WriteLine("Mining is being processed...");
                if (opt == 1)
                {
                    manager.FindRule(param);
                }
                else if (opt == 2)
                {
                    manager.FindRuleSecondApproach(param);
                }

                if (manager.GetObservableRulesCollection().Count == 0)
                {
                    Console.WriteLine("No results found");
                }
                else
                {
                    Console.WriteLine("");
                    Console.WriteLine("Mining has finished!");
                    Console.WriteLine("");
                }

                Console.WriteLine("");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                Start();
            }
        }
 public IEnumerable <Rule> FindRuleSecondApproach(RuleParameters parameters)
 {
     return(_miner.FindNegativeRuleSecondApproach(parameters));
 }