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); }
private void addApproxValue(SAFuzzySystem Approx) { double Value = Approx.approxLearnSamples(Approx.RulesDatabaseSet[0]); ValueLGoodsRMSE.Add(Value); ValueLGoodsMSE.Add(Approx.RMSEtoMSEforLearn(Value)); Value = Approx.approxTestSamples(Approx.RulesDatabaseSet[0]); ValueTGoodsRMSE.Add(Value); ValueTGoodsMSE.Add(Approx.RMSEtoMSEforTest(Value)); Value = Approx.getComplexit(); ValueComplexityFull.Add(Value); Value = Approx.getRulesCount(); ValueComplexityRules.Add(Value); Value = Approx.getNormalIndex(); ValueInterpretyNominal.Add(Value); Value = Approx.getIndexReal(); ValueInterpretyReal.Add(Value); }
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); }
public override SAFuzzySystem TuneUpFuzzySystem(SAFuzzySystem Approx, ILearnAlgorithmConf conf) // Здесь ведется оптимизация вашим алгоритмом { theFuzzySystem = Approx; iterMax = ((gsa_conf)conf).Количество_итераций; MCount = ((gsa_conf)conf).Количество_частиц; G0 = ((gsa_conf)conf).Гравитационная_постоянная; alpha = ((gsa_conf)conf).Коэффициент_уменьшения; epsilon = ((gsa_conf)conf).Малая_константа; X = new KnowlegeBaseSARules[MCount]; Errors = new double[MCount]; mass = new double[MCount]; double ErrorBest; KnowlegeBaseSARules BestSolution; double minValue; int iminIndex; KnowlegeBaseSARules temp_c_Rule = new KnowlegeBaseSARules(theFuzzySystem.RulesDatabaseSet[0]); X[0] = temp_c_Rule; Errors[0] = theFuzzySystem.RMSEtoMSEforLearn(theFuzzySystem.approxLearnSamples(X[0])); double ErrorZero = Errors[0]; ErrorBest = ErrorZero; BestSolution = temp_c_Rule; //number = X[0].TermsSet.Count * X[0].TermsSet[0].Parametrs.Count(); 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 KnowlegeBaseSARules(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[theFuzzySystem.RulesDatabaseSet.Count - 1]); Errors[i] = theFuzzySystem.RMSEtoMSEforLearn(theFuzzySystem.approxLearnSamples(theFuzzySystem.RulesDatabaseSet[theFuzzySystem.RulesDatabaseSet.Count - 1])); X[i] = theFuzzySystem.RulesDatabaseSet[theFuzzySystem.RulesDatabaseSet.Count - 1]; theFuzzySystem.RulesDatabaseSet.Remove(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)); /* if (iter >= 100) { * Console.WriteLine("Wait"); * }*/ algorithm(); for (int r = 0; r < MCount; r++) { theFuzzySystem.RulesDatabaseSet.Add(X[r]); theFuzzySystem.UnlaidProtectionFix(theFuzzySystem.RulesDatabaseSet[theFuzzySystem.RulesDatabaseSet.Count - 1]); Errors[r] = theFuzzySystem.RMSEtoMSEforLearn(theFuzzySystem.approxLearnSamples(theFuzzySystem.RulesDatabaseSet[theFuzzySystem.RulesDatabaseSet.Count - 1])); X[r] = theFuzzySystem.RulesDatabaseSet[theFuzzySystem.RulesDatabaseSet.Count - 1]; theFuzzySystem.RulesDatabaseSet.Remove(X[r]); } minValue = Errors.Min(); iminIndex = Errors.ToList().IndexOf(minValue); if (minValue < ErrorBest) { ErrorBest = minValue; BestSolution = new KnowlegeBaseSARules(X[iminIndex]); } } if (ErrorBest < ErrorZero) { theFuzzySystem.RulesDatabaseSet[0] = BestSolution; } return(theFuzzySystem); }
private void weight() { double sum = 0; double worst = mass[0]; double best = mass[0]; int[] index = new Int32[MCount]; int count = 0; for (int i = 1; i < MCount; i++) { mass[i] = Errors[i]; if (mass[i] > worst) { worst = mass[i]; } if (mass[i] < best) { best = mass[i]; } } for (int i = 0; i < MCount; i++) { if (mass[i] == worst) { count++; index[count - 1] = i; } } if (count > 1) { for (int i = 1; i < count; i++) { ///X[index[i]] = ; int f = index[i]; KnowlegeBaseSARules temp_c_Rule = new KnowlegeBaseSARules(theFuzzySystem.RulesDatabaseSet[0]); temp_c_Rule = new KnowlegeBaseSARules(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.RulesDatabaseSet.Add(X[f]); theFuzzySystem.UnlaidProtectionFix(theFuzzySystem.RulesDatabaseSet[theFuzzySystem.RulesDatabaseSet.Count - 1]); Errors[f] = theFuzzySystem.RMSEtoMSEforLearn(theFuzzySystem.approxLearnSamples(theFuzzySystem.RulesDatabaseSet[theFuzzySystem.RulesDatabaseSet.Count - 1])); X[f] = theFuzzySystem.RulesDatabaseSet[theFuzzySystem.RulesDatabaseSet.Count - 1]; theFuzzySystem.RulesDatabaseSet.Remove(X[f]); mass[f] = Errors[f]; if (mass[f] > worst) { 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; } }
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; } } }
protected static void writeAboutEstimates(XmlWriter writer, SAFuzzySystem Approximate) { writer.WriteStartElement("Estimates"); if (Approximate.TestSamplesSet != null) { writer.WriteAttributeString("Count", XmlConvert.ToString(22)); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Table", Approximate.LearnSamplesSet.FileName); writer.WriteAttributeString("Type", "RMSE"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.approxLearnSamples(Approximate.RulesDatabaseSet[0]))); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Table", Approximate.LearnSamplesSet.FileName); writer.WriteAttributeString("Type", "MSE"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.RMSEtoMSEforLearn(Approximate.approxLearnSamples(Approximate.RulesDatabaseSet[0])))); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Table", Approximate.TestSamplesSet.FileName); writer.WriteAttributeString("Type", "RMSE"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.approxTestSamples(Approximate.RulesDatabaseSet[0]))); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Table", Approximate.TestSamplesSet.FileName); writer.WriteAttributeString("Type", "MSE"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.RMSEtoMSEforTest(Approximate.approxTestSamples(Approximate.RulesDatabaseSet[0])))); writer.WriteEndElement(); } else { writer.WriteAttributeString("Count", XmlConvert.ToString(20)); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Table", Approximate.LearnSamplesSet.FileName); writer.WriteAttributeString("Type", "RMSE"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.approxLearnSamples(Approximate.RulesDatabaseSet[0]))); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Table", Approximate.LearnSamplesSet.FileName); writer.WriteAttributeString("Type", "MSE"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.RMSEtoMSEforLearn(Approximate.approxLearnSamples(Approximate.RulesDatabaseSet[0])))); writer.WriteEndElement(); } writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "GIBNormal"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getGIBNormal())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "GIBSumStraigh"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getGIBSumStrait())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "GIBSumReverse"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getGIBSumReverse())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "GICNormal"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getGICNormal())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "GICSumStraigh"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getGICSumStraigth())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "GICSumReverse"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getGICSumReverce())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "GISNormal"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getGISNormal())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "GISSumStraigh"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getGISSumStraigt())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "GISSumReverce"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getGISSumReverce())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "LindisNormal"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getLindisNormal())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "LindisSumStraigh"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getLindisSumStraight())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "LindisSumReverse"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getLindisSumReverse())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "NormalIndex"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getNormalIndex())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "RealIndex"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getIndexReal())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "SumStraigthIndex"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getIndexSumStraigt())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "SumReverseIndex"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getIndexSumReverse())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "ComplexitIt"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getComplexit())); writer.WriteEndElement(); writer.WriteStartElement("Estimate"); writer.WriteAttributeString("Type", "CountRules"); writer.WriteAttributeString("Value", XmlConvert.ToString(Approximate.getRulesCount())); writer.WriteEndElement(); writer.WriteEndElement(); }