public NN Crossover(NN parent1, NN parent2) { var r = new Random(); NN child = new NN(); child.Init(); for (int i = 0; i < parent1.Layers.Count; i++) { int crossoverPointX = r.Next(0, parent1.Layers[i].Weights.GetLength(0)); int crossoverPointY = r.Next(0, parent1.Layers[i].Weights.GetLength(1)); for (int ii = 0; ii < parent1.Layers[i].Weights.GetLength(0); ii++) { for (int iii = 0; iii < parent1.Layers[i].Weights.GetLength(1); iii++) { double gene = parent1.Layers[i].Weights[ii, iii]; if (ii > crossoverPointX && iii > crossoverPointY) { gene = parent2.Layers[i].Weights[ii, iii]; } child.Layers[i].Weights[ii, iii] = gene; } } } return(child); }
public NN[] GeneratePopulation(int popsize) { var nns = new NN[popsize]; for (int i = 0; i < popsize; i++) { var nn = new NN(); nn.Init(); nns[i] = nn; } Save(nns); return(nns); }