/// <summary> /// Constructor for the Population /// Creates "size" Individuals to the population, and sets them up with the nodes specified in "points" /// </summary> /// <param name="size">The amount of Individuals in the population</param> /// <param name="seed">The seed of the random number generation</param> /// <param name="points">The layers of nodes of the Individuals</param> public Population(int size, int seed, List <int> points, List <int> memoryDepths, float weigthRange, float weigthOffset, CrossOverInfo info) { try { this.size = size; //Initialize fields rnd = new Random(seed); currentGeneration = new List <Individual <U> >(); nextGeneration = new List <Individual <U> >(); T currentIndividual; for (int i = 0; i < size; i++) //Create Individuals, and add them to the current generation { currentIndividual = new T(); currentIndividual.createNetwork(points, memoryDepths); currentIndividual.randomizeNetwork(rnd, weigthRange, weigthOffset); //New individuals have random genes currentGeneration.Add(currentIndividual); } #region initCrossoverInfo if (info == null) { this.info = new CrossOverInfo(rnd, CrossoverType.SWAPPING); //Create the default crossOver specification info.mutationChance = 0.3f; info.mutationStrength = 0.5f; } else { this.info = info; info.setRnd(this.rnd); } #endregion } catch (Exception e) { throw new NullReferenceException("Message: " + e.Message + "\nStackTrace: " + e.StackTrace + "\nTargetSite: " + e.TargetSite + "\nSource: " + e.Source); } }
//Returns the CrossOver of two Topology-s public Topology crossOver(Topology otherParent, CrossOverInfo info) { List <int> points = new List <int>(); List <int> memoryDepths = new List <int>(); foreach (TopologyEntry entry in adj_M) { points.Add(entry.layer); memoryDepths.Add(entry.memoryDepth); } Topology ret = new Topology(points, memoryDepths); for (int i = 0; i < ret.Count; i++) { TopologyEntry pEntry1 = adj_M[i], pEntry2 = otherParent.adj_M[i]; bool batchChoice = info.Rnd > info.crossOverRatio; for (int j = 0; j < pEntry1.adj_V.Length; j++) { if (float.IsNaN(pEntry1.adj_V[j]) || float.IsNaN(pEntry2.adj_V[j])) { ret.adj_M[i].adj_V[j] = float.NaN; } else { float temp; switch (info.type) { case CrossoverType.AVERAGING: temp = pEntry1.adj_V[j] * info.crossOverRatio + pEntry2.adj_V[j] * (1 - info.crossOverRatio); break; case CrossoverType.SWAPPING: if (info.Rnd > info.crossOverRatio) { temp = pEntry2.adj_V[j]; } else { temp = pEntry1.adj_V[j]; } break; case CrossoverType.BATCH_SWAPPING: if (batchChoice) { temp = pEntry2.adj_V[j]; } else { temp = pEntry1.adj_V[j]; } break; default: return(null); } float mutation; if (info.Rnd > info.mutationChance) { mutation = (info.Rnd - 0.5f) * info.mutationStrength * 2f; } else { mutation = 0; } temp = temp + mutation; ret.adj_M[i].adj_V[j] = temp; } } } return(ret); }
public NeuralNet <T> crossOver(NeuralNet <T> otherParent, CrossOverInfo info) { return(new NeuralNet <T>(topology.crossOver(otherParent.topology, info))); }
/// <summary> /// Constructor working with random generated seed /// </summary> /// <param name="size"></param> /// <param name="points"></param> public Population(int size, List <int> points, List <int> memoryDepths, float weigthRange, float weigthOffset, CrossOverInfo info) : this(size, DateTime.Now.Millisecond, points, memoryDepths, weigthRange, weigthOffset, info) { }
public Individual <T> crossOver(Individual <T> otherParent, CrossOverInfo info) { NeuralNet <T> childNet = neuralNet.crossOver(otherParent.neuralNet, info); return(new Individual <T>(childNet)); }