예제 #1
0
 //// 使ってない?
 //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 NormalClassifier( NormalClassifier 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;
 }
예제 #2
0
 // コピーコンストラクタ
 public NormalClassifier(NormalClassifier 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;
 }
예제 #3
0
        // 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>();
                }
            }
        }
예제 #4
0
        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);
            }
        }
예제 #5
0
        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();
                }
            }
        }
        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();
                }

            }
        }
예제 #7
0
        // 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>();
             *          }
             *  }*/
        }