示例#1
0
 public TetrisChromosome(EvaluationNNParameter param) : base(NumInput * NumMiddle + NumMiddle * NumOutput)
 {
     float[] p = param.Flatten();
     for (int i = 0; i < Length; i++)
     {
         ReplaceGene(i, new Gene(p[i]));
     }
 }
示例#2
0
        private void initialized(object sender, EventArgs a)
        {
            EvaluationNNParameter parameter = new EvaluationNNParameter(
                new float[] {
                0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f
            },
                new float[] {
                0.25f, 0.25f, 0.25f, 0.25f, 0.25f
            });

            if (Setting is SinglePlaySetting)
            {
                SinglePlaySetting setting = Setting as SinglePlaySetting;
                Game = new TetrisGame(App.GetLogger());
                Game.SetRenderer(DXPanel);
                Game.SetController(DXPanel.Controller);
                Game.TimerSpan             = setting.FallInterval.Value;
                Game.RecordPlayDataEnabled = setting.RecordPlayDataEnabled.Value;
                Game.Start();
            }
            else if (Setting is AIPlaySetting)
            {
                AIPlaySetting setting = Setting as AIPlaySetting;
                var           p       = EvaluationNNParameter.Load(typeof(EvaluationNNParameter), setting.AiTrainingFile.Value.DirectoryName, setting.AiTrainingFile.Value.Name) as EvaluationNNParameter;
                if (p != null)
                {
                    parameter = p;
                }
                Evaluator evaluator = new Evaluator(parameter);
                Game = new TetrisGame(App.GetLogger());
                Game.SetRenderer(DXPanel);
                Game.SetController(new AITetrisController(evaluator, setting.AiControllInterval.Value));
                Game.TimerSpan             = setting.FallInterval.Value;
                Game.RecordPlayDataEnabled = setting.RecordPlayDataEnabled.Value;
                Game.Start();
            }
            else if (Setting is RePlaySetting)
            {
                RePlaySetting setting  = Setting as RePlaySetting;
                GamePlayData  playdata = (GamePlayData)GamePlayData.Load(typeof(GamePlayData), setting.PlayDataFile.Value.DirectoryName, setting.PlayDataFile.Value.Name);
                Game = new TetrisGame(App.GetLogger(), 10, 20, playdata.ObjectPool, playdata.ObjectQueue);
                Game.SetRenderer(DXPanel);
                Game.SetController(new ReplayController(playdata.Events));
                Game.Start();
            }
            //Game = new TetrisGame(TetrisPlayer.GetLogger(),10,20,playdata.ObjectPool,playdata.ObjectQueue);
        }
示例#3
0
文件: Form1.cs 项目: tkhs-dev/Tetris
        private void initialized(object sender, EventArgs a)
        {
            EvaluationNNParameter parameter = new EvaluationNNParameter(
                new float[] {
                0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f
            },
                new float[] {
                0.25f, 0.25f, 0.25f, 0.25f, 0.25f
            });
            string path = "parameters";

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var p = EvaluationNNParameter.Load(typeof(EvaluationNNParameter), path, "params.xml") as EvaluationNNParameter;

            if (p != null)
            {
                parameter = p;
            }
            Evaluator evaluator = new Evaluator(parameter);

            GamePlayData playdata = (GamePlayData)GamePlayData.Load(typeof(GamePlayData), "playdata", "data.xml");

            //Game = new TetrisGame(TetrisPlayer.GetLogger(),10,20,playdata.ObjectPool,playdata.ObjectQueue);
            Game = new TetrisGame(TetrisPlayer.GetLogger());
            foreach (Control c in this.Controls)
            {
                if (c is IRenderer)
                {
                    TetrisPlayer.GetLogger().Info("Find a Renderer:" + c.GetType().Name);
                    Game.SetRenderer((IRenderer)c);
                    Game.SetController((IController) new AITetrisController(evaluator, 100));
                    //Game.SetController((IController)new ReplayController(playdata.Events));
                }
            }
            Game.RecordPlayDataEnabled = false;
            Game.Start();
        }
示例#4
0
        public double Evaluate(IChromosome chromosome)
        {
            EvaluationNNParameter parameter = (chromosome as TetrisChromosome).GetParameter();
            Evaluator             evaluator = new Evaluator(parameter);
            List <TetrisGame>     games     = Enumerable.Range(0, Sample).Select(x => new TetrisGame(TetrisAITrainer.Logger)).ToList();
            List <GameResult>     results   = new List <GameResult>();

            CancellationTokenSource tokenSource = new CancellationTokenSource();
            ParallelOptions         options     = new ParallelOptions();

            options.CancellationToken = tokenSource.Token;
            try
            {
                Parallel.ForEach(games, options, x =>
                {
                    x.SetController(new AITetrisController(evaluator, 0));
                    x.TimerEnabled = true;
                    x.TimerSpan    = 100;
                    x.MaxRound     = MaxRound;
                    x.Start();
                    var result = x.WhenGameEnd().Result;
                    if (result.Score == 0)
                    {
                        //tokenSource.Cancel();
                    }
                    results.Add(result);
                    x.Dispose();
                });
            }
            catch (OperationCanceledException e)
            {
                TetrisAITrainer.Logger.Info(0);
                return(0);
            }

            /*
             * games.ForEach(x =>
             * {
             *  x.SetController(new AITetrisController(evaluator, 0));
             *  x.TimerEnabled = true;
             *  x.TimerSpan = 100;
             *  x.MaxRound = MaxRound;
             *  x.Start();
             *  results.Add(x.WhenGameEnd().Result);
             *  x.Dispose();
             * });
             */
            double av = results.Average(x => x.Score);

            if (UseVariance)
            {
                double pv = results.Select(x => (double)x.Score).PopulationVariance() / 100;
                double sm = results.Select(x => x.Score).Sum();
                TetrisAITrainer.Logger.Info("pv:" + pv);
                pv /= (sm + 1);
                double bonus = 500 / ((1 / (sm * 0.05 + 50)) * pv * pv + 500);
                av *= bonus;
            }
            TetrisAITrainer.Logger.Info(av);
            return(av);
        }
示例#5
0
 public Evaluator(EvaluationNNParameter parameter)
 {
     _parameter = parameter;
 }