Ejemplo n.º 1
0
        public NeuralNet CrossOver(MovingAgent parentOne, MovingAgent parentTwo)
        {
            NeuralNet    neuralNet        = new NeuralNet(true);
            List <float> newWeights       = new List <float>();
            List <float> parentOneWeights = parentOne.GetNetworkWeights();
            List <float> parentTwoWeights = parentTwo.GetNetworkWeights();

            int crossOverPoint;

            if (Utilities.IsRandomCrossoverPoint)
            {
                crossOverPoint = (int)Utilities.RandomMinMax(0, parentOneWeights.Count);
            }
            else
            {
                crossOverPoint = (int)(parentOneWeights.Count * Utilities.CrossoverPoint);
            }

            for (int i = 0; i < crossOverPoint; i++)
            {
                newWeights.Add(parentOneWeights[i]);
            }

            for (int i = crossOverPoint; i < parentOneWeights.Count; i++)
            {
                newWeights.Add(parentTwoWeights[i]);
            }

            neuralNet.SetWeights(ref newWeights);

            return(neuralNet);
        }
Ejemplo n.º 2
0
 //When agent moves off screen - moves them to opposite side
 public static void CheckOutOfBounds(MovingAgent agent)
 {
     if (agent.Position.X > ScreenWidth + agent.Size)
     {
         agent.ResetLines();
         //agent.Energy-=10;
         agent.PositionX = 0f;
         //agent.PositionX = ScreenWidth - agent.Size;
     }
     if (agent.Position.X < 0 - agent.Size)
     {
         agent.ResetLines();
         // agent.Energy-=10;
         agent.PositionX = ScreenWidth;
     }
     if (agent.Position.Y > ScreenHeight + agent.Size)
     {
         agent.ResetLines();
         // agent.Energy-=10;
         agent.PositionY = 0f;
     }
     if (agent.Position.Y < 0 - agent.Size)
     {
         agent.ResetLines();
         //agent.Energy-=10;
         agent.PositionY = ScreenHeight;
     }
 }
Ejemplo n.º 3
0
        public Behaviours(MovingAgent agent)
        {
            cohesionLine   = Vector2.Zero;
            seperationLine = Vector2.Zero;
            alignmentLine  = Vector2.Zero;
            seekLine       = Vector2.Zero;

            this.agent = agent;
            rand       = Utilities.Random;
        }
Ejemplo n.º 4
0
        private void DrawRadiusCircles(MovingAgent agent)
        {
            if (Utilities.IsSightRadiusVisible)
            {
                spriteBatch.Draw(circleTexture, new Rectangle((int)(agent.Position.X - (agent.SightRadius / 2)),
                                                              (int)(agent.Position.Y - (agent.SightRadius / 2)),
                                                              (int)agent.SightRadius, (int)agent.SightRadius), Color.Red);
            }

            if (Utilities.IsLateralLineVisible)
            {
                spriteBatch.Draw(circleTexture, new Rectangle((int)(agent.Position.X - (agent.LateralRadius / 2)),
                                                              (int)(agent.Position.Y - (agent.LateralRadius / 2)),
                                                              (int)agent.LateralRadius, (int)agent.LateralRadius), Color.White);
            }
        }
Ejemplo n.º 5
0
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return(1);
            }

            MovingAgent otherAgent = obj as MovingAgent;

            if (otherAgent != null)
            {
                //return this.TimeAlive.CompareTo(otherAgent.TimeAlive);
                return(otherAgent.TimeAlive - this.TimeAlive);
            }
            else
            {
                return(1);
            }
        }
Ejemplo n.º 6
0
        public MovingAgent TournamentSelection()
        {
            float       bestFitnessSoFar = 0;
            MovingAgent choosenAgent     = null;

            //preyPool.Sort();


            for (int i = 0; i < Utilities.NumOfTourCompetitors; i++)
            {
                MovingAgent currentAgent
                    = preyPool[(int)Utilities.RandomMinMax(0, preyPool.Count * Utilities.PercentToSelect)];

                if (currentAgent.TimeAlive > bestFitnessSoFar)
                {
                    choosenAgent     = currentAgent;
                    bestFitnessSoFar = currentAgent.TimeAlive;
                }
            }
            return(choosenAgent);
        }
Ejemplo n.º 7
0
        public MovingAgent FitnessProportionateSelection()
        {
            float randomSlice = Utilities.RandomMinMax(0, totalFitnessScore);

            MovingAgent choosenAgent = null;

            float fitnessTotal = 0;

            for (int i = 0; i < preyPool.Count; i++)
            {
                fitnessTotal += preyPool[i].TimeAlive;

                if (fitnessTotal > randomSlice)
                {
                    choosenAgent = preyPool[i];
                    break;
                }
            }

            return(choosenAgent);
        }
Ejemplo n.º 8
0
        public void Update(GameTime gameTime, ref List <Prey> prey, ref List <Agent> vegetation,
                           ref List <Predator> predator)
        {
            EnsureCorrectVegetationNumbers(vegetation);

            for (int i = 0; i < prey.Count; i++)
            {
                if (prey[i].IsAlive)
                {
                    UpdateAliveAgent(gameTime, prey, vegetation, predator, i);
                }
                else
                {
                    UpdatePool(prey[i]);

                    CalculateTotalFitness();



                    if (preyCreatedSoFar < Utilities.NumOfPreyOnScreen)
                    {
                        CreateAgentInList(prey, i);
                    }
                    else
                    {
                        if (preyPool.Count > 1)
                        {
                            MovingAgent parentOne = FitnessProportionateSelection();
                            MovingAgent parentTwo = FitnessProportionateSelection();

                            if (!parentOne.Equals(parentTwo))
                            {
                                CreateAgentFromCrossover(parentOne, parentTwo, prey, i);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 9
0
        private void CreateAgentFromCrossover(MovingAgent parentOne, MovingAgent parentTwo,
                                              List <Prey> prey, int i)
        {
            NeuralNet neuralNetwork = CrossOver(parentOne, parentTwo);

            int tempX = (int)Utilities.RandomMinMax(0 + Utilities.AgentTextureSize, Utilities.ScreenWidth - Utilities.AgentTextureSize);
            int tempY = (int)Utilities.RandomMinMax(0 + Utilities.AgentTextureSize, Utilities.ScreenHeight - Utilities.AgentTextureSize);

            prey[i] = new Prey(preyTexture, new Vector2(tempX, tempY),
                               Utilities.AgentTextureSize, Utilities.Mass, Utilities.SightRadiusPrey,
                               Utilities.LateralLinePrey, neuralNetwork);


            if (Utilities.RandomMinMax(0, 1) < Utilities.MutationChance)
            {
                prey[i].Mutate();
            }


            preyCreatedSoFar++;

            UpdateGenerationNumber();
        }
Ejemplo n.º 10
0
        public BehaviourManager(MovingAgent agent)
        {
            behaviourWeights = new List <float>();



            /* seperationSightWeight = 2.0f;
             * seperationLateralWeight = 2.0f;
             *
             * alignmentSightWeight = 0f;
             * alignmentLateraltWeight = 0f;
             *
             * cohesionSightWeight = 0f;
             * cohesionLateralWeight = 0f;
             *
             * wanderWeight = 0.4f;
             *
             * seekSightFoodWeight = 0f;
             * seekLateralFoodWeight = 0f;*/

            this.agent = agent;

            behaviour = new Behaviours(agent);
        }
Ejemplo n.º 11
0
        public void Update(GameTime gameTime, ref List <Prey> prey, ref List <Predator> pred)
        {
            for (int i = 0; i < pred.Count; i++)
            {
                if (pred[i].IsAlive)
                {
                    UpdateAliveAgent(gameTime, ref prey, ref pred, i);
                }
                else
                {
                    UpdatePool(pred[i]);

                    CalculateTotalFitness();



                    if (predCreatedSoFar < Utilities.NumOfPredatorsOnScreen)
                    {
                        CreateAgentInList(ref pred, i);
                    }
                    else
                    {
                        if (predPool.Count > 1)
                        {
                            MovingAgent parentOne = FitnessProportionateSelection();
                            MovingAgent parentTwo = FitnessProportionateSelection();

                            if (!parentOne.Equals(parentTwo))
                            {
                                CreateAgentFromCrossover(parentOne, parentTwo, pred, i);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 12
0
        private void DrawBehaviourLines(MovingAgent agent)
        {
            if (Utilities.IsLineCohesionDrawingOn)
            {
                if (agent.GetCohesionLine() != Vector2.Zero)
                {
                    if (agent.GetCohesionWeight() > 0 && agent.GetCohesionWeight() < 0.2)
                    {
                        DrawLine(0.2f, Color.Blue, agent.Position, agent.GetCohesionLine());
                    }
                    else
                    {
                        DrawLine(agent.GetCohesionWeight() + 0.2f, Color.Blue, agent.Position, agent.GetCohesionLine());
                    }
                }
            }

            if (Utilities.IsLineAlignmentDrawingOn)
            {
                if (agent.GetAlignmentLine() != Vector2.Zero)
                {
                    if (agent.GetAlignmentWeight() > 0 && agent.GetAlignmentWeight() < 0.2)
                    {
                        DrawLine(0.2f, Color.Green, agent.Position, agent.GetAlignmentLine());
                    }
                    else
                    {
                        DrawLine(agent.GetAlignmentWeight(), Color.Green, agent.Position, agent.GetAlignmentLine());
                    }
                }
            }

            if (Utilities.IsLineSeperationDrawingOn)
            {
                if (agent.GetSeperationLine() != Vector2.Zero)
                {
                    if (agent.GetSeperationWeight() > 0 && agent.GetSeperationWeight() < 0.2)
                    {
                        DrawLine(0.2f, Color.Red, agent.Position, agent.GetSeperationLine());
                    }
                    else
                    {
                        DrawLine(agent.GetSeperationWeight(), Color.Red, agent.Position, agent.GetSeperationLine());
                    }
                }
            }

            if (Utilities.IsLineSeekDrawingOn)
            {
                if (agent.GetSeekLine() != Vector2.Zero)
                {
                    if (agent.GetSeekWeight() > 0 && agent.GetSeekWeight() < 0.2)
                    {
                        DrawLine(0.2f, Color.Yellow, agent.Position, agent.GetSeekLine());
                    }
                    else
                    {
                        DrawLine(agent.GetAlignmentWeight(), Color.Yellow, agent.Position, agent.GetSeekLine());
                    }
                }
            }
        }