Exemplo n.º 1
0
        static void _2048(Random r)
        {
            for (int i = 0; i < 50; ++i)
            {
                Pool p = new Pool(16, 4, r);
                p.Population = 500;
                Agent2048 a2 = new Agent2048(r);
                a2.Execute   = 5;
                a2.FailLimit = 1;
                a2.InputMode = 3;

                a2.Fit = (int score, int logScore, int bitScore) =>
                {
                    double fit;
                    fit = (long)score * score * score;
                    return((long)fit);
                };

                Writer             w   = new Writer(new FileInfo(string.Format("D://NEAT/2048/CubeScoreInput3/Data{0}.txt", i)));
                Data2048Dictionary d2d = new Data2048Dictionary();
                p.Agent          = a2;
                p.DeltaThreshold = 10;
                p.DeltaDisjoint  = 10;
                p.DeltaWeight    = 4;

                p.LinkMutationChance       = 0.75;
                p.ConnectionMutationChance = 0.5;
                p.NodeMutationChance       = 0.2;

                p.WritePlayData  = true;
                p.WriteSpecies   = true;
                p.Writer         = w;
                p.DataDictionary = d2d;
                w.Start(p, a2.Execute);
                p.Initialize();
                p.DisplayTop = int.MaxValue;
                for (int k = 0; k < 300; ++k)
                {
                    p.Evaluate();
                }
            }
        }
Exemplo n.º 2
0
        private void UpdateGraphics(object sender, PaintEventArgs e)
        {
            Graphics canvas = e.Graphics;

            if (!isPlaying)
            {
                return;
            }

            LblScoreValue.Text = "" + agent.Score;

            if (agent is SnakeAgent)
            {
                SnakeAgent sa    = agent as SnakeAgent;
                Brush      color = Brushes.White;

                LblHungerValue.Text = "" + sa.Hunger;

                int x = sa.X;
                int y = sa.Y;
                int[,] cells = new int[y, x];
                cells[sa.Food.Y, sa.Food.X] = 1;
                foreach (Square s in sa.Snake)
                {
                    cells[s.Y, s.X] = -1;
                }
                for (int i = 0; i < y; ++i)
                {
                    for (int j = 0; j < x; ++j)
                    {
                        if (cells[i, j] == 0)
                        {
                            color = Brushes.White;
                        }
                        else if (cells[i, j] == -1)
                        {
                            color = Brushes.Black;
                        }
                        else if (cells[i, j] == 1)
                        {
                            color = Brushes.Red;
                        }
                        canvas.FillRectangle(color, new Rectangle(
                                                 j * GameBox.Width / x + 1, i * GameBox.Height / y + 1, GameBox.Width / x - 1, GameBox.Height / y - 1
                                                 ));
                    }
                }
            }
            else if (agent is Agent2048)
            {
                Agent2048 a2    = agent as Agent2048;
                Brush     color = Brushes.White;
                for (int i = 0; i < 16; ++i)
                {
                    if (a2.Cells[i] == 0)
                    {
                        color = Brushes.White;
                    }
                    else
                    {
                        color = new SolidBrush(Color.FromArgb(255, 255, 255 - (a2.Cells[i] * 16)));
                    }
                    canvas.FillRectangle(color, new Rectangle(
                                             (i % 4) * GameBox.Width / 4 + 1, (i / 4) * GameBox.Height / 4 + 1, GameBox.Width / 4 - 1, GameBox.Height / 4 - 1
                                             ));
                }
            }

            if (agent.Gameover)
            {
                isPlaying = false;
            }
        }