Пример #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            graphicObj = Graphics.FromImage(scrn);
            graphicObj.Clear(Color.White);

            Random rnd = new Random();

            for (int i = 0; i < fish.Length; i++)
            {
                fish[i] = new Guppy(800, 425);
            }

            for (int i = 0; i < food.Length; i++)
            {
                food[i] = new Food(rnd.Next(Constants.tankLeft, Constants.tankRight), rnd.Next(Constants.tankBottom, Constants.tankTop));
            }

            for (int i = 0; i < bomb.Length; i++)
            {
                bomb[i] = new Bomb(rnd.Next(Constants.tankLeft, Constants.tankRight), rnd.Next(Constants.tankBottom, Constants.tankTop));
            }

            this.WindowState = FormWindowState.Maximized;
        }
Пример #2
0
        private static Guppy[] reproduce(Guppy[] parents)
        {
            Guppy[] children = new Guppy[parents.Length];
            Random  rnd      = new Random();

            for (int i = 0; i < children.Length; i++)
            {
                children[i] = new Guppy(200, 200);
            }

            //Sort parents by numEaten
            Array.Sort(parents,
                       delegate(Guppy par1, Guppy par2) { return(par1.numEaten.CompareTo(par2.numEaten)); });

            List <int> roulette = new List <int>();

            //Keep 20% of the best guppies
            for (int i = children.Length - 1; i > children.Length - children.Length * .2 - 1; i--)
            {
                children[i] = parents[i];

                for (int j = 0; j < parents[i].numEaten; j++)
                {
                    roulette.Add(i);
                }
            }

            //For the other 80%, choose two of the best 20% and make a child
            for (int i = 0; i < children.Length - children.Length * .2; i++)
            {
                //Randomly choose parents from the top 20%
                //int randFatherIndex = rnd.Next(Convert.ToInt32(children.Length - children.Length * .2), Convert.ToInt32(children.Length));
                //int randMotherIndex = rnd.Next(Convert.ToInt32(children.Length - children.Length * .2), Convert.ToInt32(children.Length));

                int randFatherIndex = roulette.ElementAt(rnd.Next(0, roulette.Count));
                int randMotherIndex = roulette.ElementAt(rnd.Next(0, roulette.Count));

                //Save the mother and father indexes
                children[i].fatherIndex = randFatherIndex;
                children[i].motherIndex = randMotherIndex;

                //Create a genome for the child from the genomes of the parents
                Genome childGenome = parents[randFatherIndex]
                                     .DNA.offspring(parents[randMotherIndex].DNA, .25, .05);

                //Create a brain for the child from its genome
                Network brain = new Network(Constants.numInput, Constants.numHidden, Constants.numOutput, childGenome);

                children[i].DNA   = childGenome;
                children[i].brain = brain;
            }

            //Reset all positions and numEatens

            for (int i = 0; i < children.Length; i++)
            {
                children[i].numEaten = 0;
                //children[i].posX = rnd.Next(Constants.tankLeft, Constants.tankRight);
                //children[i].posY = rnd.Next(Constants.tankBottom, Constants.tankTop);

                children[i].posX = 800;
                children[i].posY = 400;
            }


            return(children);
        }