public ApproximationSpace(
     DecisionSystem infoSys,
     Func<double?[], double?[], bool> indiscerRel,
     Func<IEnumerable<double?[]>, IEnumerable<double?[]>, double> roughInclusion)
 {
     IS = infoSys;
     IndRelation = indiscerRel;
     RoughIncl = roughInclusion;
 }
Example #2
0
        public static void TestReduct()
        {
            // Prepare test data
            var infSys = new DecisionSystem
            {   // KhuPN, Hong - "Datamining based on Rough set theory" pg. 16
                Universe = new double?[][]
                {   //            id  1  2  3  4  5
                    new double?[]{ 1, 2, 1, 0, 0, 1},
                    new double?[]{ 2, 0, 2, 1, 2, 1},
                    new double?[]{ 3, 1, 2, 1, 1, 2},
                    new double?[]{ 4, 1, 0, 0, 1, 1},
                    new double?[]{ 5, 2, 1, 2, 0, 0},
                    new double?[]{ 6, 1, 0, 1, 1, 2},
                    new double?[]{ 7, 0, 2, 1, 0, 1},
                    new double?[]{ 8, 0, 1, 2, 1, 2}
                },
                ConditionAttributes = new[] { 1, 2, 3, 4},
                DecisionAttributes = new[] { 5 },
                DecisionAttribute = 5,
                AttributesDomain = new double[][]
                {
                    null, // no need for "id"
                    new double[]{ 0, 1, 2 },
                    new double[]{ 0, 1, 2 },
                    new double[]{ 0, 1, 2 },
                    new double[]{ 0, 1, 2 },
                    new double[]{ 0, 1, 2 },
                },
            };

            Console.WriteLine("Running...");
            QuickReductProcessor processor = new QuickReductProcessor(infSys);

            // Show results
            Console.WriteLine("\nAll reducts: ");
            for(int i = 0; i < processor.Results.Count; i++ ) {
                var rs = processor.Results[i];
                Console.WriteLine((i+1).ToString() + ". {" + string.Join(",", rs.OrderBy(x => x) ) + "}");
            }

            Console.WriteLine("\nMinimal reducts: ");
            var miRd = processor.MinimalReducts.ToArray();
            for (int i = 0; i < miRd.Length; i++)
            {
                var rs = miRd[i];
                Console.WriteLine((i + 1).ToString() + ". {" + string.Join(",", rs.OrderBy(x => x)) + "}");
            }

            Console.WriteLine("\nCore attributes: " + string.Join(",", processor.CoreAttrs));

            // Keep the console open in debug mode.
            Console.WriteLine(System.Environment.NewLine);
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        public Chi2Discretizer(DecisionSystem _DS)
        {
            this.DS = _DS;
            this.DecisionClasses = DS.AttributesDomain[DS.DecisionAttribute].OrderBy(x => x).ToList();
            this.DegreeOfFreedom = DecisionClasses.Count - 1;

            this.InitCache();

            this.Delta = this.CalcCurrentInconsistencyRate();
            //TODO: If Ei < 5 , raise an exception
        }
Example #4
0
        public static void RunTest()
        {
            // Prepare test data
            var infSys = new DecisionSystem
                {
                    // KhuPN, Hong - "Datamining based on Rough set theory" pg. 8
                    Universe = new double?[][]
                        {
                            //            id  1  2  3  4  5
                            new double?[] {1, 2, 1, 0, 1, 0},
                            new double?[] {2, 2, 0, 1, 0, 1},
                            new double?[] {3, 1, 0, 0, 1, 0},
                            new double?[] {4, 0, 1, 0, 0, 1},
                            new double?[] {5, 0, 0, 0, 1, 1},
                            new double?[] {6, 2, 0, 0, 1, 0},
                            new double?[] {7, 0, 0, 1, 0, 1},
                            new double?[] {8, 2, 1, 1, 0, 1}
                        },
                    ConditionAttributes = new[] {1,2, 3, 4},
                    DecisionAttributes = new[] {5},
                    DecisionAttribute = 5,
                    AttributesDomain = new double[][]
                        {
                            null, // no need for "id"
                            new double[] {0, 1, 2},
                            new double[] {0, 1},
                            new double[] {0, 1},
                            new double[] {0, 1},
                            new double[] {0, 1},
                        },
                };

            var r=new Rules(infSys,new int[]{1,2,3,4});
            //Output
            foreach (var discernibilityMatrix in r.DiscernMatrix)
            {
                Console.WriteLine("Attribute: "+discernibilityMatrix.RuleValue);
                Console.WriteLine("Prime Impicants:");
                foreach (var primeImplicant in discernibilityMatrix.PrimeImplicants)
                {
                    foreach (var pairId in primeImplicant)
                    {
                        Console.Write(pairId.AID+":"+pairId.VID+" ; ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
            }
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        public DecisionSystem GetDecisionSystem(List<AttributeDefinition> attributeDefinitions, string tableName)
        {
            //var infSys = new DecisionSystem
            //{ // KhuPN - Table 2 page 5
            //    Universe = new double?[][]
            //    {   //            id  1  2  3  4
            //        new double?[]{ 1, 0, 1, 1, 1 },
            //        new double?[]{ 2, 1, 0, 1, 1 },
            //        new double?[]{ 3, 1, 1, 2, 1 },
            //        new double?[]{ 4, 0, 1, 0, 0 },
            //        new double?[]{ 5, 1, 0, 1, 0 },
            //        new double?[]{ 6, 0, 1, 2, 1 },
            //    },
            //    ConditionAttributes = new[] { 1, 2, 3 },
            //    DecisionAttributes = new[] { 4 },
            //    DecisionAttribute = 4,
            //    AttributesDomain = new double[][]
            //    {
            //        null, // no need for "id"
            //        new double[]{ 0, 1    },
            //        new double[]{ 0, 1    },
            //        new double[]{ 0, 1, 2 },
            //        new double[]{ 0, 1    },
            //    },
            //};

            int[] conditionAttributes;
            int[] decisionAttributes;
            double?[][] universe;
            double[][] attributeDomain;
            int idIndex = 0;
            DataRepository.GetDataForDecisionSystem(attributeDefinitions, tableName, out universe, out attributeDomain, out decisionAttributes, out conditionAttributes, ref idIndex);
            DecisionSystem ds = new DecisionSystem
            {
                Universe = universe,
                ConditionAttributes = conditionAttributes,
                DecisionAttributes = decisionAttributes,
                DecisionAttribute = decisionAttributes[0],
                AttributesDomain = attributeDomain
            };
            return ds;
        }
Example #6
0
        public static void perfom()
        {
            //var chi2val = SpecialFunction.chisq(1, 0.5);
            //return;

            // load file
            var infSys = new DecisionSystem();
            string filename = @"data/iris.csv";
            //string filename = @"data/input1.csv";
            StreamReader reader = new StreamReader(filename);
            infSys.ConditionAttributes = new int[] { 1, 2, 3, 4 };
            infSys.DecisionAttribute = 5;

            // Universe
            reader.ReadLine(); // ignore header line
            List<double?[]> objs = new List<double?[]>();
            List<double?> obj;
            int idTracker = 0;
            Dictionary<int, List<double>> attrDomain = new Dictionary<int,List<double>>();
            for(int i = 0; i < 6; i ++)
                attrDomain.Add(i, new List<double>());
            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                obj = new List<double?>();
                obj.Add(++idTracker);
                var numbers = line.Split(',')
                    .Select((x, i) => new {str = x, idx = i})
                    .Where(o => o.idx < 5)
                    .Select(t => (double?)Double.Parse(t.str))
                    .ToList();

                obj.AddRange(numbers);
                objs.Add(obj.ToArray());
                // get value domain of all attrs
                // ignore the id (i = 0) of object
                for (int i = 1; i < 6; i++) {
                    if(obj[i] != null && !attrDomain[i].Contains((double)obj[i]) )
                        attrDomain[i].Add((double)obj[i]);
                }
            }
            infSys.Universe = objs.ToArray();
            infSys.AttributesDomain = attrDomain
                .OrderBy(p => p.Key)
                .Select(p => p.Value.ToArray())
                .ToArray();
            reader.Close();
            printDS(infSys, @"data/inputDS.txt");

            // get begin inconsistency rate
            var apprSpace = new StandardApproximationSpace(infSys, infSys.ConditionAttributes);
            double rate = apprSpace.InConsistencyRate();

            // Process data
            Chi2Discretizer dis = new Chi2Discretizer(infSys);
            dis.Process();

            // Get resulted DS
            var processedDS = dis.BuildProcessedDS();
            printDS(processedDS, @"data/processedDS.txt");

            // get rate after processed
            var apprSpace2 = new StandardApproximationSpace(processedDS, processedDS.ConditionAttributes);
            double rate2 = apprSpace2.InConsistencyRate();
            Console.WriteLine(rate2);

            // Print report
            StreamWriter writer = new StreamWriter(@"data/report.txt", false);

            writer.WriteLine("Data file: " + filename);
            writer.WriteLine("- Item Count: " + infSys.Universe.Length);
            writer.WriteLine("- Original condition attributes: " + string.Join(",", infSys.ConditionAttributes));
            writer.WriteLine("- Selected attributes (reduct): " + string.Join(",", dis.SelectedConditionAttrs));
            writer.WriteLine("- Original domain count: ");
            foreach (int attr in infSys.ConditionAttributes) {
                writer.Write(attr + ": ");
                writer.WriteLine(infSys.AttributesDomain[attr].Length.ToString());
            }
            writer.WriteLine("- Processed domain count: ");
            foreach (int attr in processedDS.ConditionAttributes)
            {
                writer.Write(attr + ": ");
                writer.WriteLine(processedDS.AttributesDomain[attr].Length.ToString());
            }
            writer.WriteLine("- Removed attributes: " + string.Join(",", dis.RemovedConditionAttrs));
            writer.WriteLine("- Original in-consistency rate: " + rate);
            writer.WriteLine("- Processed in-consistency rate: " + rate2);
            if (rate2 > 0)
            {
                var inConClasses = apprSpace2.IndiscernibilityClasses()
                    .Where(X => ApproximationSpace.GetAllDecisionClasses(X, processedDS.DecisionAttribute).Count() > 1);
                writer.WriteLine("- All in-consistent classes:");
                int count = 0;
                foreach(var klass in inConClasses){
                    count++;
                    writer.WriteLine("\t+ Class: " + count);
                    foreach (var o in klass) {
                        writer.WriteLine("\t\t" + string.Join(",", o));
                    }
                }
            }
            writer.Close();
        }
Example #7
0
 public static void printDS(DecisionSystem ds, string fileName)
 {
     StreamWriter writer = new StreamWriter(fileName);
     foreach (var obj in ds.Universe) {
         var line = obj
             .Select(x => x.ToString())
             .Aggregate((joinstr, str) => joinstr + "\t" + str);
         writer.WriteLine(line);
     }
     writer.Close();
 }
        // check whether new inconsistency rate will exceed the original rate
        private bool WillNextMergedIntervalIncreaseInConsisRate(int targetAttr, List<double> chi2Values)
        {
            int firstIntervalIdx = chi2Values.IndexOf(chi2Values.Min());
            double replacedInterval = this.processedAttrDomains[targetAttr][firstIntervalIdx];
            double removedInterval = this.processedAttrDomains[targetAttr][firstIntervalIdx + 1];
            var newUniverse = this.crnUniverse.Select(x => (double?[])x.Clone()).ToArray();
            for (int i = 0; i < newUniverse.Count();  i++)
            {
                var newObj = newUniverse[i];
                //FIXME: for test
                if (newObj[0] == 77)
                    Console.WriteLine();
                if (newObj[targetAttr] == removedInterval)
                    newObj[targetAttr] = replacedInterval;
            }

            var newDS = new DecisionSystem(newUniverse.ToArray(), this.SelectedConditionAttrs, this.DS.DecisionAttribute);
            var newApprSpace = new StandardApproximationSpace(newDS, this.SelectedConditionAttrs);
            var newRate = newApprSpace.InConsistencyRate();

            return (newRate > this.Delta);
        }
        public DecisionSystem BuildProcessedDS()
        {
            DecisionSystem newDS = new DecisionSystem();
            newDS.AttributesDomain = this.processedAttrDomains
                .OrderBy(pair => pair.Key)
                .Select(pair => pair.Value.ToArray())
                .ToArray();
            newDS.ConditionAttributes = this.SelectedConditionAttrs;
            newDS.DecisionAttribute = this.DS.DecisionAttribute;
            newDS.DecisionAttributes = this.DS.DecisionAttributes;
            newDS.Universe = this.crnUniverse;

            return newDS;
        }
 public StandardApproximationSpace(DecisionSystem infoSys, IEnumerable<int> B)
     : base(infoSys, IndiscernibilityRelation.Equivalence(B), RoughInclusion.Standard)
 {
     if (!B.Any()) throw new ArgumentException("Should have at least 1 condition attributes", "B");
     Attrs = B;
 }