예제 #1
0
        public void Generate()
        {
            ConcurrentBag <AssociationRule>            result       = new ConcurrentBag <AssociationRule>();
            ConcurrentBag <AlgorithmProgressExecution> algExecution = new ConcurrentBag <AlgorithmProgressExecution>();
            ConcurrentBag <Task> tasks = new ConcurrentBag <Task>();
            int index = 0;

            foreach (Row singleRow in InformationSystem.Rows)
            {
                List <Attribute> attrs = singleRow.Attributes.Where(x =>
                                                                    InformationSystem.DecisionAttributes[x.Name] == true).Select(x => x).ToList();
                foreach (Attribute decisionAttribute in attrs)
                {
                    var localCopy = System.Tuple.Create <Row, Attribute>(singleRow, decisionAttribute);
                    Task <AssociationRule> task = Task.Factory.StartNew <AssociationRule>((variables) =>
                    {
                        var decAttr = ((System.Tuple <Row, Attribute>)variables).Item2;
                        var row     = ((System.Tuple <Row, Attribute>)variables).Item1;

                        #region algorithm body

                        Condition decisionAttributeInResult = new Condition(decAttr);
                        IList <Condition> conditions        = new List <Condition>();
                        IList <Row> separatedRows;

                        separatedRows =
                            InformationSystem.Rows.
                            GetRowsSeparatedByAttribute
                                (row,
                                decAttr);

                        IList <Row> separatedRowsCopy = separatedRows;
                        int alreadyCovered            = 0;
                        List <int> deltas             = new List <int>();
                        int minimumNumberOfRowsToSeparate
                            = System.Convert.ToInt32(System.Math
                                                     .Ceiling((1 - InformationSystem.Alpha) * separatedRows.Count));

                        AlgorithmProgressExecution progress = new AlgorithmProgressExecution()
                        {
                            DecisionAttribute = decAttr
                        };

                        while (alreadyCovered < minimumNumberOfRowsToSeparate)
                        {
                            AlgorithmIteration iteration = new AlgorithmIteration()
                            {
                                IterationNumber = progress.Iterations.Count() + 1,
                                ObjectsToCover  = separatedRowsCopy.Count
                            };

                            IList <Subset> subsets = separatedRowsCopy.CreateSubsets(row, decAttr, true);

                            string subsetWithMaxElements = subsets
                                                           .OrderByDescending(x => x.Quotient)
                                                           .Select(x => x.Name)
                                                           .First();

                            //alreadyCovered += subsets.Where(
                            //        x => x.Name == subsetWithMaxElements)
                            //        .Select(x => x.Count)
                            //        .First();
                            int elementsQuantityInSelectedSubset = subsets.Where(
                                x => x.Name == subsetWithMaxElements)
                                                                   .Select(x => x.Count)
                                                                   .First();

                            iteration.ObjectsCovered = elementsQuantityInSelectedSubset;
                            deltas.Add(elementsQuantityInSelectedSubset);

                            alreadyCovered += elementsQuantityInSelectedSubset;

                            conditions.Add(new Condition(
                                               row.Attributes.Where(x => x.Name == subsetWithMaxElements).First()));

                            separatedRowsCopy = separatedRowsCopy
                                                .RemoveElementsFromAlreadyCoveredSubsets
                                                    (subsets, subsetWithMaxElements);

                            progress.Iterations.Add(iteration);
                        }

                        var rule = new AssociationRule(conditions, decisionAttributeInResult);

                        var supportAndConfidence = rule.CalculateSupportAndConfidence(InformationSystem.Rows);
                        rule.Support             = supportAndConfidence.Item1.Round();
                        rule.Confidence          = supportAndConfidence.Item2.Round();

                        var upperLowerBound = rule.CalculateUpperAndLowerBound(minimumNumberOfRowsToSeparate, deltas);
                        rule.UpperBound     = upperLowerBound.Item1;
                        rule.LowerBound     = upperLowerBound.Item2;

                        algExecution.Add(progress);

                        #endregion

                        return(rule);
                    }, localCopy, CancellationToken.None, TaskCreationOptions.AttachedToParent, TaskScheduler.Default);
                    task.ContinueWith(x =>
                    {
                        if (x.Status == TaskStatus.RanToCompletion)
                        {
                            lock (locker)
                            {
                                result.Add(((Task <AssociationRule>)x).Result);
                                Notify(++index);
                            }
                        }
                    }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, guiScheduler);

                    tasks.Add(task);
                }
            }

            var finish = Task.Factory.ContinueWhenAll(tasks.ToArray(), x =>
            {
                SeparatedRows(algExecution);
                Rules(result);
            }, CancellationToken.None, TaskContinuationOptions.None, guiScheduler);
        }
예제 #2
0
        public void Generate()
        {
            ConcurrentBag <AssociationRule> result = new ConcurrentBag <AssociationRule>();

            foreach (Row row in InformationSystem.Rows)
            {
                //get all attributes marked as a decision attr.
                List <Attribute> attrs = row.Attributes.Where(x =>
                                                              InformationSystem.DecisionAttributes[x.Name] == true).Select(x => x).ToList();

                foreach (Attribute decisionAttribute in attrs)
                {
                    Condition         decisionAttributeInResult = new Condition(decisionAttribute);
                    IList <Condition> conditions = new List <Condition>();

                    separatedRows = InformationSystem.Rows.GetRowsSeparatedByAttribute(row, decisionAttribute);

                    IList <Row> separatedRowsCopy = separatedRows;
                    int         alreadyCovered    = 0;
                    List <int>  deltas            = new List <int>();//{ 0 };
                    while (alreadyCovered < MinimumNumberOfRowsToSeparate)
                    {
                        IList <Subset> subsets = separatedRowsCopy.CreateSubsets(row, decisionAttribute, false);

                        string subsetWithMaxElements = subsets
                                                       .OrderByDescending(x => x.Count)
                                                       .Select(x => x.Name)
                                                       .First();

                        int elementsQuantityInSelectedSubset = subsets.Where(
                            x => x.Name == subsetWithMaxElements)
                                                               .Select(x => x.Count)
                                                               .First();

                        deltas.Add(elementsQuantityInSelectedSubset);

                        alreadyCovered += elementsQuantityInSelectedSubset;

                        conditions.Add(new Condition(
                                           row.Attributes.Where(x => x.Name == subsetWithMaxElements).First()));

                        separatedRowsCopy = separatedRowsCopy
                                            .RemoveElementsFromAlreadyCoveredSubsets(subsets, subsetWithMaxElements);
                    }

                    var rule = new AssociationRule(conditions, decisionAttributeInResult);

                    var supportAndConfidence = rule.CalculateSupportAndConfidence(InformationSystem.Rows);
                    rule.Support    = supportAndConfidence.Item1.Round();
                    rule.Confidence = supportAndConfidence.Item2.Round();

                    var upperLowerBound = rule.CalculateUpperAndLowerBound(MinimumNumberOfRowsToSeparate, deltas);
                    rule.UpperBound = upperLowerBound.Item1;
                    rule.LowerBound = upperLowerBound.Item2;

                    result.Add(rule);
                    alreadyCovered = 0;
                    separatedRows  = null;
                }
            }

            Rules(result);
        }