// Covering public SigmaNormalClassifier( State S, List<char> Actions, int ExpThreshold ) { // Covering済みConditionセット this.C = S; // MatchSetにない行動をランダムに選ぶ this.A = Actions[Configuration.MT.Next( Actions.Count - 1 )]; this.P = Configuration.P_I; this.Epsilon = Configuration.Epsilon_I; this.F = Configuration.F_I; this.Exp = 0; this.Ts = Configuration.T; this.As = 1; this.N = 1; this.Epsilon_0 = Configuration.Epsilon_0; this.St = 0; this.M = 0; this.S = 0; this.GenerateTime = Configuration.T; this.ExpThreshold = 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] = Configuration.F_I; } this.ConvergenceFlag = false; }
/// <summary> /// すべての取り得るStateを生成 /// </summary> /// <param name="Length">Multiplexerの長さ</param> /// <returns>すべての取り得るState</returns> public static State[] AllState( int Length ) { State[] All = new State[( int )Math.Pow( 2, Length )]; for( int i = 0; i < All.Length; i++ ) { string S = Convert.ToString( i, 2 ); // 頭に足りない分0を付加 while( S.Length < Length ) { S = "0" + S; } All[i] = new BinaryState( S ); } return All; }
// 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>(); } } }
// Covering public NormalClassifier( State S, List<char> Actions ) { // Covering済みConditionセット this.C = S; // MatchSetにない行動をランダムに選ぶ this.A = Actions[Configuration.MT.Next( Actions.Count - 1 )]; this.P = Configuration.P_I; this.Epsilon = Configuration.Epsilon_I; this.F = Configuration.F_I; this.Exp = 0; this.Ts = Configuration.T; this.As = 1; this.N = 1; this.Epsilon_0 = Configuration.Epsilon_0; this.St = 0; this.M = 0; this.S = 0; this.GenerateTime = Configuration.T; }
public NormalClassifier(State S) { // Covering済みConditionセット this.C = S; //action がない問題 //this.A = Actions[Configuration.MT.Next(Actions.Count - 1)]; this.P = Configuration.P_I; this.Epsilon = Configuration.Epsilon_I; this.F = Configuration.F_I; this.Exp = 0; this.Ts = Configuration.T; this.As = 1; this.N = 1; this.Epsilon_0 = Configuration.Epsilon_0; this.St = 0; this.M = 0; this.S = 0; this.GenerateTime = Configuration.T; }
/// <summary> /// GA /// </summary> /// <param name="Situation">situation</param> /// <param name="P">Population</param> public abstract void RunGA( State Situation, Population P );
// 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 ); }
public override void Mutation( State S ) { int i = 0; string state = ""; do { if( Configuration.MT.NextDouble() < Configuration.Myu ) { // #とstateの切り替え if( this.C.state[i] == '#' ) { state += S.state[i]; } else { state += '#'; } } else { state += this.C.state[i]; } i++; } while( i < this.C.state.Length ); this.C.state = state; this.C.CountSharp(); if( Configuration.MT.NextDouble() < Configuration.Myu ) { if( Configuration.Type == "Binary" ) { this.A = ( this.A == '0' ) ? '1' : '0'; } } }
//// #の数 //abstract public int NumberOfSharp(); /// <summary> /// i番目の要素を自分とSで入れ替える /// </summary> /// <param name="S">入れ替え対象</param> /// <param name="i">入れ替える場所</param> public abstract void Switch( State S, int i );
//public override int NumberOfSharp() //{ // int n = 0; // for(int i = 0; i < this.state.Length; i++) // { // if(this.state[i] == '#') // { // n++; // } // } // return n; //} // i番目を入れ替え public override void Switch( State S, int i ) { string t = ""; string s = ""; for( int j = 0; j < this.Length; j++ ) { if( i == j ) { t += S.state[j]; s += this.state[j]; } else { t += this.state[j]; s += S.state[j]; } } this.state = t; S.state = s; this.CountSharp(); S.CountSharp(); }
/// <summary> /// situationに一致するClassifierをMatchSetに渡す /// </summary> /// <param name="S">situation</param> /// <returns>situationに一致するClassifierの集合</returns> public abstract List<Classifier> MatchSituation( State S );
/// <summary> /// situationに基づく突然変異 /// </summary> /// <param name="S">situation</param> public abstract void Mutation( State S );
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(); } } }
// Situationに合ったClassifierをMatchSetに渡す public override List<Classifier> MatchSituation( State S ) { List<Classifier> MatchSet = new List<Classifier>(); foreach( Classifier C in this.CList ) { if( S.Match( C.C ) ) { MatchSet.Add( C ); } } return MatchSet; }
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 NormalMatchSet( State S, Population P ) { this.CList = new List<Classifier>(); this.Covering( S, P ); }
// コピーコンストラクタ public BinaryState( State S ) { this.state = S.state; this.Length = S.Length; this.NumberOfSharp = S.NumberOfSharp; }
/// <summary> /// 足りないときのCovering /// </summary> /// <param name="S">situation</param> /// <param name="P">Population</param> protected abstract void Covering( State S, Population P );
/// <summary> /// Stateに一致するか(#を考慮) /// </summary> /// <param name="S">比較対象</param> /// <returns>一致(true)</returns> public bool Match( State S ) { if( this.state.Length != S.state.Length ) { return false; } for( int i = 0; i < this.state.Length; i++ ) { if( ( this.state[i] != S.state[i] ) && ( this.state[i] != '#' ) && ( S.state[i] != '#' ) ) { return false; } } return true; }