public World(ContentManager content, Rectangle Bounds) { this.Bounds = Bounds; this.TextureCreature = content.Load<Texture2D>("Bug"); this.TexturePoint = content.Load<Texture2D>("Point"); this.TextureFood = content.Load<Texture2D>("Circle"); this.MyFont = content.Load<SpriteFont>("MyFont"); this.camera = new Camera(new Viewport(this.Bounds)); this.basicshapes = new BasicShapes(); creature = new List<Creature>(); for (int i = 0; i < NrOfCreatures; i++) { Creature c = new Creature(Bounds); creature.Add(c); } food = new List<Food>(); for (int i = 0; i < NrOfFood; i++) { Food f = new Food(Bounds); food.Add(f); } obstacle = new List<Obstacle>(); for (int i = 0; i < NrOfObstacles; i++) { Obstacle o = new Obstacle(Bounds); obstacle.Add(o); } camera = new Camera(new Viewport(0, 0, 1600, 900)); GA = new GeneticAlgorithm(60, 1); NrOfDeaths = 0; GraphValue = new double[32000]; DoDraw = true; }
public void CrossOver(List<Creature> creature, Rectangle Bounds) { int NrOfCrossOver = (int)(creature.Count() * (double)(CrossOverChance / 100)); for (int j = 0; j < NrOfCrossOver; j++) { Random random = new Random(Guid.NewGuid().GetHashCode()); Creature ParentA = Selection(); Creature ParentB = Selection(); double[] ParentAWeights = ParentA.Brain.GetWeights(); double[] ParentBWeights = ParentB.Brain.GetWeights(); double[] ChildWeights = new double[ParentAWeights.Length]; int CrossOverPoint = random.Next(0, ParentAWeights.Length); for (int i = 0; i < CrossOverPoint; i++) { ChildWeights[i] = ParentAWeights[i]; } for (int i = CrossOverPoint; i < ParentAWeights.Length; i++) { ChildWeights[i] = ParentBWeights[i]; } Creature Child = new Creature(Bounds); Child.Brain.SetWeights(ChildWeights); NextGeneration.Add(Child); } }