private List <KnowlegeBasePCRules> SetPopulation(List <KnowlegeBasePCRules> Population) { KnowlegeBasePCRules TempRule = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]); Population.Add(TempRule); Universal = TempRule; for (int i = 1; i < numberOfAllParts; i++) { Population.Add(new KnowlegeBasePCRules(TempRule)); for (int j = 0; j < Population[i].TermsSet.Count; j++) { for (int k = 0; k < Population[i].TermsSet[j].Parametrs.Length; k++) { Population[i].TermsSet[j].Parametrs[k] = GaussRandom.Random_gaussian(rand, Population[i].TermsSet[j].Parametrs[k], 0.1 * Population[i].TermsSet[j].Parametrs[k]); } } result.UnlaidProtectionFix(Population[i]); } Universal = new KnowlegeBasePCRules(TempRule); for (int i = 0; i < Universal.TermsSet.Count; i++) { for (int j = 0; j < Universal.TermsSet[i].Parametrs.Length; j++) { Universal.TermsSet[i].Parametrs[j] = GaussRandom.Random_gaussian(rand, Universal.TermsSet[i].Parametrs[j], 0.1 * Universal.TermsSet[i].Parametrs[j]); } } return(Population); }
private void SetPopulation() { Population = new KnowlegeBaseTSARules[numberOfAllParts]; KnowlegeBaseTSARules TempRule = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]); Population[0] = TempRule; Universal = TempRule; for (int i = 1; i < numberOfAllParts; i++) { Population[i] = new KnowlegeBaseTSARules(TempRule); for (int j = 0; j < Population[i].TermsSet.Count; j++) { for (int k = 0; k < Population[i].TermsSet[j].Parametrs.Length; k++) { Population[i].TermsSet[j].Parametrs[k] = GaussRandom.Random_gaussian(rand, Population[i].TermsSet[j].Parametrs[k], 0.1 * Population[i].TermsSet[j].Parametrs[k]); } } result.UnlaidProtectionFix(Population[i]); } Universal = new KnowlegeBaseTSARules(TempRule); for (int i = 0; i < Universal.TermsSet.Count; i++) { for (int j = 0; j < Universal.TermsSet[i].Parametrs.Length; j++) { Universal.TermsSet[i].Parametrs[j] = GaussRandom.Random_gaussian(rand, Universal.TermsSet[i].Parametrs[j], 0.1 * Universal.TermsSet[i].Parametrs[j]); } } }
public Hromosom mutate_SKO_RO(List <double> Covariance_matrix_pruned, Random rand) { for (int i = 0; i < Fenotip_terms.Count; i++) { for (int j = 0; j < Fenotip_terms[i].Parametrs.Count(); j++) { Fenotip_terms[i].Parametrs[j] = Fenotip_terms[i].Parametrs[j] + GaussRandom.Random_gaussian(rand, 0, Covariance_matrix_pruned[Fenotip_terms[i].NumVar]); } } for (int i = 0; i < Fenotip_kons.Count; i++) { Fenotip_kons[i] = GaussRandom.Random_gaussian(rand, Fenotip_kons[i], Fenotip_kons[i] * 0.1); } double Max_kons = Fenotip_kons.Max(); double Min_kons = Fenotip_kons.Min(); double Scatter_kons = Max_kons - Min_kons; for (int i = 0; i < Fenotip_kons.Count; i++) { Fenotip_kons[i] = (Fenotip_kons[i] - Min_kons + 0.1) / (Scatter_kons + 0.1); } return(this); }
public override SAFuzzySystem TuneUpFuzzySystem(SAFuzzySystem Classifier, ILearnAlgorithmConf conf) { Init(conf); KnowlegeBaseSARules temp_c_Rule = new KnowlegeBaseSARules(Classifier.RulesDatabaseSet[0]); SAFuzzySystem result = Classifier; for (int t = 0; t < population_count; t++) { monkey[t] = new KnowlegeBaseSARules(result.RulesDatabaseSet[0]); for (int k = 0; k < result.RulesDatabaseSet[0].TermsSet.Count; k++) { for (int q = 0; q < result.RulesDatabaseSet[0].TermsSet[k].CountParams; q++) { //monkey[t].TermsSet[k].Parametrs[q] = StaticRandom.NextDouble() * (result.RulesDatabaseSet[0].TermsSet[k].Max - result.RulesDatabaseSet[0].TermsSet[k].Min); monkey[t].TermsSet[k].Parametrs[q] = GaussRandom.Random_gaussian(rand, monkey[t].TermsSet[k].Parametrs[q], monkey[t].TermsSet[k].Parametrs[q] * 0.05); } } result.UnlaidProtectionFix(monkey[t]); // delete testvals[t] = result.ErrorLearnSamples(monkey[t]); Console.WriteLine("Begin: " + t.ToString() + " " + iter.ToString() + " " + testvals[t].ToString()); } bestsolution = new KnowlegeBaseSARules(monkey.SelectBest(result, 1)[0]); bestsolutionnumber = result.ErrorLearnSamples(bestsolution); iter_amount = somersault_iter * (1 + jump_iter * (1 + crawl_iter)); deltaLength = result.RulesDatabaseSet[0].TermsSet.Sum(x => x.Parametrs.Length); for (int r = 0; r < somersault_iter; r++) { for (int t = 0; t < jump_iter; t++) { for (int e = 0; e < crawl_iter; e++) { iter++; CheckForBest(result); oneClimb(result, deltaLength, step); //Console.WriteLine(iter_amount.ToString() + "/" + iter.ToString()); } iter++; oneWatchJump(result); //Console.WriteLine(iter_amount.ToString() + "/" + iter.ToString()); } iter++; oneGlobalJump(result); Console.WriteLine(iter_amount.ToString() + "/" + iter.ToString()); } /* for (int t = 0; t < population_count; t++) * if (result.ErrorLearnSamples(monkey[best]) < result.ErrorLearnSamples(monkey[t])) * best = t; */ CheckForBest(result); if (bestsolutionnumber < result.ErrorLearnSamples(result.RulesDatabaseSet[0])) { result.RulesDatabaseSet[0] = bestsolution; } iter = 0; return(result); }
public Hromosom mutate_SKO(List <double> sko, Random rand) { for (int i = 0; i < Fenotip_terms.Count; i++) { for (int j = 0; j < Fenotip_terms[i].Parametrs.Count(); j++) { Fenotip_terms[i].Parametrs[j] = Fenotip_terms[i].Parametrs[j] + sko[Fenotip_terms[i].NumVar] * GaussRandom.Random_gaussian(rand); } } for (int i = 0; i < Fenotip_kons.Count; i++) { Fenotip_kons[i] = GaussRandom.Random_gaussian(rand, Fenotip_kons[i]); } double Max_kons = Fenotip_kons.Max(); double Min_kons = Fenotip_kons.Min(); double Scatter_kons = Max_kons - Min_kons; for (int i = 0; i < Fenotip_kons.Count; i++) { Fenotip_kons[i] = (Fenotip_kons[i] - Min_kons + 0.1) / (Scatter_kons + 0.1); } return(this); }
public override Fuzzy_system.Approx_Singletone.a_Fuzzy_System TuneUpFuzzySystem(a_Fuzzy_System Approximate, Abstract_learn_algorithm_conf conf) { a_Fuzzy_System result = Approximate; count_iteration = ((Term_Config_Random_Search_conf)conf).Количество_итераций; count_Rules = ((Term_Config_Random_Search_conf)conf).Количество_генерируемых_баз_правил_за_итерацию; for (int i = 0; i < count_iteration; i++) { int temp_prev_count_c_Rule = result.Rulles_Database_Set.Count; double temp_best_result = result.approx_Learn_Samples(); int temp_best_index = 0; for (int j = 0; j < count_Rules; j++) { Knowlege_base_ARules temp_a_Rule = new Knowlege_base_ARules(result.Rulles_Database_Set[0]); result.Rulles_Database_Set.Add(temp_a_Rule); int temp_index = result.Rulles_Database_Set.Count - 1; for (int k = 0; k < result.Rulles_Database_Set[temp_index].Terms_Set.Count; k++) { result.Rulles_Database_Set[temp_index].Terms_Set[k] = randomize_term(result.Rulles_Database_Set[temp_index].Terms_Set[k]); } double[] kons = result.Rulles_Database_Set[temp_index].all_conq_of_rules; for (int k = 0; k < kons.Count(); k++) { kons[k] = GaussRandom.Random_gaussian(rand, kons[k], kons[k] / 10); } result.Rulles_Database_Set[temp_index].all_conq_of_rules = kons; bool success = true; double current_score = 0; try { current_score = result.approx_Learn_Samples(temp_index); } catch (Exception) { success = false; } if (success && (current_score >= temp_best_result)) { temp_best_result = current_score; temp_best_index = temp_index; } } result.Rulles_Database_Set[0] = result.Rulles_Database_Set[temp_best_index]; result.Rulles_Database_Set.RemoveRange(temp_prev_count_c_Rule, result.Rulles_Database_Set.Count - temp_prev_count_c_Rule); } GC.Collect(); return(result); }
private Term randomize_term(Term source) { Term result = source; for (int k = 0; k < Member_Function.Count_Params_For_Term(source.Term_Func_Type); k++) { result.Parametrs[k] = GaussRandom.Random_gaussian(rand, result.Parametrs[k], result.Parametrs[k] / 10); } return(result); }
private void preIterate(SAFuzzySystem result) { for (int i = 0; i < count_particle; i++) { KnowlegeBaseSARules temp_c_Rule = new KnowlegeBaseSARules(result.RulesDatabaseSet[0]); X[i] = temp_c_Rule; Errors[i] = result.approxLearnSamples(result.RulesDatabaseSet[0]); OldErrors[i] = Errors[i]; Pi[i] = new KnowlegeBaseSARules(X[i]); V[i] = new KnowlegeBaseSARules(X[i]); for (int j = 0; j < V[i].TermsSet.Count; j++) { for (int k = 0; k < Term.CountParamsinSelectedTermType(V[i].TermsSet[j].TermFuncType); k++) { if (i == 0) { V[i].TermsSet[j].Parametrs[k] = 0; } else { V[i].TermsSet[j].Parametrs[k] = rnd.NextDouble() - 0.5; } } double[] bf = new double[V[i].all_conq_of_rules.Length]; for (int k = 0; k < V[i].all_conq_of_rules.Length; k++) { if (i == 0) { bf[k] = V[i].all_conq_of_rules[k]; } else { bf[k] = GaussRandom.Random_gaussian(rand, V[i].all_conq_of_rules[k], V[i].all_conq_of_rules[k] * 0.01); } } V[i].all_conq_of_rules = bf; } } Pg = new KnowlegeBaseSARules(result.RulesDatabaseSet[0]); minError = Errors[0]; best_new = minError; best_old = minError; worst_new = minError; }
private void SetPopulation() { Population = new KnowlegeBaseSARules[Npop]; KnowlegeBaseSARules TempRule = new KnowlegeBaseSARules(result.RulesDatabaseSet[0]); Population[0] = TempRule; for (int i = 1; i < Npop; i++) { TempRule = new KnowlegeBaseSARules(result.RulesDatabaseSet[0]); Population[i] = TempRule; for (int j = 0; j < Population[i].TermsSet.Count; j++) { for (int k = 0; k < Population[i].TermsSet[j].Parametrs.Length; k++) { Population[i].TermsSet[j].Parametrs[k] = GaussRandom.Random_gaussian(rand, Population[i].TermsSet[j].Parametrs[k], 0.1 * Population[i].TermsSet[j].Parametrs[k]); } } } }
private List <KnowlegeBasePCRules> SetPopulation(List <KnowlegeBasePCRules> Population) { KnowlegeBasePCRules TempRule = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]); Population.Add(TempRule); for (int i = 1; i < N; i++) { TempRule = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]); Population.Add(TempRule); for (int j = 0; j < Population[i].TermsSet.Count; j++) { for (int k = 0; k < Population[i].TermsSet[j].Parametrs.Length; k++) { Population[i].TermsSet[j].Parametrs[k] = GaussRandom.Random_gaussian(rand, Population[i].TermsSet[j].Parametrs[k], 0.1 * Population[i].TermsSet[j].Parametrs[k]); } } } return(Population); }
public Hromosom mutate_SKO_RO(List <double> Covariance_matrix_pruned, Random rand) { for (int i = 0; i < Fenotip_terms.Count; i++) { for (int j = 0; j < Fenotip_terms[i].Parametrs.Count(); j++) { Fenotip_terms[i].Parametrs[j] = Fenotip_terms[i].Parametrs[j] + GaussRandom.Random_gaussian(rand, 0, Covariance_matrix_pruned[Fenotip_terms[i].Number_of_Input_Var]); } } for (int i = 0; i < Fenotip_kons.Count; i++) { Fenotip_kons[i] = GaussRandom.Random_gaussian(rand, Fenotip_kons[i], Fenotip_kons[i] * 0.1); } return(this); }
public Hromosom mutate_SKO(List <double> sko, Random rand) { for (int i = 0; i < Fenotip_terms.Count; i++) { for (int j = 0; j < Fenotip_terms[i].Parametrs.Count(); j++) { Fenotip_terms[i].Parametrs[j] = Fenotip_terms[i].Parametrs[j] + sko[Fenotip_terms[i].Number_of_Input_Var] * GaussRandom.Random_gaussian(rand); } } for (int i = 0; i < Fenotip_kons.Count; i++) { Fenotip_kons[i] = GaussRandom.Random_gaussian(rand, Fenotip_kons[i]); } return(this); }
private void SetPopulation() { Population = new KnowlegeBasePCRules[Nkrill]; KnowlegeBasePCRules TempRule = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]); Population[0] = TempRule; for (int i = 1; i < Nkrill; i++) { TempRule = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]); Population[i] = TempRule; for (int j = 0; j < Population[i].TermsSet.Count; j++) { for (int k = 0; k < Population[i].TermsSet[j].Parametrs.Length; k++) { // если что поменять параметр 0,1 Population[i].TermsSet[j].Parametrs[k] = GaussRandom.Random_gaussian(rand, Population[i].TermsSet[j].Parametrs[k], 0.1 * Population[i].TermsSet[j].Parametrs[k]); //перебрать значения параметров } } } }
private List <KnowlegeBasePCRules> SetPopulation(List <KnowlegeBasePCRules> Population) { KnowlegeBasePCRules TempRule = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]); Population.Add(TempRule); for (int i = 1; i < Nkrill; i++) { TempRule = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]); Population.Add(TempRule); for (int j = 0; j < Population[i].TermsSet.Count; j++) { for (int k = 0; k < Population[i].TermsSet[j].Parametrs.Length; k++) { // если что поменять параметр 0,1 Population[i].TermsSet[j].Parametrs[k] = GaussRandom.Random_gaussian(rand, Population[i].TermsSet[j].Parametrs[k], 0.1 * Population[i].TermsSet[j].Parametrs[k]); //перебрать значения параметров } } } return(Population); }
public Individ mutate(Random rand, double tau1, double tau2, FuzzySystem.FuzzyAbstract.learn_algorithm.conf.ESConfig.Type_Mutate type_mutate, double b_ro) { switch (type_mutate) { case FuzzySystem.FuzzyAbstract.learn_algorithm.conf.ESConfig.Type_Mutate.СКО: { hrom_vector.mutate_SKO(step_sko, rand); for (int i = 0; i < step_sko.Count; i++) { step_sko[i] = step_sko[i] * Math.Exp(tau1 * GaussRandom.Random_gaussian(rand) + tau2 * GaussRandom.Random_gaussian(rand)); } break; } case FuzzySystem.FuzzyAbstract.learn_algorithm.conf.ESConfig.Type_Mutate.СКО_РО: { List <double> Covariance_matrix_pruned = new List <double>(); for (int i = 0; i < step_sko.Count; i++) // mutate { step_sko[i] = step_sko[i] * Math.Exp(tau1 * GaussRandom.Random_gaussian(rand) + tau2 * GaussRandom.Random_gaussian(rand)); step_rotate[i] = step_rotate[i] + b_ro * GaussRandom.Random_gaussian(rand); } double avr_ro = step_rotate.Sum() / step_rotate.Count; double avr_sko = step_sko.Sum() / step_sko.Count; for (int i = 0; i < step_sko.Count; i++) //calc covariance_matrix_pruned { Covariance_matrix_pruned.Add(Math.Abs( (step_sko[i] - avr_sko) * (step_rotate[i] - avr_ro) ) ); } hrom_vector.mutate_SKO_RO(Covariance_matrix_pruned, rand); break; } } return(this); }
public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Class, ILearnAlgorithmConf conf) // Здесь ведется оптимизация вашим алгоритмом { theFuzzySystem = Class; // Console.WriteLine(theFuzzySystem.RulesDatabaseSet[0].TermsSet.Count); iterMax = ((gsa_conf)conf).Количество_итераций; MCount = ((gsa_conf)conf).Количество_частиц; G0 = ((gsa_conf)conf).Гравитационная_постоянная; alpha = ((gsa_conf)conf).Коэффициент_уменьшения; epsilon = ((gsa_conf)conf).Малая_константа; X = new KnowlegeBasePCRules[MCount]; Errors = new double[MCount]; mass = new double[MCount]; temp_c_Rule = new KnowlegeBasePCRules(theFuzzySystem.RulesDatabaseSet[0]); X[0] = temp_c_Rule; Errors[0] = theFuzzySystem.ErrorLearnSamples(X[0]); ErrorZero = Errors[0]; ErrorBest = Errors[0]; BestSolution = new KnowlegeBasePCRules(theFuzzySystem.RulesDatabaseSet[0]); R = new double[MCount][, , ]; speed = new double[MCount, X[0].TermsSet.Count, X[0].TermsSet[0].Parametrs.Count()]; for (int i = 0; i < MCount; i++) { R[i] = new double[MCount, X[0].TermsSet.Count, X[0].TermsSet[0].Parametrs.Count()]; } RR = new double[MCount, MCount]; a = new double[MCount, X[0].TermsSet.Count, X[0].TermsSet[0].Parametrs.Count()]; for (int i = 1; i < MCount; i++) { temp_c_Rule = new KnowlegeBasePCRules(theFuzzySystem.RulesDatabaseSet[0]); X[i] = temp_c_Rule; for (int j = 0; j < X[i].TermsSet.Count; j++) { for (int k = 0; k < X[i].TermsSet[j].Parametrs.Count(); k++) { X[i].TermsSet[j].Parametrs[k] = GaussRandom.Random_gaussian(rand, X[i].TermsSet[j].Parametrs[k], 0.1 * (X[i].TermsSet[j].Parametrs[k])) + theFuzzySystem.LearnSamplesSet.InputAttributes[X[i].TermsSet[j].NumVar].Scatter * 0.05; } } //theFuzzySystem.RulesDatabaseSet.Add(X[i]); //theFuzzySystem.UnlaidProtectionFix(theFuzzySystem.RulesDatabaseSet.Count - 1); //Errors[i] = theFuzzySystem.ErrorLearnSamples(X[i]); //X[i] = theFuzzySystem.RulesDatabaseSet[theFuzzySystem.RulesDatabaseSet.Count - 1]; //theFuzzySystem.RulesDatabaseSet.Remove(X[i]); theFuzzySystem.UnlaidProtectionFix(X[i]); Errors[i] = theFuzzySystem.ErrorLearnSamples(X[i]); } for (int iter = 0; iter < iterMax; iter++) { //g(t) = G(0)*e^(-a*t/T); G = G0 * Math.Pow(Math.E, ((-1) * alpha * iter / iterMax)); algorithm(); for (int r = 0; r < MCount; r++) { theFuzzySystem.UnlaidProtectionFix(X[r]); Errors[r] = theFuzzySystem.ErrorLearnSamples(X[r]); } minValue = Errors.Min(); iminIndex = Errors.ToList().IndexOf(minValue); if (minValue < ErrorBest) { ErrorBest = minValue; BestSolution = new KnowlegeBasePCRules(X[iminIndex]); } } //theFuzzySystem.RulesDatabaseSet[0] = BestSolution; if (ErrorBest < ErrorZero) { theFuzzySystem.RulesDatabaseSet[0] = BestSolution; } return(theFuzzySystem); }
private void weight() { double sum = 0; double best = mass[0]; double worst = mass[0]; int[] index = new Int32[MCount]; int count = 0; for (int i = 1; i < MCount; i++) { mass[i] = Errors[i]; if (mass[i] > best) { best = mass[i]; } if (mass[i] < worst) { worst = mass[i]; } } for (int i = 0; i < MCount; i++) { if (mass[i] == best) { count++; index[count - 1] = i; } } if (count > 1) { for (int i = 1; i < count; i++) { ///X[index[i]] = ; int f = index[i]; KnowlegeBasePCRules temp_c_Rule = new KnowlegeBasePCRules(theFuzzySystem.RulesDatabaseSet[0]); temp_c_Rule = new KnowlegeBasePCRules(theFuzzySystem.RulesDatabaseSet[0]); X[f] = temp_c_Rule; for (int j = 0; j < X[f].TermsSet.Count; j++) { for (int k = 0; k < X[f].TermsSet[j].Parametrs.Count(); k++) { X[f].TermsSet[j].Parametrs[k] = GaussRandom.Random_gaussian(rand, X[f].TermsSet[j].Parametrs[k], 0.1 * (X[f].TermsSet[j].Parametrs[k])) + theFuzzySystem.LearnSamplesSet.InputAttributes[X[f].TermsSet[j].NumVar].Scatter * 0.05; } } theFuzzySystem.UnlaidProtectionFix(X[f]); Errors[f] = theFuzzySystem.ErrorLearnSamples(X[f]); mass[f] = Errors[f]; if (mass[f] > best) { i--; } } } for (int i = 0; i < MCount; i++) { mass[i] = (mass[i] - worst) / (best - worst); sum = sum + mass[i]; } for (int i = 0; i < MCount; i++) { mass[i] = mass[i] / sum; } }
static void Main(string[] args) { Console.WriteLine("Exponential Random:\n"); Console.Write("Enter lambda: "); string lambdaLine = Console.ReadLine(); IRandom random = new ExponentialRandom(); if (double.TryParse(lambdaLine, out double lambda)) { random = new ExponentialRandom(lambda); } List <double> list = new List <double>(10000); for (int i = 0; i < 10000; i++) { list.Add(random.Next()); } Show(list); random.DistributionStatistics(); DistributionStatistics(list); Console.WriteLine(" chi-square: " + random.IndificateDistributionLaw(list).ToString("F3")); Console.WriteLine("\nUniform Random:\n"); random = new UniformRandom(); list = new List <double>(10000); for (int i = 0; i < 10000; i++) { list.Add(random.Next()); } Show(list); random.DistributionStatistics(); DistributionStatistics(list); Console.WriteLine(" chi-square: " + random.IndificateDistributionLaw(list).ToString("F3")); Console.WriteLine("\nGauss Random:\n"); Console.Write("Enter sigma: "); string sigmaLine = Console.ReadLine(); Console.Write("Enter alpha: "); string alphaLine = Console.ReadLine(); random = new GaussRandom(); if (double.TryParse(sigmaLine, out double sigma) && double.TryParse(alphaLine, out double alpha)) { random = new GaussRandom(sigma, alpha); } list = new List <double>(10000); for (int i = 0; i < 10000; i++) { list.Add(random.Next()); } Show(list); random.DistributionStatistics(); DistributionStatistics(list); Console.WriteLine(" chi-square: " + random.IndificateDistributionLaw(list).ToString("F3")); Console.WriteLine("\nPress any key to continue..."); Console.ReadKey(); }
public virtual void oneClimb(TSAFuzzySystem result, int length, double st) { int i; double step = GaussRandom.Random_gaussian(rand, st, st * 0.05); KnowlegeBaseTSARules temp; double[] delta = new double[length]; bool sign_f; int sign_num; for (int j = 0; j < population_count; j++) { i = 0; if (j % 4 < 2) { delta = ClimbVector(length, step); } else { delta = ClimbVectorR(length, step); } monkeysum = new KnowlegeBaseTSARules(monkey[j]); monkeysub = new KnowlegeBaseTSARules(monkey[j]); temp = new KnowlegeBaseTSARules(monkey[j]); for (int k = 0; k < result.RulesDatabaseSet[0].TermsSet.Count; k++) { for (int q = 0; q < result.RulesDatabaseSet[0].TermsSet[k].CountParams; q++, i++) { monkeysum.TermsSet[k].Parametrs[q] += delta[i]; monkeysub.TermsSet[k].Parametrs[q] -= delta[i]; } } if (result.ErrorLearnSamples(monkeysum) < result.ErrorLearnSamples(monkeysub)) { sign_num = 1; } else { sign_num = -1; } i = 0; if (j % 2 == 0) { for (int k = 0; k < result.RulesDatabaseSet[0].TermsSet.Count; k++) { for (int q = 0; q < result.RulesDatabaseSet[0].TermsSet[k].CountParams; q++, i++) { // old //temp.TermsSet[k].Parametrs[q] = monkey[j].TermsSet[k].Parametrs[q] + step * sign(monkeysum, monkeysub, delta[i], result); // Двойной шаг // temp.TermsSet[k].Parametrs[q] = monkey[j].TermsSet[k].Parametrs[q] + step * sign_num * delta[i]; temp.TermsSet[k].Parametrs[q] = monkey[j].TermsSet[k].Parametrs[q] + sign_num * delta[i]; } } } // Идея ГИВ else { sign_f = (result.ErrorLearnSamples(monkeysum) < result.ErrorLearnSamples(monkeysub)); if (sign_f) { temp = monkeysum; } else { temp = monkeysub; } } if ((result.ErrorLearnSamples(temp) < result.ErrorLearnSamples(monkey[j]))) { monkey[j] = temp; } //delete double testval = result.ErrorLearnSamples(monkey[j]); if (testval < testvals[j]) { //Console.WriteLine(j.ToString() + " " + iter.ToString() + " " + testval.ToString() + " - Climb"); testvals[j] = testval; } } }
public override TSAFuzzySystem TuneUpFuzzySystem(TSAFuzzySystem Classifier, ILearnAlgorithmConf conf) { Init(conf); KnowlegeBaseTSARules temp_c_Rule = new KnowlegeBaseTSARules(Classifier.RulesDatabaseSet[0]); TSAFuzzySystem result = Classifier; string file_string = @"..\logs_" + result.TestSamplesSet.FileName + ".txt"; string file_string_to_txt = @"..\result_" + result.TestSamplesSet.FileName + ".txt"; for (int t = 0; t < population_count; t++) { monkey[t] = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]); if (t > 3) { for (int k = 0; k < result.RulesDatabaseSet[0].TermsSet.Count; k++) { for (int q = 0; q < result.RulesDatabaseSet[0].TermsSet[k].CountParams; q++) { //monkey[t].TermsSet[k].Parametrs[q] = StaticRandom.NextDouble() * (result.RulesDatabaseSet[0].TermsSet[k].Max - result.RulesDatabaseSet[0].TermsSet[k].Min); monkey[t].TermsSet[k].Parametrs[q] = GaussRandom.Random_gaussian(rand, monkey[t].TermsSet[k].Parametrs[q], monkey[t].TermsSet[k].Parametrs[q] * 0.05); } } } double unlaidtest = result.ErrorLearnSamples(monkey[t]); //result.UnlaidProtectionFix(monkey[t]); //Console.WriteLine("Unlaid: " + result.ErrorLearnSamples(monkey[0]).ToString()); if (double.IsNaN(unlaidtest) || double.IsInfinity(unlaidtest)) { result.UnlaidProtectionFix(monkey[t]); } // delete testvals[t] = result.ErrorLearnSamples(monkey[t]); Console.WriteLine("Begin: " + t.ToString() + " " + iter.ToString() + " " + testvals[t].ToString()); } bestsolution = new KnowlegeBaseTSARules(monkey.SelectBest(result, 1)[0]); bestsolutionnumber = result.ErrorLearnSamples(bestsolution); deltaLength = result.RulesDatabaseSet[0].TermsSet.Sum(x => x.Parametrs.Length); if (debug) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(file_string, true)) { file.WriteLine(DateTime.Today.ToString() + "\t" + result.LearnSamplesSet.FileName); file.WriteLine("Parameters:"); file.WriteLine("Population\t" + population_count.ToString()); file.WriteLine("Iteration count\t" + iter_amount.ToString()); file.WriteLine("Crawl count\t" + crawl_iter.ToString()); file.WriteLine("Jump count\t" + jump_iter.ToString()); file.WriteLine("Somersault count\t" + somersault_iter.ToString()); file.WriteLine("Crawl step\t" + step.ToString()); // crawl step file.WriteLine("Jump step\t" + watch_jump_parameter.ToString()); file.WriteLine("Somersault left border\t" + somersault_interval_left.ToString()); file.WriteLine("Somersault right border\t" + somersault_interval_right.ToString()); file.WriteLine("\t\tMonkeys"); file.Write("Iterations\t"); for (int t = 0; t < population_count; t++) { file.Write("\t" + t); } file.WriteLine(); file.Write("0\tbegin"); for (int t = 0; t < population_count; t++) { file.Write("\t" + testvals[t].ToString()); } // excel вставки // наибольший в таблице file.WriteLine(); } } //iter_amount = somersault_iter * (1 + jump_iter * (1 + crawl_iter)); iter_amount = (((crawl_iter + jump_iter) * jump_iter) + somersault_iter) * somersault_iter; for (int r = 0; r < somersault_iter; r++) { for (int t = 0; t < jump_iter; t++) { for (int e = 0; e < crawl_iter; e++) { iter++; oneClimb(result, deltaLength, step); CheckForBest(result); //Console.WriteLine(iter_amount.ToString() + "/" + iter.ToString // дебаг if (debug) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(file_string, true)) { file.Write(iter.ToString() + "\tcrawl"); for (int p = 0; p < population_count; p++) { file.Write("\t" + result.ErrorLearnSamples(monkey[p]).ToString()); } file.WriteLine(); } } } for (int e = 0; e < jump_iter; e++) { iter++; oneWatchJump(result); //Console.WriteLine(iter_amount.ToString() + "/" + iter.ToString()); CheckForBest(result); // дебаг if (debug) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(file_string, true)) { file.Write(iter.ToString() + "\tlocaljump"); for (int p = 0; p < population_count; p++) { file.Write("\t" + result.ErrorLearnSamples(monkey[p]).ToString()); } file.WriteLine(); } } } } for (int e = 0; e < somersault_iter; e++) { iter++; oneGlobalJump(result); CheckForBest(result); // дебаг if (debug) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(file_string, true)) { file.Write(iter.ToString() + "\tglobaljump"); for (int p = 0; p < population_count; p++) { file.Write("\t" + result.ErrorLearnSamples(monkey[p]).ToString()); } file.WriteLine(); } } Console.WriteLine(iter_amount.ToString() + "/" + iter.ToString()); } } //Console.WriteLine(final_iter.ToString() + "/" + final_counter.ToString()); //FOR VICTORY!!! while ((final_iter < final_counter) && (last == true)) { step *= 0.9; watch_jump_parameter *= 0.9; somersault_interval_left *= 0.9; somersault_interval_right *= 0.9; for (int r = 0; r < somersault_iter; r++) { oneClimb(result, deltaLength, step); CheckForBest(result); iter++; } for (int t = 0; t < jump_iter; t++) { oneWatchJump(result); CheckForBest(result); iter++; } for (int e = 0; e < crawl_iter; e++) { oneGlobalJump(result); CheckForBest(result); iter++; } Console.WriteLine(iter_amount.ToString() + "/" + iter.ToString()); } /* for (int t = 0; t < population_count; t++) * if (result.ErrorLearnSamples(monkey[best]) < result.ErrorLearnSamples(monkey[t])) * best = t; */ CheckForBest(result); if (bestsolutionnumber <= result.ErrorLearnSamples(result.RulesDatabaseSet[0])) { result.RulesDatabaseSet[0] = bestsolution; } iter = 0; if (debug) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(file_string, true)) { file.WriteLine("Results\t" + result.ErrorLearnSamples(bestsolution).ToString() + "\t" + result.ErrorTestSamples(bestsolution).ToString()); } } if (totxt) { using (System.IO.StreamWriter file_result = new System.IO.StreamWriter(file_string_to_txt, true)) { file_result.WriteLine(result.ErrorLearnSamples(bestsolution).ToString() + "\t" + result.ErrorTestSamples(bestsolution).ToString()); } } return(result); }
private void ChangeParticles() { foreach (var Particle in Population) { countOnes = 0; countZeros = 0; for (int i = 0; i < Particle.Length; i++) { if (Particle[i] == 0) { countZeros += 1; } else { countOnes += 1; } } u = GaussRandom.Random_gaussian(rand, MathExpect, Deviation); if (u > 0) { l = Convert.ToInt32(Math.Round(countZeros * hypTang(u))); List <int> Zeroes = new List <int>(); for (int i = 0; i < Particle.Length; i++) { if (Particle[i] == 0) { Zeroes.Add(i); } } for (int i = l; i > 0; i--) { int randindex = rand.Next(0, Zeroes.Count); int index = Zeroes[randindex]; Zeroes.RemoveAt(randindex); if (iter < 200) { if (rand.NextDouble() > 0.5) { Particle[index] = 1; } } else { if (rand.NextDouble() > (BestParticles[index][0] / (BestParticles[index][0] + BestParticles[index][1]))) { Particle[index] = 1; } } } } else { l = Math.Abs(Convert.ToInt32(Math.Round(countOnes * hypTang(u)))); List <int> Ones = new List <int>(); for (int i = 0; i < Particle.Length; i++) { if (Particle[i] == 1) { Ones.Add(i); } } for (int i = l; i > 0; i--) { int randindex = rand.Next(0, Ones.Count); int index = Ones[randindex]; Ones.RemoveAt(randindex); if (iter < 200) { if (rand.NextDouble() > 0.5) { Particle[index] = 0; } } else { if (rand.NextDouble() > (BestParticles[index][1] / (BestParticles[index][0] + BestParticles[index][1]))) { Particle[index] = 0; } } } } } }