// situationに合うものをPopulationから取り、足りないときはCovering protected override void Covering( State S, Population P ) { while( this.CList.Count == 0 ) { // situationにあうものをPopulationから探す this.CList = P.MatchSituation( S ); // Actionの種類 List<char> Actions = new List<char>(); int NumberOfActions = 0; // Multiplexer(2進数) if( Configuration.Type == "Binary" ) { Actions.Add( '0' ); Actions.Add( '1' ); foreach( Classifier C in this.CList ) { Actions.Remove( C.A ); } NumberOfActions = 2 - Actions.Count; } // MatchSetにある行動が少ないとき if( NumberOfActions < Configuration.Theta_mna ) { // 一部を変化させたCondition State state = new BinaryState( S ); state.Covering(); Classifier CC; if( Configuration.ASName == "CS" || Configuration.ASName == "MaxCS" || Configuration.ASName == "Max" || Configuration.ASName == "Updatee0CS" ) { CC = new SigmaNormalClassifier( state, Actions, Configuration.ExpThreshold ); } else { CC = new NormalClassifier( state, Actions ); } P.Add( CC ); // 整理 P.Delete(); this.CList = new List<Classifier>(); } } }
public NormalMatchSet( State S, Population P ) { this.CList = new List<Classifier>(); this.Covering( S, P ); }
// 包摂 protected override void Subsumption( Population Pop ) { Classifier Cl = null; foreach( Classifier C in this.CList ) { if( C.CouldSubsume() ) { if( ( Cl == null ) || ( C.C.NumberOfSharp > Cl.C.NumberOfSharp ) || ( ( C.C.NumberOfSharp == Cl.C.NumberOfSharp ) && ( Configuration.MT.NextDouble() < 0.5 ) ) ) { Cl = C;//actionsetなかに最も一般的な分類子をClにする } } } if( Cl != null ) { // 削除中にforeachできない List<Classifier> CL = new List<Classifier>(); foreach( Classifier C in this.CList ) { if( Cl.IsMoreGeneral( C ) ) { SigmaNormalClassifier Snc_ko = (SigmaNormalClassifier)C; SigmaNormalClassifier Snc_oya = (SigmaNormalClassifier)Cl; //10の数字は適当 可変にするか? 1/10にする 10-7 あんまり変わらない if (Cl.Kappa == 1 && Math.Abs(Cl.Epsilon - C.Epsilon) < 10 && Snc_ko.IsConvergenceEpsilon() && Snc_oya.IsConvergenceEpsilon())//subsumption するやつの分散が小さい かつε収束時だけ subsumption { Cl.N += C.N; // 削除Classifier登録 CL.Add(C); } } } foreach( Classifier C in CL ) { SigmaNormalClassifier SNC = ( SigmaNormalClassifier )Cl; //Configuration.ESW.WriteLine( Configuration.T + "," + C.Exp + "," + Cl.C.state + "," + SNC.Epsilon_0 + "," + SNC.Epsilon + "," + SNC.Exp ); //if( C.C.state == "000###" | C.C.state == "001###" | C.C.state == "01#0##" | C.C.state == "01#1##" | C.C.state == "10##0#" | C.C.state == "10##1#" | C.C.state == "11###0" | C.C.state == "11###1" ) // Configuration.ESW.WriteLine(Configuration.T + "," + Cl.C.state + "," + C.C.state + "," + Cl.Exp + "," + Cl.Epsilon + "," + Cl.Epsilon_0 + "," + C.Exp + "," + C.Epsilon + "," + C.Epsilon_0 + "," + ++Configuration.Count); //this.Remove( C ); //Pop.Remove( C ); 9-7 chou subsumption した分類子見たい if (C.C.state == "1000##" | C.C.state == "0100##" | C.C.state == "0010##" | C.C.state == "0001##" | C.C.state == "1100##" | C.C.state == "0011##" | C.C.state == "1#00##" | C.C.state == "#100##" | C.C.state == "001###" | C.C.state == "00#1##"|C.C.state=="1000##"|C.C.state=="0100##"|C.C.state=="0010##" |C.C.state=="00#1##") { Configuration.ESW.WriteLine(Configuration.T + "," + Cl.C.state + "," +Cl.A+","+ C.C.state + ","+C.A+"," + Cl.Exp + "," + Cl.Epsilon + "," + Cl.Epsilon_0 + "," + C.Exp + "," + C.Epsilon + "," + C.Epsilon_0 + "," + ++Configuration.Count); } //if (C.C.state == "1#00##") //{ // SigmaNormalClassifier Snc = (SigmaNormalClassifier)C; // Configuration.Problem_1_00.WriteLine(Configuration.T + "," + Snc.C.state + "," + C.F + "," + C.Kappa + "," // + C.Epsilon + "," + C.Epsilon_0 + "," + C.N + "," + (Snc.IsConvergenceEpsilon() ? 1 : 0)); //} this.Remove(C); Pop.Remove(C); } } }
public override void RunGA( State Situation, Population P ) { double NumerositySum = 0.0; double TimeStampSum = 0.0; foreach( Classifier C in this.CList ) { NumerositySum += C.N; TimeStampSum += C.Ts * C.N; } if( Configuration.T - TimeStampSum / NumerositySum > Configuration.Theta_GA ) { foreach( Classifier C in this.CList ) { C.Ts = Configuration.T; } Classifier Parent_1 = this.SelectOffspring(); Classifier Parent_2 = this.SelectOffspring(); Classifier Child_1 = new SigmaNormalClassifier( ( SigmaNormalClassifier )Parent_1 ); Classifier Child_2 = new SigmaNormalClassifier( ( SigmaNormalClassifier )Parent_2 ); ///nakata added Child_1.F /= Child_1.N; Child_2.F /= Child_2.N; ////////////// Child_1.N = Child_2.N = 1; Child_1.Exp = Child_2.Exp = 0; Child_1.St = Child_2.St = 0; Child_1.M = Child_2.M = 0; Child_1.S = Child_2.S = 0; //Child_1.Epsilon_0 = Child_2.Epsilon_0 = Configuration.Epsilon_0; SigmaNormalClassifier SNP1 = ( SigmaNormalClassifier )Parent_1; SigmaNormalClassifier SNP2 = ( SigmaNormalClassifier )Parent_2; SigmaNormalClassifier SNC1 = ( SigmaNormalClassifier )Child_1; SigmaNormalClassifier SNC2 = ( SigmaNormalClassifier )Child_2; // 交叉 if( Configuration.MT.NextDouble() < Configuration.Chai ) { Child_1.Crossover( Child_2 ); Child_1.P = ( Parent_1.P + Parent_2.P ) / 2; //Child_1.Epsilon = Parent_1.Epsilon + Parent_2.Epsilon; Child_1.Epsilon = ( Parent_1.Epsilon + Parent_2.Epsilon ) / 2; Child_1.F = ( Parent_1.F + Parent_2.F ) / 2; //Child_1.M = ( Parent_1.M + Parent_2.M ) / 2; //Child_1.S = ( Parent_1.S + Parent_2.S ) / 2; Child_1.Epsilon_0 = ( Parent_1.Epsilon_0 + Parent_2.Epsilon_0 ) / 2; //Child_1.Epsilon_0 = Math.Min(Parent_1.Epsilon_0, Parent_2.Epsilon_0); Child_2.P = Child_1.P; Child_2.Epsilon = Child_1.Epsilon; Child_2.F = Child_1.F; //Child_2.M = Child_1.M; //Child_2.S = Child_1.S; Child_2.Epsilon_0 = Child_1.Epsilon_0; for( int i = 0; i < SNC1.WinningRate.Count(); i++ ) { SNC1.WinningRate[i] = SNC2.WinningRate[i] = ( SNP1.WinningRate[i] + SNP2.WinningRate[i] ) / 2; } } Child_1.F *= 0.1; Child_2.F *= 0.1; for( int i = 0; i < SNC1.WinningRate.Count(); i++ ) { SNC1.WinningRate[i] *= 0.1; SNC2.WinningRate[i] *= 0.1; } // bothChild Child_1.Mutation( Situation ); Child_2.Mutation( Situation ); if( Configuration.DoGASubsumption ) { if( Parent_1.DoesSubsume( Child_1 ) ) { Parent_1.N++; } else if( Parent_2.DoesSubsume( Child_1 ) ) { Parent_2.N++; } else { P.Insert( Child_1 ); } P.Delete(); if( Parent_1.DoesSubsume( Child_2 ) ) { Parent_1.N++; } else if( Parent_2.DoesSubsume( Child_2 ) ) { Parent_2.N++; } else { P.Insert( Child_2 ); } P.Delete(); } else { P.Insert( Child_1 ); P.Delete(); P.Insert( Child_2 ); P.Delete(); } } }
public override void Update( Population Pop, double P, StdList Sigma ) { double SumNumerosity = 0; foreach( Classifier C in this.CList ) { SumNumerosity += C.N; } //if( Configuration.URE_Epsilon0 != Configuration.Rho ) //{ // Configuration.URE_Epsilon0 += Configuration.Epsilon_0 * 5; // foreach( Classifier C in this.CList ) // { // C.Epsilon_0 += Configuration.Beta * ( Configuration.URE_Epsilon0 - C.Epsilon_0 ); // } //} //else //{ //if( SNC.IsConvergenceEpsilon() && Sigma.IsConvergenceSigma() ) //{ // foreach( Classifier C in this.CList ) // { // } //} //} foreach( Classifier C in this.CList ) { C.Exp++; if( C.Exp < 1 /Configuration.Beta ) { C.P += ( P - C.P ) / C.Exp; } else { C.P += Configuration.Beta * ( P - C.P ); } //if( C.Exp < Configuration.Beta ) // 9-23 張 ここscript は exp < 1/beta if (C.Exp < Configuration.Beta) { C.As += ( SumNumerosity - C.As ) / C.Exp; } else { C.As += Configuration.Beta * ( SumNumerosity - C.As ); } if( C.C.state == "11###0" && C.A == '1' ) { //Configuration.ESW.WriteLine( Configuration.T + "," + C.F + "," + C.Kappa + "," + C.Epsilon_0 + "," + C.Epsilon + "," + C.Exp + "," + Math.Sqrt( C.S / ( C.St - 1 ) ) ); //Console.WriteLine( Configuration.T + " " + C.Exp ); } // 標準偏差計算 C.St++; double X = P - C.M; C.M += X / C.St; C.S += ( C.St - 1 ) * X * X / C.St; if( C.GetType().Name == "SigmaNormalClassifier" ) { // このIterationまでのepsilonを記録ずらし SigmaNormalClassifier SNC = ( SigmaNormalClassifier )C; for( int index = SNC.EpsilonList.Count() - 1; index > 0; index-- ) { SNC.EpsilonList[index] = SNC.EpsilonList[index - 1]; } SNC.EpsilonList[0] = Math.Sqrt(C.S / (C.St - 1)); //bool StateFlag = true; //foreach(StdList SL in Configuration.Stdlist) //{ // // 分類子に含まれるstate // //if(SL.IsIncluded(SNC.C.state, SNC.A)) // //{ // // 一つでも収束していなければ // if(!SL.IsConvergenceSigma()) // { // StateFlag = false; // } // //} //} //if( SNC.IsConvergenceEpsilon() ) //if( SNC.IsConvergenceEpsilon() && StateFlag ) if( Sigma.IsConvergenceSigma() ) { double sigma_a=0; double sigma_b=0; //9-24 張 1#00 算出する時用のもの追加 if(C.C.state=="1#00##") { //1100 と 1000 のsigma で1#00を更新 foreach(StdList SL in Configuration.Stdlist) { if(SL.C=="110000"){ sigma_a =SL.S;} if(SL.C=="100000"){ sigma_b =SL.S;} } //sigma はvariancetableから C.Epsilon_0 += Configuration.Beta * (Math.Sqrt(sigma_a*sigma_a +sigma_b*sigma_b) + Configuration.Epsilon_0 - C.Epsilon_0); } if (C.C.state == "#100##") { //1100 と 1000 のsigma で1#00を更新 foreach (StdList SL in Configuration.Stdlist) { if (SL.C == "110000") { sigma_a = SL.S; } if (SL.C == "010000") { sigma_b = SL.S; } } //sigma はvariancetableから C.Epsilon_0 += Configuration.Beta * (Math.Sqrt(sigma_a * sigma_a + sigma_b * sigma_b) + Configuration.Epsilon_0 - C.Epsilon_0); } if (C.C.state == "001###") { //1100 と 1000 のsigma で1#00を更新 foreach (StdList SL in Configuration.Stdlist) { if (SL.C == "001100") { sigma_a = SL.S; } if (SL.C == "001000") { sigma_b = SL.S; } } //sigma はvariancetableから C.Epsilon_0 += Configuration.Beta * (Math.Sqrt(sigma_a * sigma_a + sigma_b * sigma_b) + Configuration.Epsilon_0 - C.Epsilon_0); } if (C.C.state == "00#1##") { //1100 と 1000 のsigma で1#00を更新 foreach (StdList SL in Configuration.Stdlist) { if (SL.C == "001100") { sigma_a = SL.S; } if (SL.C == "000100") { sigma_b = SL.S; } } //sigma はvariancetableから C.Epsilon_0 += Configuration.Beta * (Math.Sqrt(sigma_a * sigma_a + sigma_b * sigma_b) + Configuration.Epsilon_0 - C.Epsilon_0); } else { C.Epsilon_0 += Configuration.Beta * ( Sigma.S + Configuration.Epsilon_0 - C.Epsilon_0 ); } } if( SNC.IsConvergenceEpsilon() ) //if( SNC.IsConvergenceEpsilon() && StateFlag ) //if(SNC.Exp > 1) //if(SNC.IsConvergenceEpsilon() && Sigma.IsConvergenceSigma()) { Configuration.FlagSigma = true; SNC.Epsilon = SNC.EpsilonList[0]; //SNC.Epsilon += Configuration.Beta * ( SNC.EpsilonList[0] - SNC.Epsilon ); //if( C.Exp < Configuration.Beta ) //{ // C.Epsilon += ( Math.Abs( P - C.P ) - C.Epsilon ) / C.Exp; //} //else //{ // C.Epsilon += Configuration.Beta * ( Math.Abs( P - C.P ) - C.Epsilon ); //} //C.Epsilon_0 += Configuration.Beta * ( Sigma.S + Configuration.Epsilon_0 - C.Epsilon_0 ); //C.Epsilon_0 = Sigma.S + Configuration.Epsilon_0; //C.Epsilon_0 = Sigma.S; } else { //if (C.Exp < Configuration.Beta)//9-23 張 ここscript は exp<Beta if (C.Exp < Configuration.Beta) { C.Epsilon += ( Math.Abs( P - C.P ) - C.Epsilon ) / C.Exp; } else { C.Epsilon += Configuration.Beta * ( Math.Abs( P - C.P ) - C.Epsilon ); } } } else //SigmaNormalClassifier ではない場合 { //if( C.Exp < Configuration.Beta )//9-23 張 ここscript は exp<Beta if (C.Exp < Configuration.Beta) { C.Epsilon += ( Math.Abs( P - C.P ) - C.Epsilon ) / C.Exp; } else { C.Epsilon += Configuration.Beta * ( Math.Abs( P - C.P ) - C.Epsilon ); } } //if(C.St > 250) //{ //C.Epsilon_0 += Configuration.Beta * (Configuration.Epsilon_0 + Math.Sqrt( C.S / ( C.St - 1 ) ) - C.Epsilon_0); //Console.WriteLine(C.Epsilon_0); //Console.WriteLine("st" + C.St); //} //if( Configuration.T > 5000 ) //if(Configuration.IsConvergenceSigma()) } this.UpdateFitness(); if( Configuration.DoActionSetSubsumption ) { this.Subsumption( Pop ); } }
/// <summary> /// Subsumpiton /// </summary> /// <param name="Pop">Population</param> protected abstract void Subsumption( Population Pop );
/// <summary> /// Fitness更新 /// </summary> /// <param name="Pop">Population</param> /// <param name="P">報酬の値</param> public abstract void Update( Population Pop, double P, StdList Sigma );
protected override void Subsumption( Population Pop ) { Classifier Cl = null; foreach( Classifier C in this.CList ) { if( C.CouldSubsume() ) { if( ( Cl == null ) || ( C.C.NumberOfSharp > Cl.C.NumberOfSharp ) || ( ( C.C.NumberOfSharp == Cl.C.NumberOfSharp ) && ( Configuration.MT.NextDouble() < 0.5 ) ) ) { Cl = C; } } } if( Cl != null ) { // 削除中にforeachできない List<Classifier> CL = new List<Classifier>(); foreach( Classifier C in this.CList ) { if( Cl.IsMoreGeneral( C ) ) { Cl.N += C.N; // 削除Classifier登録 CL.Add( C ); } } foreach( Classifier C in CL ) { if(C.C.state=="1#00##") { SigmaNormalClassifier SNC = (SigmaNormalClassifier)C; Configuration.Problem_1_00.WriteLine(Configuration.T + "," + SNC.C.state + "," + C.F + "," + C.Kappa + "," + C.Epsilon + "," + C.Epsilon_0 + "," + C.N + "," + (SNC.IsConvergenceEpsilon() ? 1 : 0)); } this.Remove( C ); Pop.Remove( C ); } } }
// situationに合うものをPopulationから取り、足りないときはCovering protected override void Covering( State S, Population P ) { }
public NotCoveringMatchSet( State S, Population P ) { this.CList = new List<Classifier>(); this.CList = P.MatchSituation( S ); }
/// <summary> /// 評価 /// </summary> /// <param name="Env">環境</param> /// <param name="P">Population</param> /// <returns>performance</returns> public static double Exploit( Population P ) { // situation取得 State S = Configuration.ExploitEnv.GetState(); // Matchset生成 MatchSet M = new NotCoveringMatchSet( S, P ); // PredictionArray生成 PredictionArray PA = new GreedyPredictionArray( M ); // Action決定 char Action = PA.SelectAction(); // 報酬獲得 double Rho = Configuration.ExploitEnv.ActionExploit( Action ); //if( Rho == 0 && Configuration.T > 35530 ) //{ // Console.WriteLine( Configuration.T + " : " + S.state + " : " + Action + " : " + Rho ); // foreach(double d in PA.PA) // { // Console.WriteLine(d); // } // System.Threading.Thread.Sleep( 1000 ); //} if(Action == '0') { Configuration.ZeroList.Add( Rho ); } else if(Action == '1') { Configuration.OneList.Add( Rho ); } return Rho; }
public static void Experiment( Environment Env, Population P ) { // 変数初期化 Configuration.T = 0; ActionSet PreviousAS = null; double PreviousRho = 0; State PreviousS = null; // stdlist収束 bool ConvergenceStelist = false; // 移動平均計算用 double[] RhoArray = new double[Configuration.Iteration]; int[] Num = new int[Configuration.Iteration]; //double[] Std = new double[Configuration.Iteration]; Configuration.ZeroList = new List<double>(); Configuration.OneList = new List<double>(); // 提案手法 Condition毎の分散 Configuration.Stdlist = new StdList[( int )Math.Pow( 2, Configuration.L ) * 2]; for( int i = 0; i < ( int )Math.Pow( 2, Configuration.L ); i++ ) { Configuration.Stdlist[i * 2] = new StdList( i, '0' ); Configuration.Stdlist[i * 2 + 1] = new StdList( i, '1' ); } //StreamWriter LD = new StreamWriter( "./LearningData_" + Configuration.Seed + "CnoiseWidth_" + Configuration.NoiseWidth // + "AS_" + Configuration.ASName + "ET_" + Configuration.ExpThreshold + "DS_" + Configuration.DifferenceSigma + "LS_" + Configuration.LookBackSigma // + "DE_" + Configuration.DifferenceEpsilon + "LE_" + Configuration.LookBackEpsilon + ".csv", true, System.Text.Encoding.GetEncoding( "shift_jis" ) ); //LD.WriteLine( "T,state,Action,Rho,ActionExploit" ); // 実験1a ノイズを既知のものとして扱う if(Configuration.ASName == "WellKnown") { Configuration.Epsilon_0 += Configuration.NoiseWidth; } // メインループ #region main roop while ( Configuration.T < Configuration.Iteration ) { if (Configuration.T == 40269) { Console.WriteLine("stoped"); Console.ReadLine(); } //Console.WriteLine( Configuration.T + " : " + P.CountNumerosity() + " : " + P.CList.Count() ); // situation取得 State S = Env.GetState(); // MatchSet生成 MatchSet M = new NormalMatchSet( S, P ); // PredictionArray取得 PredictionArray PA = new EpsilonGreedyPredictionArray( M ); // Action決定 char Action = PA.SelectAction(); // ActionSet選択 ActionSet AS; if( Configuration.ASName == "CS" ) { AS = new ConditionSigmaActionSet( M.MatchAction( Action ) ); } //else if( Configuration.ASName == "MaxCS" || Configuration.ASName == "Max" ) //{ // AS = new MaxConditionSigmaActionSet( M.MatchAction( Action ) ); //} else if( Configuration.ASName == "WellKnown" ) // 実験1a { AS = new NormalActionSet( M.MatchAction( Action ) ); } else if( Configuration.ASName == "Updatee0CS" ) // 実験1b { AS = new UpdateEpsilon_0_ConditionSigmaActionSet( M.MatchAction( Action ) ); } else if(Configuration.ASName == "Tornament") { AS = new TornamentSelectionActionSet( M.MatchAction( Action ) ); } else { AS = new NormalActionSet( M.MatchAction( Action ) ); } // EnvにActionし、報酬獲得 double Rho = Env.ExecuteAction(Action); //if ( == "1#00##" ) //{ // Configuration.Problem.WriteLine(S.state + "," + Action + "," + Configuration.T + "," + Rho); //} StdList Sigma = null; // 提案手法 分散の計算 foreach( StdList SL in Configuration.Stdlist ) { if( ( SL.C == S.state ) && ( SL.A == Action ) ) { // situationの分散取得 SL.Update( Rho ); Sigma = SL; } } // 提案手法(中田) if( Configuration.ASName == "CS" ) { Configuration.URE_Epsilon0 = -1; // 最小値 double d = Configuration.Rho; foreach( SigmaNormalClassifier C in AS.CList ) { if( d > C.S && C.IsConvergenceEpsilon() ) { d = Math.Sqrt( C.S / ( C.St - 1 ) ); } } Configuration.URE_Epsilon0 = d; } //if (Configuration.T > 18936&&Configuration.T<18938) //{ // foreach (Classifier C in AS.CList) // { // if (C.C.state == "1#00##" ) // { // Console.WriteLine(Configuration.T + " : " + S.state + " : " + Action + " : " + Rho + " : " + C.Exp); // } // } //} // 学習データ記録 //LD.WriteLine( Configuration.T + "," + S.state + "," + Action + "," + Rho + "," + Env.ActionExploit( Action ) ); // マルチステップ問題 if( PreviousAS != null ) { double p = PreviousRho + Configuration.Gamma * PA.MAX(); PreviousAS.Update( P, p, Sigma ); PreviousAS.RunGA( PreviousS, P ); //komine } // マルチステップ問題の終着またはシングルステップ問題 if( Env.Eop ) { double p = Rho; AS.Update( P, p, Sigma ); AS.RunGA( S, P );//komine PreviousAS = null; } else { PreviousAS = AS; PreviousRho = Rho; PreviousS = S; } //if(Configuration.ASName == "CS") //{ // SigmaNormalPopulation SP = ( SigmaNormalPopulation )P; // SP.RoundRobin(); //} // accuracyが一定以上のfitnessの平均値 //double SumFitness = 0; //int NumberOfClassifiers = 0; //foreach( Classifier C in P.CList ) //{ // //SumFitness += C.F; // //NumberOfClassifiers++; // if( C.C.state == "11####" && C.A == '0' ) // Configuration.ESW.WriteLine( Configuration.T + "," + C.Epsilon ); //} foreach(StdList C in Configuration.Stdlist) { //if( C.C == "111111" && C.A == '0' ) //Configuration.ESW.WriteLine( Configuration.T + "," + C.S ); } //Configuration.ESW.WriteLine( Configuration.T + "," + SumFitness + "," + NumberOfClassifiers + "," + SumFitness / NumberOfClassifiers); RhoArray[Configuration.T] = Exploit( P ); //Num[Configuration.T] = P.CountNumerosity(); Num[Configuration.T] = P.CList.Count(); //Std[Configuration.T] = Math.Sqrt( Stdlist[20].Sigma / (Stdlist[20].T - 1)); if( Configuration.StartTime < 0 && Configuration.FlagSigma && Configuration.FlagEpsilon ) { Configuration.StartTime = Configuration.T; } Configuration.FlagSigma = Configuration.FlagEpsilon = false; //if( ( Configuration.T < 20001 && Configuration.T % 1000 == 0 ) || Configuration.T % 5000 == 0 ) //{ // P.Show(); //} //if( Configuration.T % 1000 == 0 && Configuration.T < 20001 && ( Configuration.ASName == "CS" || Configuration.ASName == "MaxCS" || Configuration.ASName == "Max" || Configuration.ASName == "Updatee0CS" ) ) //{ // StreamWriter stdSw = new StreamWriter( "./VarianceTable_" + Configuration.T + "_" + Configuration.Seed + "CnoiseWidth" + Configuration.NoiseWidth //+ "AS_" + Configuration.ASName + "ET_" + Configuration.ExpThreshold + "DS_" + Configuration.DifferenceSigma + "LS_" + Configuration.LookBackSigma //+ "DE_" + Configuration.DifferenceEpsilon + "LE_" + Configuration.LookBackEpsilon + ".csv", true, System.Text.Encoding.GetEncoding( "shift_jis" ) ); // stdSw.WriteLine( "condition,action,sigma,time,convergence" ); // foreach( StdList SL in Configuration.Stdlist ) // { // stdSw.WriteLine( SL.C + "," + SL.A + "," + SL.S + "," + SL.T + "," + ( SL.IsConvergenceSigma() ? 1 : 0 ) ); // 1 : 収束 // } // stdSw.Close(); //} if( !ConvergenceStelist && ( Configuration.ASName == "CS" || Configuration.ASName == "MaxCS" || Configuration.ASName == "Max" || Configuration.ASName == "Updatee0CS" ) ) { int i = 1; foreach( StdList SL in Configuration.Stdlist ) { i *= ( SL.IsConvergenceSigma() ? 1 : 0 ); } if( i == 1 ) { StreamWriter stdSw = new StreamWriter( "./ConvergenceVT_" + Configuration.T + "_" + Configuration.Seed + "CnoiseWidth" + Configuration.NoiseWidth + "AS_" + Configuration.ASName + "ET_" + Configuration.ExpThreshold + "DS_" + Configuration.DifferenceSigma + "LS_" + Configuration.LookBackSigma + "DE_" + Configuration.DifferenceEpsilon + "LE_" + Configuration.LookBackEpsilon + ".csv", true, System.Text.Encoding.GetEncoding( "shift_jis" ) ); stdSw.WriteLine( "condition,action,sigma,time,convergence" ); foreach( StdList SL in Configuration.Stdlist ) { stdSw.WriteLine( SL.C + "," + SL.A + "," + SL.S + "," + SL.T + "," + ( SL.IsConvergenceSigma() ? 1 : 0 ) ); // 1 : 収束 } stdSw.Close(); ConvergenceStelist = true; } } //if (Configuration.T > 14896& Configuration.T <14899) //{ // P.Show(); // M.Show(); // AS.Show();//9-7 chou ループたびのpopulation 見たい //} foreach (Classifier C in P.CList) { if (C.GetType().Name == "SigmaNormalClassifier") { if (C.C.state == "1000##") { SigmaNormalClassifier SNC = (SigmaNormalClassifier)C; Configuration.Problem_1000.WriteLine(Configuration.T + "," + SNC.C.state + ","+SNC.A+"," + C.F + "," + C.Kappa + "," + C.Epsilon + "," + C.Epsilon_0 + "," + C.N + "," + (SNC.IsConvergenceEpsilon() ? 1 : 0)); } if (C.C.state == "0100##") { SigmaNormalClassifier SNC = (SigmaNormalClassifier)C; Configuration.Problem_0100.WriteLine(Configuration.T + "," + SNC.C.state + ","+SNC.A +"," + C.F + "," + C.Kappa + "," + C.Epsilon + "," + C.Epsilon_0 + "," + C.N + "," + (SNC.IsConvergenceEpsilon() ? 1 : 0)); } if (C.C.state == "0010##") { SigmaNormalClassifier SNC = (SigmaNormalClassifier)C; Configuration.Problem_0010.WriteLine(Configuration.T + "," + SNC.C.state + "," + SNC.A + "," + C.F + "," + C.Kappa + "," + C.Epsilon + "," + C.Epsilon_0 + "," + C.N + "," + (SNC.IsConvergenceEpsilon() ? 1 : 0)); } if (C.C.state == "0001##") { SigmaNormalClassifier SNC = (SigmaNormalClassifier)C; Configuration.Problem_0001.WriteLine(Configuration.T + "," + SNC.C.state + "," + SNC.A + "," + C.F + "," + C.Kappa + "," + C.Epsilon + "," + C.Epsilon_0 + "," + C.N + "," + (SNC.IsConvergenceEpsilon() ? 1 : 0)); } if (C.C.state == "1#00##") { SigmaNormalClassifier SNC = (SigmaNormalClassifier)C; Configuration.Problem__100.WriteLine(Configuration.T + "," + SNC.C.state + "," + C.A + "," + C.F + "," + C.Kappa + "," + C.Epsilon + "," + C.Epsilon_0 + "," + C.N + "," + (SNC.IsConvergenceEpsilon() ? 1 : 0)); } if (C.C.state == "#100##") { SigmaNormalClassifier SNC = (SigmaNormalClassifier)C; Configuration.Problem__100.WriteLine(Configuration.T + "," + SNC.C.state + "," + C.A + "," + C.F + "," + C.Kappa + "," + C.Epsilon + "," + C.Epsilon_0 + "," + C.N + "," + (SNC.IsConvergenceEpsilon() ? 1 : 0)); } if (C.C.state == "001###") { SigmaNormalClassifier SNC = (SigmaNormalClassifier)C; Configuration.Problem__100.WriteLine(Configuration.T + "," + SNC.C.state + "," + C.A + "," + C.F + "," + C.Kappa + "," + C.Epsilon + "," + C.Epsilon_0 + "," + C.N + "," + (SNC.IsConvergenceEpsilon() ? 1 : 0)); } if (C.C.state == "00#1##") { SigmaNormalClassifier SNC = (SigmaNormalClassifier)C; Configuration.Problem__100.WriteLine(Configuration.T + "," + SNC.C.state + "," + C.A + "," + C.F + "," + C.Kappa + "," + C.Epsilon + "," + C.Epsilon_0 + "," + C.N + "," + (SNC.IsConvergenceEpsilon() ? 1 : 0)); } } } //if (Configuration.T > 19835 && Configuration.T < 19840) //{ // P.Show(); //} //if (Configuration.T > 7200 && Configuration.T < 7300) //{ // P.Show(); //} //if (Configuration.T > 7700 && Configuration.T < 7850) //{ // P.Show(); //} //if (Configuration.T > 8400 && Configuration.T < 8600) //{ // P.Show(); //} //if (Configuration.T >11400 && Configuration.T < 11500) //{ // P.Show(); //} //if (Configuration.T > 14200 && Configuration.T < 14300) //{ // P.Show(); //} Configuration.T++; //if (Configuration.T > 19996) //{ // Console.Read(); //} } #endregion Configuration.Problem_1_00.Close(); Configuration.Problem__100.Close(); Configuration.Problem_1000.Close(); Configuration.Problem_0100.Close(); Configuration.Problem_0010.Close(); Configuration.Problem_0001.Close(); P.Show(); if( ( Configuration.ASName == "CS" || Configuration.ASName == "MaxCS" || Configuration.ASName == "Max" || Configuration.ASName == "Updatee0CS" ) ) { StreamWriter stdSw = new StreamWriter( "./VarianceTable_" + Configuration.T + "_" + Configuration.Seed + "CnoiseWidth" + Configuration.NoiseWidth + "AS_" + Configuration.ASName + "ET_" + Configuration.ExpThreshold + "DS_" + Configuration.DifferenceSigma + "LS_" + Configuration.LookBackSigma + "DE_" + Configuration.DifferenceEpsilon + "LE_" + Configuration.LookBackEpsilon + ".csv", true, System.Text.Encoding.GetEncoding( "shift_jis" ) ); stdSw.WriteLine( "condition,action,sigma,time,convergence,convergencetime" ); foreach( StdList SL in Configuration.Stdlist ) { stdSw.WriteLine( SL.C + "," + SL.A + "," + SL.S + "," + SL.T + "," + ( SL.IsConvergenceSigma() ? 1 : 0 ) + "," + SL.ConvergenceTime ); // 1 : 収束 } stdSw.Close(); } //LD.Close(); StreamWriter sw = new StreamWriter( "./performance_" + Configuration.Seed + "CnoiseWidth_" + Configuration.NoiseWidth + "AS_" + Configuration.ASName + "ET_" + Configuration.ExpThreshold + "DS_" + Configuration.DifferenceSigma + "LS_" + Configuration.LookBackSigma + "DE_" + Configuration.DifferenceEpsilon + "LE_" + Configuration.LookBackEpsilon + ".csv", true, System.Text.Encoding.GetEncoding( "shift_jis" ) ); //StreamWriter sw = new StreamWriter( "./performance_" + Configuration.Seed + "CnoiseWidth_" + Configuration.NoiseWidth // + "AS_" + "CS" + "ET_" + Configuration.ExpThreshold + "DS_" + Configuration.DifferenceSigma + "LS_" + Configuration.LookBackSigma // + "DE_" + Configuration.DifferenceEpsilon + "LE_" + Configuration.LookBackEpsilon + ".csv", true, System.Text.Encoding.GetEncoding( "shift_jis" ) ); sw.WriteLine( "Performance,PopulationSize," + Configuration.StartTime ); for( int i = 0; i < RhoArray.Count() - Configuration.SMA; i++ ) { double R = 0; double N = 0; for( int j = 0; j < Configuration.SMA; j++ ) { R += RhoArray[i + j]; N += Num[i + j]; } R /= Configuration.SMA; N /= Configuration.SMA; sw.WriteLine( R + "," + N ); } sw.Close(); StreamWriter Zerosw = new StreamWriter( "./Zero_per_" + Configuration.Seed + "CnoiseWidth_" + Configuration.NoiseWidth + "AS_" + Configuration.ASName + "ET_" + Configuration.ExpThreshold + "DS_" + Configuration.DifferenceSigma + "LS_" + Configuration.LookBackSigma + "DE_" + Configuration.DifferenceEpsilon + "LE_" + Configuration.LookBackEpsilon + ".csv", true, System.Text.Encoding.GetEncoding( "shift_jis" ) ); //StreamWriter sw = new StreamWriter( "./performance_" + Configuration.Seed + "CnoiseWidth_" + Configuration.NoiseWidth // + "AS_" + "CS" + "ET_" + Configuration.ExpThreshold + "DS_" + Configuration.DifferenceSigma + "LS_" + Configuration.LookBackSigma // + "DE_" + Configuration.DifferenceEpsilon + "LE_" + Configuration.LookBackEpsilon + ".csv", true, System.Text.Encoding.GetEncoding( "shift_jis" ) ); Zerosw.WriteLine( "Performance,dummy," + Configuration.StartTime ); for( int i = 0; i < Configuration.ZeroList.Count() - Configuration.SMA; i++ ) { double R = 0; double N = 0; for( int j = 0; j < Configuration.SMA; j++ ) { R += Configuration.ZeroList[i + j]; N += Num[i + j]; } R /= Configuration.SMA; N /= Configuration.SMA; Zerosw.WriteLine( R + "," + N ); } Zerosw.Close(); StreamWriter Onesw = new StreamWriter( "./One_per_" + Configuration.Seed + "CnoiseWidth_" + Configuration.NoiseWidth + "AS_" + Configuration.ASName + "ET_" + Configuration.ExpThreshold + "DS_" + Configuration.DifferenceSigma + "LS_" + Configuration.LookBackSigma + "DE_" + Configuration.DifferenceEpsilon + "LE_" + Configuration.LookBackEpsilon + ".csv", true, System.Text.Encoding.GetEncoding( "shift_jis" ) ); //StreamWriter sw = new StreamWriter( "./performance_" + Configuration.Seed + "CnoiseWidth_" + Configuration.NoiseWidth // + "AS_" + "CS" + "ET_" + Configuration.ExpThreshold + "DS_" + Configuration.DifferenceSigma + "LS_" + Configuration.LookBackSigma // + "DE_" + Configuration.DifferenceEpsilon + "LE_" + Configuration.LookBackEpsilon + ".csv", true, System.Text.Encoding.GetEncoding( "shift_jis" ) ); Onesw.WriteLine( "Performance,dummy," + Configuration.StartTime ); for( int i = 0; i < Configuration.OneList.Count() - Configuration.SMA; i++ ) { double R = 0; double N = 0; for( int j = 0; j < Configuration.SMA; j++ ) { R += Configuration.OneList[i + j]; N += Num[i + j]; } R /= Configuration.SMA; N /= Configuration.SMA; Onesw.WriteLine( R + "," + N ); } Onesw.Close(); foreach( StdList SL in Configuration.Stdlist ) { //Console.WriteLine( SL.C + " " + SL.A + " " + SL.T + " " + SL.M + " " + SL.S ); } //StreamWriter sow = new StreamWriter( "./sigma_" + Configuration.Seed + "CnoiseWidth_" + Configuration.NoiseWidth + "e0_" + Configuration.Epsilon_0 + ".csv", true, System.Text.Encoding.GetEncoding( "shift_jis" ) ); //foreach( double d in Std ) //{ // sow.WriteLine( d ); //} //sow.Close(); Configuration.ESW.Close(); //Configuration.Problem.Close(); System.IO.Directory.SetCurrentDirectory( "../"); StreamWriter swP = new StreamWriter( "PPP.csv", true, System.Text.Encoding.GetEncoding( "shift_jis" ) ); swP.WriteLine( Configuration.NoiseWidth + "," + Configuration.ASName + "," + P.CList.Count() ); swP.Close(); }
// 包摂 protected override void Subsumption( Population Pop ) { Classifier Cl = null; foreach( Classifier C in this.CList ) { if( C.CouldSubsume() ) { if( ( Cl == null ) || ( C.C.NumberOfSharp > Cl.C.NumberOfSharp ) || ( ( C.C.NumberOfSharp == Cl.C.NumberOfSharp ) && ( Configuration.MT.NextDouble() < 0.5 ) ) ) { Cl = C; } } } if( Cl != null ) { // 削除中にforeachできない List<Classifier> CL = new List<Classifier>(); foreach( Classifier C in this.CList ) { if( Cl.IsMoreGeneral( C ) ) { Cl.N += C.N; // 削除Classifier登録 CL.Add( C ); } } foreach( Classifier C in CL ) { this.Remove( C ); Pop.Remove( C ); } } }
public override void Update( Population Pop, double P, StdList Sigma ) { double SumNumerosity = 0; foreach( Classifier C in this.CList ) { SumNumerosity += C.N; } //if( Configuration.URE_Epsilon0 != Configuration.Rho ) //{ // Configuration.URE_Epsilon0 += Configuration.Epsilon_0 * 5; // foreach( Classifier C in this.CList ) // { // C.Epsilon_0 += Configuration.Beta * ( Configuration.URE_Epsilon0 - C.Epsilon_0 ); // } //} //else //{ //if( SNC.IsConvergenceEpsilon() && Sigma.IsConvergenceSigma() ) //{ // foreach( Classifier C in this.CList ) // { // } //} //} foreach( Classifier C in this.CList ) { C.Exp++; if( C.Exp < 1 / Configuration.Beta ) { C.P += ( P - C.P ) / C.Exp; } else { C.P += Configuration.Beta * ( P - C.P ); } if( C.Exp < Configuration.Beta ) { C.As += ( SumNumerosity - C.As ) / C.Exp; } else { C.As += Configuration.Beta * ( SumNumerosity - C.As ); } //if( C.C.state == "0000#######" && C.A == '0' ) //{ // Configuration.ESW.WriteLine( Configuration.T + "," + C.F + "," + C.Kappa + "," + C.Epsilon_0 + "," + C.Epsilon + "," + C.Exp + "," + Math.Sqrt( C.S / ( C.St - 1 ) ) ); // Console.WriteLine( Configuration.T + " " + C.Exp ); //} //if( C.C.state == "10##0#" && C.A == '0' ) //{ // Configuration.ESW.WriteLine( Configuration.T + "," + C.F + "," + C.Kappa + "," + C.Epsilon_0 + "," + C.Epsilon + "," + C.Exp + "," + Math.Sqrt( C.S / ( C.St - 1 ) ) ); // //Console.WriteLine( Configuration.T + " " + this.Exp ); //} // 標準偏差計算 C.St++; double X = P - C.M; C.M += X / C.St; C.S += ( C.St - 1 ) * X * X / C.St; if( C.GetType().Name == "SigmaNormalClassifier" ) { // このIterationまでのepsilonを記録ずらし SigmaNormalClassifier SNC = ( SigmaNormalClassifier )C; for( int index = SNC.EpsilonList.Count() - 1; index > 0; index-- ) { SNC.EpsilonList[index] = SNC.EpsilonList[index - 1]; } SNC.EpsilonList[0] = Math.Sqrt( C.S / ( C.St - 1 ) ); //bool StateFlag = true; //foreach(StdList SL in Configuration.Stdlist) //{ // // 分類子に含まれるstate // //if(SL.IsIncluded(SNC.C.state, SNC.A)) // //{ // // 一つでも収束していなければ // if(!SL.IsConvergenceSigma()) // { // StateFlag = false; // } // //} //} //if( SNC.IsConvergenceEpsilon() ) //if( SNC.IsConvergenceEpsilon() && StateFlag ) if( Sigma.IsConvergenceSigma() ) { C.Epsilon_0 += Configuration.Beta * ( Sigma.S + Configuration.Epsilon_0 - C.Epsilon_0 ); } if( C.Exp < Configuration.Beta ) { C.Epsilon += ( Math.Abs( P - C.P ) - C.Epsilon ) / C.Exp; } else { C.Epsilon += Configuration.Beta * ( Math.Abs( P - C.P ) - C.Epsilon ); } } else { if( C.Exp < Configuration.Beta ) { C.Epsilon += ( Math.Abs( P - C.P ) - C.Epsilon ) / C.Exp; } else { C.Epsilon += Configuration.Beta * ( Math.Abs( P - C.P ) - C.Epsilon ); } } //if(C.St > 250) //{ //C.Epsilon_0 += Configuration.Beta * (Configuration.Epsilon_0 + Math.Sqrt( C.S / ( C.St - 1 ) ) - C.Epsilon_0); //Console.WriteLine(C.Epsilon_0); //Console.WriteLine("st" + C.St); //} //if( Configuration.T > 5000 ) //if(Configuration.IsConvergenceSigma()) } this.UpdateFitness(); if( Configuration.DoActionSetSubsumption ) { this.Subsumption( Pop ); } }
public override void RunGA( State Situation, Population P ) { double NumerositySum = 0.0; double TimeStampSum = 0.0; foreach( Classifier C in this.CList ) { NumerositySum += C.N; TimeStampSum += C.Ts * C.N; } if( Configuration.T - TimeStampSum / NumerositySum > Configuration.Theta_GA ) { foreach( Classifier C in this.CList ) { C.Ts = Configuration.T; } Classifier Parent_1 = this.SelectOffspring(); Classifier Parent_2 = this.SelectOffspring(); Classifier Child_1 = new NormalClassifier( ( NormalClassifier )Parent_1 ); Classifier Child_2 = new NormalClassifier( ( NormalClassifier )Parent_2 ); Child_1.N = Child_2.N = 1; Child_1.Exp = Child_2.Exp = 0; Child_1.St = Child_2.St = 0; Child_1.M = Child_2.M = 0; Child_1.S = Child_2.S = 0; if( Configuration.MT.NextDouble() < Configuration.Chai ) { Child_1.Crossover( Child_2 ); Child_1.P = ( Parent_1.P + Parent_2.P ) / 2; Child_1.Epsilon = ( Parent_1.Epsilon + Parent_2.Epsilon ) / 2; Child_1.F = ( Parent_1.F + Parent_2.F ) / 2; //Child_1.M = ( Parent_1.M + Parent_2.M ) / 2; //Child_1.S = ( Parent_1.S + Parent_2.S ) / 2; Child_2.P = Child_1.P; Child_2.Epsilon = Child_1.Epsilon; Child_2.F = Child_1.F; //Child_2.M = Child_1.M; //Child_2.S = Child_1.S; } Child_1.F *= 0.1; Child_2.F *= 0.1; // bothChild Child_1.Mutation( Situation ); Child_2.Mutation( Situation ); if( Configuration.DoGASubsumption ) { if( Parent_1.DoesSubsume( Child_1 ) ) { Parent_1.N++; } else if( Parent_2.DoesSubsume( Child_1 ) ) { Parent_2.N++; } else { P.Insert( Child_1 ); } P.Delete(); if( Parent_1.DoesSubsume( Child_2 ) ) { Parent_1.N++; } else if( Parent_2.DoesSubsume( Child_2 ) ) { Parent_2.N++; } else { P.Insert( Child_2 ); } P.Delete(); } else { P.Insert( Child_1 ); P.Delete(); P.Insert( Child_2 ); P.Delete(); } } }
/// <summary> /// GA /// </summary> /// <param name="Situation">situation</param> /// <param name="P">Population</param> public abstract void RunGA( State Situation, Population P );
/// <summary> /// 足りないときのCovering /// </summary> /// <param name="S">situation</param> /// <param name="P">Population</param> protected abstract void Covering( State S, Population P );
// Sigmaは飾り public override void Update( Population Pop, double P, StdList Sigma ) { double SumNumerosity = 0; foreach( Classifier C in this.CList ) { SumNumerosity += C.N; } foreach( Classifier C in this.CList ) { C.Exp++; if( C.Exp < 1 / Configuration.Beta ) { C.P += ( P - C.P ) / C.Exp; } else { C.P += Configuration.Beta * ( P - C.P ); } if( C.Exp < Configuration.Beta ) { C.Epsilon += ( Math.Abs( P - C.P ) - C.Epsilon ) / C.Exp; } else { C.Epsilon += Configuration.Beta * ( Math.Abs( P - C.P ) - C.Epsilon ); } if( C.Exp < Configuration.Beta ) { C.As += ( SumNumerosity - C.As ) / C.Exp; } else { C.As += Configuration.Beta * ( SumNumerosity - C.As ); } //if( C.C.state == "11####" && C.A == '0' ) //{ // Configuration.ESW.WriteLine( Configuration.T + "," + C.F + "," + C.Kappa + "," + C.Epsilon_0 + "," + C.Epsilon + "," + C.Exp + "," + Math.Sqrt( C.S / ( C.St - 1 ) ) ); // //Console.WriteLine( Configuration.T + " " + this.Exp ); //} // 標準偏差計算 C.St++; double X = P - C.M; C.M += X / C.St; C.S += ( C.St - 1 ) * X * X / C.St; } this.UpdateFitness(); if( Configuration.DoActionSetSubsumption ) { this.Subsumption( Pop ); } }