public override SAFuzzySystem TuneUpFuzzySystem(SAFuzzySystem Approx, ILearnAlgorithmConf conf) { result = Approx; List <int[]> groups = new List <int[]>(); Init(conf); SetPopulation(); Population = ListSingletonApproximateTool.SortRules(Population, result); 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; /*StringBuilder sb = new StringBuilder(); * sb.AppendLine("sep=.");*/ for (int i = 1; i <= MaxIter; i++) { Console.Clear(); Console.WriteLine((double)i * 100 / MaxIter + "%"); 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; } //sb.AppendLine((result.RMSEtoMSEforLearn(result.approxLearnSamples(Population[0]))).ToString() + "." + (result.RMSEtoMSEforTest(result.approxTestSamples(Population[0]))).ToString()); } /*FileStream file1 = new FileStream("F:\\Table.scv", FileMode.Create); * StreamWriter writer = new StreamWriter(file1); * writer.Write(sb); * writer.Close(); * file1.Close();*/ Console.WriteLine(ToString(true)); Console.WriteLine("Итер - " + BestIter + " MSET - " + BestMSETest + " MSEL - " + BestMSELearn); result.RulesDatabaseSet[0] = Population[0]; return(result); }
public override SAFuzzySystem TuneUpFuzzySystem(SAFuzzySystem Approx, ILearnAlgorithmConf conf) { result = Approx; Init(conf); HeadLeader = new KnowlegeBaseSARules(result.RulesDatabaseSet[0]); VelocityVector = new KnowlegeBaseSARules(result.RulesDatabaseSet[0]); VelocityVectorLL = new KnowlegeBaseSARules(result.RulesDatabaseSet[0]); VelocityVectorHL = new KnowlegeBaseSARules(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 <KnowlegeBaseSARules, KnowlegeBaseSARules>(); foreach (var Particle in Population) { ParticlesBest.Add(Particle, Universal); } LocalLeaders = new KnowlegeBaseSARules[numberOfLocalLeaders]; Console.WriteLine(LocalLeaders.Length); ExplorerParticles = new KnowlegeBaseSARules[numberOfAllParts - numberOfAimlessParts - numberOfLocalLeaders - 1]; Console.WriteLine(ExplorerParticles.Length); AimlessParticles = new KnowlegeBaseSARules[numberOfAimlessParts]; Console.WriteLine(AimlessParticles.Length); while (iter < MaxIter) { Population = ListSingletonApproximateTool.SortRules(Population, result); SetRoles(); ChangeExplorersPositions(); ChangeAimlessPositions(); DiscardRoles(); iter++; Console.WriteLine("Iteration: " + iter.ToString()); Console.WriteLine(result.RMSEtoMSEforLearn(result.approxLearnSamples(Population[0]))); Console.WriteLine(result.RMSEtoMSEforTest(result.approxTestSamples(Population[0]))); Console.WriteLine(result.approxLearnSamples(Population[numberOfLocalLeaders + 1])); } result.RulesDatabaseSet[0] = Population[0]; return(result); }
//основные вычисления public override SAFuzzySystem TuneUpFuzzySystem(SAFuzzySystem Approx, ILearnAlgorithmConf conf) { result = Approx; Init(conf); SetPopulation(); KnowlegeBaseSARules BEST = new KnowlegeBaseSARules(result.RulesDatabaseSet[0]); double bestError = result.ErrorLearnSamples(BEST); //отчистка консоли #if debug Console.Clear(); #endif //запуск итераций for (int it = 0; it < iter; it++) { #if debug //вывод номера итерации Console.Write("Итерация __№__ = "); Console.WriteLine(it); #endif //расчитыавем значение фитнес-функции Population = ListSingletonApproximateTool.SortRules(Population, result); double[] K = new double[Population.Length]; for (int i = 0; i < Population.Length; i++) { K[i] = result.ErrorLearnSamples(Population[i]); #if debug Console.Write("Значние K[i1] = "); Console.WriteLine(K[i]); #endif if (double.IsNaN(K[i]) || double.IsInfinity(K[i])) { result.UnlaidProtectionFix(Population[i]); K[i] = result.ErrorLearnSamples(Population[i]); #if debug Console.Write("Значние K[i2] = "); Console.WriteLine(K[i]); #endif } } Kworst = K.Max(); if (double.IsNaN(Kworst) || double.IsInfinity(Kworst)) { int iworst = K.ToList().IndexOf(Kworst); #if debug Console.Write("Значние iworst = "); Console.WriteLine(iworst); #endif } #if debug //вывод Kworst Console.Write("Значние KWorst = "); Console.WriteLine(Kworst); #endif Kbest = K.Min(); #if debug //вывод Kbest Console.Write("Значние Kbest = "); Console.WriteLine(Kbest); #endif int ibest = K.ToList().IndexOf(Kbest); #if debug //вывод ibest Console.Write("Значние ibest = "); Console.WriteLine(ibest); #endif //перебрать значения фитнес функции //расчитыавем значение D double dit; dit = it; double diter; diter = iter; double D = (dmax * (rand.NextDouble() * 2 - 1) * (dit / diter)); //расчитываем значение rand1 для D double rand1; rand1 = D / (dmax * (it) / iter); #if debug //выводим значение rand1 для D Console.Write("Значение Drand = "); Console.WriteLine(rand1); //выводим значение D Console.Write("Значение __D__ = "); Console.WriteLine(D); #endif //расчитыавем значение Xfood double divide = K.Select(x => 1 / x).ToList().Sum(); var Xfood = new KnowlegeBaseSARules(Population[0]); for (int t = 0; t < Xfood.TermsSet.Count; t++) { for (int p = 0; p < Xfood.TermsSet[t].CountParams; p++) { Xfood.TermsSet[t].Parametrs[p] = 0; for (int i = 0; i < Population.Length; i++) { Xfood.TermsSet[t].Parametrs[p] += Population[i].TermsSet[t].Parametrs[p] / K[i]; #if debug //выводим значение Xfood Console.Write("Значение Xfood = "); Console.WriteLine(Xfood.TermsSet[t].Parametrs[p]); #endif } Xfood.TermsSet[t].Parametrs[p] /= divide; } } #if debug //вывод divide Console.Write("Значние divide = "); Console.WriteLine(divide); #endif //расчитываем значение Kfood double Kfood = result.ErrorLearnSamples(Xfood); if (double.IsNaN(Kfood) || double.IsInfinity(Kfood)) { result.UnlaidProtectionFix(Xfood); Kfood = result.ErrorLearnSamples(Xfood); } #if debug //выводим значение Kfood Console.Write("Значение Kfood = "); Console.WriteLine(Kfood); #endif //расчитываем значение Cfood double Cfood = 2 * (1 - (dit / diter)); #if debug //выводим значение Cfood Console.Write("Значение Cfood = "); Console.WriteLine(Cfood); #endif //расчитываем значение Bfood KnowlegeBaseSARules[] Bfood = new KnowlegeBaseSARules[Population.Length]; for (int i = 0; i < Population.Length; i++) { Bfood[i] = new KnowlegeBaseSARules(Population[i]); double KRoofifood = CalcKroof(K[i], Kfood); KnowlegeBaseSARules Xroofifood = new KnowlegeBaseSARules(CalcXroof(Population[i], Xfood)); for (int t = 0; t < Bfood[i].TermsSet.Count; t++) { for (int p = 0; p < Bfood[i].TermsSet[t].CountParams; p++) { Bfood[i].TermsSet[t].Parametrs[p] = Cfood * KRoofifood * Xroofifood.TermsSet[t].Parametrs[p]; #if debug //выводим значение Bfood Console.Write("Значение Bfood = "); Console.WriteLine(Bfood[i].TermsSet[t].Parametrs[p]); #endif } } } //расчитываем значение Bbest KnowlegeBaseSARules[] Bbest = new KnowlegeBaseSARules[Population.Length]; for (int i = 0; i < Population.Length; i++) { Bbest[i] = new KnowlegeBaseSARules(Population[i]); double KRoofifood = CalcKroof(K[i], K[ibest]); KnowlegeBaseSARules Xroofifood = new KnowlegeBaseSARules(CalcXroof(Population[i], Population[ibest])); for (int t = 0; t < Bbest[i].TermsSet.Count; t++) { for (int p = 0; p < Bbest[i].TermsSet[t].CountParams; p++) { Bbest[i].TermsSet[t].Parametrs[p] = KRoofifood * Xroofifood.TermsSet[t].Parametrs[p]; #if debug //выводим значение Bbest Console.Write("Значение Bbest = "); Console.WriteLine(Bbest[i].TermsSet[t].Parametrs[p]); #endif } } } //расчитываем значение B KnowlegeBaseSARules[] B = new KnowlegeBaseSARules[Population.Length]; for (int i = 0; i < Population.Length; i++) { B[i] = new KnowlegeBaseSARules(Population[i]); for (int t = 0; t < B[i].TermsSet.Count; t++) { for (int p = 0; p < B[i].TermsSet[t].CountParams; p++) { B[i].TermsSet[t].Parametrs[p] = Bfood[i].TermsSet[t].Parametrs[p] + Bbest[i].TermsSet[t].Parametrs[p]; #if debug //выводим значение B Console.Write("Значение __B__ = "); Console.WriteLine(B[i].TermsSet[t].Parametrs[p]); #endif } } } //расчитываем значение F KnowlegeBaseSARules[] F = new KnowlegeBaseSARules[Population.Length]; for (int i = 0; i < Population.Length; i++) { if (i == 0) { F[i] = new KnowlegeBaseSARules(Population[i]); for (int t = 0; t < F[i].TermsSet.Count; t++) { for (int p = 0; p < F[i].TermsSet[t].CountParams; p++) { F[i].TermsSet[t].Parametrs[p] = Vf * B[i].TermsSet[t].Parametrs[p]; #if debug //выводим значение F Console.Write("Значение __F__ = "); Console.WriteLine(F[i].TermsSet[t].Parametrs[p]); #endif } } } else { F[i] = new KnowlegeBaseSARules(Population[i]); for (int t = 0; t < F[i].TermsSet.Count; t++) { for (int p = 0; p < F[i].TermsSet[t].CountParams; p++) { F[i].TermsSet[t].Parametrs[p] = Vf * B[i].TermsSet[t].Parametrs[p] + wf * F[i - 1].TermsSet[t].Parametrs[p]; #if debug //выводим значение F Console.Write("Значение __F__ = "); Console.WriteLine(F[i].TermsSet[t].Parametrs[p]); #endif } } } } List <int> [] neihbors = new List <int> [Population.Length]; //расчитываем значение alocal KnowlegeBaseSARules[] alocal = new KnowlegeBaseSARules[Population.Length]; for (int i = 0; i < Population.Length; i++) { alocal[i] = new KnowlegeBaseSARules(Population[i]); neihbors[i] = countneihbors(Population[i]); /* #if debug * //вывод значений количества соседей * for (int g = 0; g < Population.Length; g++) * { * Console.Write("Знаение countneihbors = "); * Console.WriteLine(countneihbors(Population[g])); * } #endif */ for (int t = 0; t < alocal[i].TermsSet.Count; t++) { for (int p = 0; p < alocal[i].TermsSet[t].CountParams; p++) { alocal[i].TermsSet[t].Parametrs[p] = 0; for (int j = 0; j < neihbors[i].Count; j++) { double KRoofij = CalcKroof(K[i], K[neihbors[i][j]]); KnowlegeBaseSARules XRoofij = new KnowlegeBaseSARules(CalcXroof(Population[i], Population[neihbors[i][j]])); alocal[i].TermsSet[t].Parametrs[p] += KRoofij * XRoofij.TermsSet[t].Parametrs[p]; #if debug //выводим значение alocal Console.Write("Знаение alocal = "); Console.WriteLine(alocal[i].TermsSet[t].Parametrs[p]); #endif } } } } //расчитываем значение Cbest double Cbest = 2 * (rand.NextDouble() - (dit / diter)); #if debug //выводим значение Cbest Console.Write("Значение Сbest = "); Console.WriteLine(Cbest); #endif //расчитываем значение rand для Cbest double rand2; rand2 = it / iter - Cbest / 2; #if debug //выводим значение rand2 для Cbest Console.Write("Значение Crand = "); Console.WriteLine(rand2); #endif //расчитываем значение atarget KnowlegeBaseSARules[] atarget = new KnowlegeBaseSARules[Population.Length]; for (int i = 0; i < Population.Length; i++) { atarget[i] = new KnowlegeBaseSARules(Population[i]); double KRoofibest = CalcKroof(K[i], K[ibest]); KnowlegeBaseSARules XRoofibest = new KnowlegeBaseSARules(CalcXroof(Population[i], Population[ibest])); for (int t = 0; t < alocal[i].TermsSet.Count; t++) { for (int p = 0; p < atarget[i].TermsSet[t].CountParams; p++) { atarget[i].TermsSet[t].Parametrs[p] = Cbest * KRoofibest * XRoofibest.TermsSet[t].Parametrs[p]; #if debug //выводим значение atarget Console.Write("Знание atarget = "); Console.WriteLine(atarget[i].TermsSet[t].Parametrs[p]); #endif } } } //расчитываем значение a KnowlegeBaseSARules[] a = new KnowlegeBaseSARules[Population.Length]; for (int i = 0; i < Population.Length; i++) { a[i] = new KnowlegeBaseSARules(Population[i]); for (int t = 0; t < a[i].TermsSet.Count; t++) { for (int p = 0; p < a[i].TermsSet[t].CountParams; p++) { a[i].TermsSet[t].Parametrs[p] = atarget[i].TermsSet[t].Parametrs[p] + alocal[i].TermsSet[t].Parametrs[p]; #if debug //выводим значение a Console.Write("Значение __a__ = "); Console.WriteLine(a[i].TermsSet[t].Parametrs[p]); #endif } } } //расчитываем значение N KnowlegeBaseSARules[] N = new KnowlegeBaseSARules[Population.Length]; for (int i = 0; i < Population.Length; i++) { if (i == 0) { N[i] = new KnowlegeBaseSARules(Population[i]); for (int t = 0; t < N[i].TermsSet.Count; t++) { for (int p = 0; p < F[i].TermsSet[t].CountParams; p++) { N[i].TermsSet[t].Parametrs[p] = Vf * a[i].TermsSet[t].Parametrs[p]; #if debug //выводим значение N Console.Write("Значение __N__ = "); Console.WriteLine(N[i].TermsSet[t].Parametrs[p]); #endif } } } else { N[i] = new KnowlegeBaseSARules(Population[i]); for (int t = 0; t < F[i].TermsSet.Count; t++) { for (int p = 0; p < N[i].TermsSet[t].CountParams; p++) { N[i].TermsSet[t].Parametrs[p] = nmax * a[i].TermsSet[t].Parametrs[p] + wn * N[i - 1].TermsSet[t].Parametrs[p]; #if debug //выводим значение N Console.Write("Значение __N__ = "); Console.WriteLine(N[i].TermsSet[t].Parametrs[p]); #endif } } } } //расчитываем значение dX KnowlegeBaseSARules[] dX = new KnowlegeBaseSARules[Population.Length]; for (int i = 0; i < Population.Length; i++) { dX[i] = new KnowlegeBaseSARules(Population[i]); for (int t = 0; t < a[i].TermsSet.Count; t++) { for (int p = 0; p < a[i].TermsSet[t].CountParams; p++) { dX[i].TermsSet[t].Parametrs[p] = F[i].TermsSet[t].Parametrs[p] + N[i].TermsSet[t].Parametrs[p] + D; #if debug //выводим значение dX Console.Write("Значение _dX__ = "); Console.WriteLine(dX[i].TermsSet[t].Parametrs[p]); #endif } } } //выводим значение BEST // Console.Write("Значение BEST_ = "); // Console.WriteLine(BEST); //расчитываем значение X(t+dt) for (int i = 0; i < Population.Length; i++) { Population[i] = new KnowlegeBaseSARules(Population[i]); for (int t = 0; t < Population[i].TermsSet.Count; t++) { for (int p = 0; p < F[i].TermsSet[t].CountParams; p++) { Population[i].TermsSet[t].Parametrs[p] = Population[i].TermsSet[t].Parametrs[p] + calcdeltat(ct) * dX[i].TermsSet[t].Parametrs[p]; #if debug //выводим значение Xnew Console.Write("Знание X(t+dt) = "); Console.WriteLine(Population[i].TermsSet[t].Parametrs[p]); #endif } } } for (int i = 0; i < Population.Length; i++) { double temp = result.ErrorLearnSamples(Population[i]); if (double.IsNaN(temp) || double.IsInfinity(temp)) { result.UnlaidProtectionFix(Xfood); temp = result.ErrorLearnSamples(Population[i]); } if (temp < bestError) { BEST = new KnowlegeBaseSARules(Population[i]); bestError = temp; } } double y = it; if (y % 50 == 0 & y != 0) { Console.WriteLine(it); Console.WriteLine(bestError); } #if debug // выводим значение лучшей ошибки Kbest Console.Write("Значние BestError = "); Console.WriteLine(bestError); Console.WriteLine("."); #endif } result.RulesDatabaseSet[0] = BEST; return(result); }