Ejemplo n.º 1
0
        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();
        }