public Network(Network n) { for (int i = 0; i < _neurons.Length; i++){ _neurons[i] = new Neuron[NETWORK_BREADTH]; for (int j = 0; j < _neurons[i].Length; j++){ _neurons[i][j] = new Neuron(n._neurons[i][j]); } } }
private bool AddToRated(float rating, Network n) { try{ _rated.Add(rating, n); return true; } catch (Exception e){ return false; } }
private void Breed() { for (int i = _survivedSize; i + 1 < _popSize - _freshSize; i += 2){ int p1 = RandomParentId(INF,true); int p2 = RandomParentId(p1,false); _population[i] = new Network(_population[p1]); _population[i + 1] = new Network(_population[p2]); Network.Crossover(_population[i], _population[i + 1]); } for (int i = _popSize - _freshSize; i < _popSize; i++){ _population[i] = new Network(); } for (int i = 0; i < _popSize - _freshSize; i++){ if (Program.RandomGenerator.Next()%100 < _mutationChance){ _population[i].Mutate(); } } }
public static void Crossover(Network s1, Network s2) { for (int i = 0; i < s1._neurons.Length; i++){ for (int j = 0; j < s1._neurons[i].Length; j++){ var los = Program.RandomGenerator.Next(3); switch (los){ case 0: break; case 1: var temp = s1._neurons[i][j]; s1._neurons[i][j] = s2._neurons[i][j]; s2._neurons[i][j] = temp; break; case 2: Neuron.Crossover(s1._neurons[i][j], s2._neurons[i][j]); break; } } } }
private void InitPopulation() { _population = new Network[_popSize]; for (int i = 0; i < _popSize; i++){ _population[i] = new Network(); } }
private float RateNetwork(Network network) { float rating = 0; foreach (var test in _tests){ float eval = network.Calculate(test) - test[0]; rating += Math.Abs(eval); } return rating; }