public XorEvolver(IFitnessTester fitnessTester) { this.fitnessTester = fitnessTester; IEnumerable <NeuralNetwork> networks = Helpers.Repeat( () => new NeuralNetwork(NeuralDna.Random(9), 2, 2, 1), PopulationSize); this.population = networks.ToDictionary(nn => nn, nn => this.fitnessTester.Fitness(nn)); }
public void ProcessGeneration() { List <NeuralNetwork> newNetworks = new List <NeuralNetwork>(); for (int i = 0; i < this.population.Count(); i++) { NeuralNetwork father = this.population.Keys.Random(nn => this.population[nn]); NeuralNetwork mother = this.population.Keys.Except(new[] { father }).Random(nn => this.population[nn]); NeuralDna childDna = father.Dna.Crossover(mother.Dna); childDna.Mutate(MutationRate); NeuralNetwork child = new NeuralNetwork(childDna, father.NumInputs, father.LayerNums); newNetworks.Add(child); } this.population = newNetworks.ToDictionary(nn => nn, nn => this.fitnessTester.Fitness(nn)); }
public NeuralNetwork(NeuralDna dna, int numInputs, params int[] layerNums) { this.dna = dna; this.numInputs = numInputs; this.layerNums = layerNums; Queue <double> genes = new Queue <double>(dna.Genes); List <NeuronLayer> layers = new List <NeuronLayer>(); int previousLayer = numInputs; foreach (int layerNum in layerNums) { layers.Add(new NeuronLayer(genes, layerNum, previousLayer)); previousLayer = layerNum; } if (genes.Any()) { throw new ArgumentException(); } this.layers = layers; }