// コピーコンストラクタ public SigmaNormalClassifier(SigmaNormalClassifier C) { this.C = new BinaryState(C.C); //this.A = C.A; this.P = C.P; this.Epsilon = C.Epsilon; this.F = C.F; this.N = C.N; this.Exp = C.Exp; this.Ts = C.Ts; this.As = C.As; this.Kappa = C.Kappa; this.Epsilon_0 = C.Epsilon_0; this.St = C.St; this.M = C.M; this.S = C.S; this.GenerateTime = Configuration.T; this.ExpThreshold = C.ExpThreshold; this.EpsilonList = new double[Configuration.LookBackEpsilon + 1]; this.WinningRate = new double[Configuration.LookBackEpsilon]; // はじめは消されにくいように for (int i = 0; i < this.WinningRate.Count(); i++) { this.WinningRate[i] = C.WinningRate[i]; } this.ConvergenceFlag = false; }
//// 使ってない? //public NormalClassifier( int Length, string Type ) //{ // // Stateの様式決定 // if( Type == "Binary" ) // { // this.C = new BinaryState( Length ); // this.C.GetState(); // 仮 // } // this.A = '1'; // 仮 // if( Configuration.MT.Next() % 2 == 0 ) // { // this.A = '0'; // } // this.P = 10; // 仮 // this.Epsilon = 0.1; // 仮 // this.F = 10; // 仮 // this.N = 1; // this.Epsilon_0 = Configuration.Epsilon_0; // this.St = 0; // this.M = 0; // this.S = 0; //} // コピーコンストラクタ public SigmaNormalClassifier( SigmaNormalClassifier C ) { this.C = new BinaryState( C.C ); this.A = C.A; this.P = C.P; this.Epsilon = C.Epsilon; this.F = C.F; this.N = C.N; this.Exp = C.Exp; this.Ts = C.Ts; this.As = C.As; this.Kappa = C.Kappa; this.Epsilon_0 = C.Epsilon_0; this.St = C.St; this.M = C.M; this.S = C.S; this.GenerateTime = Configuration.T; this.ExpThreshold = C.ExpThreshold; this.EpsilonList = new double[Configuration.LookBackEpsilon + 1]; this.WinningRate = new double[Configuration.LookBackEpsilon]; // はじめは消されにくいように for( int i = 0; i < this.WinningRate.Count(); i++ ) { this.WinningRate[i] = C.WinningRate[i]; } this.ConvergenceFlag = false; }
/// <summary> /// 総当たり評価 /// </summary> public void RoundRobin() { //double[,] Results = new double[this.CList.Count, this.CList.Count]; //// 総当たり比較 //for( int i = 0; i < this.CList.Count - 1; i++ ) // 比較元 //{ // for( int j = i + 1; j < this.CList.Count; j++ ) // { // //if(this.CList[i].Kappa > this.CList[j].Kappa) // if( this.CList[i].F > this.CList[j].F ) // { // Results[i, j] = 1; // 勝ち点1 // } // //else if(this.CList[i].Kappa == this.CList[j].Kappa) // else if( this.CList[i].F == this.CList[j].F ) // { // Results[i, j] = 0.5; // 引き分け点0.5 // } // else // { // Results[i, j] = 0; // } // } //} // 勝率計算 for (int i = 0; i < this.CList.Count; i++) { //double WinningPoint = 0; //for( int j = 0; j < i; j++ ) //{ // WinningPoint += 1 - Results[j, i]; //} //for( int j = i + 1; j < this.CList.Count; j++ ) //{ // WinningPoint += Results[i, j]; //} SigmaNormalClassifier SNC = ( SigmaNormalClassifier )this.CList[i]; // 移動 for (int index = SNC.WinningRate.Count() - 1; index > 0; index--) { SNC.WinningRate[index] = SNC.WinningRate[index - 1]; } //SNC.WinningRate[0] = WinningPoint / ( this.CList.Count - 1 ); SNC.WinningRate[0] = SNC.F; } }
protected override double DeletionVote(Classifier C, double AvFitness) { // SigmaNormalClassifier前提 SigmaNormalClassifier SNC = ( SigmaNormalClassifier )C; double Vote = SNC.As * SNC.N; //Console.WriteLine("AS: " + SNC.As + " N: " + SNC.N); if ((SNC.Exp > Configuration.Theta_del) && (SNC.WinningRate.Average() / SNC.N < Configuration.Delta * AvFitness)) { if (SNC.WinningRate.Average() == 0) { //Console.WriteLine("0 : " + Vote); return(Vote); } Vote *= AvFitness / (SNC.WinningRate.Average() / SNC.N); //Console.WriteLine("hoge"); } //Console.WriteLine(SNC.WinningRate.Average() + " : " + Vote); return(Vote); }
// 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 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 Compact() { List <Classifier> copyActionSet = new List <Classifier>(); foreach (Classifier classifier in CList) { copyActionSet.Add(classifier); } int N = copyActionSet.Count; for (int i = 0; i < N; i++) { #region subsume Classifier Cl = null; //なかに最も一般的な分類子をClにする foreach (Classifier C in copyActionSet) { 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; } } } //Cl が包摂できる分類子を吸収する if (Cl != null) { // 削除中にforeachできない List <Classifier> CL = new List <Classifier>(); foreach (Classifier C in copyActionSet) { if (Cl.IsMoreGeneral(C)) { SigmaNormalClassifier Snc_ko = (SigmaNormalClassifier)C; SigmaNormalClassifier Snc_oya = (SigmaNormalClassifier)Cl; //10の数字は適当 可変にするか? 1/10にする 10-7 あんまり変わらない //分散が同じの判断基準 他に??t test を使う 予定 特殊化がでない // //double t = (Snc_oya.M - Snc_ko.M)/Math.Sqrt(Snc_oya.S/30 + Snc_ko.S/30); //double studentT = StudentT.InvCDF(0, 1, 60, 0.005); //if (t < studentT || t > Math.Abs(studentT))//t test で有意差がある 包摂しない //{ //} // else//有意差がない 統合しでもよい ただ 特殊化ルールどう識別するか //{ if (/*Math.Abs(Cl.M - C.M) < 10 &&*/ // Math.Abs(Cl.Epsilon_0 - C.Epsilon_0)< 10&& // Cl.Kappa == 1 //&& Snc_oya.Epsilon < Snc_ko.Epsilon (Cl.Epsilon_0 < C.Epsilon_0 || Math.Abs(Cl.Epsilon_0 - C.Epsilon_0) < Cl.Epsilon_0 / 10) //&& Math.Abs(Cl.Epsilon_0 - C.Epsilon_0) < 10//case 1 +- 10 // Cl.Epsilon < C.Epsilon && Snc_ko.IsConvergenceEpsilon() && Snc_oya.IsConvergenceEpsilon() ) // { Cl.N += C.N; // 包摂された、削除したいClassifier C をCLに登録 CL.Add(C); } // } } } foreach (Classifier C in CL) { this.Remove(C);//包摂された分類子 C を pop からCを削除 } } //最も一般化されるもの copyActionSetからを削除 copyActionSet.Remove(Cl); #endregion } }
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; C.As += (SumNumerosity - C.As) / C.Exp; } else { C.P += Configuration.Beta * (P - C.P); C.As += Configuration.Beta * (SumNumerosity - C.As); } // 標準偏差計算 C.St++; double X = P - C.M; C.M += X / C.St; C.S += (C.St - 1) * X * X / C.St; //あっている?? if (double.IsNaN(C.S)) { Console.ReadLine(); } 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)); #region 分類子が照合する全状態のVTの分散と平均を使って e0 を推測 chou 160107 //分類子が照合する全状態のVTの分散と平均を使って e0 を推測 chou 160107 if (Configuration.IsConvergenceVT) { if (C.Exp > 2) //0120 cho { if (SNC.IsConvergenceEpsilon()) //分類子のstd が収束するときepsilon を更新 { C.Epsilon = SNC.EpsilonList[0]; } else//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); } } } Classifier cl0 = new NormalClassifier(); cl0.S = 0; cl0.M = 0; cl0.St = 0; List <StdList> cpStdLists = new List <StdList>(); foreach (var std in Configuration.ConvergentedVT) { if (std.IsIncluded(C.C.state)) { cpStdLists.Add(std.Clone()); //クローンメソット } } if (cpStdLists.Count == 1) { C.Epsilon_0 = cpStdLists[0].S + Configuration.Epsilon_0; } else { foreach (var std in cpStdLists) { //St 出現回数 cl0.St += std.T; cl0.M += std.M * std.T; } //ここst= 0 , cl0.M がNULLになる cl0.M = cl0.M / cl0.St; foreach (var std in cpStdLists) { cl0.S += std.T * Math.Pow(std.S, 2) + std.T * Math.Pow((cl0.M - std.M), 2); } cl0.S = Math.Sqrt(cl0.S / cl0.St); C.Epsilon_0 = cl0.S + Configuration.Epsilon_0; } } #endregion #region tatumi 160106 XCS-SAC VTが全部収束したら 加重平均でe0更新 //if (Configuration.IsConvergenceVT) //{ // //tatumi 160106 XCS-SAC VTが全部収束したら 加重平均でe0更新 // if (C.Exp > 2 && SNC.IsConvergenceEpsilon())//0120 cho // { // C.Epsilon = SNC.EpsilonList[0]; // } // double WeightedSum = 0; // int WeightedCount = 0; // foreach (StdList SL in Configuration.Stdlist) // { // if (SL.IsIncluded(C.C.state)) // { // if (!double.IsNaN(SL.S)) // { // WeightedSum += (SL.T - 1) * Math.Pow(SL.S, 2); // WeightedCount += SL.T; // } // } // } // // 下駄適応済み // if (WeightedCount > 1) // { // WeightedSum = Math.Sqrt(WeightedSum / (WeightedCount - 1)) + Configuration.Epsilon_0; // } // else // { // WeightedSum = Configuration.Epsilon_0; // } // C.Epsilon_0 = WeightedSum; //} //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); // } //} #endregion } 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); } } } 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 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.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.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 (Child_1.C.state[4] == '0' & Child_1.C.state[7] == '1')//"bath0 rehabi1" { Configuration.Problem.WriteLine(Child_1.C.state + "," + Configuration.T + "," + Child_1.P + "," + Child_1.M + "," + Child_1.Epsilon + "," + Child_1.F + "," + Child_1.N + "," + Child_1.Exp + "," + Child_1.Ts + "," + Child_1.As + "," + Child_1.Kappa + "," + Child_1.Epsilon_0 + "," + Child_1.St + "," + Child_1.GenerateTime + ", child 2 "); Configuration.Problem.WriteLine(Parent_1.C.state + "," + Configuration.T + "," + Parent_1.P + "," + Parent_1.M + "," + Parent_1.Epsilon + "," + Parent_1.F + "," + Parent_1.N + "," + Parent_1.Exp + "," + Parent_1.Ts + "," + Parent_1.As + "," + Parent_1.Kappa + "," + Parent_1.Epsilon_0 + "," + Parent_1.St + "," + Parent_1.GenerateTime + ",Parent1"); Configuration.Problem.WriteLine(Parent_2.C.state + "," + Configuration.T + "," + Parent_2.P + "," + Parent_2.M + "," + Parent_2.Epsilon + "," + Parent_2.F + "," + Parent_2.N + "," + Parent_2.Exp + "," + Parent_2.Ts + "," + Parent_2.As + "," + Parent_2.Kappa + "," + Parent_2.Epsilon_0 + "," + Parent_2.St + "," + Parent_2.GenerateTime + ",Parent2"); } if (Child_2.C.state[4] == '0' & Child_2.C.state[7] == '1')//"bath0 rehabi1" { Configuration.Problem.WriteLine(Child_2.C.state + "," + Configuration.T + "," + Child_2.P + "," + Child_2.M + "," + Child_2.Epsilon + "," + Child_2.F + "," + Child_2.N + "," + Child_2.Exp + "," + Child_2.Ts + "," + Child_2.As + "," + Child_2.Kappa + "," + Child_2.Epsilon_0 + "," + Child_2.St + "," + Child_2.GenerateTime + ",child 2"); Configuration.Problem.WriteLine(Parent_1.C.state + "," + Configuration.T + "," + Parent_1.P + "," + Parent_1.M + "," + Parent_1.Epsilon + "," + Parent_1.F + "," + Parent_1.N + "," + Parent_1.Exp + "," + Parent_1.Ts + "," + Parent_1.As + "," + Parent_1.Kappa + "," + Parent_1.Epsilon_0 + "," + Parent_1.St + "," + Parent_1.GenerateTime + ",Parent1"); Configuration.Problem.WriteLine(Parent_2.C.state + "," + Configuration.T + "," + Parent_2.P + "," + Parent_2.M + "," + Parent_2.Epsilon + "," + Parent_2.F + "," + Parent_2.N + "," + Parent_2.Exp + "," + Parent_2.Ts + "," + Parent_2.As + "," + Parent_2.Kappa + "," + Parent_2.Epsilon_0 + "," + Parent_2.St + "," + Parent_2.GenerateTime + ",Parent2"); } 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(); } } }
// 包摂 protected override void Subsumption(Population Pop) { List <Classifier> copyActionSet = new List <Classifier>(); foreach (Classifier classifier in CList) { copyActionSet.Add(classifier); } int N = copyActionSet.Count; //最大N回実行する //for (int i = 0; i < N; i++) //{ #region subsume Classifier Cl = null; //actionsetなかに最も一般的な分類子をClにする foreach (Classifier C in copyActionSet) { 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>(); // 包摂された、削除したいClassifier C をCLに登録 foreach (Classifier C in copyActionSet) { if (Cl.IsMoreGeneral(C)) { SigmaNormalClassifier Snc_ko = (SigmaNormalClassifier)C; SigmaNormalClassifier Snc_oya = (SigmaNormalClassifier)Cl; // e0 の値を3位まで見る、近いものは差がないとみなす var subsumer = Math.Round(Cl.Epsilon_0, 3); var subsumed = Math.Round(C.Epsilon_0, 3); if ((subsumer <= (subsumed + subsumer / 10)) && Snc_ko.IsConvergenceEpsilon() && Snc_oya.IsConvergenceEpsilon() ) { Cl.N += C.N; CL.Add(C); } } } foreach (Classifier C in CL) { SigmaNormalClassifier SNC = (SigmaNormalClassifier)Cl; if (C.C.state[4] == '0' & C.C.state[7] == '1') //"bath0 rehabi1" { Configuration.Problem.WriteLine(C.C.state + "," + Configuration.T + "," + C.P + "," + C.M + "," + C.Epsilon + "," + C.F + "," + C.N + "," + C.Exp + "," + C.Ts + "," + C.As + "," + C.Kappa + "," + C.Epsilon_0 + "," + C.St + "," + C.GenerateTime + ", in AS"); } this.Remove(C); //as から削除 Pop.Remove(C); //pop から削除 } //いまの最も一般化されたものを削除する //copyActionSet.Remove(Cl); } #endregion //} }
protected override void UpdateFitness() { double AccuracySum = 0; foreach (Classifier C in this.CList) { SigmaNormalClassifier SNC = ( SigmaNormalClassifier )C; if (Configuration.T > 1000) { if (//またがるのものを排除 平均プラマイ Pのプラマイ (C.M - C.Epsilon) < Configuration.RewardAverage && (C.M + C.Epsilon) > Configuration.RewardAverage ) {//またがる分類子の正確性を極端に下げる PS:0にしてはいけない //またがる部分とEの割合でKappaの正確性を下げていく、またがる部分が大きいければ 下がるのが早い double crossPercentage; double mincross; //puls minus 0.5e どうなるかな0128 ダメだった 余計なもの残した mincross = Math.Min(Math.Abs(C.M + C.Epsilon - Configuration.RewardAverage), Math.Abs(C.M - C.Epsilon - Configuration.RewardAverage)); //crossPercentage = mincross / (C.Epsilon); crossPercentage = mincross / Configuration.RewardAverage;//12/11変更、許容範囲の図り方、平均値と比較 //規制緩和 c.epsilon の 20% ぐらいまたがる を許す、緩和しない基本的に+-epsilon 十分緩いい //±σ 相当 片側85% と 片側15% の関係 if (crossPercentage < Configuration.CoverPersentage)//許容範囲で通常のやり方 { if (C.Epsilon < C.Epsilon_0) { C.Kappa = 1; } else {//epsilon>epsilon0 の場合 C.Kappa = Configuration.Alpha * Math.Pow(C.Epsilon / C.Epsilon_0, -Configuration.Nyu); } } else//許容範囲を超えた { C.Kappa = Configuration.Alpha * Math.Pow(1 + crossPercentage, -Configuration.Nyu); // C.Kappa = Math.Pow(Math.E,-Math.Pow(5*crossPercentage,2)); } AccuracySum += C.Kappa * C.N; if (double.IsNaN(AccuracySum)) { Console.ReadLine(); } } else //またがらない分類子は 通常のやり方 { if (C.Epsilon <= C.Epsilon_0) { C.Kappa = 1; } else {//epsilon>epsilon0 の場合 C.Kappa = Configuration.Alpha * Math.Pow(C.Epsilon / C.Epsilon_0, -Configuration.Nyu); } AccuracySum += C.Kappa * C.N; //Accuracy はNaNなるとき 止める if (double.IsNaN(AccuracySum)) { Console.ReadLine(); } } } else //1000 回以下の場合 { if (C.Epsilon <= C.Epsilon_0) { C.Kappa = 1; } else { //ここC.Epsilon_0 == 0, kappa の計算がおかしいから, SNC.epsilon_0 下駄を足す C.Kappa = Configuration.Alpha * Math.Pow(C.Epsilon / SNC.Epsilon_0, -Configuration.Nyu); } if (double.IsNaN(C.Kappa)) { Console.WriteLine("kappa =NaN"); Console.ReadLine(); } AccuracySum += C.Kappa * C.N; } } // foreach (Classifier C in this.CList) { C.F += Configuration.Beta * (C.Kappa * C.N / AccuracySum - C.F); if (double.IsNaN(C.F)) { Console.ReadLine(); } } }
// Populationからfitnessが一番小さいものを削除 public override void Delete() { //if( this.CountNumerosity() <= Configuration.N ) //{ // return; //} //int SumNumerosity = 0; //double SumFitness = 0.0; //List<Classifier> DeleteCandidate = new List<Classifier>(); //if(Configuration.ASName == "CS") //{ // foreach(Classifier C in this.CList) // { // SigmaNormalClassifier SC = ( SigmaNormalClassifier )C; // double WR = 0; // foreach(double d in SC.WinningRate) // { // WR += d; // } // if(SC.HasMatured() && (WR < 0.4 * SC.WinningRate.Count())) // { // DeleteCandidate.Add( SC ); // } // } // if(DeleteCandidate.Count == 0) // { // DeleteCandidate = this.CList; // } // //else // //{ // // bool Flag = true; // // foreach( Classifier C in DeleteCandidate ) // // { // // SigmaNormalClassifier SNC = ( SigmaNormalClassifier )C; // // double WR = 0; // // foreach(double d in SNC.WiningRate) // // { // // WR += d; // // } // // // 悪い子探し // // if( WR < 0.4 * SNC.WiningRate.Count() ) // // { // // Flag = false; // // } // // } // // // 悪い子が一人もいなかったら // // if( Flag ) // // { // // DeleteCandidate = this.CList; // // } // //} //} //else //{ // DeleteCandidate = this.CList; //} //foreach( Classifier C in DeleteCandidate ) //{ // SumNumerosity += C.N; // SumFitness += C.F; //} //double AvFitness = SumFitness / SumNumerosity; //double VoteSum = 0; //foreach( Classifier C in DeleteCandidate ) //{ // VoteSum += this.DeletionVote( C, AvFitness ); //} //double ChoicePoint = Configuration.MT.NextDouble() * VoteSum; //VoteSum = 0; //foreach( Classifier C in DeleteCandidate ) //{ // VoteSum += this.DeletionVote( C, AvFitness ); // if( VoteSum > ChoicePoint ) // { // if( C.N > 1 ) // { // C.N--; // } // else // { // this.Remove( C ); // } // return; // } //} int SumNumerosity = 0; double SumFitness = 0.0; foreach (Classifier C in this.CList) { SigmaNormalClassifier SNC = ( SigmaNormalClassifier )C; SumNumerosity += SNC.N; //SumFitness += C.F; SumFitness += SNC.WinningRate.Average(); //Console.WriteLine(SNC.C.state + " ave: " + SNC.WinningRate.Average()); } if (SumNumerosity <= Configuration.N) { return; } double AvFitness = SumFitness / SumNumerosity; double VoteSum = 0; foreach (Classifier C in this.CList) { VoteSum += this.DeletionVote(C, AvFitness); } double ChoicePoint = Configuration.MT.NextDouble() * VoteSum; VoteSum = 0; foreach (Classifier C in this.CList) { VoteSum += this.DeletionVote(C, AvFitness); if (VoteSum > ChoicePoint) { if (C.N > 1) { C.N--; } else { this.Remove(C); } return; } } }
// situationに合うものをPopulationから取り、足りないときはCovering protected override void Covering(State S, Population P) { while (this.CList.Count == 0) { // situationにあうものをPopulationから探す this.CList = P.MatchSituation(S); if (CList.Count == 0) { // 一部を変化させたCondition State state = new IntegralState(S); state.Covering(); Classifier CC; if (Configuration.ASName == "CS" || Configuration.ASName == "MaxCS" || Configuration.ASName == "Max" || Configuration.ASName == "Updatee0CS") { CC = new SigmaNormalClassifier(state, Configuration.ExpThreshold); } else { CC = new NormalClassifier(state); } P.Add(CC); // 整理 P.Delete(); this.CList = new List <Classifier>(); } } /*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 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>(); List <string> DataList = Env.GetDataList(); List <string> DistinctDataList = DataList.Distinct().ToList(); int DistinctDataNum = DistinctDataList.Count(); // 提案手法 入力データ個数分の分散 Configuration.Stdlist = new StdList[DistinctDataNum]; // 収束した VTの値を保存する ちょう Configuration.ConvergentedVT = new StdList[DistinctDataNum]; for (int i = 0; i < DistinctDataNum; i++) { Configuration.ConvergentedVT[i] = new StdList(DistinctDataList[i], '0'); //Configuration.ConvergentedVT[i * 4 + 1] = new StdList(i, '1'); } for (int i = 0; i < DistinctDataNum; i++) { Configuration.Stdlist[i] = new StdList(DistinctDataList[i], '0'); //Configuration.Stdlist[i * 4 + 1] = new StdList( i, '1' ); } // 実験1a ノイズを既知のものとして扱う if (Configuration.ASName == "WellKnown") { Configuration.Epsilon_0 += Configuration.NoiseWidth; } Configuration.Problem.WriteLine("state ,iter,P , cl.M ,cl.Epsilon , cl.F , cl.N , cl.Exp , cl.Ts ,cl.As , cl.Kappa ,cl.Epsilon_0 , cl.St , cl.GenerateTime"); StreamWriter goodsleep1 = new StreamWriter("./goodsleep_rule1.csv"); goodsleep1.WriteLine("state ,iter,P , cl.M ,cl.Epsilon , cl.F , cl.N , cl.Exp , cl.Ts ,cl.As , cl.Kappa ,cl.Epsilon_0 , cl.St , cl.GenerateTime"); StreamWriter goodsleep2 = new StreamWriter("./goodsleep_rule2.csv"); goodsleep2.WriteLine("state ,iter,P , cl.M ,cl.Epsilon , cl.F , cl.N , cl.Exp , cl.Ts ,cl.As , cl.Kappa ,cl.Epsilon_0 , cl.St , cl.GenerateTime"); StreamWriter badsleep = new StreamWriter("./badsleep_rule.csv"); badsleep.WriteLine("state ,iter,P , cl.M ,cl.Epsilon , cl.F , cl.N , cl.Exp , cl.Ts ,cl.As , cl.Kappa ,cl.Epsilon_0 , cl.St , cl.GenerateTime"); // メインループ #region main roop while (Configuration.T < Configuration.Iteration) { // VTの収束 if (!Configuration.IsConvergenceVT) { bool flag = true; //入力データのSLが収束すれば、VTが収束とみなす。 foreach (StdList SL in Configuration.Stdlist) { if (flag && !SL.IsConvergenceSigma()) { flag = false; break; } } if (flag) // 初めてTrue { Configuration.IsConvergenceVT = true; //収束したVTを保存する for (int i = 0; i < DistinctDataList.Count; i++) { Configuration.ConvergentedVT[i].M = Configuration.Stdlist[i].M; //Configuration.ConvergentedVT[i * 4+1].M = Configuration.Stdlist[i * 2+1].M; Configuration.ConvergentedVT[i].S = Configuration.Stdlist[i].S; //Configuration.ConvergentedVT[i * 4 + 1].S = Configuration.Stdlist[i*2+1].S; Configuration.ConvergentedVT[i].T = Configuration.Stdlist[i].T; //Configuration.ConvergentedVT[i * 4 + 1].T = Configuration.Stdlist[i * 2 + 1].T; } // [P]の全てを新しい基準で再評価 foreach (Classifier C in P.CList) { // 加重平均 double ST = 0; int SumT = 0; foreach (StdList SL in Configuration.Stdlist) { if (SL.IsIncluded(C.C.state)) { ST += SL.S * SL.T; SumT += SL.T; } } ST /= SumT; SigmaNormalClassifier SNC = (SigmaNormalClassifier)C; C.Epsilon_0 = ST + Configuration.Epsilon_0; if (C.Exp > 2) { C.Epsilon = SNC.EpsilonList[0]; } } } } State S = Env.GetState(); // MatchSet生成 MatchSet M = new NormalMatchSet(S, P); foreach (Classifier cl in M.CList) { if (cl.C.state[4] == '2' & cl.C.state[7] == '#')//"bath2 rehabi# or bath# rehabi0" { goodsleep1.WriteLine(cl.C.state + "," + Configuration.T + "," + cl.P + "," + cl.M + "," + cl.Epsilon + "," + cl.F + "," + cl.N + "," + cl.Exp + "," + cl.Ts + "," + cl.As + "," + cl.Kappa + "," + cl.Epsilon_0 + "," + cl.St + "," + cl.GenerateTime); } if (cl.C.state[4] == '#' & cl.C.state[7] == '0')//"bath2 rehabi# or bath# rehabi0" { goodsleep2.WriteLine(cl.C.state + "," + Configuration.T + "," + cl.P + "," + cl.M + "," + cl.Epsilon + "," + cl.F + "," + cl.N + "," + cl.Exp + "," + cl.Ts + "," + cl.As + "," + cl.Kappa + "," + cl.Epsilon_0 + "," + cl.St + "," + cl.GenerateTime); } } foreach (Classifier cl in P.CList) { if (cl.C.state[4] == '0' & cl.C.state[7] == '1')//"bath0 rehabi1" { badsleep.WriteLine(cl.C.state + "," + Configuration.T + "," + cl.P + "," + cl.M + "," + cl.Epsilon + "," + cl.F + "," + cl.N + "," + cl.Exp + "," + cl.Ts + "," + cl.As + "," + cl.Kappa + "," + cl.Epsilon_0 + "," + cl.St + "," + cl.GenerateTime); } } // ActionSetはただMをコピーするだけ,アクションがないから ActionSet AS; if (Configuration.ASName == "CS") { AS = new ConditionSigmaActionSet(M.CList); } else { AS = new NormalActionSet(M.CList); /*M.MatchAction(Action))*/; } char Action = '0';//action ないから、全部0にする double Rho = Env.ExecuteAction(Action); 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; } //chou 1000回の報酬平均を保存 if (Configuration.T < 1000) { Configuration.RewardList.Add(Rho); } if (Configuration.T == 1000) { Configuration.RewardAverage = Configuration.RewardList.Mean(); } // マルチステップ問題の終着またはシングルステップ問題 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; } 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 (!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,average,time,convergence"); foreach (StdList SL in Configuration.Stdlist) { stdSw.WriteLine(SL.C + "," + SL.A + "," + SL.S + "," + SL.M + "," + SL.T + "," + (SL.IsConvergenceSigma() ? 1 : 0)); // 1 : 収束 } stdSw.Close(); ConvergenceStelist = true; } } Configuration.T++; Console.WriteLine(Configuration.T); } P.Show(); #endregion goodsleep1.Close(); goodsleep2.Close(); badsleep.Close(); Configuration.Problem.Close(); P.Compact(); //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")); 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")); 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")); 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(); 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(); }