public override Fuzzy_system.Class_Pittsburgh.c_Fuzzy_System TuneUpWeigth(Fuzzy_system.Class_Pittsburgh.c_Fuzzy_System Classifier, Abstract_weigth_config_conf conf) { int count_iteration = ((Weigth_Config_Random_Search_conf)conf).Количество_итераций; int count_generation_by_iteration = ((Weigth_Config_Random_Search_conf)conf).Количество_генерируемых_векторов_веса_за_итерацию; c_Fuzzy_System result = Classifier; for (int i = 0; i < count_iteration; i++) { double [][] weigth = new double[count_generation_by_iteration + 1][]; weigth[0] = Classifier.Rulles_Database_Set[0].Weigth; double best_result = result.Classify_Learn_Samples(); int best_index = 0; for (int j = 1; j < count_generation_by_iteration + 1; j++) { weigth[j] = new double[weigth[0].Count()]; for (int k = 0; k < weigth[0].Count(); k++) { weigth[j][k] = rand.NextDouble(); } result.Rulles_Database_Set[0].Weigth = weigth[j]; double current_result = result.Classify_Learn_Samples(); if (current_result > best_result) { best_result = current_result; best_index = j; } result.Rulles_Database_Set[0].Weigth = weigth[best_index]; } } return(result); }
public override c_Fuzzy_System Generate(Fuzzy_system.Class_Pittsburgh.c_Fuzzy_System Classifier, Abstract_generator_conf config) { Random rand = new Random(); c_Fuzzy_System result = Classifier; if (result.Count_Rulles_Databases == 0) { Knowlege_base_CRules temp_rules = new Knowlege_base_CRules(); result.Rulles_Database_Set.Add(temp_rules); } Type_Term_Func_Enum type_term = ((Generator_Rulles_simple_random_conf)config).Функция_принадлежности; int stable_terms = (int)((Generator_Rulles_simple_random_conf)config).Тип_Термов; int count_rules = ((Generator_Rulles_simple_random_conf)config).Количество_правил; for (int j = 0; j < count_rules; j++) { int[] order = new int[result.Count_Vars]; Type_Term_Func_Enum temp_type_term; if (stable_terms == 0) { temp_type_term = type_term; } else { temp_type_term = Generator_type_term(); } List <Term> temp_term_list = new List <Term>(); for (int k = 0; k < result.Count_Vars; k++) { double[] parametrs = new double[c_Fuzzy_System.Count_Params_For_Term(temp_type_term)]; switch (temp_type_term) { case Type_Term_Func_Enum.Треугольник: parametrs[0] = result.Learn_Samples_set.Attribute_Min(k) + rand.NextDouble() * (result.Learn_Samples_set.Attribute_Max(k) - result.Learn_Samples_set.Attribute_Min(k)); parametrs[1] = result.Learn_Samples_set.Attribute_Min(k) + rand.NextDouble() * (result.Learn_Samples_set.Attribute_Max(k) - result.Learn_Samples_set.Attribute_Min(k)); parametrs[2] = result.Learn_Samples_set.Attribute_Min(k) + rand.NextDouble() * (result.Learn_Samples_set.Attribute_Max(k) - result.Learn_Samples_set.Attribute_Min(k)); Array.Sort(parametrs); break; case Type_Term_Func_Enum.Гауссоида: parametrs[0] = result.Learn_Samples_set.Attribute_Min(k) + rand.NextDouble() * (result.Learn_Samples_set.Attribute_Max(k) - result.Learn_Samples_set.Attribute_Min(k)); parametrs[1] = (rand.NextDouble() + 0.01) * 0.5 * (result.Learn_Samples_set.Attribute_Max(k) - result.Learn_Samples_set.Attribute_Min(k)); break; case Type_Term_Func_Enum.Парабола: parametrs[0] = result.Learn_Samples_set.Attribute_Min(k) + rand.NextDouble() * (result.Learn_Samples_set.Attribute_Max(k) - result.Learn_Samples_set.Attribute_Min(k)); parametrs[1] = result.Learn_Samples_set.Attribute_Min(k) + rand.NextDouble() * (result.Learn_Samples_set.Attribute_Max(k) - result.Learn_Samples_set.Attribute_Min(k)); Array.Sort(parametrs); break; case Type_Term_Func_Enum.Трапеция: parametrs[0] = result.Learn_Samples_set.Attribute_Min(k) + rand.NextDouble() * (result.Learn_Samples_set.Attribute_Max(k) - result.Learn_Samples_set.Attribute_Min(k)); parametrs[1] = result.Learn_Samples_set.Attribute_Min(k) + rand.NextDouble() * (result.Learn_Samples_set.Attribute_Max(k) - result.Learn_Samples_set.Attribute_Min(k)); parametrs[2] = result.Learn_Samples_set.Attribute_Min(k) + rand.NextDouble() * (result.Learn_Samples_set.Attribute_Max(k) - result.Learn_Samples_set.Attribute_Min(k)); parametrs[3] = result.Learn_Samples_set.Attribute_Min(k) + rand.NextDouble() * (result.Learn_Samples_set.Attribute_Max(k) - result.Learn_Samples_set.Attribute_Min(k)); Array.Sort(parametrs); break; } Term temp_term = new Term(parametrs, temp_type_term, k); result.Rulles_Database_Set[0].Terms_Set.Add(temp_term); temp_term_list.Add(temp_term); order[k] = result.Rulles_Database_Set[0].Terms_Set.Count - 1; } string class_label = result.Nearest_Class(temp_term_list); CRule temp_Rule = new CRule(result.Rulles_Database_Set[0].Terms_Set, order, class_label, 1.0); result.Rulles_Database_Set[0].Rules_Database.Add(temp_Rule); } result.unlaid_protection_fix(); return(result); }
public override Fuzzy_system.Class_Pittsburgh.c_Fuzzy_System TuneUpFuzzySystem(Fuzzy_system.Class_Pittsburgh.c_Fuzzy_System Classifier, Abstract_learn_algorithm_conf conf) { count_iteration = ((Term_Config_PSO_Search_conf)conf).Количество_итераций; c1 = ((Term_Config_PSO_Search_conf)conf).Коэффициент_c1; c2 = ((Term_Config_PSO_Search_conf)conf).Коэффициент_c2; w = 1; count_particle = ((Term_Config_PSO_Search_conf)conf).Особей_в_популяции; c_Fuzzy_System result = Classifier; Knowlege_base_CRules[] X = new Knowlege_base_CRules[count_particle]; Knowlege_base_CRules[] V = new Knowlege_base_CRules[count_particle]; Knowlege_base_CRules[] Pi = new Knowlege_base_CRules[count_particle]; Knowlege_base_CRules Pg = new Knowlege_base_CRules(); double[] Errors = new double[count_particle]; double[] OldErrors = new double[count_particle]; double minError = 0; Random rnd = new Random(); for (int i = 0; i < count_particle; i++) { Knowlege_base_CRules temp_c_Rule = new Knowlege_base_CRules(result.Rulles_Database_Set[0]); X[i] = temp_c_Rule; Errors[i] = result.Classify_Learn_Samples(0); OldErrors[i] = Errors[i]; Pi[i] = new Knowlege_base_CRules(X[i]); V[i] = new Knowlege_base_CRules(X[i]); // for (int j = 0; j < V[i].Terms_Set.Count; j++) { for (int k = 0; k < c_Fuzzy_System.Count_Params_For_Term(V[i].Terms_Set[j].Term_Func_Type); k++) { if (i == 0) { V[i].Terms_Set[j].Parametrs[k] = 0; } else { V[i].Terms_Set[j].Parametrs[k] = rnd.NextDouble() - 0.5; } } double[] bf = new double[V[i].Weigth.Length]; for (int k = 0; k < V[i].Weigth.Length; k++) { if (i == 0) { bf[k] = 1; } else { //System.Windows.Forms.MessageBox.Show(rnd.NextDouble().ToString()); bf[k] = rnd.NextDouble() / 200; } } V[i].Weigth = bf; } } Pg = new Knowlege_base_CRules(result.Rulles_Database_Set[0]); minError = Errors[0]; for (int i = 0; i < count_iteration; i++) { for (int j = 0; j < count_particle; j++) { w = 1 / (1 + Math.Exp(-(Errors[j] - OldErrors[j]) / 0.01)); for (int k = 0; k < X[j].Terms_Set.Count; k++) { for (int q = 0; q < c_Fuzzy_System.Count_Params_For_Term(X[j].Terms_Set[k].Term_Func_Type); q++) { double bp = Pi[j].Terms_Set[k].Parametrs[q]; V[j].Terms_Set[k].Parametrs[q] = V[j].Terms_Set[k].Parametrs[q] * w + c1 * rnd.NextDouble() * (bp - X[j].Terms_Set[k].Parametrs[q]) + c2 * rnd.NextDouble() * (Pg.Terms_Set[k].Parametrs[q] - X[j].Terms_Set[k].Parametrs[q]); X[j].Terms_Set[k].Parametrs[q] += V[j].Terms_Set[k].Parametrs[q]; } } double[] bf = new double[V[j].Weigth.Length]; double[] bfw = new double[V[j].Weigth.Length]; for (int k = 0; k < V[j].Weigth.Length; k++) { bfw[k] = V[j].Weigth[k] * w + c1 * rnd.NextDouble() * (Pi[j].Weigth[k] - X[j].Weigth[k]) + c2 * rnd.NextDouble() * (Pg.Weigth[k] - X[j].Weigth[k]); double sw = X[j].Weigth[k] + bfw[k]; if (sw > 0 && sw <= 2) { bf[k] = sw; } else { bf[k] = X[j].Weigth[k]; bfw[k] = V[j].Weigth[k]; } } X[j].Weigth = bf; V[j].Weigth = bfw; double newError = 0; result.Rulles_Database_Set.Add(X[j]); int temp_index = result.Rulles_Database_Set.Count - 1; bool success = true; try { newError = result.Classify_Learn_Samples(temp_index); } catch (Exception) { success = false; } result.Rulles_Database_Set.RemoveAt(temp_index); if (success && (newError > Errors[j])) { OldErrors[j] = Errors[j]; Errors[j] = newError; Pi[j] = new Knowlege_base_CRules(X[j]); } if (minError < newError) { minError = newError; Pg = new Knowlege_base_CRules(X[j]); } } } result.Rulles_Database_Set[0] = Pg; return(result); }
public abstract c_Fuzzy_System TuneUpWeigth(Fuzzy_system.Class_Pittsburgh.c_Fuzzy_System Classifier, Abstract_weigth_config_conf conf);
public override Fuzzy_system.Class_Pittsburgh.c_Fuzzy_System TuneUpFuzzySystem(Fuzzy_system.Class_Pittsburgh.c_Fuzzy_System Classifier, Abstract_learn_algorithm_conf conf) { int count_iteration = ((Term_Config_Random_Search_conf)conf).Количество_итераций; int count_c_Rules = ((Term_Config_Random_Search_conf)conf).Количество_генерируемых_баз_правил_за_итерацию; c_Fuzzy_System result = Classifier; for (int i = 0; i < count_iteration; i++) { int temp_prev_count_c_Rule = result.Rulles_Database_Set.Count; double temp_best_result = result.Classify_Learn_Samples(); int temp_best_index = 0; for (int j = 0; j < count_c_Rules; j++) { Knowlege_base_CRules temp_c_Rule = new Knowlege_base_CRules(result.Rulles_Database_Set[0]); result.Rulles_Database_Set.Add(temp_c_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]); } bool success = true; double current_score = 0; try { current_score = result.Classify_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); } return(result); }