public NeuralNet(NeuralNet net1, NeuralNet net2, float mutationRate) { // Creates a network from crossing over two parent networks, with a chance of mutation NeuralNet[] parents = { net1, net2 }; layers = new int[net1.layers.Length]; List <float[]> neuronList = new List <float[]>(); List <float[]> biasList = new List <float[]>(); List <float[][]> weightList = new List <float[][]>(); for (int i = 0; i < layers.Length; i++) { layers[i] = net1.layers[i]; neuronList.Add(new float[layers[i]]); float[] bias = new float[layers[i]]; List <float[]> layerWeightList = new List <float[]>(); for (int j = 0; j < layers[i]; j++) { // Choose a random parent's bias bias[j] = Rand.Choice(parents).biases[i][j]; // A small chance of slightly mutating the bias if (Rand.Range(1f) < mutationRate) { bias[j] = Math.Max(-0.5f, Math.Min(0.5f, bias[j] + Rand.Range(-0.1f, 0.1f))); } if (i > 0) { float[] neuronWeights = new float[layers[i - 1]]; for (int k = 0; k < layers[i - 1]; k++) { // Choose a random parent's weight neuronWeights[k] = Rand.Choice(parents).weights[i - 1][j][k]; // A small chance of slightly mutating the weight if (Rand.Range(1f) < mutationRate) { neuronWeights[k] = Math.Max(-0.5f, Math.Min(0.5f, neuronWeights[k] + Rand.Range(-0.1f, 0.1f))); } } layerWeightList.Add(neuronWeights); } } biasList.Add(bias); if (i > 0) { weightList.Add(layerWeightList.ToArray()); } } neurons = neuronList.ToArray(); biases = biasList.ToArray(); weights = weightList.ToArray(); }
public Creature(Vector2 _position, string _name, Simulation _sim, Color _color, NeuralNet _network, int _generation) : base(_position) { // Constructs a creature using given network, colour, and generation name = _name; generation = _generation; color = _color; sim = _sim; network = new NeuralNet(13, sim.settings.hiddenLayerCount, sim.settings.hiddenLayerSize, 4); energy = sim.settings.maxEnergy / 2; scale = 0.1f; rotation = Rand.Range(360); }
public Creature(Vector2 _position, string _name, Simulation _sim) : base(_position) { // Constructs a creature using a random network, colour, and starting at generation 0 name = _name; generation = 0; color = Rand.RandColor(); sim = _sim; network = new NeuralNet(13, sim.settings.hiddenLayerCount, sim.settings.hiddenLayerSize, 4); energy = sim.settings.maxEnergy / 2; scale = 0.1f; rotation = Rand.Range(360); }
public CreatureSave(Creature creature) { name = creature.name; network = creature.network; x = creature.position.X; y = creature.position.Y; childrenCount = creature.childrenCount; generation = creature.generation; age = creature.age; reproductionTimer = creature.reproductionTimer; energy = creature.energy; colorValue = creature.color.PackedValue; rotation = creature.rotation; scale = creature.scale; }