IBM ILOG Solver User's Manual > Evolutionary Algorithms > Modeling and Solving a Basic EA Problem > Creating an initial solution pool > Suggested Answers

Exercise 1

For this exercise, you should use a solution pool iterator and the getObjectiveValue API of IloSolution.

  for (IloSolutionPool::Iterator pi(population); pi.ok(); ++pi)
    env.out() << (*pi).getObjectiveValue() << " ";
  env.out() << endl;

Exercise 2

The average is calculated by iterating and accumulating a sum which will eventually be divided by the population size to produce an average. The best and worse could also be maintained in the iterator loop by examination of the objectives of each solution. An alternative is to use the getBestSolution/getWorstSolution API of IloSolutionPool, as shown here:

  IloNum sum = 0.0;
  for (IloSolutionPool::Iterator pi(population); pi.ok(); ++pi)
    sum += (*pi).getObjectiveValue(); 
  IloNum best = population.getBestSolution().getObjectiveValue();
  IloNum worst = population.getWorstSolution().getObjectiveValue();
  env.out() << "Best  = " << best << endl
            << "Worst = " << worst << endl
            << "Mean  = " << sum / population.getSize() << endl;