Exemplo n.º 1
0
        public void Generate()
        {
            ConcurrentBag <AssociationRule> result = new ConcurrentBag <AssociationRule>();

            foreach (Row row in InformationSystem.Rows)
            {
                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;
                    while (alreadyCovered < MinimumNumberOfRowsToSeparate)
                    {
                        IList <Subset> subsets = separatedRowsCopy.CreateSubsets(row, decisionAttribute, 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();
                        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();

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

            Rules(result);
        }
Exemplo n.º 2
0
        public void Generate()
        {
            ConcurrentBag <AssociationRule> result = new ConcurrentBag <AssociationRule>();

            var task = Task.Factory.StartNew(() =>
            {
                int index = 0;
                foreach (Row row in InformationSystem.Rows)
                {
                    var counter = Task.Factory.StartNew(() =>
                    {
                        lock (locker)
                        {
                            Notify(++index);
                            if (index == InformationSystem.Rows.Count)
                            {
                                Rules(result);
                            }
                        }
                    }, CancellationToken.None, TaskCreationOptions.None, scheduler);

                    #region algorithm body

                    decimal gamma = InformationSystem.Gamma;
                    decimal alpha = InformationSystem.Alpha;
                    IList <Row> separatedRows;

                    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);

                        decimal M = System.Math.Ceiling(separatedRows.Count() * (1 - alpha));
                        decimal N = System.Math.Ceiling(separatedRows.Count() * (1 - gamma));

                        IList <Row> separatedRowsCopy = separatedRows;
                        int alreadyCovered            = 0;
                        while (alreadyCovered < M)
                        {
                            IList <Subset> subsets = separatedRowsCopy.CreateSubsets(row, decisionAttribute, false);

                            //here we must calculate for each subset its quotient: w(fj)/min(|U(T,r,d,fj)\D|,N-|D|)
                            //where D is output set, empty at the beginning
                            Dictionary <Subset, decimal> quotients = new Dictionary <Subset, decimal>();
                            subsets.Where(x => x.Count > 0).ToList().ForEach(subset =>
                            {
                                quotients[subset] =
                                    System.Convert.ToDecimal(Weights.Where(x => x.Key == subset.Name)
                                                             .Select(x => x.Value).First()) /
                                    System.Math.Min(subset.Rows.Count, N - alreadyCovered);
                            });

                            string subsetWithMinimumQuotient = quotients
                                                               .OrderBy(x => x.Value)
                                                               .Select(x => x.Key)
                                                               .First()
                                                               .Name;

                            alreadyCovered += subsets.Where(x => x.Name == subsetWithMinimumQuotient)
                                              .Select(x => x.Count).First();
                            conditions.Add(new Condition(
                                               row.Attributes.Where(x => x.Name == subsetWithMinimumQuotient).First()));

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

                        var rule = new AssociationRule(conditions, decisionAttributeInResult);

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

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

                    #endregion
                }

                return(result);
            });
        }
Exemplo n.º 3
0
        public void Generate()
        {
            ConcurrentBag <AssociationRule> result = new ConcurrentBag <AssociationRule>();

            var task = Task.Factory.StartNew(() =>
            {
                int index = 0;
                foreach (Row row in InformationSystem.Rows)
                {
                    var counter = Task.Factory.StartNew(() =>
                    {
                        lock (locker)
                        {
                            Notify(++index);
                            if (index == InformationSystem.Rows.Count)
                            {
                                Rules(result);
                            }
                        }
                    }, CancellationToken.None, TaskCreationOptions.None, scheduler);

                    #region algorithm body

                    IList <Row> separatedRows;

                    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;
                        int minimumNumberOfRowsToSeparate
                            = System.Convert.ToInt32(System.Math
                                                     .Ceiling((1 - InformationSystem.Alpha) * separatedRows.Count));

                        while (alreadyCovered < minimumNumberOfRowsToSeparate)
                        {
                            IList <Subset> subsets = separatedRowsCopy.CreateSubsets(row, decisionAttribute, 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();

                            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();

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

                    #endregion
                }

                return(result);
            });
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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)
            {
                decimal gamma = InformationSystem.Gamma;
                decimal alpha = InformationSystem.Alpha;

                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

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

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

                        decimal M = System.Math.Ceiling(separatedRows.Count() * (1 - alpha));
                        decimal N = System.Math.Ceiling(separatedRows.Count() * (1 - gamma));

                        IList <Row> separatedRowsCopy = separatedRows;
                        int alreadyCovered            = 0;

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

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

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

                            //here we must calculate for each subset its quotient: w(fj)/min(|U(T,r,d,fj)\D|,N-|D|)
                            //where D is output set, empty at the beginning
                            Dictionary <Subset, decimal> quotients = new Dictionary <Subset, decimal>();
                            subsets.Where(x => x.Count > 0).ToList().ForEach(subset =>
                            {
                                quotients[subset] =
                                    System.Convert.ToDecimal(Weights.Where(x => x.Key == subset.Name)
                                                             .Select(x => x.Value).First()) /
                                    System.Math.Min(subset.Rows.Count, N - alreadyCovered);
                            });

                            string subsetWithMinimumQuotient = quotients
                                                               .OrderBy(x => x.Value)
                                                               .Select(x => x.Key)
                                                               .First()
                                                               .Name;

                            var elementsQuantityInSelectedSubset = subsets.Where(x => x.Name == subsetWithMinimumQuotient)
                                                                   .Select(x => x.Count).First();

                            alreadyCovered += elementsQuantityInSelectedSubset;
                            //int elementsQuantityInSelectedSubset = subsets.Where(
                            //      x => x.Name == subsetWithMinimumQuotient)
                            //      .Select(x => x.Count)
                            //      .First();
                            iteration.ObjectsCovered = elementsQuantityInSelectedSubset;
                            //deltas.Add(elementsQuantityInSelectedSubset);

                            //alreadyCovered += elementsQuantityInSelectedSubset;

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

                            separatedRowsCopy = separatedRowsCopy
                                                .RemoveElementsFromAlreadyCoveredSubsets(subsets, subsetWithMinimumQuotient);

                            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(System.Convert.ToInt32(M), deltas);
                        rule.UpperBound = -1; // upperLowerBound.Item1;
                        rule.LowerBound = -1; //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);
        }