Example #1
0
        public void evaluate(Labirinto labirinto)
        {
            //First pos
            Point position = labirinto.Entry;

            //get DNA
            for (int i = 0; i < DNA.Length; i++)
            {
                switch (DNA[i])
                {
                case Gene.Left:
                    if (position.Y == 0)
                    {
                        break;
                    }
                    if (labirinto.grid[position.X, position.Y - 1] == 0)
                    {
                        break;
                    }
                    position.Y--; break;

                case Gene.Right:
                    if (position.Y == labirinto.Width - 1)
                    {
                        break;
                    }
                    if (labirinto.grid[position.X, position.Y + 1] == 0)
                    {
                        break;
                    }
                    position.Y++; break;

                case Gene.Up:
                    if (position.X == 0)
                    {
                        break;
                    }
                    if (labirinto.grid[position.X - 1, position.Y] == 0)
                    {
                        break;
                    }
                    position.X--; break;

                case Gene.Down:
                    if (position.X == labirinto.Height - 1)
                    {
                        break;
                    }
                    if (labirinto.grid[position.X + 1, position.Y] == 0)
                    {
                        break;
                    }
                    position.X++; break;
                }
            }
            double distance = Math.Sqrt(Math.Pow(labirinto.Exit.X - position.X, 2)
                                        + (Math.Pow(labirinto.Exit.Y - position.Y, 2)));

            fitness = 1 / distance;
        }
Example #2
0
        private void Form1_Load(object sender, EventArgs e)
        {
            genomesize           = Convert.ToInt16(textBoxPopulation.Text);
            PopulationSize       = Convert.ToInt16(textBoxPopulation.Text);
            labelGeneration.Text = Convert.ToString(generationnumb);
            PSelec  = Convert.ToInt16(textBoxCrossover.Text);
            PMutate = Convert.ToInt16(textBoxMutation.Text);
            step    = Convert.ToInt16(textBoxStep.Text);


            //int[,] l = {
            //    //------------------------------------------------->y
            //    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},//|
            //    {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//|
            //    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},//|
            //    {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//|
            //    {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//|
            //    {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//|
            //    {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//|
            //    {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//|
            //    {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//|
            //    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0},//V
            //    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,3},//x

            //};
            //int[,] l = {
            //    //------------------------------------------------->y
            //    {0,0,0,2,0,0,0,0,0,},
            //    {0,1,1,1,0,0,0,0,0,},
            //    {0,1,0,1,0,0,0,0,0,},
            //    {0,1,0,1,1,1,1,1,0,},
            //    {0,1,0,0,0,0,1,0,0,},
            //    {0,1,0,1,1,1,1,1,0,},
            //    {0,0,0,0,1,0,0,0,0,},
            //    {0,1,1,1,1,0,0,0,0,},
            //    {0,0,0,0,3,0,0,0,0,},
            //};

            /*int[,] l = {
             *      {0,0,0,2,0},
             *      {0,1,1,1,0},
             *      {0,1,0,0,0},
             *      {0,1,1,1,0},
             *      {0,0,0,3,0}
             *
             *
             *  };*/
            int[,] l =
            {
                //------------------------------------------------->y
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 2, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, },
                { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, },
                { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, },
                { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, },
                { 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, },
                { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, },
                { 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, },
                { 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, },
                { 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, },
                { 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, },
                { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, },
                { 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, },
                { 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, },
                { 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, },
                { 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, },
                { 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, },
                { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, },
                { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 3, },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            };
            //labirinto = new Labirinto(l, new Point(0,3), new Point(4,3));
            //labirinto = new Labirinto(l, new Point(0, 4), new Point(4, 8));
            labirinto = new Labirinto(l, new Point(1, 0), new Point(20, 21));

            pictures   = new PictureBox[labirinto.Height, labirinto.Width];
            population = new Individual[PopulationSize];

            panel1.Width  = labirinto.Width * 20;
            panel1.Height = labirinto.Height * 20;

            labirinto.draw(panel1, pictures);
        }