/// <summary> /// Initialise new lifeform /// </summary> /// <param name="_genotype">The genotype to use</param> /// <param name="layerSizes">The layerSizes of the neural network</param> public LifeForm(Genotype _genotype, params uint[] layerSizes) { isAlive = false; GenoType = _genotype; NN = new NeuralNetwork(layerSizes); //Check for matching number of genes to weights if (NN.WeightCount != _genotype.GeneCount) { throw new ArgumentException("Lifeform(): Genotype gene count does not match the number of weights in neural network"); } IEnumerator <float> chromosome = _genotype.GetEnumerator(); //Assign chromosome to weights in neural network foreach (NeuralLayer layer in NN.Layers) { for (int x = 0; x < layer.Weights.GetLength(0); x++) //Neurons of current layer { for (int y = 0; y < layer.Weights.GetLength(1); y++) //Neurons of next layer { layer.Weights[x, y] = chromosome.Current; chromosome.MoveNext(); } } } }
/// <summary> /// Initialises a new agent from given genotype, constructing a new feedfoward neural network from /// the parameters of the genotype. /// </summary> /// <param name="genotype">The genotpye to initialise this agent from.</param> /// <param name="topology">The topology of the feedforward neural network to be constructed from given genotype.</param> public Agent(Genotype genotype, NeuralLayer.ActivationFunction defaultActivation, params uint[] topology) { IsAlive = false; this.Genotype = genotype; FNN = new NeuralNetwork(topology); foreach (NeuralLayer layer in FNN.Layers) { layer.NeuronActivationFunction = defaultActivation; } //Check if topology is valid if (FNN.WeightCount != genotype.ParameterCount) { throw new ArgumentException("The given genotype's parameter count must match the neural network topology's weight count."); } //Construct FNN from genotype IEnumerator <float> parameters = genotype.GetEnumerator(); foreach (NeuralLayer layer in FNN.Layers) //Loop over all layers { for (int i = 0; i < layer.Weights.GetLength(0); i++) //Loop over all nodes of current layer { for (int j = 0; j < layer.Weights.GetLength(1); j++) //Loop over all nodes of next layer { layer.Weights[i, j] = parameters.Current; parameters.MoveNext(); } } } }