public virtual void preIterate(PCFuzzySystem Classify, ILearnAlgorithmConf conf) { result = Classify; Init(conf); HeadLeader = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]); VelocityVector = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]); VelocityVectorLL = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]); VelocityVectorHL = new KnowlegeBasePCRules(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 <KnowlegeBasePCRules, KnowlegeBasePCRules>(); foreach (var Particle in Population) { ParticlesBest.Add(Particle, Universal); } LocalLeaders = new KnowlegeBasePCRules[numberOfLocalLeaders]; ExplorerParticles = new KnowlegeBasePCRules[numberOfAllParts - numberOfAimlessParts - numberOfLocalLeaders - 1]; AimlessParticles = new KnowlegeBasePCRules[numberOfAimlessParts]; iter = 0; Population = ListPittsburgClassifierTool.SortRules(Population, result); }
public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Classify, ILearnAlgorithmConf conf) { preIterate(Classify, conf); //Оптимизируем параметры iter = 1; while (iter <= MaxIter) { if (iter % changeParts == 0) { swapParticles(); } oneIterate(); } //Выводим точность классификации для лучшей частицы из каждой популяции for (int j = 0; j < Populations.Count; j++) { Populations[j] = ListPittsburgClassifierTool.SortRules(Populations[j], result); Console.WriteLine("Популяция №" + j + ":"); Console.WriteLine("Обуч: " + Math.Round(result.ClassifyLearnSamples(Populations[j][0]), 2)); Console.WriteLine("Тест: " + Math.Round(result.ClassifyTestSamples(Populations[j][0]), 2)); } //Находим самое лучшее решение из всех популяций List <KnowlegeBasePCRules> BestPopulation = new List <KnowlegeBasePCRules>(); for (int i = 0; i < numberOfPopulations; i++) { BestPopulation.Add(Populations[i][0]); } BestPopulation = ListPittsburgClassifierTool.SortRules(BestPopulation, result); result.RulesDatabaseSet[0] = BestPopulation[0]; //Возвращаем результат return(result); }
public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Classifier, ILearnAlgorithmConf conf) { result = Classifier; groups = new List <int[]>(); Init(conf); SetPopulation(); Population = SortRules(Population); NS = new int[m]; for (int i = 0; i < m; i++) { NS[i] = (N - 1) / m; } cur_iter = 0; while (cur_iter < iter) { groups = GroupStream(); if (p_one > rand.NextDouble()) { ChooseOneCluster(); } else { ChooseTwoClusters(); } Population = ListPittsburgClassifierTool.SortRules(Population, result); Console.WriteLine(cur_iter + " - Итерация"); Console.WriteLine("Обуч. выборка = " + result.ErrorLearnSamples(Population[0])); Console.WriteLine("Тест. выборка = " + result.ErrorTestSamples(Population[0])); cur_iter++; } Population = ListPittsburgClassifierTool.SortRules(Population, result); result.RulesDatabaseSet[0] = Population[0]; return(result); }
public virtual void oneIterate() { for (int p_i = 0; p_i < Populations.Count; p_i++) { Populations[p_i] = ListPittsburgClassifierTool.SortRules(Populations[p_i], result); SetRoles(Populations[p_i]); ChangeExplorersPositions(); ChangeAimlessPositions(); Populations[p_i] = DiscardRoles(Populations[p_i]); } iter++; }
public virtual void oneIterate() { Task[] AlgTasks = new Task[numberOfPopulations]; Parallel.For(0, Populations.Count, i => { int p_i = i; Populations[p_i] = ListPittsburgClassifierTool.SortRules(Populations[p_i], result); SetRoles(Populations[p_i], p_i); ChangeExplorersPositions(p_i); ChangeAimlessPositions(p_i); Populations[p_i] = DiscardRoles(Populations[p_i], p_i); }); iter++; }
public virtual void oneIterate() { SetRoles(); ChangeExplorersPositions(); ChangeAimlessPositions(); DiscardRoles(); Population = ListPittsburgClassifierTool.SortRules(Population, result); iter++; if (iter == MaxIter) { Console.WriteLine("Обуч: " + Math.Round(result.ClassifyLearnSamples(Population[0]), 2)); Console.WriteLine("Тест: " + Math.Round(result.ClassifyTestSamples(Population[0]), 2)); //Console.WriteLine(); } }
//основные вычисления public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Classify, ILearnAlgorithmConf conf) { rand = new Random(); result = Classify; Init(conf); SetPopulation(); KnowlegeBasePCRules BEST = new KnowlegeBasePCRules(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 = ListPittsburgClassifierTool.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 KnowlegeBasePCRules(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 KnowlegeBasePCRules[] Bfood = new KnowlegeBasePCRules[Population.Length]; for (int i = 0; i < Population.Length; i++) { Bfood[i] = new KnowlegeBasePCRules(Population[i]); double KRoofifood = CalcKroof(K[i], Kfood); KnowlegeBasePCRules Xroofifood = new KnowlegeBasePCRules(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 KnowlegeBasePCRules[] Bbest = new KnowlegeBasePCRules[Population.Length]; for (int i = 0; i < Population.Length; i++) { Bbest[i] = new KnowlegeBasePCRules(Population[i]); double KRoofifood = CalcKroof(K[i], K[ibest]); KnowlegeBasePCRules Xroofifood = new KnowlegeBasePCRules(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 KnowlegeBasePCRules[] B = new KnowlegeBasePCRules[Population.Length]; for (int i = 0; i < Population.Length; i++) { B[i] = new KnowlegeBasePCRules(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 KnowlegeBasePCRules[] F = new KnowlegeBasePCRules[Population.Length]; for (int i = 0; i < Population.Length; i++) { if (i == 0) { F[i] = new KnowlegeBasePCRules(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 KnowlegeBasePCRules(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 KnowlegeBasePCRules[] alocal = new KnowlegeBasePCRules[Population.Length]; for (int i = 0; i < Population.Length; i++) { alocal[i] = new KnowlegeBasePCRules(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]]); KnowlegeBasePCRules XRoofij = new KnowlegeBasePCRules(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 KnowlegeBasePCRules[] atarget = new KnowlegeBasePCRules[Population.Length]; for (int i = 0; i < Population.Length; i++) { atarget[i] = new KnowlegeBasePCRules(Population[i]); double KRoofibest = CalcKroof(K[i], K[ibest]); KnowlegeBasePCRules XRoofibest = new KnowlegeBasePCRules(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 KnowlegeBasePCRules[] a = new KnowlegeBasePCRules[Population.Length]; for (int i = 0; i < Population.Length; i++) { a[i] = new KnowlegeBasePCRules(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 KnowlegeBasePCRules[] N = new KnowlegeBasePCRules[Population.Length]; for (int i = 0; i < Population.Length; i++) { if (i == 0) { N[i] = new KnowlegeBasePCRules(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 KnowlegeBasePCRules(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 KnowlegeBasePCRules[] dX = new KnowlegeBasePCRules[Population.Length]; for (int i = 0; i < Population.Length; i++) { dX[i] = new KnowlegeBasePCRules(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 KnowlegeBasePCRules(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 KnowlegeBasePCRules(Population[i]); bestError = temp; } } double y = it; if (y % 10 == 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); }
public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Classifier, ILearnAlgorithmConf conf) { result = Classifier; //Узнаем название папки с данными string path_name = "../../OLD/Data/Keel/Classifier/KEEL-10/"; string folder_name = ""; foreach (var letter in result.LearnSamplesSet.FileName) { if (letter != '-') { folder_name += letter; } else { break; } } groups = new List <int[]>(); Init(conf); //Создаем новые обучающую и тестовую выбоки и удаляем из них некоторое количество случайных элементов List <PCFuzzySystem> results = new List <PCFuzzySystem>(); for (int i = 0; i < numberOfPopulations; i++) { SampleSet new_learn = new SampleSet(path_name + folder_name + "/" + result.LearnSamplesSet.FileName); SampleSet new_test = new SampleSet(path_name + folder_name + "/" + result.TestSamplesSet.FileName); results.Add(new PCFuzzySystem(new_learn, new_test)); int ground = (int)Math.Round(results[i].LearnSamplesSet.DataRows.Count * 0.25); for (int j = 0; j < ground; j++) { results[i].LearnSamplesSet.DataRows.RemoveAt(rand.Next(0, results[i].LearnSamplesSet.DataRows.Count)); } } Populations = new List <List <KnowlegeBasePCRules> >(); for (int i = 0; i < numberOfPopulations; i++) { Populations.Add(SetPopulation(new List <KnowlegeBasePCRules>())); Populations[i] = ListPittsburgClassifierTool.SortRules(Populations[i], result); } NS = new int[m]; for (int i = 0; i < m; i++) { NS[i] = (N - 1) / m; } cur_iter = 0; while (cur_iter < iter) { for (int p_i = 0; p_i < Populations.Count; p_i++) { groups = GroupStream(Populations[p_i]); if (p_one > rand.NextDouble()) { ChooseOneCluster(Populations[p_i]); } else { ChooseTwoClusters(Populations[p_i]); } Populations[p_i] = ListPittsburgClassifierTool.SortRules(Populations[p_i], results[p_i]); //Console.WriteLine(cur_iter + " - Итерация"); //Console.WriteLine("Обуч. выборка = " + result.ErrorLearnSamples(Populations[p_i][0])); //Console.WriteLine("Тест. выборка = " + result.ErrorTestSamples(Populations[p_i][0])); } cur_iter++; } //Выводим точность классификации для лучшей частицы из каждой популяции for (int j = 0; j < Populations.Count; j++) { Populations[j] = ListPittsburgClassifierTool.SortRules(Populations[j], results[j]); Console.WriteLine("Популяция №" + j + ":"); Console.WriteLine("Обуч: " + Math.Round(result.ClassifyLearnSamples(Populations[j][0]), 2)); Console.WriteLine("Тест: " + Math.Round(result.ClassifyTestSamples(Populations[j][0]), 2)); } //Допобавляем в базу правил лучшие решения if (result.RulesDatabaseSet.Count == 1) { result.RulesDatabaseSet.Clear(); } for (int i = 0; i < Populations.Count; i++) { result.RulesDatabaseSet.Add(Populations[i][0]); } //Возвращаем результат return(result); }
public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Classify, ILearnAlgorithmConf conf) { result = Classify; //Узнаем название папки с данными string path_name = "../../OLD/Data/Keel/Classifier/KEEL-10/"; string folder_name = ""; foreach (var letter in result.LearnSamplesSet.FileName) { if (letter != '-') { folder_name += letter; } else { break; } } //Инициализируем параметры Init(conf); //Создаем новые обучающую и тестовую выбоки и удаляем из них некоторое количество случайных элементов List <PCFuzzySystem> results = new List <PCFuzzySystem>(); for (int i = 0; i < numberOfPopulations; i++) { SampleSet new_learn = new SampleSet(path_name + folder_name + "/" + result.LearnSamplesSet.FileName); SampleSet new_test = new SampleSet(path_name + folder_name + "/" + result.TestSamplesSet.FileName); results.Add(new PCFuzzySystem(new_learn, new_test)); int ground = (int)Math.Round(results[i].LearnSamplesSet.DataRows.Count * 0.25); for (int j = 0; j < ground; j++) { results[i].LearnSamplesSet.DataRows.RemoveAt(rand.Next(0, results[i].LearnSamplesSet.DataRows.Count)); } } //Инициализируем и зануляем вектора алгоритма HeadLeader = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]); VelocityVector = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]); VelocityVectorLL = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]); VelocityVectorHL = new KnowlegeBasePCRules(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; } } //Создаем популяции и архив лучших положений каждой частицы Populations = new List <List <KnowlegeBasePCRules> >(); for (int i = 0; i < numberOfPopulations; i++) { Populations.Add(SetPopulation(new List <KnowlegeBasePCRules>())); } ParticlesBest = new Dictionary <KnowlegeBasePCRules, KnowlegeBasePCRules>(); foreach (var Population in Populations) { foreach (var Particle in Population) { ParticlesBest.Add(Particle, Universal); } } //Инициализируем роли частиц LocalLeaders = new KnowlegeBasePCRules[numberOfLocalLeaders]; ExplorerParticles = new KnowlegeBasePCRules[numberOfAllParts - numberOfAimlessParts - numberOfLocalLeaders - 1]; AimlessParticles = new KnowlegeBasePCRules[numberOfAimlessParts]; //Оптимизируем параметры iter = 1; while (iter < MaxIter) { for (int p_i = 0; p_i < Populations.Count; p_i++) { Populations[p_i] = ListPittsburgClassifierTool.SortRules(Populations[p_i], results[p_i]); SetRoles(Populations[p_i]); ChangeExplorersPositions(); ChangeAimlessPositions(); Populations[p_i] = DiscardRoles(Populations[p_i]); } iter++; } //Выводим точность классификации для лучшей частицы из каждой популяции for (int j = 0; j < Populations.Count; j++) { Populations[j] = ListPittsburgClassifierTool.SortRules(Populations[j], results[j]); Console.WriteLine("Популяция №" + j + ":"); Console.WriteLine("Обуч: " + Math.Round(result.ClassifyLearnSamples(Populations[j][0]), 2)); Console.WriteLine("Тест: " + Math.Round(result.ClassifyTestSamples(Populations[j][0]), 2)); } //Допобавляем в базу правил лучшие решения if (result.RulesDatabaseSet.Count == 1) { result.RulesDatabaseSet.Clear(); } for (int i = 0; i < Populations.Count; i++) { result.RulesDatabaseSet.Add(Populations[i][0]); } //Возвращаем результат return(result); }
public void oneIterate() { for (int p_i = 0; p_i < Populations.Count; p_i++) { //расчитыавем значение фитнес-функции Populations[p_i] = ListPittsburgClassifierTool.SortRules(Populations[p_i], results[p_i]); double[] K = new double[Populations[p_i].Count]; for (int i = 0; i < Populations[p_i].Count; i++) { K[i] = results[p_i].ErrorLearnSamples(Populations[p_i][i]); if (double.IsNaN(K[i]) || double.IsInfinity(K[i]) || K[i] == 100) { results[p_i].UnlaidProtectionFix(Populations[p_i][i]); K[i] = results[p_i].ErrorLearnSamples(Populations[p_i][i]); } } Kworst = K.Max(); if (double.IsNaN(Kworst) || double.IsInfinity(Kworst)) { int iworst = K.ToList().IndexOf(Kworst); } Kbest = K.Min(); int ibest = K.ToList().IndexOf(Kbest); //перебрать значения фитнес функции //расчитыавем значение 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); //расчитыавем значение Xfood double divide = K.Select(x => 1 / x).ToList().Sum(); var Xfood = new KnowlegeBasePCRules(Populations[p_i][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 < Populations[p_i].Count; i++) { Xfood.TermsSet[t].Parametrs[p] += Populations[p_i][i].TermsSet[t].Parametrs[p] / K[i]; } Xfood.TermsSet[t].Parametrs[p] /= divide; } } //расчитываем значение Kfood double Kfood = results[p_i].ErrorLearnSamples(Xfood); if (double.IsNaN(Kfood) || double.IsInfinity(Kfood) || Kfood == 100) { results[p_i].UnlaidProtectionFix(Xfood); Kfood = results[p_i].ErrorLearnSamples(Xfood); } //расчитываем значение Cfood double Cfood = 2 * (1 - (dit / diter)); //расчитываем значение Bfood KnowlegeBasePCRules[] Bfood = new KnowlegeBasePCRules[Populations[p_i].Count]; for (int i = 0; i < Populations[p_i].Count; i++) { Bfood[i] = new KnowlegeBasePCRules(Populations[p_i][i]); double KRoofifood = CalcKroof(K[i], Kfood); KnowlegeBasePCRules Xroofifood = new KnowlegeBasePCRules(CalcXroof(Populations[p_i][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]; } } } //расчитываем значение Bbest KnowlegeBasePCRules[] Bbest = new KnowlegeBasePCRules[Populations[p_i].Count]; for (int i = 0; i < Populations[p_i].Count; i++) { Bbest[i] = new KnowlegeBasePCRules(Populations[p_i][i]); double KRoofifood; if (i != ibest) { KRoofifood = CalcKroof(K[i], K[ibest]); } else { KRoofifood = 1.0; } KnowlegeBasePCRules Xroofifood = new KnowlegeBasePCRules(CalcXroof(Populations[p_i][i], Populations[p_i][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]; } } } //расчитываем значение B KnowlegeBasePCRules[] B = new KnowlegeBasePCRules[Populations[p_i].Count]; for (int i = 0; i < Populations[p_i].Count; i++) { B[i] = new KnowlegeBasePCRules(Populations[p_i][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]; } } } //расчитываем значение F KnowlegeBasePCRules[] F = new KnowlegeBasePCRules[Populations[p_i].Count]; for (int i = 0; i < Populations[p_i].Count; i++) { if (i == 0) { F[i] = new KnowlegeBasePCRules(Populations[p_i][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]; } } } else { F[i] = new KnowlegeBasePCRules(Populations[p_i][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]; } } } } List <int>[] neihbors = new List <int> [Populations[p_i].Count]; //расчитываем значение alocal KnowlegeBasePCRules[] alocal = new KnowlegeBasePCRules[Populations[p_i].Count]; for (int i = 0; i < Populations[p_i].Count; i++) { alocal[i] = new KnowlegeBasePCRules(Populations[p_i][i]); neihbors[i] = countneihbors(Populations[p_i][i], Populations[p_i]); 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]]); KnowlegeBasePCRules XRoofij = new KnowlegeBasePCRules(CalcXroof(Populations[p_i][i], Populations[p_i][neihbors[i][j]])); alocal[i].TermsSet[t].Parametrs[p] += KRoofij * XRoofij.TermsSet[t].Parametrs[p]; } } } } //расчитываем значение Cbest double Cbest = 2 * (rand.NextDouble() - (dit / diter)); //расчитываем значение rand для Cbest double rand2; rand2 = it / iter - Cbest / 2; //расчитываем значение atarget KnowlegeBasePCRules[] atarget = new KnowlegeBasePCRules[Populations[p_i].Count]; for (int i = 0; i < Populations[p_i].Count; i++) { atarget[i] = new KnowlegeBasePCRules(Populations[p_i][i]); double KRoofibest = CalcKroof(K[i], K[ibest]); KnowlegeBasePCRules XRoofibest = new KnowlegeBasePCRules(CalcXroof(Populations[p_i][i], Populations[p_i][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]; } } } //расчитываем значение a KnowlegeBasePCRules[] a = new KnowlegeBasePCRules[Populations[p_i].Count]; for (int i = 0; i < Populations[p_i].Count; i++) { a[i] = new KnowlegeBasePCRules(Populations[p_i][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]; } } } //расчитываем значение N KnowlegeBasePCRules[] N = new KnowlegeBasePCRules[Populations[p_i].Count]; for (int i = 0; i < Populations[p_i].Count; i++) { if (i == 0) { N[i] = new KnowlegeBasePCRules(Populations[p_i][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]; } } } else { N[i] = new KnowlegeBasePCRules(Populations[p_i][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]; } } } } //расчитываем значение dX KnowlegeBasePCRules[] dX = new KnowlegeBasePCRules[Populations[p_i].Count]; for (int i = 0; i < Populations[p_i].Count; i++) { dX[i] = new KnowlegeBasePCRules(Populations[p_i][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; } } } for (int i = 0; i < Populations[p_i].Count; i++) { Populations[p_i][i] = new KnowlegeBasePCRules(Populations[p_i][i]); for (int t = 0; t < Populations[p_i][i].TermsSet.Count; t++) { for (int p = 0; p < F[i].TermsSet[t].CountParams; p++) { Populations[p_i][i].TermsSet[t].Parametrs[p] = Populations[p_i][i].TermsSet[t].Parametrs[p] + calcdeltat(ct) * dX[i].TermsSet[t].Parametrs[p]; } } } for (int i = 0; i < Populations[p_i].Count; i++) { double temp = result.ErrorLearnSamples(Populations[p_i][i]); if (double.IsNaN(temp) || double.IsInfinity(temp) || temp == 100) { result.UnlaidProtectionFix(Xfood); temp = result.ErrorLearnSamples(Populations[p_i][i]); } if (temp < bestError[p_i]) { BEST[p_i] = new KnowlegeBasePCRules(Populations[p_i][i]); bestError[p_i] = temp; } } } }