public Individual<A> geneticAlgorithm(Collection<Individual<A>> initPopulation,FitnessFunction<A> fitnessFn,GoalTest goalTest,long maxTimeMilliseconds){
  Individual<A> bestIndividual=null;
  List<Individual<A>> population=new ArrayList<Individual<A>>(initPopulation);
  validatePopulation(population);
  updateMetrics(population,0,0L);
  long startTime=System.currentTimeMillis();
  int itCount=0;
  do {
    population=nextGeneration(population,fitnessFn);
    bestIndividual=retrieveBestIndividual(population,fitnessFn);
    updateMetrics(population,++itCount,System.currentTimeMillis() - startTime);
    if (maxTimeMilliseconds > 0L && (System.currentTimeMillis() - startTime) > maxTimeMilliseconds)     break;
    if (CancelableThread.currIsCanceled())     break;
  }
 while (!goalTest.isGoalState(bestIndividual));
  notifyProgressTracers(itCount,population);
  return bestIndividual;
}

public Individual < A > geneticAlgorithm ( Collection < Individual < A > > initPopulation , FitnessFunction < A > fitnessFn , GoalTest goalTest , long maxTimeMilliseconds ) { Individual < A > bestIndividual = null ; List < Individual < A > > population = new ArrayList < Individual < A > > ( initPopulation ) ; validatePopulation ( population ) ; updateMetrics ( population , 0 , 0L ) ; long startTime = System . currentTimeMillis ( ) ; int itCount = 0 ; do { population = nextGeneration ( population , fitnessFn ) ; bestIndividual = retrieveBestIndividual ( population , fitnessFn ) ; updateMetrics ( population , ++ itCount , System . currentTimeMillis ( ) - startTime ) ; if ( maxTimeMilliseconds > 0L && ( System . currentTimeMillis ( ) - startTime ) > maxTimeMilliseconds ) break ; if ( CancelableThread . currIsCanceled ( ) ) break ; } while ( ! goalTest . isGoalState ( bestIndividual ) ) ; notifyProgressTracers ( itCount , population ) ; return bestIndividual ; }
template method controlling search . it returns the best individual in the specified population , according to the specified fitness - fn and goal test .
