Esempio n. 1
0
 public static double[] denominateCoefficient(TSAFuzzySystem System, Matrix Teta)
 {
     double[] result = new double[Teta.RowDimension - 1];
     for (int i = 1; i < Teta.RowDimension; i++)
     {
         result[i - 1] = (Teta.GetElement(i, 0));    //    *System.LearnSamplesSet.Output_Attributes.Scatter) / System.LearnSamplesSet.Input_Attributes[i - 1].Scatter;
     }
     return(result);
 }
Esempio n. 2
0
 public TSAFuzzySystem(TSAFuzzySystem Source)
     : base(Source)
 {
     RulesDatabaseSet = new List <KnowlegeBaseTSARules>();
     for (int i = 0; i < Source.RulesDatabaseSet.Count; i++)
     {
         RulesDatabaseSet.Add(new KnowlegeBaseTSARules(Source.RulesDatabaseSet[i]));
     }
 }
 public static double[] denominateCoefficient(TSAFuzzySystem System, Matrix Teta)
 {
     double[] result = new double[Teta.RowDimension - 1];
     for (int i = 1; i < Teta.RowDimension; i++)
     {
         result[i - 1] = (Teta.GetElement(i, 0));
     }
     return(result);
 }
Esempio n. 4
0
 public override string ToString(bool with_param = false)
 {
     if (with_param)
     {
         string result = "Swallow Swarm Optimization{";
         result += "}";
         return(result);
     }
     return("Swallow Swarm Optimization");
 }
Esempio n. 5
0
        public static double denominateIndependCoeff(TSAFuzzySystem System, Matrix Teta)
        {
            double result = Teta.GetElement(0, 0);

            /*   double sum = 0;
             * for (int i = 1; i < Teta.RowDimension; i++)
             * {
             *     sum = sum + (Teta.GetElement(i, 0) * System.LearnSamplesSet.InputAttributeMin(i - 1)) / System.LearnSamplesSet.InputAttributeScatter(i - 1);
             * }
             * result = result - sum;
             * result *= System.LearnSamplesSet.Output_Attributes.Scatter;
             * result += System.LearnSamplesSet.Output_Attributes.Min;  */
            return(result);
        }
Esempio n. 6
0
 public override string ToString(bool with_param = false)
 {
     if (with_param)
     {
         string result = "Water cycle algorithm" + "  {" + Environment.NewLine;
         result += "Итераций= " + MaxIter.ToString() + " ;" + Environment.NewLine;
         result += "Капель= " + Npop.ToString() + " ;" + Environment.NewLine;
         result += "Рек= " + NRivers.ToString() + " ;" + Environment.NewLine;
         result += "Dmax= " + Dmax.ToString() + " ;" + Environment.NewLine;
         result += "const= " + con.ToString() + ";" + Environment.NewLine;
         result += "Испарение - " + flag + "; }" + Environment.NewLine;
         return(result);
     }
     return("Water cycle algorithm");
 }
Esempio n. 7
0
        public static double EvaluteConsiquent(TSAFuzzySystem System, List <Term> termSet, out double[] RegressionCoefficents)
        {
            Matrix nominateVector, PnominatorMul1, PnominatorMul2, PnominatorMul3, UnidenominatorPart1, Unidenominator2, TetaResultPart1, Tetanominator1, TetaResultBr2Part1;
            double TetaResultBr2, UniDenominator;

            RegressionCoefficents = null;
            int    countParams = System.CountFeatures;// termSet.Count();
            double a           = Math.Pow(10, 100);
            Matrix P           = new Matrix(countParams + 1, countParams + 1, 0);

            for (int i = 0; i < countParams + 1; i++)
            {
                P.SetElement(i, i, a);
            }
            Matrix Teta = new Matrix(countParams + 1, 1, 0);
            List <SampleSet.RowSample> RealValue     = SelectPoint(System, termSet, countParams);
            List <SampleSet.RowSample> NominateValue = nominateValue(System, RealValue);

            for (int i = 0; i < NominateValue.Count; i++)
            {
                nominateVector      = new  Matrix(NominateValue[i].InputAttributeValue, countParams + 1);
                UnidenominatorPart1 = nominateVector.Transpose().Multiply(P);
                Unidenominator2     = UnidenominatorPart1.Multiply(nominateVector);
                UniDenominator      = 1.0 / (1.0 / (calcWeigth(NominateValue[i], termSet)) + Unidenominator2.GetElement(0, 0));


                Tetanominator1  = P.Multiply(nominateVector);
                TetaResultPart1 = Tetanominator1.Multiply(UniDenominator);

                TetaResultBr2Part1 = nominateVector.Transpose().Multiply(Teta);

                TetaResultBr2 = NominateValue[i].DoubleOutput - TetaResultBr2Part1.GetElement(0, 0);

                Teta = Teta + TetaResultPart1.Multiply(TetaResultBr2);


                // Calc P
                PnominatorMul1 = P.Multiply(nominateVector);
                PnominatorMul2 = PnominatorMul1.Multiply(nominateVector.Transpose());
                PnominatorMul3 = PnominatorMul2.Multiply(P);

                P = P - PnominatorMul3.Multiply(UniDenominator);
                //End
            }

            RegressionCoefficents = denominateCoefficient(System, Teta);
            return(denominateIndependCoeff(System, Teta));
        }
Esempio n. 8
0
        public void ConstructNewRule(List <Term> terms, TSAFuzzySystem FS)
        {
            TSARule Result;

            int[] order_of_terms = new int[terms.Count()];
            for (int i = 0; i < terms.Count(); i++)
            {
                order_of_terms[i] = TermsSet.Count;
                TermsSet.Add(terms[i]);
            }
            double [] temp_regressionCoefficent;
            double    kons_Value = LSMWeghtReqursiveSimple.EvaluteConsiquent(FS, TermsSet, out temp_regressionCoefficent);

            Result = new TSARule(TermsSet, order_of_terms, kons_Value, temp_regressionCoefficent);
            RulesDatabase.Add(Result);
        }
        private static List <SampleSet.RowSample> MakeXValue(TSAFuzzySystem System, List <SampleSet.RowSample> Source)
        {
            List <SampleSet.RowSample> result = new List <SampleSet.RowSample>();

            for (int i = 0; i < Source.Count; i++)
            {
                double[] inputValue = new double[Source[i].InputAttributeValue.Length + 1];
                inputValue[0] = 1;
                for (int j = 1; j < Source[i].InputAttributeValue.Length + 1; j++)
                {
                    inputValue[j] = (Source[i].InputAttributeValue[j - 1]);
                }
                double Value = (Source[i].DoubleOutput);
                result.Add(new SampleSet.RowSample(inputValue, null, Value, ""));
            }
            return(result);
        }
Esempio n. 10
0
        public override TSAFuzzySystem TuneUpFuzzySystem(TSAFuzzySystem Approx, ILearnAlgorithmConf conf)
        {
            result = Approx;
            Init(conf);
            HeadLeader       = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);
            VelocityVector   = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);
            VelocityVectorLL = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);
            VelocityVectorHL = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);
            for (int i = 0; i < VelocityVector.TermsSet.Count; i++)
            {
                for (int j = 0; j < VelocityVector.TermsSet[i].Parametrs.Length; j++)
                {
                    VelocityVector.TermsSet[i].Parametrs[j]   = 0;
                    VelocityVectorLL.TermsSet[i].Parametrs[j] = 0;
                    VelocityVectorHL.TermsSet[i].Parametrs[j] = 0;
                }
            }
            SetPopulation();
            ParticlesBest = new Dictionary <KnowlegeBaseTSARules, KnowlegeBaseTSARules>();
            foreach (var Particle in Population)
            {
                ParticlesBest.Add(Particle, Universal);
            }
            LocalLeaders      = new KnowlegeBaseTSARules[numberOfLocalLeaders];
            ExplorerParticles = new KnowlegeBaseTSARules[numberOfAllParts - numberOfAimlessParts - numberOfLocalLeaders - 1];
            AimlessParticles  = new KnowlegeBaseTSARules[numberOfAimlessParts];
            iter = 0;
            while (iter < MaxIter)
            {
                Population = ListTakagiSugenoApproximateTool.SortRules(Population, result);
                SetRoles();
                ChangeExplorersPositions();
                ChangeAimlessPositions();
                DiscardRoles();
                if (iter == (MaxIter - 1) || iter == 0)
                {
                    Console.WriteLine("Iteration: " + (iter + 1).ToString());
                    Console.WriteLine(Math.Round(result.RMSEtoMSEforLearn(result.approxLearnSamples(Population[0])), 3));
                    Console.WriteLine(Math.Round(result.RMSEtoMSEforTest(result.approxTestSamples(Population[0])), 3));
                }
                iter++;
            }

            result.RulesDatabaseSet[0] = Population[0];
            return(result);
        }
Esempio n. 11
0
        private static List <SampleSet.RowSample> nominateValue(TSAFuzzySystem System, List <SampleSet.RowSample> Source)
        {
            List <SampleSet.RowSample> result = new List <SampleSet.RowSample>();

            for (int i = 0; i < Source.Count; i++)
            {
                double[] inputValue = new double[Source[i].InputAttributeValue.Length + 1];
                inputValue[0] = 1;
                for (int j = 1; j < Source[i].InputAttributeValue.Length + 1; j++)
                {
                    inputValue[j] = (Source[i].InputAttributeValue[j - 1]); //   - System.LearnSamplesSet.InputAttributeMin(j - 1) ) / (System.LearnSamplesSet.InputAttributeScatter(j - 1));
                }
                double Value = (Source[i].DoubleOutput);                    //    - System.LearnSamplesSet.Output_Attributes.Min) / System.LearnSamplesSet.Output_Attributes.Scatter;

                result.Add(new SampleSet.RowSample(inputValue, null, Value, ""));
            }
            return(result);
        }
        public static List <SampleSet.RowSample> SelectPoint(TSAFuzzySystem System, List <Term> termSet, int CountParams, double SelectLevel = 0.000000001)
        {
            List <SampleSet.RowSample> result = new List <SampleSet.RowSample>();
            List <double> Distance            = new List <double>();

            double[] min = new double[termSet.Count];
            double[] max = new double[termSet.Count];
            double[] temp;
            for (int i = 0; i < termSet.Count; i++)
            {
                temp = termSet[i].getXValueByLevelMembership(SelectLevel);
                min[termSet[i].NumVar] = temp[0];
                max[termSet[i].NumVar] = temp[1];
            }

            for (int i = 0; i < System.LearnSamplesSet.CountSamples; i++)
            {
                bool flag = true;
                for (int j = 0; j < termSet.Count; j++)
                {
                    if (System.AcceptedFeatures[termSet[j].NumVar] == false)
                    {
                        continue;
                    }
                    if ((System.LearnSamplesSet.DataRows[i].InputAttributeValue[termSet[j].NumVar] <= min[termSet[j].NumVar]) &&
                        (System.LearnSamplesSet.DataRows[i].InputAttributeValue[termSet[j].NumVar] >= max[termSet[j].NumVar]))
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    result.Add(System.LearnSamplesSet[i]);
                }
            }
            return(result);
        }
Esempio n. 13
0
        public override TSAFuzzySystem TuneUpFuzzySystem(TSAFuzzySystem Approximate, ILearnAlgorithmConf Conf)
        {
            result = Approximate;
            List <int[]> groups = new List <int[]>();

            Init(Conf);
            SetPopulation();
            Population = SortRules(Population);
            NS         = new int[Nsr];
            NS         = SetNS(Population, Nsr);
            groups     = GroupStream();
            double BestMSETest  = result.RMSEtoMSEforTest(result.approxTestSamples(Population[0]));
            double BestMSELearn = result.RMSEtoMSEforLearn(result.approxLearnSamples(Population[0]));
            int    BestIter     = 0;

            for (int i = 1; i <= MaxIter; i++)
            {
                Console.WriteLine(i + " - Итерация");
                Population = SetNextPosition(groups, Population);
                Population = Replacement(groups, Population);
                if (flag)
                {
                    Evaporation(groups.Last());//Испарение
                }
                if (BestMSETest > result.RMSEtoMSEforTest(result.approxTestSamples(Population[0])))
                {
                    BestMSETest  = result.RMSEtoMSEforTest(result.approxTestSamples(Population[0]));
                    BestMSELearn = result.RMSEtoMSEforLearn(result.approxLearnSamples(Population[0]));
                    BestIter     = i;
                }
            }
            Console.WriteLine(ToString(true));
            Console.WriteLine("Итер - " + BestIter + " MSET - " + BestMSETest + " MSEL - " + BestMSELearn);
            result.RulesDatabaseSet[0] = Population[0];
            return(result);
        }
Esempio n. 14
0
        public override FuzzySystem.TakagiSugenoApproximate.TSAFuzzySystem Generate(FuzzySystem.TakagiSugenoApproximate.TSAFuzzySystem Approximate, FuzzySystem.FuzzyAbstract.conf.IGeneratorConf config)
        {
            TSAFuzzySystem result = Approximate;

            type_func = TypeTermFuncEnum.Гауссоида;

            ResultSystem = Approximate;

            var kliConf = config as KLI_conf;

            if (kliConf != null)
            {
                double meanValue = result.LearnSamplesSet.DataRows.Select(x => x.DoubleOutput).Average();
                var    mayError  = kliConf.MaxValue * meanValue;


                double centerValue = mayError;

                ScoreListElem[]      ResultsKLI      = new ScoreListElem[3];
                ScoreListElem[]      ResultsKLI2     = new ScoreListElem[3];
                List <ScoreListElem> ResultsKLIFull  = new List <ScoreListElem>();
                List <ScoreListElem> ResultsKLI2Full = new List <ScoreListElem>();
                double magic = 0.25;
                KLI.kliGenerate(result, type_func, centerValue);
                ScoreListElem tempRes = new ScoreListElem();
                tempRes.MaxError = centerValue;
                tempRes.Alg      = "Кли";
                tempRes.Res      = result.RulesDatabaseSet[0];
                tempRes.Error    = result.ErrorLearnSamples(tempRes.Res);
                ResultsKLI[1]    = new ScoreListElem(tempRes);
                if (!double.IsNaN(tempRes.Error))
                {
                    ResultsKLIFull.Add(new ScoreListElem(tempRes));
                }

                for (int i = 0; i < 100; i++)
                {
                    double leftValue  = centerValue * (1 - magic);
                    double rigthValue = centerValue * (1 + magic);

                    tempRes = new ScoreListElem();
                    KLI.kliGenerate(result, type_func, leftValue);
                    tempRes.Alg      = "Кли";
                    tempRes.MaxError = leftValue;
                    tempRes.Res      = result.RulesDatabaseSet[0];
                    tempRes.Error    = result.ErrorLearnSamples(tempRes.Res);
                    ResultsKLI[0]    = new ScoreListElem(tempRes);
                    if (!double.IsNaN(tempRes.Error))
                    {
                        ResultsKLIFull.Add(new ScoreListElem(tempRes));
                    }

                    result.RulesDatabaseSet.Clear();

                    tempRes = new ScoreListElem();
                    KLI.kliGenerate(result, type_func, rigthValue);
                    tempRes.Alg      = "Кли";
                    tempRes.MaxError = rigthValue;
                    tempRes.Res      = result.RulesDatabaseSet[0];
                    tempRes.Error    = result.ErrorLearnSamples(tempRes.Res);
                    ResultsKLI[2]    = tempRes;
                    if (!double.IsNaN(tempRes.Error))
                    {
                        ResultsKLIFull.Add(new ScoreListElem(tempRes));
                    }

                    result.RulesDatabaseSet.Clear();

                    if (((ResultsKLI[0].Error >= ResultsKLI[1].Error) || double.IsNaN(ResultsKLI[0].Error)) && ((ResultsKLI[1].Error <= ResultsKLI[2].Error) || (double.IsNaN(ResultsKLI[2].Error))))
                    {
                        magic *= 0.7;
                    }
                    double err = ResultsKLIFull.Min(y => y.Error);
                    ResultsKLI[1].Error = err;
                    centerValue         = ResultsKLIFull.First(x => x.Error == err).MaxError;
                }

                centerValue = mayError;
                tempRes     = new ScoreListElem();
                kliGenerate(result, type_func, centerValue);
                tempRes.Alg      = "Кли2";
                tempRes.MaxError = centerValue;
                tempRes.Res      = result.RulesDatabaseSet[0];
                tempRes.Error    = result.ErrorLearnSamples(tempRes.Res);
                ResultsKLI2[1]   = new ScoreListElem(tempRes);
                if (!double.IsNaN(tempRes.Error))
                {
                    ResultsKLI2Full.Add(new ScoreListElem(tempRes));
                }
                result.RulesDatabaseSet.Clear();

                magic = 0.25;
                for (int i = 0; i < 100; i++)
                {
                    double leftValue  = centerValue * (1 - magic);
                    double rigthValue = centerValue * (1 + magic);


                    tempRes = new ScoreListElem();
                    kliGenerate(result, type_func, leftValue);
                    tempRes.Alg      = "Кли2";
                    tempRes.MaxError = leftValue;
                    tempRes.Res      = result.RulesDatabaseSet[0];
                    tempRes.Error    = result.ErrorLearnSamples(tempRes.Res);
                    ResultsKLI2[0]   = new ScoreListElem(tempRes);
                    if (!double.IsNaN(tempRes.Error))
                    {
                        ResultsKLI2Full.Add(new ScoreListElem(tempRes));
                    }

                    result.RulesDatabaseSet.Clear();

                    tempRes = new ScoreListElem();
                    kliGenerate(result, type_func, rigthValue);
                    tempRes.Alg      = "Кли2";
                    tempRes.MaxError = rigthValue;
                    tempRes.Res      = result.RulesDatabaseSet[0];
                    tempRes.Error    = result.ErrorLearnSamples(tempRes.Res);
                    ResultsKLI2[2]   = new ScoreListElem(tempRes);
                    if (!double.IsNaN(tempRes.Error))
                    {
                        ResultsKLI2Full.Add(new ScoreListElem(tempRes));
                    }

                    result.RulesDatabaseSet.Clear();

                    if (((ResultsKLI2[0].Error >= ResultsKLI2[1].Error) || double.IsNaN(ResultsKLI2[0].Error)) && ((ResultsKLI2[1].Error <= ResultsKLI2[2].Error) || (double.IsNaN(ResultsKLI2[2].Error))))
                    {
                        magic *= 0.7;
                    }
                    double err = ResultsKLI2Full.Min(y => y.Error);
                    ResultsKLI2[1].Error = err;
                    centerValue          = ResultsKLI2Full.First(x => x.Error == err).MaxError;
                }


                ScoreListElem BestKli  = ResultsKLIFull.Where(x => x.Error == ResultsKLIFull.Min(y => y.Error)).First();
                ScoreListElem BestKli2 = ResultsKLI2Full.Where(x => x.Error == ResultsKLI2Full.Min(y => y.Error)).First();

                if (BestKli.Error > BestKli2.Error)
                {
                    Best = BestKli2;
                }
                else
                {
                    Best = BestKli;
                }


                result.RulesDatabaseSet.Add(Best.Res);
            }
            return(result);
        }
Esempio n. 15
0
        public static List <SampleSet.RowSample> SelectPoint(TSAFuzzySystem System, List <Term> termSet, int CountParams, double SelectLevel = 0.000000001)
        {
            List <SampleSet.RowSample> result = new List <SampleSet.RowSample>();
            List <double> Distance            = new List <double>();

            double[] min = new double[termSet.Count];
            double[] max = new double[termSet.Count];
            double[] temp;
            for (int i = 0; i < termSet.Count; i++)
            {
                temp = termSet[i].getXValueByLevelMembership(SelectLevel);
                min[termSet[i].NumVar] = temp[0];
                max[termSet[i].NumVar] = temp[1];
            }

            for (int i = 0; i < System.LearnSamplesSet.CountSamples; i++)
            {
                bool flag = true;
                for (int j = 0; j < System.LearnSamplesSet.CountVars; j++)
                {
                    if (System.AcceptedFeatures[j] == false)
                    {
                        continue;
                    }
                    if ((System.LearnSamplesSet.DataRows[i].InputAttributeValue[j] <= min[j]) && (System.LearnSamplesSet.DataRows[i].InputAttributeValue[j] >= max[j]))
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    result.Add(System.LearnSamplesSet[i]);
                }
            }

            if (result.Count <= CountParams)
            {
                for (int c = 0; c < System.LearnSamplesSet.CountSamples; c++)
                {
                    double current_diff = 0;
                    for (int i = 0; i < termSet.Count; i++)
                    {
                        if (System.AcceptedFeatures[termSet[i].NumVar] == false)
                        {
                            continue;
                        }

                        if (termSet[i] != null)
                        {
                            switch (termSet[i].TermFuncType)
                            {
                            case TypeTermFuncEnum.Треугольник:
                                current_diff +=
                                    Math.Abs(System.LearnSamplesSet.DataRows[c].InputAttributeValue[termSet[i].NumVar] -
                                             termSet[i].Parametrs[1]);
                                break;

                            case TypeTermFuncEnum.Гауссоида: current_diff +=
                                Math.Abs(System.LearnSamplesSet.DataRows[c].InputAttributeValue[termSet[i].NumVar] -
                                         termSet[i].Parametrs[0]);
                                break;

                            case TypeTermFuncEnum.Парабола:
                                double argv = (termSet[i].Parametrs[0] + termSet[i].Parametrs[1]) / 2;
                                current_diff +=
                                    Math.Abs(System.LearnSamplesSet.DataRows[c].InputAttributeValue[termSet[i].NumVar] -
                                             argv);
                                break;

                            case TypeTermFuncEnum.Трапеция: double argvTR = (termSet[i].Parametrs[1] + termSet[i].Parametrs[2]) / 2;
                                current_diff +=
                                    Math.Abs(System.LearnSamplesSet.DataRows[c].InputAttributeValue[termSet[i].NumVar] -
                                             argvTR);
                                break;
                            }
                        }
                    }
                    if (result.Contains(System.LearnSamplesSet.DataRows[c]))
                    {
                        Distance.Add(double.PositiveInfinity);
                    }
                    else
                    {
                        Distance.Add(current_diff);
                    }
                }

                while (result.Count <= CountParams)
                {
                    double cur_min   = Distance.Min();
                    int    iMinIndex = Distance.IndexOf(cur_min);
                    result.Add(System.LearnSamplesSet.DataRows[iMinIndex]);
                    Distance[iMinIndex] = double.PositiveInfinity;
                }
            }

            return(result);
        }
Esempio n. 16
0
 public static double EvaluteConsiquent(TSAFuzzySystem System, TermSetGlobal <Term> termSet, out double[] RegressionCoefficents)
 {
     return(EvaluteConsiquent(System, termSet.ToList(), out RegressionCoefficents));
 }