Пример #1
0
 public void EmitIndividual(Individual indvd)
 {
     AddIndividual(indvd);
     if (Population.Sum(s => s.Count) > StaticWorldParameters.numberOfIndividualsMax -1)
     {
         swarmInXOrder.Remove(swarmInBirthOrder[swarmInBirthOrder.IndexOf(swarmInBirthOrder.First())]);
         swarmInYOrder.Remove(swarmInBirthOrder[swarmInBirthOrder.IndexOf(swarmInBirthOrder.First())]);
         Population.TryRemoveFromExisitingSpecies(swarmInBirthOrder.First());
         swarmInBirthOrder.Remove(swarmInBirthOrder.First());
     }
 }
Пример #2
0
 public void EraseIndividual(Individual match)
 {
     if (Population.Sum(s => s.Count) >= 2 &&
         match != null)
     {
         swarmInXOrder.Remove(swarmInBirthOrder[swarmInBirthOrder.IndexOf(match)]);
         swarmInYOrder.Remove(swarmInBirthOrder[swarmInBirthOrder.IndexOf(match)]);
         Population.TryRemoveFromExisitingSpecies(match);
         swarmInBirthOrder.Remove(match);
     }
 }
Пример #3
0
 private static SaveGenome GetSavedGenomeFromIndividual(Individual individual)
 {
     SaveGenome savedGenome = new SaveGenome();
     savedGenome.neighborhoodRadius = individual.Genome.getNeighborhoodRadius();
     savedGenome.normalSpeed = individual.Genome.getNormalSpeed();
     savedGenome.maxSpeed = individual.Genome.getMaxSpeed();
     savedGenome.c1 = individual.Genome.getC1();
     savedGenome.c2 = individual.Genome.getC2();
     savedGenome.c3 = individual.Genome.getC3();
     savedGenome.c4 = individual.Genome.getC4();
     savedGenome.c5 = individual.Genome.getC5();
     savedGenome.x = individual.X;
     savedGenome.y = individual.Y;
     savedGenome.dx = individual.Dx;
     savedGenome.dy = individual.Dy;
     savedGenome.dx2 = individual.Dx2;
     savedGenome.dy2 = individual.Dy2;
     savedGenome.type = individual.EmitterType;
     savedGenome.isMobile = individual.IsMobile;
     return savedGenome;
 }
Пример #4
0
 private void ResetColor(Individual indvd)
 {
     //TODO Hard coded color should come from config
     indvd.setDisplayColor(Color.MidnightBlue);
 }
Пример #5
0
        /// <summary>
        /// Find if an individual agent is in a cluster
        /// </summary>
        /// <param name="individual">Agent in cluster</param>
        /// <returns></returns>
        private bool InExistingCluster(Individual individual)
        {
            List<int> clustersIds = new List<int>();
            lastfew = new List<Individual>();

            for (int c = 0; c < tempClusters.Count; c++)
            {
                lastfew = tempClusters[c].Skip(Math.Max(0, tempClusters[c].Count() - clusterBackCount)).Take(clusterBackCount).ToList();

                for (int i = 0; i < lastfew.Count(); i++)
                {
                    double newdis = (individual.X - lastfew[i].X) * (individual.X - lastfew[i].X) + (individual.Y - lastfew[i].Y) * (individual.Y - lastfew[i].Y);

                    if (newdis < lastfew[i].Genome.getNeighborhoodRadius() * lastfew[i].Genome.getNeighborhoodRadius())
                    {
                        clustersIds.Add(c);
                        break;
                    }
                }
            }

            if (clustersIds.Count > 0)
            {
                if (clustersIds.Count > 1)
                {
                    //Merge the clusters
                    tempClusters[clustersIds[0]].AddRange(tempClusters[clustersIds[1]]);
                    tempClusters.RemoveAt(clustersIds[1]);
                    tempClusters[clustersIds[0]].Add(individual);
                    return true;
                }
                tempClusters[clustersIds[0]].Add(individual);
                return true;
            }
            return false;
        }
Пример #6
0
        public override void HandleInput(InputHelper input, Microsoft.Xna.Framework.GameTime gameTime)
        {
            supers.Clear();
            #if WINDOWS
            var surfacetouches = input.SurfaceTouches;
            if (surfacetouches.Count > 0)
            {
                //Surface
                for (int i = 0; i < surfacetouches.Count; i++)
                {
                    Vector2 position = Camera.ConvertScreenToWorldAndDisplayUnits(new Vector2(surfacetouches[i].X, surfacetouches[i].Y));

                    supers[i] = new Individual(i,((double)position.X),
                         ((double)position.Y),
                         0.0, 0.0, new Parameters());
                }
            }
            else
            #endif
            if (input.Touches.Count > 0)
            {
                //Everthing else touch
                for (int i = 0; i < input.Touches.Count; i++)
                {
                    Vector2 position = Camera.ConvertScreenToWorldAndDisplayUnits(new Vector2(input.Touches[i].Position.X, input.Touches[i].Position.Y));

                    supers[i] = new Individual(i, ((double)position.X),
                                              ((double)position.Y),
                                              0.0, 0.0, new SuperParameters());
                }
            }
            else if (supers.Count <= 0 &&
                    (input.Cursor != Vector2.Zero) &&
                    input.MouseState.LeftButton == ButtonState.Pressed)
            {
                //Mouse
                supers.Add(0, new Individual());
                for (int i = 0; i < supers.Count; i++)
                {
                    Vector2 position = Camera.ConvertScreenToWorldAndDisplayUnits(input.Cursor);

                    supers[i] = new Individual(i, ((double)position.X),
                         ((double)position.Y),
                         0, 0, new SuperParameters());
                }
            }

            if (input.IsNewKeyPress(Keys.A))
            {
                controlClient.ToggleAnalyze();
            }

            if (input.IsNewKeyPress(Keys.C))
            {
                debugComponent.SetVisiblity();
            }

            ButtonSection.HandleInput(input, gameTime);
            base.HandleInput(input, gameTime);
        }
Пример #7
0
 private void DrawIndividual(Individual indvd, Color color, Texture2D texture, EmitterActionType type)
 {
     if (!indvd.IsMobile)
     {
         ScreenManager.SpriteBatch.Draw(bigIndividualTexture, new Rectangle(
             (int)indvd.X,
             (int)indvd.Y, bigIndividualTexture.Width, bigIndividualTexture.Height),
             null,
             color,
             (float)Math.Atan2(indvd.Dy, indvd.Dx),
             new Vector2(bigIndividualTexture.Width / 2, bigIndividualTexture.Height / 2),
             SpriteEffects.None, 0);
     }
     else
     {
         ScreenManager.SpriteBatch.Draw(texture, new Rectangle(
             (int)indvd.X,
             (int)indvd.Y, texture.Width, texture.Height),
             null,
             color,
             (float)Math.Atan2(indvd.Dy, indvd.Dx),
             new Vector2(texture.Width / 2, texture.Height / 2),
             SpriteEffects.None, 0);
     }
 }
Пример #8
0
        public void Update(List<Individual> individuals)
        {
            int numberOfSwarm = individuals.Count;
            int currentX, currentY;

            for (int i = 0; i < numberOfSwarm; i++)
            {
                currentInd = individuals[i];
                currentX = (int)currentInd.X;
                currentY = (int)currentInd.Y;

                if (currentX > rightBound)
                {
                    //Right
                    HandleWallAction(borderWalls[2].GetWallActionType(), borderWalls[2].GetWallOrientation(), currentInd);
                    //debugScreen.AddDebugItem("BORDER RIGHT", currentInd.X.ToString(), ScreenSystem.Debug.DebugFlagType.Odd);
                }
                else if (currentX < -rightBound)
                {
                    //Left
                    HandleWallAction(borderWalls[0].GetWallActionType(), borderWalls[0].GetWallOrientation(), currentInd);
                    //debugScreen.AddDebugItem("BORDER LEFT", currentInd.Y.ToString());

                }

                if (currentY > bottomBound)
                {
                    //Bottom
                    HandleWallAction(borderWalls[3].GetWallActionType(), borderWalls[3].GetWallOrientation(), currentInd);

                }
                else if (currentY < -bottomBound)
                {
                    //Top
                    HandleWallAction(borderWalls[1].GetWallActionType(), borderWalls[1].GetWallOrientation(), currentInd);
                }
            }
        }
Пример #9
0
 private void InitCollections(Individual indvd)
 {
     swarmInBirthOrder.Add(indvd);
     swarmInXOrder.Add(indvd);
     swarmInYOrder.Add(indvd);
 }
Пример #10
0
 private void AddIndividual(Individual indvd)
 {
     if (Population.Sum(s => s.Count) < StaticWorldParameters.numberOfIndividualsMax)
     {
         InitCollections(indvd);
         Population.TryAddToExistingSpecies(indvd);
     }
 }
Пример #11
0
        public void stepSimulation(List<Individual> temporaryIndividuals, int weightOfTemporaries)
        {
            int numberOfSwarm = swarmInBirthOrder.Count();

            for (int i = 0; i < numberOfSwarm; i++)
            {
                currentInd = swarmInBirthOrder[i];
                param = currentInd.Genome;
                tempX = currentInd.X;
                tempY = currentInd.Y;

                neighborhoodRadiusSquared = param.getNeighborhoodRadius()
                        * param.getNeighborhoodRadius();

                List<Individual> neighbors = new List<Individual>();

                // Detecting neighbors using sorted lists
                minX = tempX - param.getNeighborhoodRadius();
                maxX = tempX + param.getNeighborhoodRadius();
                minY = tempY - param.getNeighborhoodRadius();
                maxY = tempY + param.getNeighborhoodRadius();
                minRankInXOrder = currentInd.RankInXOrder;
                maxRankInXOrder = currentInd.RankInXOrder;
                minRankInYOrder = currentInd.RankInYOrder;
                maxRankInYOrder = currentInd.RankInYOrder;

                //TODO: Make this faster
                for (int j = currentInd.RankInXOrder - 1; j >= 0; j--)
                {
                    if (swarmInXOrder[j].X >= minX)
                        minRankInXOrder = j;
                    else
                        break;
                }
                for (int j = currentInd.RankInXOrder + 1; j < numberOfSwarm; j++)
                {
                    if (swarmInXOrder[j].X <= maxX)
                        maxRankInXOrder = j;
                    else
                        break;
                }
                for (int j = currentInd.RankInYOrder - 1; j >= 0; j--)
                {
                    if (swarmInYOrder[j].Y >= minY)
                        minRankInYOrder = j;
                    else
                        break;
                }
                for (int j = currentInd.RankInYOrder + 1; j < numberOfSwarm; j++)
                {
                    if (swarmInYOrder[j].Y <= maxY)
                        maxRankInYOrder = j;
                    else
                        break;
                }

                if (maxRankInXOrder - minRankInXOrder < maxRankInYOrder
                        - minRankInYOrder)
                {
                    for (int j = minRankInXOrder; j < maxRankInXOrder; j++)
                    {
                        tempSwarm2 = swarmInXOrder[j];
                        if (currentInd != tempSwarm2)
                            if (tempSwarm2.RankInYOrder >= minRankInYOrder
                                    && tempSwarm2.RankInYOrder <= maxRankInYOrder)
                            {
                                if ((tempSwarm2.X - currentInd.X)
                                        * (tempSwarm2.X - currentInd.X)
                                        + (tempSwarm2.Y - currentInd.Y)
                                        * (tempSwarm2.Y - currentInd.Y) < neighborhoodRadiusSquared)
                                    neighbors.Add(tempSwarm2);
                            }
                    }
                }
                else
                {
                    for (int j = minRankInYOrder; j < maxRankInYOrder; j++)
                    {
                        tempSwarm2 = swarmInYOrder[j];
                        if (currentInd != tempSwarm2)
                            if (tempSwarm2.RankInXOrder >= minRankInXOrder
                                    && tempSwarm2.RankInXOrder <= maxRankInXOrder)
                            {
                                if ((tempSwarm2.X - currentInd.X)
                                        * (tempSwarm2.X - currentInd.X)
                                        + (tempSwarm2.Y - currentInd.Y)
                                        * (tempSwarm2.Y - currentInd.Y) < neighborhoodRadiusSquared)
                                    neighbors.Add(tempSwarm2);
                            }
                    }
                }

                for (int k = 0; k < temporaryIndividuals.Count; k++)
                {
                    if ((temporaryIndividuals[k].X - currentInd.X)
                        * (temporaryIndividuals[k].X - currentInd.X)
                        + (temporaryIndividuals[k].Y - currentInd.Y)
                        * (temporaryIndividuals[k].Y - currentInd.Y) < neighborhoodRadiusSquared)
                        for (int j = 0; j < weightOfTemporaries; j++)
                            neighbors.Add(temporaryIndividuals[k]);
                }

                // simulating the behavior of swarm agents

                int n = neighbors.Count();

                if (n == 0)
                {

                    tempAx = rand.NextDouble() - 0.5;
                    tempAy = rand.NextDouble() - 0.5;
                }
                else
                {
                    localCenterX = 0;
                    localCenterY = 0;
                    localDX = 0;
                    localDY = 0;
                    for (int j = 0; j < n; j++)
                    {
                        tempSwarm2 = neighbors[j];
                        localCenterX += tempSwarm2.X;
                        localCenterY += tempSwarm2.Y;
                        localDX += tempSwarm2.Dx;
                        localDY += tempSwarm2.Dy;
                    }
                    localCenterX /= n;
                    localCenterY /= n;
                    localDX /= n;
                    localDY /= n;

                    tempAx = tempAy = 0;

                    tempAx += (localCenterX - tempX) * param.getC1();
                    tempAy += (localCenterY - tempY) * param.getC1();

                    tempAx += (localDX - currentInd.Dx) * param.getC2();
                    tempAy += (localDY - currentInd.Dy) * param.getC2();

                    for (int j = 0; j < n; j++)
                    {
                        tempSwarm2 = neighbors[j];
                        tempX2 = tempSwarm2.X;
                        tempY2 = tempSwarm2.Y;
                        double d = (tempX - tempX2) * (tempX - tempX2) + (tempY - tempY2)
                                * (tempY - tempY2);
                        if (d == 0)
                            d = 0.001;
                        tempAx += (tempX - tempX2) / d * param.getC3();
                        tempAy += (tempY - tempY2) / d * param.getC3();
                    }

                    //rand = new Random();
                    //if (rand.NextDouble() < param.getC4())
                    //{
                    //    tempAx += rand.NextDouble() * 10 - 5;
                    //    tempAy += rand.NextDouble() * 10 - 5;
                    //}
                }

                currentInd.accelerate(tempAx, tempAy, param.getMaxSpeed());

                tempDX = currentInd.Dx2;
                tempDY = currentInd.Dy2;
                double f = Math.Sqrt(tempDX * tempDX + tempDY * tempDY);
                if (f == 0)
                    f = 0.001;
                currentInd.accelerate(tempDX * (param.getNormalSpeed() - f) / f
                        * param.getC5(),
                        tempDY * (param.getNormalSpeed() - f) / f * param.getC5(),
                        param.getMaxSpeed());

            };
            updateInternalState();
        }
Пример #12
0
 private void EmitIndividual(Individual indvd, string group, ControlGroups groups)
 {
     indvd.ID = GetNextIndividualID();
     populationSimulator.EmitIndividual(indvd);
     groups.Add(indvd.ID, group);
 }