/** * Sets the population size and resets the GA generation count, as well as initializing the * population with random individuals. * * @param inSize the size of the new GA population. */ protected void ResizeAndInitialize(int inSize) throws Exception { _populations = new GAIndividual[2][inSize]; _currentPopulation = 0; _generationCount = 0; Object iObject = _individualClass.newInstance(); if (!(iObject instanceof GAIndividual)) throw new Exception("individual-class must inherit from class GAIndividual"); GAIndividual individual = (GAIndividual) iObject; for (int i = 0; i < inSize; i++) { _populations[0][i] = individual.clone(); InitIndividual(_populations[0][i]); } }
/** * Evaluates the current population and updates their fitness values. This method may be * overridden by subclasses to customize GA behavior. */ protected void Evaluate() { double totalFitness = 0; _bestMeanFitness = Float.MAX_VALUE; for (int n = 0; n < _populations[_currentPopulation].length; n++) { GAIndividual i = _populations[_currentPopulation][n]; EvaluateIndividual(i); totalFitness += i.GetFitness(); if (i.GetFitness() < _bestMeanFitness) { _bestMeanFitness = i.GetFitness(); _bestIndividual = n; _bestErrors = i.GetErrors(); } } _populationMeanFitness = totalFitness / _populations[_currentPopulation].length; }