Example #1
0
 public VelocityFunctionPredator(Predator current, Prey best, double clamp, double alphax, double betax, double alphay, double betay, Bitmap bmp, double tired)
 {
     this.current   = current;
     this.best      = best;
     this.clamp     = clamp;
     this.alphax    = alphax + huntBest;
     this.betax     = betax * fogetPast;
     this.alphay    = alphay + huntBest;
     this.betay     = betay * fogetPast;
     this.bmp       = bmp;
     this.maxheight = bmp.Height - 1;
     this.maxwidth  = bmp.Width - 1;
     this.tired     = tired;
 }
 public VelcoityFunctionPrey(Prey current, List <Predator> predatorsBeforeMoveList, Prey best, double clamp, double alphax, double betax, double alphay, double betay, Bitmap bmp, double tired, bool predatorConsider, double fearRadius, double fearReaction)
 {
     rnd          = new Random();
     this.current = current;
     this.best    = best;
     this.clamp   = clamp;
     //this.predators = predators;
     this.alphax = alphax;
     this.betax  = betax;
     this.alphay = alphay;
     this.betay  = betay;
     this.bmp    = bmp;
     this.predatorsBeforeMoveList = predatorsBeforeMoveList;
     this.maxheight        = bmp.Height - 1;
     this.maxwidth         = bmp.Width - 1;
     this.tired            = tired;
     this.predatorConsider = predatorConsider;
     this.fearReaction     = fearReaction;
     this.fearRadius       = fearRadius;
 }
        private void runPreditor(object sender, EventArgs e)
        {
            numPredators  = (int)numpreditors.Value;
            TempImg       = savedImg.Clone(new Rectangle(0, 0, savedImg.Width, savedImg.Height), savedImg.PixelFormat);
            particlesList = new List <Particle>();
            //create prey
            int x;
            int y;

            BestPrey = new Prey(0, 0, 0);

            for (int i = 0; i < numPrey; i++)
            {
                x = rnd.Next(width);
                y = rnd.Next(height);
                particlesList.Add(new Prey(x, y, savedImg.GetPixel(x, y).B));
                if (BestPrey.CurrentPostion.score < savedImg.GetPixel(x, y).B)
                {
                    BestPrey.CurrentPostion.x     = x;
                    BestPrey.CurrentPostion.y     = y;
                    BestPrey.CurrentPostion.score = savedImg.GetPixel(x, y).B;
                    BestPrey.Posbest.y            = y;
                    BestPrey.Posbest.x            = x;
                    BestPrey.Posbest.score        = savedImg.GetPixel(x, y).B;
                    //System.Diagnostics.Debug.WriteLine(" I found " + savedImg.GetPixel(x, y).B);
                }
            }
            for (int i = 0; i < numPredators; i++)
            {
                x = rnd.Next(width);
                y = rnd.Next(height);

                particlesList.Add(new Predator(x, y, savedImg.GetPixel(x, y).B));
            }

            DrawAllOfIt(TempImg, particlesList);


            //intialise
            currentScoreMax    = 0;
            countGameIteration = 0;
            tiredcount         = 0;
            tiredcountPredator = 0;
            //tired = 0.1;
            //tiredPredator = 0.2;

            // now run algorithm
            while (true)
            {
                TempImg = savedImg.Clone(new Rectangle(0, 0, savedImg.Width, savedImg.Height), savedImg.PixelFormat);
                //int countParticle = 0;
                Particle[]      particlesArray          = particlesList.ToArray();
                List <Predator> predatorsBeforeMoveList = new List <Predator>();
                for (int i = 0; i < particlesArray.Length; i++)
                {
                    if (particlesArray[i] is Predator)
                    {
                        // save predator
                        predatorsBeforeMoveList.Add((Predator)particlesArray[i]);
                    }
                }

                //perform algorithm
                for (int i = 0; i < particlesArray.Length; i++)
                {
                    if (particlesArray[i] is Prey)
                    {
                        VelcoityFunctionPrey vfp = new VelcoityFunctionPrey((Prey)particlesArray[i], predatorsBeforeMoveList, BestPrey, clamp, alphax, betax, alphay, betay, TempImg, tired, enablePreditor, fearRadius, fearReaction);
                        particlesArray[i] = vfp.newPrey();
                        //System.Diagnostics.Debug.WriteLine(" new PreyScore " + vfp.newPrey().CurrentPostion.score);
                        //System.Diagnostics.Debug.WriteLine( vfp.newPrey().CurrentPostion.score);
                    }
                    else
                    {
                        // lets so predator stuff here
                        VelocityFunctionPredator vfp = new VelocityFunctionPredator((Predator)particlesArray[i], BestPrey, clamp, alphax, betax, alphay, betay, TempImg, tiredPredator);
                        particlesArray[i] = vfp.newPredator();

                        // give the predator less stamina
                    }
                }
                // update max score :) and we gucchi
                BestPrey.CurrentPostion.score = 0;
                BestPrey.Posbest.score        = 0;


                for (int i = 0; i < particlesArray.Length; i++)
                {
                    if (particlesArray[i] is Prey)
                    {
                        if (BestPrey.CurrentPostion.score < particlesArray[i].CurrentPostion.score)
                        {
                            BestPrey.CurrentPostion.score = ((Prey)particlesArray[i]).CurrentPostion.score;
                            BestPrey.CurrentPostion.x     = ((Prey)particlesArray[i]).CurrentPostion.x;
                            BestPrey.CurrentPostion.y     = ((Prey)particlesArray[i]).CurrentPostion.y;
                            BestPrey.Posbest.score        = ((Prey)particlesArray[i]).Posbest.score;
                            BestPrey.Posbest.x            = ((Prey)particlesArray[i]).Posbest.x;
                            BestPrey.Posbest.y            = ((Prey)particlesArray[i]).Posbest.y;
                            BestPrey.Velocity.x           = ((Prey)particlesArray[i]).Velocity.x;
                            BestPrey.Velocity.y           = ((Prey)particlesArray[i]).Velocity.y;
                        }
                    }
                }
                System.Diagnostics.Debug.WriteLine(BestPrey.CurrentPostion.score);
                textBox2.Text = BestPrey.CurrentPostion.score.ToString();
                // make things tired

                if (tiredcount > stamina)
                {
                    tired = tired * tiredRatePrey;
                }

                if (tiredcountPredator > staminaPredator)
                {
                    tiredPredator      = tiredPredator * tiredRatePredator;
                    tiredcountPredator = 0;
                }
                tiredcountPredator++;
                tiredcount++;


                particlesList = particlesArray.ToList();
                DrawAllOfIt(TempImg, particlesList);

                if (EndCondition(currentScoreMax))
                {
                    break;
                }
                txtiteration.Text = countGameIteration.ToString();
                countGameIteration++;

                if (maxIteration(countGameIteration))
                {
                    break;
                }
            }
        }