public Population(int size, IFitnessFunction fitnessFunction, IReproduction reproductionFunction, INodeMutator mutator, ISelection selection) { this.populationSize = size; this.fitnessFunction = fitnessFunction; this.reproductionFunction = reproductionFunction; this.mutator = mutator; this.selector = selection; this.fitnessFunction.Initialise(); // Create the initial population for (int i = 0; i < size; i++) { try { NodeContext zeroContext = new NodeContext(); zeroContext.AvailableCollections = fitnessFunction.GetCollections(); zeroContext.AvailableInputs = fitnessFunction.GetInputs(); INode candidateNode = NodeFactory.GenerateNode(zeroContext); // Make sure we have a decent candidate (i.e. not too large) double fitness = this.fitnessFunction.CalculateFitness(candidateNode); if (fitness == Double.MaxValue) continue; this.population.Add(NodeFactory.GenerateNode(zeroContext)); } catch (StackOverflowException) { } } }