Beispiel #1
0
        public int gecisKurali(int mevcutTepe, Ant ant)
        {
            int q            = rnd.Next() % 101;
            int gidilcekTepe = 0;

            if (q <= 90)
            {
                gidilcekTepe = gecisKuraliFeromonunMaxOldugu(mevcutTepe, ant.gidilebilenTepeler);
            }
            else
            {
                gidilcekTepe = rouletteWheel(mevcutTepe, ant.gidilebilenTepeler);
            }

            //Gidilcek Tepeyi Döndür !

            return(gidilcekTepe);
        }
Beispiel #2
0
        public double antToplamYol(Ant ant)
        {
            double toplamYol = 0;

            for (int i = 0; i < ant.gidilenTepeler.Count - 1; i++)
            {
                int           m, g;
                List <String> saydim = new List <String>();//bulundugu iterasyonda aynı yola birden fazla kez feromon koymaması icin
                m = ant.gidilenTepeler.ElementAt(i);
                g = ant.gidilenTepeler.ElementAt(i + 1);
                if (!saydim.Contains(m + "," + g) && !saydim.Contains(g + "," + m))
                {
                    saydim.Add(m + "," + g);
                    if (yollardakiUzakliklar[ant.gidilenTepeler.ElementAt(i), ant.gidilenTepeler.ElementAt(i + 1)] != 0)
                    {
                        toplamYol += 1 / yollardakiUzakliklar[ant.gidilenTepeler.ElementAt(i), ant.gidilenTepeler.ElementAt(i + 1)];
                    }
                }
            }
            return(toplamYol);
        }
Beispiel #3
0
        public void lokalFeromonGuncelle(Ant ant)//her karinca icin cagirilcak
        {
            for (int i = 0; i < ant.gidilenTepeler.Count - 1; i++)
            {
                int           m, g;
                List <String> saydim = new List <String>();//bulundugu iterasyonda aynı yola birden fazla kez feromon koymaması icin
                m = ant.gidilenTepeler.ElementAt(i);
                g = ant.gidilenTepeler.ElementAt(i + 1);
                if (!saydim.Contains(m + "," + g) && !saydim.Contains(g + "," + m))
                {
                    saydim.Add(m + "," + g);

                    // yollardakiFeromonlar[m,g] += 1 / ant.toplamYol;
                    //yollardakiFeromonlar[m, g] -=ant.sure/(ant.sure*cezaPuani);
                    double k = yollardakiFeromonlar[m, g];
                    //yollardakiFeromonlar[m, g] += 1 / (ant.toplamYol)+1/ant.sure;
                    yollardakiFeromonlar[m, g] += Q / (ant.toplamYol);
                    yollardakiFeromonlar[g, m]  = yollardakiFeromonlar[m, g];


                    if (Double.IsInfinity(yollardakiFeromonlar[m, g]) || Double.IsInfinity(yollardakiFeromonlar[g, m]))
                    {
                        int z = 0;
                    }
                }
            }


/*
 *          for (int i = 0; i < ant.gidilenYol.Count - 1; i++)
 *          {
 *              int[] xy = new int[2];
 *
 *              xy=splitAntYol(ant, i);
 *              yollardakiFeromonlar[xy[0],xy[1]] += 1 / ant.toplamYol;
 *              yollardakiFeromonlar[xy[1],xy[0]] = yollardakiFeromonlar[xy[0], xy[1]];//simetrigini ekledim
 *          }*/
        }
Beispiel #4
0
        public Ant gidilebilenTepeleriBelirle(Ant ant)
        {
            if (ant.gidilenTepeler.Count > 0)
            {
                int mevcutTepe = ant.gidilenTepeler.ElementAt(ant.gidilenTepeler.Count - 1);
                for (int j = 0; j < tepeSayisi; j++)
                {
                    if (yollardakiUzakliklar[mevcutTepe, j] != 0 && yollardakiUzakliklar[j, mevcutTepe] != 0)
                    {
                        if (!ant.gidilenYol.Contains(mevcutTepe + "," + j) && !ant.gidilenYol.Contains(j + "," + mevcutTepe))
                        {
                            ant.gidilebilenTepeler.Add(j);
                        }
                    }
                }
            }
            else
            {
                int mevcutTepe = ant.gidilenTepeler.ElementAt(ant.gidilenTepeler.Count - 1);
                for (int j = 0; j < tepeSayisi; j++)
                {
                    if (yollardakiUzakliklar[mevcutTepe, j] != 0 && yollardakiUzakliklar[j, mevcutTepe] != 0)
                    {
                        ant.gidilebilenTepeler.Add(j);
                    }
                }
            }
            if (ant.gidilebilenTepeler.Count == 0)//gidebilecegi yol yoksa geri donecek ve geri dondugu tepe gidilen tepelere eklencek
            {
                ant.gidilenYol.Clear();
                //ant.gidilenYol.Remove(ant.gidilenTepeler.ElementAt(ant.gidilenTepeler.Count-1 ) + "," + ant.gidilenTepeler.ElementAt(ant.gidilenTepeler.Count - 2));
                //ant.gidilenYol.Remove(ant.gidilenTepeler.ElementAt(ant.gidilenTepeler.Count - 2) + "," + ant.gidilenTepeler.ElementAt(ant.gidilenTepeler.Count - 1));

                gidilebilenTepeleriBelirle(ant);
            }

            return(ant);
        }
Beispiel #5
0
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            // if (!oneTime)
            // {
            GraphicsDevice.Clear(DIRT_COLOR);
            // oneTime = true;
            //}
            spriteBatch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend);
            if (!isSimulation)
            {
                spriteBatch.Draw(startScreen, new Rectangle(0, 0, startScreen.Width, startScreen.Height), Color.White);
            }
            else
            {
                spriteBatch.Draw(home, new Rectangle(50, 50, home.Width, home.Height), Color.SandyBrown);

                for (int i = 0; i < 800; i++)
                {
                    for (int j = 0; j < 600; j++)
                    {
                        // Color pixelColor;
                        if (food.dmapVals[i, j] == true)
                        {
                            //  Console.WriteLine(i + " " + j);
                            //    // Draw food
                            //    // pixelColor = FOOD_COLOR;
                            spriteBatch.Draw(whiteRectangle, new Vector2((float)i, (float)j), null,
                                             FOOD_COLOR, 0f, Vector2.Zero, new Vector2(.56f, .56f),
                                             SpriteEffects.None, 0f);
                        }
                    }
                }

                for (int i = 0; i < col.ants.Length; i++)
                {
                    // Console.WriteLine(col.ants.Length);
                    Ant thisAnt = col.ants[i];
                    TrackColor = DIRT_COLOR;
                    oneTime    = false;
                    thisAnt.step();

                    int   thisXi = thisAnt.intX;
                    int   thisYi = thisAnt.intY;
                    float thisXf = (float)thisAnt.x;
                    float thisYf = (float)thisAnt.y;

                    FillColor = (ANT_COLOR);

                    if (thisAnt.hasFood)
                    {
                        oneTime    = true;
                        TrackColor = PHER_FOOD_COLOR;
                        FillColor  = (FOOD_COLOR);
                        if (thisXi > col.x - 10 && thisXi < col.x + 10 && thisYi > col.y - 10 && thisYi < col.y + 10)
                        {
                            // Close enough to home
                            thisAnt.hasFood  = false;
                            thisAnt.homePher = 100;
                            TrackColor       = PHER_FOOD_COLOR;
                        }
                    }
                    else if (food.getValue(thisXi, thisYi))
                    {
                        oneTime          = true;
                        thisAnt.hasFood  = true;
                        thisAnt.foodPher = 100;
                        food.bite(thisXi, thisYi);
                        TrackColor = PHER_FOOD_COLOR;
                    }

                    if (Math.Abs(thisAnt.dx) > Math.Abs(thisAnt.dy))
                    {
                        // Horizontal ant
                        spriteBatch.Draw(whiteRectangle, new Vector2(thisXf, thisYf), null,
                                         FillColor, 0f, Vector2.Zero, new Vector2(3f, 2f),
                                         SpriteEffects.None, 0f);
                        if (oneTime)
                        {
                            trackpoints.Add(new trackpoint(new Vector2(thisXf, thisYf), TrackColor));
                        }
                    }
                    else
                    {
                        // Vertical ant
                        spriteBatch.Draw(whiteRectangle, new Vector2(thisXf, thisYf), null,
                                         FillColor, 0f, Vector2.Zero, new Vector2(2f, 3f),
                                         SpriteEffects.None, 0f);
                        if (oneTime)
                        {
                            trackpoints.Add(new trackpoint(new Vector2(thisXf, thisYf), TrackColor));
                        }
                    }
                }

                foreach (trackpoint p in trackpoints)
                {
                    spriteBatch.Draw(whiteRectangle, new Vector2(p.getPoint().X, p.getPoint().Y), null,
                                     p.getColor() * p.alpha, 0f, Vector2.Zero, new Vector2(0.8f, 0.8f),
                                     SpriteEffects.None, 0f);
                }
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }