Exemple #1
0
        protected void EvalXiBold(double[][] X)
        {
            // foreach dataVector in dataSet
            for (int i = 0; i < m; i++)
            {
                xiBoldValue[i] = new HyperVector(n + 1, R);

                // xiBoldValue[i][0] <- xiValue[i]
                for (int j = 0; j < R; j++)
                {
                    xiBoldValue[i].Elements[0].Elements[j] = 1; //xiValue[i][j];
                }
                // xiBoldValue[i][j] <- xiValue[i] * x[j]
                for (int j = 0; j < n; j++)
                {
                    for (int k = 0; k < R; k++)
                    {
                        xiBoldValue[i].Elements[j + 1].Elements[k] = X[i][j] * 1; //xiValue[i][k];
                    }
                }
            }
        }
Exemple #2
0
        public void kliGenerate(TSAFuzzySystem Approximate, TypeTermFuncEnum typeFunc, double mayError)
        {
            if ((Approximate.RulesDatabaseSet == null) || (Approximate.RulesDatabaseSet.Count == 0))
            {
                Approximate.RulesDatabaseSet.Add(new KnowlegeBaseTSARules());
            }
            originalSimpleSet = new List <SampleSet.RowSample>(Approximate.LearnSamplesSet.DataRows);
            originalSimpleSetDistanteToPoint = new List <double>();
            CalcFarPoint();


            int ruleIndex = 0;


            while (originalSimpleSet.Count > 0)
            {
                int IndexOfMax = originalSimpleSetDistanteToPoint.IndexOf(originalSimpleSetDistanteToPoint.Max());

                var maxPoint = originalSimpleSet[IndexOfMax];
                originalSimpleSet.Remove(maxPoint);
                originalSimpleSetDistanteToPoint.RemoveAt(IndexOfMax);

                var iterClasterPoints = new List <SampleSet.RowSample>()
                {
                    maxPoint
                };
                var mnk = new MNK()
                {
                    n = maxPoint.InputAttributeValue.Count()
                };
                var P = new Matrix(new[] { new double[] { 0 } });
                var B = new HyperVector(0, 0);

                mnk.mnkIter(iterClasterPoints.Last().InputAttributeValue.ToList(), iterClasterPoints.Last().DoubleOutput,
                            ref P, ref B, true);

                //     var errorIter = errorsMnk(B, iterClasterPoints);

                for (int z = 0; z < Approximate.CountFeatures; z++)
                {
                    int NextIndex2 = maxPoint.NearestNormalizedInputIndex(originalSimpleSet, ResultSystem.LearnSamplesSet);

                    var nextPoint2 = originalSimpleSet[NextIndex2];
                    mnk.mnkIter(nextPoint2.InputAttributeValue.ToList(), nextPoint2.DoubleOutput, ref P, ref B, false);
                    iterClasterPoints.Add(nextPoint2);
                    originalSimpleSet.Remove(nextPoint2);
                }
                var errorIter = errorsMnk(B, iterClasterPoints);


                while (errorIter < mayError)
                {
                    if (originalSimpleSet.Count < Approximate.CountFeatures)
                    {
                        while (originalSimpleSet.Count > 0)
                        {
                            int NextIndex2 = maxPoint.NearestNormalizedInputIndex(originalSimpleSet, ResultSystem.LearnSamplesSet);

                            var nextPoint2 = originalSimpleSet[NextIndex2];
                            mnk.mnkIter(nextPoint2.InputAttributeValue.ToList(), nextPoint2.DoubleOutput, ref P, ref B, false);
                            iterClasterPoints.Add(nextPoint2);
                            originalSimpleSet.Remove(nextPoint2);
                            originalSimpleSetDistanteToPoint.RemoveAt(NextIndex2);
                        }
                        break;
                    }


                    int NextIndex = maxPoint.NearestNormalizedInputIndex(originalSimpleSet, ResultSystem.LearnSamplesSet);

                    var nextPoint = originalSimpleSet[NextIndex];
                    mnk.mnkIter(nextPoint.InputAttributeValue.ToList(), nextPoint.DoubleOutput, ref P, ref B, false);
                    errorIter = errorsMnk(B, new List <SampleSet.RowSample>(iterClasterPoints)
                    {
                        nextPoint
                    });
                    if (errorIter < mayError)
                    {
                        iterClasterPoints.Add(nextPoint);
                        maxPoint = nextPoint;
                        originalSimpleSet.Remove(nextPoint);
                        originalSimpleSetDistanteToPoint.RemoveAt(NextIndex);
                    }
                }
                var         numbersRule = new List <int>();
                List <Term> forRWLSM    = new List <Term>();
                for (int i = 0; i < Approximate.CountFeatures; i++)
                {
                    var parametrs = new double[Term.CountParamsinSelectedTermType(typeFunc)];
                    parametrs[0] = iterClasterPoints.Sum(x => x.InputAttributeValue[i]) / iterClasterPoints.Count;
                    parametrs[1] =
                        Math.Sqrt(
                            iterClasterPoints.Sum(x => Math.Pow(x.InputAttributeValue[i] - parametrs[0], 2.0)) /
                            iterClasterPoints.Count);
                    if (parametrs[1] < Math.Pow(10, -300))
                    {
                        parametrs[1] = Approximate.LearnSamplesSet.InputAttributes[i].Scatter / 100.0;
                    }


                    var temp_term = new Term(parametrs, typeFunc, i);
                    forRWLSM.Add(temp_term);
                    Approximate.RulesDatabaseSet[0].TermsSet.Add(temp_term);
                    numbersRule.Add(Approximate.RulesDatabaseSet[0].TermsSet.Count - 1);
                }
                ruleIndex++;
                double[] coeffs    = null;
                double   coef      = LSMWeghtReqursiveSimple.EvaluteConsiquent(ResultSystem, forRWLSM, out coeffs);
                var      temp_rule = new TSARule(Approximate.RulesDatabaseSet[0].TermsSet, numbersRule.ToArray(), coef, coeffs);

                Approximate.RulesDatabaseSet[0].RulesDatabase.Add(temp_rule);
            }
        }
Exemple #3
0
 public static double errorsMnk(HyperVector B, List <SampleSet.RowSample> samples)
 {
     return(Math.Sqrt((from x in samples let summ = x.InputAttributeValue.ToList().Select((a, i) => a * B.Elements[i + 1].Elements[0]).Sum() + B.Elements[0].Elements[0] select Math.Pow((summ - x.DoubleOutput), 2.0)).Sum()));
 }
Exemple #4
0
        public override TSAFuzzySystem TuneUpFuzzySystem(TSAFuzzySystem Approximate, ILearnAlgorithmConf conf)
        {
            TSAFuzzySystem result = Approximate;

            double errorBefore = result.approxLearnSamples(result.RulesDatabaseSet[0]);

            var a = 2000;

            #region Basic initialization
            var config = (RLSconfig)conf;
            numberOfIterations = config.NumberOfIterantions;
            lambda             = config.ForgettingFactor;

            var knowledgeBaseToOptimize = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);
            var kbToOptimize            = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);

            R = knowledgeBaseToOptimize.RulesDatabase.Count;                                  // Number of rules
            m = result.LearnSamplesSet.CountSamples;                                          // Number of samples
            n = knowledgeBaseToOptimize.RulesDatabase[0].RegressionConstantConsequent.Length; // Number of variables
            #endregion

            #region x, y
            double[][] x = new double[m][];
            double[]   y = new double[m];
            for (int i = 0; i < m; i++)
            {
                x[i] = result.LearnSamplesSet.DataRows[i].InputAttributeValue;
                y[i] = result.LearnSamplesSet.DataRows[i].DoubleOutput;
            }
            #endregion

            #region B
            double[][] consequents = new double[n + 1][];

            // B[0]
            consequents[0] = new double[R];
            for (int i = 0; i < R; i++)
            {
                consequents[0][i] = knowledgeBaseToOptimize.RulesDatabase[i].IndependentConstantConsequent;
            }

            // B[1..n+1]
            for (int i = 1; i < n + 1; i++)
            {
                consequents[i] = new double[R];
                for (int j = 0; j < R; j++)
                {
                    consequents[i][j] = knowledgeBaseToOptimize.RulesDatabase[j].RegressionConstantConsequent[i - 1];
                }
            }

            HyperVector B = new HyperVector(consequents);
            #endregion

            #region P
            double[][] p = new double[n + 1][];
            for (int i = 0; i < n + 1; i++)
            {
                p[i] = new double[n + 1];
                for (int j = 0; j < n + 1; j++)
                {
                    p[i][j] = (i == j) ? a : 0;
                }
            }

            Matrix P = new Matrix(p);
            #endregion

            #region Xi
            dividerXi = new double[m];
            xiValue   = new double[m][];
            for (int i = 0; i < m; i++)
            {
                xiValue[i] = new double[R];
            }
            EvalXi(x, y, knowledgeBaseToOptimize);

            // XiBold
            xiBoldValue = new HyperVector[m];
            EvalXiBold(x);
            #endregion

            GC.Collect();

            //double[][] aDoubles = new[]
            //                          {
            //                              new[] { 0.1, 0.2, 0.123 },
            //                              new[] { 1.213, 2.1, 1.2 },
            //                              new[] { 13.3, 0.1231, 31.1 }
            //                          },
            //           bDoubles = new[]
            //                          {
            //                              new[] { 0.1, 12.2, 5.445 },
            //                              new[] { 5.3, 4.553, 1.545 },
            //                              new[] { 3.4, 87.545, 0.255 }
            //                          };
            //Matrix aMatrix = new Matrix(aDoubles);
            //HyperVector bVector = new HyperVector(bDoubles);
            //HyperVector cVector = new HyperVector(aDoubles);

            //var c = aMatrix * bVector;
            //var cc = c;
            //var d = bVector * cVector;
            //var dd = d;
            //var e = bVector ^ cVector;
            //var ee = e;
            //var f = cVector * aMatrix;
            //var ff = f;



            #region The Cycle
            for (int i = 0; i < numberOfIterations; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    var temp1 = -1d * P;
                    var temp3 = temp1 * xiBoldValue[j];

                    var temp5 = xiBoldValue[j] * P * xiBoldValue[j];
                    var temp6 = lambda + temp5;
                    var temp7 = temp3 * (1d / temp6);
                    var temp8 = xiBoldValue[j] * P;
                    var temp9 = temp7 ^ temp8;
                    P += temp9;
                    P /= lambda;

                    ////P = (P + (-1d * P * xiBoldValue[j] * (1d / (lambda + (xiBoldValue[j] * P * xiBoldValue[j]))) ^ xiBoldValue[j] * P)) / lambda;

                    B += P * xiBoldValue[j] * (y[j] - xiBoldValue[j] * B);
                }
            }
            #endregion

            #region Comparison
            // Get consequents into the KB
            for (int i = 0; i < R; i++)
            {
                knowledgeBaseToOptimize.RulesDatabase[i].IndependentConstantConsequent = B.Elements[0].Elements[i];

                for (int j = 1; j < n + 1; j++)
                {
                    knowledgeBaseToOptimize.RulesDatabase[i].RegressionConstantConsequent[j - 1] = B.Elements[j].Elements[i]; // NOT WORKING!!!
                }
            }

            // Get the best knowledge base on the 1st place
            double errorAfter = result.approxLearnSamples(kbToOptimize);

            if (errorAfter < errorBefore)
            {
                result.RulesDatabaseSet.Insert(0, knowledgeBaseToOptimize);
            }
            else
            {
                result.RulesDatabaseSet.Insert(0, kbToOptimize);
                //    result.RulesDatabaseSet.Add(kbToOptimize);
            }

            return(result);

            #endregion
        }
Exemple #5
0
        public static void kliGenerate(TSAFuzzySystem Approximate, TypeTermFuncEnum typeFunc, double mayError)
        {
            var ssr = new List <SampleSet.RowSample>();

            for (int index = 0; index < Approximate.LearnSamplesSet.DataRows.Count; index++)
            {
                var           row = new SampleSet.RowSample();
                List <double> InputAttributeValueLIST = new List <double>();
                for (int i = 0; i < Approximate.AcceptedFeatures.Count(); i++)
                {
                    double q = Convert.ToDouble(Approximate.LearnSamplesSet.DataRows[index].InputAttributeValue[i].ToString());
                    if (Approximate.AcceptedFeatures[i])
                    {
                        InputAttributeValueLIST.Add(q);
                    }


                    row.InputAttributeValue = InputAttributeValueLIST.ToArray();
                    row.DoubleOutput        = Approximate.LearnSamplesSet.DataRows[index].DoubleOutput;
                }


                ssr.Add(row);
            }


            List <SampleSet.AttributeInfo> ss =
                new List <SampleSet.AttributeInfo>(Approximate.LearnSamplesSet.InputAttributes);

            for (int i = (Approximate.AcceptedFeatures.Count() - 1); i >= 0; i--)
            {
                if (!Approximate.AcceptedFeatures[i])
                {
                    ss.RemoveAt(i);
                }
            }

            if ((Approximate.RulesDatabaseSet == null) || (Approximate.RulesDatabaseSet.Count == 0))
            {
                Approximate.RulesDatabaseSet.Add(new KnowlegeBaseTSARules());
                //wtf????????????? что за, если null придет то всё плохо плохо, а мы его пропускаем выше лол
            }
            var originalSimpleSet = new List <SampleSet.RowSample>(ssr);

            int ruleIndex = 0;

            while (originalSimpleSet.Count > 0)
            {
                //а тут нормировка особой роли не играет, хммм ну или почти не играет
                var maxPoint = originalSimpleSet.OrderByDescending(x => (Math.Sqrt(x.InputAttributeValue.Sum(y => Math.Pow(y, 2.0)) /
                                                                                   x.InputAttributeValue.Count()))).First();
                var iterClasterPoints = new List <SampleSet.RowSample>()
                {
                    maxPoint
                };
                var mnk = new MNK()
                {
                    n = maxPoint.InputAttributeValue.Count()
                };
                var P = new Matrix(new[] { new double[] { 0 } });
                var B = new HyperVector(0, 0);

                mnk.mnkIter(iterClasterPoints.Last().InputAttributeValue.ToList(), iterClasterPoints.Last().DoubleOutput,
                            ref P, ref B, true);

                var errorIter = errorsMnk(B, iterClasterPoints);
                while (errorIter < mayError)
                {
                    originalSimpleSet.RemoveAll(x => iterClasterPoints.Contains(x));
                    if (originalSimpleSet.Count == 0)
                    {
                        break;
                    }
                    //это без нормировки
                    // var nextPoint =
                    // originalSimpleSet.OrderBy(x =>(Math.Sqrt(x.InputAttributeValue.Sum(y => Math.Pow(y -maxPoint.InputAttributeValue[x.InputAttributeValue.ToList().IndexOf(y)],2.0))/x.InputAttributeValue.Count()))).First();

                    //а это с нормировкой
                    var nextPoint = originalSimpleSet.OrderBy(simple => Math.Sqrt(simple.InputAttributeValue.Select((inputValue, index) => Math.Pow((inputValue - maxPoint.InputAttributeValue[index]) / (ss[index].Scatter), 2.0)).Sum())).First();

                    mnk.mnkIter(nextPoint.InputAttributeValue.ToList(), nextPoint.DoubleOutput, ref P, ref B, false);
                    errorIter = errorsMnk(B, new List <SampleSet.RowSample>(iterClasterPoints)
                    {
                        nextPoint
                    });
                    if (errorIter < mayError)
                    {
                        iterClasterPoints.Add(nextPoint);
                    }
                }

                var numbersRule = new List <int>();
                int spear       = 0;

                for (int i = 0; i < Approximate.AcceptedFeatures.Count(); i++)//.CountVars
                {
                    if (!Approximate.AcceptedFeatures[i])
                    {
                        spear++;
                        continue;
                    }
                    var parametrs = new double[Term.CountParamsinSelectedTermType(typeFunc)];
                    parametrs[0] = iterClasterPoints.Sum(x => x.InputAttributeValue[i - spear]) / iterClasterPoints.Count;
                    parametrs[1] =
                        Math.Sqrt(
                            iterClasterPoints.Sum(x => Math.Pow(x.InputAttributeValue[i - spear] - parametrs[0], 2.0)) * 2 /
                            iterClasterPoints.Count);

                    var temp_term = new Term(parametrs, typeFunc, i);
                    Approximate.RulesDatabaseSet[0].TermsSet.Add(temp_term);
                    numbersRule.Add(ruleIndex + i - spear);
                }
                ruleIndex += Approximate.AcceptedFeatures.Where(x => x == true).Count();//.CountVars

                var temp_rule = new TSARule(Approximate.RulesDatabaseSet[0].TermsSet, numbersRule.ToArray(), B.Elements[0].Elements[0], B.Elements.Skip(1).Select(x => x.Elements[0]).ToArray());
                Approximate.RulesDatabaseSet[0].RulesDatabase.Add(temp_rule);
            }
        }
Exemple #6
0
        public void mnkIter(List <double> x, double y, ref Matrix Pout, ref HyperVector Bout, bool useInit)
        {
            var a = 2000;

            #region Basic initialization

            double lambda = 1;

            #endregion

            #region B

            double[][] consequents = new double[n + 1][];

            // B[0]
            consequents[0] = new double[R];
            for (int i = 0; i < R; i++)
            {
                consequents[0][i] = 1d;
            }

            // B[1..n+1]
            for (int i = 1; i < n + 1; i++)
            {
                consequents[i] = new double[R];
                for (int j = 0; j < R; j++)
                {
                    consequents[i][j] = 0;
                }
            }

            var B = useInit ? new HyperVector(consequents) : Bout;

            #endregion

            #region P

            double[][] p = new double[n + 1][];
            for (int i = 0; i < n + 1; i++)
            {
                p[i] = new double[n + 1];
                for (int j = 0; j < n + 1; j++)
                {
                    p[i][j] = (i == j) ? a : 0;
                }
            }

            var P = useInit ? new Matrix(p) : Pout;

            #endregion

            GC.Collect();
            xiBoldValue = new HyperVector[m];
            EvalXiBold(new double[][] { x.ToArray() });

            #region The Cycle

            {
                {
                    var temp1 = -1d * P;
                    var temp3 = temp1 * xiBoldValue[0];

                    var temp5 = xiBoldValue[0] * P * xiBoldValue[0];
                    var temp6 = lambda + temp5;
                    var temp7 = temp3 * (1d / temp6);
                    var temp8 = xiBoldValue[0] * P;
                    var temp9 = temp7 ^ temp8;
                    P += temp9;
                    P /= lambda;
                    B += P * xiBoldValue[0] * (y - xiBoldValue[0] * B);
                }
            }

            #endregion

            Pout = P;
            Bout = B;
        }