/// <summary> /// Initialize Seekers List /// </summary> private void InitSeekers() { StaticClass.StartSeekerRot = (float)StaticClass.rnd.NextDouble() * MathHelper.TwoPi; for (int i = 0; i < StaticClass.SeekerNum; i++) { NeuralNetwork.NeuralNetwork Neuralnetwork = new NeuralNetwork.NeuralNetwork( StaticClass.SeekerInputs, StaticClass.hiddenLayersConfig[StaticClass.rnd.Next(StaticClass.hiddenLayersConfig.Count)], StaticClass.SeekerOutputs); SeekerPop.Add(new Seeker(StaticClass.SeekerAnimation, Neuralnetwork, new BotKeys(), StaticClass.StartSeekerPos, null, Color.White, StaticClass.StartSeekerRot, new Vector2(StaticClass.SeekerAnimation.First().Value.FrameWidth / 2, StaticClass.SeekerAnimation.First().Value.FrameHeight / 2), new Vector2(StaticClass.CharacterScale), 0, 0)); } }
public Seeker(Dictionary <string, Animation.Animation> animations, NeuralNetwork.NeuralNetwork Neuralnetwork, BaseKeys keys, Vector2 position, Rectangle?sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) : base(animations.First().Value, position, sourceRectangle, color, rotation, origin, scale, effects, layerDepth) { this.animations = animations; this.keys = keys; LifeTime = 1; Score = 0; this.Neuralnetwork = Neuralnetwork; Energy = 501; MainGame.UpdateEvent += update; }
/// <summary> /// Create seeker population by the best seekers /// </summary> private void CreateSeekerPopulation() { // sort dead seekers by the best fitness they achive DeadSeekers.Sort(new SeekerCompare()); DeadSeekers.Reverse(); // set the best seeker by the most fitness points if (StaticClass.TopSeeker == null || DeadSeekers[0].Fitness > StaticClass.TopSeeker.Fitness) { StaticClass.TopSeeker2 = StaticClass.TopSeeker; StaticClass.TopSeeker = DeadSeekers[0]; } // Change the dictionary if (StaticClass.TopSeeker2 == null) { StaticClass.TopSeeker2 = DeadSeekers[1]; } StaticClass.Stats["MaxFitness"] = Math.Max(StaticClass.Stats["MaxFitness"], DeadSeekers[0].Fitness); StaticClass.Stats["LastFitness"] = DeadSeekers[0].Fitness; if (DeadSeekers[0].Fitness == 0) { throw new Exception(); } // create green seekers that Their Neural Network Similar the best seeker for (int i = 0; i < StaticClass.SeekerNum * 0.35; i++) { NeuralNetwork.NeuralNetwork Neuralnetwork = StaticClass.TopSeeker.Neuralnetwork.Copy(); Neuralnetwork.Cross(DeadSeekers[0].Neuralnetwork); Neuralnetwork.ChangeNeuronWeights(StaticClass.shakeRate); SeekerPop.Add(new Seeker(StaticClass.SeekerAnimation, Neuralnetwork, new BotKeys(), StaticClass.StartSeekerPos, null, Color.Green, StaticClass.StartSeekerRot, new Vector2(StaticClass.SeekerAnimation.First().Value.FrameWidth / 2, StaticClass.SeekerAnimation.First().Value.FrameHeight / 2), new Vector2(StaticClass.CharacterScale), 0, 0)); } // create blue seekers that Their Neural Network With a little change of the best seeker for (int i = 0; i < StaticClass.SeekerNum * 0.35; i++) { NeuralNetwork.NeuralNetwork Neuralnetwork = StaticClass.TopSeeker.Neuralnetwork.Copy(); Neuralnetwork.ChangeNeuronWeights(StaticClass.shakeRate); SeekerPop.Add(new Seeker(StaticClass.SeekerAnimation, Neuralnetwork, new BotKeys(), StaticClass.StartSeekerPos, null, Color.Blue, StaticClass.StartSeekerRot, new Vector2(StaticClass.SeekerAnimation.First().Value.FrameWidth / 2, StaticClass.SeekerAnimation.First().Value.FrameHeight / 2), new Vector2(StaticClass.CharacterScale), 0, 0)); } // create red seekers that Their Neural Network With a little change of the second best seeker for (int i = 0; i < StaticClass.SeekerNum * 0.2; i++) { NeuralNetwork.NeuralNetwork Neuralnetwork = StaticClass.TopSeeker2.Neuralnetwork.Copy(); Neuralnetwork.ChangeNeuronWeights(StaticClass.shakeRate); SeekerPop.Add(new Seeker(StaticClass.SeekerAnimation, Neuralnetwork, new BotKeys(), StaticClass.StartSeekerPos, null, Color.Red, StaticClass.StartSeekerRot, new Vector2(StaticClass.SeekerAnimation.First().Value.FrameWidth / 2, StaticClass.SeekerAnimation.First().Value.FrameHeight / 2), new Vector2(StaticClass.CharacterScale), 0, 0)); } // create regular seekers that Their Neural Network is Random for (int i = 0; i < StaticClass.SeekerNum * 0.1; i++) { NeuralNetwork.NeuralNetwork Neuralnetwork = new NeuralNetwork.NeuralNetwork( StaticClass.SeekerInputs, StaticClass.hiddenLayersConfig[ StaticClass.rnd.Next(StaticClass.hiddenLayersConfig.Count)], StaticClass.SeekerOutputs); SeekerPop.Add(new Seeker(StaticClass.SeekerAnimation, Neuralnetwork, new BotKeys(), StaticClass.StartSeekerPos, null, Color.White, StaticClass.StartSeekerRot, new Vector2(StaticClass.SeekerAnimation.First().Value.FrameWidth / 2, StaticClass.SeekerAnimation.First().Value.FrameHeight / 2), new Vector2(StaticClass.CharacterScale), 0, 0)); } StaticClass.LiveTopSeeker = null; DeadSeekers.Clear(); StaticClass.PopulationNumber++; StaticClass.Stats["NumOfFounds"] = StaticClass.NumOfFounds; StaticClass.NumOfFounds = 0; }