示例#1
0
        private string ErrorInfoSA(IFuzzySystem FS)
        {
            SAFuzzySystem IFS = FS as SAFuzzySystem;

            if (IFS.RulesDatabaseSet.Count < 1)
            {
                return("Точность нечеткой системы недоступна");
            }


            approxLearnResult.Add(IFS.approxLearnSamples(IFS.RulesDatabaseSet[0]));
            approxTestResult.Add(IFS.approxTestSamples(IFS.RulesDatabaseSet[0]));

            approxLearnResultMSE.Add(IFS.RMSEtoMSEforLearn(approxLearnResult[approxLearnResult.Count - 1]));
            approxTestResultMSE.Add(IFS.RMSEtoMSEforTest(approxTestResult[approxTestResult.Count - 1]));

            approxLearnResultMSEdiv2.Add(IFS.RMSEtoMSEdiv2forLearn(approxLearnResult[approxLearnResult.Count - 1]));
            approxTestResultMSEdiv2.Add(IFS.RMSEtoMSEdiv2forTest(approxTestResult[approxTestResult.Count - 1]));


            return("Точностью на обучающей выборке(RSME)  " + approxLearnResult [approxLearnResult.Count - 1].ToString() + " , Точность на тестовой выборке(RMSE)  " + approxTestResult[approxTestResult.Count - 1].ToString() + " " + Environment.NewLine +
                   "Точностью на обучающей выборке(MSE)  " + approxLearnResultMSE[approxLearnResultMSE.Count - 1].ToString() + " , Точность на тестовой выборке(MSE)  " + approxTestResultMSE[approxTestResultMSE.Count - 1].ToString() + " " + Environment.NewLine +
                   "Точностью на обучающей выборке(MSE/2)  " + approxLearnResultMSEdiv2[approxLearnResultMSEdiv2.Count - 1].ToString() + " , Точность на тестовой выборке(MSE/2)  " + approxTestResultMSEdiv2[approxTestResultMSEdiv2.Count - 1].ToString() + " " + Environment.NewLine);
        }
        protected override void make_Log(Log_line EventCall, SAFuzzySystem FS = null, string name_Alg = "", DateTime TimerValue = new DateTime(), TimeSpan TimerSpan = new TimeSpan())
        {
            switch (EventCall)
            {
            case Log_line.Start:
            {
                LOG += "(" + TimerValue.ToString() + ")" + " Начало построения системы" + Environment.NewLine;
                break;
            }

            case Log_line.StartGenerate:
            {
                LOG += "(" + TimerValue.ToString() + ")" + " Начата генерация системы" + Environment.NewLine;

                break;
            }

            case Log_line.StartOptimaze:
            {
                LOG += "(" + DateTime.Now.ToString() + ")" + " Начата оптимизация системы" + Environment.NewLine;
                break;
            }


            case Log_line.PreGenerate_log:
            {
                LOG += "(" + DateTime.Now.ToString() + ")" + " Генерация алгоритмом " + name_Alg.ToString() + Environment.NewLine;
                break;
            }

            case Log_line.PostGenerate_log:
            {
                double LearnResult = FS.approxLearnSamples();
                double TestResult  = FS.approxTestSamples();

                double LearnResultMSE = FS.RMSEtoMSEforLearn(LearnResult);
                double TestResultMSE  = FS.RMSEtoMSEforTest(TestResult);

                double LearnResultMSEdiv2 = FS.RMSEtoMSEdiv2forLearn(LearnResult);
                double TestResultMSEdiv2  = FS.RMSEtoMSEdiv2forTest(TestResult);


                LOG += "(" + DateTime.Now.ToString() + ")" + " Сгенерирована система сложностью " + FS.ValueComplexity().ToString() + Environment.NewLine +
                       "Точностью на обучающей выборке(RSME) " + LearnResult.ToString() + ", Точность на тестовой выборке(RMSE) " + TestResult.ToString() + Environment.NewLine +
                       "Точностью на обучающей выборке(MSE) " + LearnResultMSE.ToString() + ", Точность на тестовой выборке(MSE) " + TestResultMSE.ToString() + Environment.NewLine +
                       "Точностью на обучающей выборке(MSE/2) " + LearnResultMSEdiv2.ToString() + ", Точность на тестовой выборке(MSE/2) " + TestResultMSEdiv2.ToString() + Environment.NewLine;

                LOG += "Использован " + name_Alg.ToString() + Environment.NewLine;
                break;
            }

            case Log_line.PreOptimaze_log:
            {
                LOG += "(" + DateTime.Now.ToString() + ")" + " Оптимизация алгоритмом " + name_Alg.ToString() + Environment.NewLine;

                break;
            }

            case Log_line.PostOptimaze_log:
            {
                double LearnResult = FS.approxLearnSamples();
                double TestResult  = FS.approxTestSamples();


                double LearnResultMSE = FS.RMSEtoMSEforLearn(LearnResult);
                double TestResultMSE  = FS.RMSEtoMSEforTest(TestResult);

                double LearnResultMSEdiv2 = FS.RMSEtoMSEdiv2forLearn(LearnResult);
                double TestResultMSEdiv2  = FS.RMSEtoMSEdiv2forTest(TestResult);

                LOG += "(" + DateTime.Now.ToString() + ")" + " оптимизированная система сложностью " + FS.ValueComplexity().ToString() + Environment.NewLine +
                       "Точностью на обучающей выборке(RMSE) " + LearnResult.ToString() + ", Точность на тестовой выборке(RMSE) " + TestResult.ToString() + Environment.NewLine +
                       "Точностью на обучающей выборке(MSE) " + LearnResultMSE.ToString() + ", Точность на тестовой выборке(MSE) " + TestResultMSE.ToString() + Environment.NewLine +
                       "Точностью на обучающей выборке(MSE/2) " + LearnResultMSEdiv2.ToString() + ", Точность на тестовой выборке(MSE/2) " + TestResultMSEdiv2.ToString() + Environment.NewLine;


                LOG += "Использован " + name_Alg.ToString() + Environment.NewLine;

                break;
            }


            case Log_line.EndCircle:
            {
                LOG += "(" + DateTime.Now.ToString() + ")" + " Время построения системы" + TimerSpan.TotalSeconds.ToString() + Environment.NewLine; break;
            }

            case Log_line.End:
            {
                LOG += "(" + DateTime.Now.ToString() + ")" + " Время построения всех систем" + TimerSpan.TotalSeconds.ToString() + Environment.NewLine; break;
            }

            default: { LOG += "Не верный вызов" + Environment.NewLine; break; }
            }
        }
示例#3
0
        public override SAFuzzySystem TuneUpFuzzySystem(SAFuzzySystem Approximate, ILearnAlgorithmConf conf) // + override
        {
            result = Approximate;


            List <KnowlegeBaseSARules> Archive = new List <KnowlegeBaseSARules>();
            List <double> ErrorsArchive        = new List <double>();

            var config = (DynamicTuneConf)conf;

            maxError  = config.MaxError;
            RuleCount = config.RulesCount;
            TryCount  = config.TryCount;
            double error        = result.RMSEtoMSEdiv2forLearn(result.approxLearnSamples(result.RulesDatabaseSet[0]));
            var    kbToOptimize = new KnowlegeBaseSARules(result.RulesDatabaseSet[0]);
            var    kbBest       = new KnowlegeBaseSARules(kbToOptimize);
            double errorBefore  = Double.MaxValue;

            result.UnlaidProtectionFix(kbToOptimize);

            List <input_space> variable_spaces = new List <input_space>();

            for (int i = 0; i < result.LearnSamplesSet.InputAttributes.Count; i++)
            {
                List <Term> terms_of_variable = new List <Term>();
                terms_of_variable = kbToOptimize.TermsSet.Where(term => term.NumVar == i).ToList();
                variable_spaces.Add(new input_space(terms_of_variable, i));
            }

            int indexRegion = -1,
                indexVar    = -1,
                number_of_input_variables = variable_spaces.Count;

            int tryCount = 0;



            while (error > maxError)
            {
                if (Double.IsInfinity(error))
                {
                    throw new Exception("Something went wrong, error is Infinity, region: " + indexRegion);
                }
                if (Double.IsNaN(error))
                {
                    throw new Exception("Something went wrong, error is NaN, region: " + indexRegion);
                }

                region_side[][] sides = new region_side[number_of_input_variables][];
                for (int i = 0; i < number_of_input_variables; i++)
                {
                    sides[i] = variable_spaces[i].get_region_sides();
                }
                var cartresult = CartesianProduct.Get(sides);

                List <region2> regions = new List <region2>();

                foreach (var x in cartresult)
                {
                    regions.Add(new region2(x.ToList(), result, variable_spaces));
                }

                List <double> region_errors = regions.Select(x => x.region_error()).ToList();
                indexRegion = region_errors.IndexOf(region_errors.Max());

                for (int i = 0; i < region_errors.Count; i++)
                {
                    if (Double.IsNaN(region_errors[i]) || Double.IsInfinity(region_errors[i]) ||
                        Double.IsNegativeInfinity(region_errors[i]) || Double.IsPositiveInfinity(region_errors[i]))
                    {
                        region_errors[i] = 0;
                    }
                }

                List <double> variable_errors = regions[indexRegion].variable_errors();
                bool          check1          = false;
                for (int i = 1; i < variable_errors.Count; i++)
                {
                    if (variable_errors[i - 1] != variable_errors[i])
                    {
                        check1 = true;
                        break;
                    }
                }
                if (!check1)
                {
                    indexVar = StaticRandom.Next(variable_errors.Count - 1);
                }
                else
                {
                    indexVar = variable_errors.IndexOf(variable_errors.Max());
                }

                Term new_term = regions[indexRegion].new_term(indexVar);
                result.RulesDatabaseSet[0] = kbToOptimize;
                kbToOptimize.TermsSet.Add(new_term);

                // Rules (CHECK REFERENCE TYPES)
                int @var = indexVar;

                var rulesLeft = kbToOptimize.RulesDatabase.Where(
                    rule => rule.ListTermsInRule.Contains(regions[indexRegion].sides[indexVar].left)).ToList();
                var rulesRight = kbToOptimize.RulesDatabase.Where(
                    rule => rule.ListTermsInRule.Contains(regions[indexRegion].sides[indexVar].right)).ToList();
                for (int j = 0; j < rulesLeft.Count; j++)
                {
                    int[] order = new int[rulesLeft[j].ListTermsInRule.Count];
                    for (int k = 0; k < rulesLeft[j].ListTermsInRule.Count; k++)
                    {
                        Term temp_term = rulesLeft[j].ListTermsInRule[k];
                        if (temp_term == regions[indexRegion].sides[indexVar].left)
                        {
                            temp_term = new_term;
                        }
                        order[k] = kbToOptimize.TermsSet.FindIndex(x => x == temp_term);
                    }
///!!!!
                    double temp_approx_Values = kbToOptimize.RulesDatabase[j].IndependentConstantConsequent;

                    /*        double[] temp_approx_RegressionConstantConsequent =
                     *          kbToOptimize.RulesDatabase[j].RegressionConstantConsequent.Clone() as double[];
                     */


                    SARule temp_rule = new SARule(
                        kbToOptimize.TermsSet, order, temp_approx_Values);

                    // double[] dC = null;
//!!!
                    temp_rule.IndependentConstantConsequent = KNNConsequent.NearestApprox(result, temp_rule.ListTermsInRule.ToList());



                    kbToOptimize.RulesDatabase.Add(temp_rule);


//!!!
                    rulesLeft[j].IndependentConstantConsequent = KNNConsequent.NearestApprox(result, rulesLeft[j].ListTermsInRule.ToList());
                    //           rulesLeft[j].RegressionConstantConsequent = (double[])dC.Clone();
                }

                foreach (var rule in rulesRight)
                {
//!!!

                    rule.IndependentConstantConsequent = KNNConsequent.NearestApprox(
                        result, rule.ListTermsInRule.ToList());
                    //               rule.RegressionConstantConsequent = dC;
                }

                variable_spaces[indexVar].terms.Add(new_term);
                variable_spaces[indexVar].terms.Sort(new CompararerByPick());

                // Re-evaluate the system's error
                error = result.RMSEtoMSEdiv2forLearn(result.ErrorLearnSamples(kbToOptimize));

                if ((kbToOptimize.RulesDatabase.Count > config.RulesCount))
                {
                    break;
                }

#if Console
                Console.WriteLine(error + " " + kbToOptimize.TermsSet.Count + " terms\n");
                for (int i = 0; i < variable_spaces.Count; i++)
                {
                    Console.WriteLine(variable_spaces[i].terms.Count + " термов по " + i + "му параметру\n");
                }
#endif
                result.RulesDatabaseSet[0] = kbToOptimize;
                // Get the best knowledge base on the 1st place
                if (error < errorBefore)
                {
                    kbBest      = new KnowlegeBaseSARules(kbToOptimize);
                    errorBefore = error;
                    tryCount    = 0;
                }
                else
                {
                    tryCount++;
                }
                if (tryCount > TryCount)
                {
                    break;
                }
            }


            result.RulesDatabaseSet[0] = kbBest;
            RuleCount = kbBest.RulesDatabase.Count;
            TryCount  = tryCount;

            return(result);
        }