/// <summary> /// Simulation entry point /// </summary> /// <param name="args">not used</param> public static void Main(string[] args) { // Create and run the simulation engine Engine = new EvoEngine(); Engine.Run(); Engine.Dispose(); }
internal virtual void Think(EvoEngine engine) { Food food = GetClosestFood(engine); if (food == null) { return; // Do nothing if no food is found } if (food.Location.X > snake.Location.X) { snake.AccelerateRight(); } else { snake.AccelerateLeft(); } if (food.Location.Y > snake.Location.Y) { snake.AccelerateDown(); } else { snake.AccelerateUp(); } }
internal override void Think(EvoEngine engine) { if (Network == null) { Network = new ActivationNetwork(new BipolarSigmoidFunction(), WorldScene.NeuralNetworkShape[0], WorldScene.NeuralNetworkShape.Skip(1).ToArray()); // Nw = (I+1)*H1 +(H1+1)*H2 +(H2+1)*O // I = inputs // H1 = neurons in hidden layer 1 // H2 = neurons in hidden layer 2 // O = Number of outputs // Nw = (5+1)*0 + (0+1)*0 + (5+1)*4 = 24 // 5in 4out // Nw = (5+1)*4 + (4+1)*0 + (4+1)*4 = 44 // 5in 1hl4 4out // Nw = (11+1)*4 + (4+1)*0 + (4+1)*4 = 68 // 11in 1hl4 4out var dna = GetCurrentGene(); SetNetworkWeights(dna); //float scale = (float)dna.Genes[scene.NeuralNetworkWeightsCount - 1 + 0]; //if (scale < 0.4) scale = 0.4f; //if (scale > 0.6) scale = 0.6f; //Snake.SetScale(scale); //int div = scene.NeuralNetworkWeightsCount / 3; //Color c = new Color( // (int)Math.Max((dna.Genes[div*0] * 255), 128), // (int)Math.Max((dna.Genes[div*1] * 255), 128), // (int)Math.Max((dna.Genes[div*2] * 255), 128) // ); //Snake.Color = c; } if (engine.KeyState.GetPressedKeys().Length > 0) { return; } double[] netout = Network.Compute(convertToDouble(Snake.GetFoodSensorsActivation())); //if (Math.Max(netout[0], netout[1]) > 0.5) if (netout[0] > netout[1]) { Snake.Accelerate(); } else { Snake.Brake(); } //if (Math.Max(netout[2], netout[3]) > 0.5) if (netout[2] > netout[3]) { Snake.RotateLeft(); } else { Snake.RotateRight(); } }
private Food GetClosestFood(EvoEngine engine) { WorldScene scene = Extensions.GetWorldScene(); Food food = null; float dist = float.MaxValue; foreach (IEntity entity in scene.World) { if (entity.GetType() == typeof(Food)) { float fooddist = Extensions.GetVectorDistance(snake.Location, entity.Location); if (fooddist < dist && dist > 50.0f) //Ensure we do not circle current food { food = (Food)entity; dist = fooddist; } } } return(food); }
public WorldScene(EvoEngine engine) : base(0, false, false, false) { this.engine = engine; this.NeuralNetworkWeightsCount = Extensions.NeuralNetworkWeightCount(NeuralNetworkShape); }