public NeuralNetwork[] AddHidden(NeuralNetwork[] neuralNetworkArgs, int percentNegative, int percentHidden) { List<NeuralNetwork> neuralNetwork = new List<NeuralNetwork>(); NeuralNetwork neuralNetworkMethods = new NeuralNetwork(0, 0, 0); int randomInt = random.Next(101); for (int x = 0; x < neuralNetworkArgs.Length; x++) { if (randomInt < percentHidden) { List<Neuron> input = new List<Neuron>(), hidden = new List<Neuron>(), output = new List<Neuron>(); foreach (Neuron neuronArgs in neuralNetworkArgs[x].input) input.Add(neuronArgs); foreach (Neuron neuronArgs in neuralNetworkArgs[x].output) output.Add(neuronArgs); for (int y = 0; y < neuralNetworkArgs[x].hidden.Length; y++) { hidden.Add(neuralNetworkArgs[x].hidden[y]); if (y == neuralNetworkArgs[x].hidden.Length - 1) { if (randomInt < percentNegative) hidden.Add(neuralNetworkMethods.CreateHidden(neuralNetworkArgs[x].hidden.Length, true)); else hidden.Add(neuralNetworkMethods.CreateHidden(neuralNetworkArgs[x].hidden.Length, false)); neuralNetworkMethods.MutateNeuron(hidden[y + 1], output.ToArray()); neuralNetworkMethods.ConnectInput(input.ToArray(), hidden[y + 1]); } } neuralNetwork.Add(new NeuralNetwork(input.Count, hidden.Count, output.Count)); neuralNetwork[x].EstablishExistingNetwork(input.ToArray(), hidden.ToArray(), output.ToArray()); } else { neuralNetwork.Add(neuralNetworkArgs[x]); } randomInt = random.Next(101); } return neuralNetwork.ToArray(); }
public void ResetFish() { brain = new NeuralNetwork(2, random.Next(1, 4), 3); brain.EstablishRandomNetwork(); position = new Vector2(random.Next((Game1.screenWidth - Game1.theoreticalScreenWidth) / 2, ((Game1.screenWidth - Game1.theoreticalScreenWidth) / 2) + Game1.theoreticalScreenWidth - texture.Width), random.Next((Game1.screenHeight - Game1.theoreticalScreenHeight) / 2, ((Game1.screenHeight - Game1.theoreticalScreenHeight) / 2) + Game1.theoreticalScreenHeight - texture.Height)); rectangle = new Rectangle((int)(position.X), (int)(position.Y), texture.Width, texture.Height); collisionCircle = new CollisionCircle((int)(position.X - origin.X), (int)(position.Y - origin.Y)); rotation = 90; }
public Fish(NeuralNetwork newBrain, CollisionCircle newCollisionCircle) { texture = content.Load<Texture2D>("Sprites/fish.png"); brain = newBrain; position = new Vector2(random.Next((Game1.screenWidth - Game1.theoreticalScreenWidth) / 2, ((Game1.screenWidth - Game1.theoreticalScreenWidth) / 2) + Game1.theoreticalScreenWidth - texture.Width), random.Next((Game1.screenHeight - Game1.theoreticalScreenHeight) / 2, ((Game1.screenHeight - Game1.theoreticalScreenHeight) / 2) + Game1.theoreticalScreenHeight - texture.Height)); rectangle = new Rectangle((int)(position.X), (int)(position.Y), texture.Width, texture.Height); origin = new Vector2(texture.Width / 2, texture.Height / 2); collisionCircle = newCollisionCircle; rotation = 90; proximitySensor[0] = new ProximitySensor((int)(position.X), (int)(position.Y)); proximitySensor[1] = new ProximitySensor((int)(position.X), (int)(position.Y)); proximitySensor[2] = new ProximitySensor((int)(position.X), (int)(position.Y)); }
public NeuralNetwork[] Mutate(NeuralNetwork[] neuralNetworkArgs, int percentAmountMutate, int percentMutate) { List<NeuralNetwork> neuralNetwork = new List<NeuralNetwork>(); NeuralNetwork neuralNetworkMethods = new NeuralNetwork(0, 0, 0); int randomInt = random.Next(101); for (int x = 0; x < neuralNetworkArgs.Length; x++) { if (randomInt < percentAmountMutate) { List<Neuron> input = new List<Neuron>(), hidden = new List<Neuron>(), output = new List<Neuron>(); randomInt = random.Next(101); for (int y = 0; y < neuralNetworkArgs[x].input.Length; y++) { if (randomInt < percentMutate) input.Add(neuralNetworkMethods.MutateNeuron(neuralNetworkArgs[x].input[y], neuralNetworkArgs[x].hidden)); else input.Add(neuralNetworkArgs[x].input[y]); randomInt = random.Next(101); } for (int y = 0; y < neuralNetworkArgs[x].hidden.Length; y++) { if (randomInt < percentMutate) hidden.Add(neuralNetworkMethods.MutateNeuron(neuralNetworkArgs[x].hidden[y], neuralNetworkArgs[x].output)); else hidden.Add(neuralNetworkArgs[x].hidden[y]); randomInt = random.Next(101); } foreach (Neuron neuron in neuralNetworkArgs[x].output) output.Add(neuron); neuralNetwork.Add(new NeuralNetwork(input.Count, hidden.Count, output.Count)); neuralNetwork[x].EstablishExistingNetwork(input.ToArray(), hidden.ToArray(), output.ToArray()); } else { neuralNetwork.Add(neuralNetworkArgs[x]); } randomInt = random.Next(101); } return neuralNetwork.ToArray(); }
public NeuralNetwork[] CrossMutate(NeuralNetwork[] neuralNetworkArgs, int total) { List<NeuralNetwork> neuralNetwork = new List<NeuralNetwork>(); NeuralNetwork neuralNetworkMethods = new NeuralNetwork(0, 0, 0); int randomInt = random.Next(neuralNetworkArgs.Length); for (int x = 0; x < total; x++) { List<Neuron> input = new List<Neuron>(), hidden = new List<Neuron>(), output = new List<Neuron>(); List<int> additionalConnections = new List<int>(); for (int y = 0; y < neuralNetworkArgs[0].output.Length; y++) { output.Add(neuralNetworkArgs[randomInt].output[y]); randomInt = random.Next(neuralNetworkArgs.Length); } for (int y = 0; y < neuralNetworkArgs[0].hidden.Length; y++) { if (neuralNetworkArgs[0].hidden.Length > neuralNetworkArgs[randomInt].hidden.Length && y > neuralNetworkArgs[randomInt].hidden.Length - 1) { hidden.Add(neuralNetworkMethods.CloneConnections(neuralNetworkArgs[0].hidden[y], output.ToArray(), neuralNetworkArgs[0].hidden[y].negative)); additionalConnections.Add(randomInt); } else hidden.Add(neuralNetworkMethods.CloneConnections(neuralNetworkArgs[randomInt].hidden[y], output.ToArray(), neuralNetworkArgs[randomInt].hidden[y].negative)); randomInt = random.Next(neuralNetworkArgs.Length); } for (int y = 0; y < neuralNetworkArgs[0].input.Length; y++) { bool passable = true; if (additionalConnections.Count > 0) { while (passable == false) { passable = true; foreach (int z in additionalConnections) { if (randomInt == z) passable = false; } randomInt = random.Next(neuralNetworkArgs.Length); } input.Add(neuralNetworkMethods.CloneConnections(neuralNetworkArgs[randomInt].input[y], hidden.ToArray(), neuralNetworkArgs[randomInt].input[y].negative)); } else input.Add(neuralNetworkMethods.CloneConnections(neuralNetworkArgs[randomInt].input[y], hidden.ToArray(), neuralNetworkArgs[randomInt].input[y].negative)); randomInt = random.Next(neuralNetworkArgs.Length); } neuralNetwork.Add(new NeuralNetwork(input.Count, hidden.Count, output.Count)); neuralNetwork[x].EstablishExistingNetwork(input.ToArray(), hidden.ToArray(), output.ToArray()); } return neuralNetwork.ToArray(); }