public override object Clone()
        {
            Game game = new FlappyBird(Speed, r, EnableGraphics)
            {
                ControlSize = ControlSize,
                posY        = posY,
                rad         = rad,
                minPosY     = minPosY,
                maxPosY     = maxPosY,
                pipeWidth   = pipeWidth,
                pipeHeight  = pipeHeight,
                pipeSpacing = pipeSpacing
            };

            if (EnableGraphics)
            {
                (game as FlappyBird).SetPictures();
            }
            return(game);
        }
Exemple #2
0
        private void TrainButton_Click(object sender, EventArgs e)
        {
            int    generationSize, proceedToNext, generations, maxScore, gamesToPlay;
            double mutationProbability, mutationRate;

            if (!int.TryParse(GenerationSize.Text, out generationSize) || generationSize < 1)
            {
                MessageBox.Show("Generation size must be positive integer.");
                return;
            }
            if (!int.TryParse(ProceedToNext.Text, out proceedToNext) || proceedToNext < 1)
            {
                MessageBox.Show("Proceed to next size must be positive integer.");
                return;
            }
            if (!int.TryParse(GenerationsCount.Text, out generations) || generations < 1)
            {
                MessageBox.Show("Maximum count of generations must be positive integer.");
                return;
            }
            if (!int.TryParse(MaxScore.Text, out maxScore) || maxScore < 1)
            {
                MessageBox.Show("Maximum score must be positive integer.");
                return;
            }
            if (!int.TryParse(GamesToPlay.Text, out gamesToPlay) || gamesToPlay < 1)
            {
                MessageBox.Show("Games to play must be positive integer.");
                return;
            }
            if (!double.TryParse(MutationProbability.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out mutationProbability) || mutationProbability <= 0 || mutationProbability > 1)
            {
                MessageBox.Show("Mutation probability must be value between 0 and 1.");
                return;
            }
            if (!double.TryParse(MutationRate.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out mutationRate) || mutationRate <= 0)
            {
                MessageBox.Show("Mutation rate must be positive value");
                return;
            }
            string[]   splittedNeurons = HiddenNeurons.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            List <int> hiddenNeurons;

            try
            {
                hiddenNeurons = splittedNeurons.Select(x => int.Parse(x)).ToList();
                hiddenNeurons.ForEach(x =>
                {
                    if (x < 1)
                    {
                        MessageBox.Show("Neurons count in hidden layers must be positive integer");
                        return;
                    }
                });
            }
            catch
            {
                MessageBox.Show("Neurons count in hidden layers must be positive integer");
                return;
            }
            scoreHistory = new List <double>();
            GenerationProgress.Maximum = generationSize;
            Random r = new Random();
            Model  defaultModel;
            Game   etalonGame;

            if (GamesComboBox.SelectedItem == GamesComboBox.Items[0])
            {
                defaultModel = new Model(new NeuralNetwork(4, hiddenNeurons, 1, r), Model.Games.FlappyBird);
                etalonGame   = new FlappyBird(1, r, false);
            }
            else
            {
                MessageBox.Show("Dino is not supported yet");
                return;
                ////defaultModel = new Model(new NeuralNetwork(4, hiddenNeurons, 1, r), Model.Games.FlappyBird);
                //etalonGame = new Dino(1, r, false);
            }
            etalonGame.ControlSize = GameForm.ControlSize;
            etalonGame.SetDefaultOptions();
            Trainer trainer = new Trainer(defaultModel, etalonGame, generationSize,
                                          proceedToNext, generations, maxScore,
                                          gamesToPlay, mutationProbability, mutationRate, r);

            trainer.ModelTested += (obj, value) =>
            {
                GenerationProgress.Invoke((MethodInvoker)(() =>
                {
                    GenerationProgress.Value = value;
                }));
            };
            trainer.GenerationPassed  += GenerationPassed;
            TrainButton.Enabled        = false;
            TrainingParameters.Enabled = false;
            thread = new Thread(() => Train(trainer));
            thread.Start();
        }