IBM ILOG Solver User's Manual > Evolutionary Algorithms > Bin Packing Using EA > Problem solving: Initial population

After creating the model of the problem, the next step in creating an evolutionary or genetic algorithm is normally the building of a solution prototype defining the decision variables of the problem and the objective function. This solution prototype will then be used by the genetic algorithm as a template for the creation of new solutions.

Step 2   -  

Create the solution prototype

To create the solution prototype add the following code after the comment
// Solution prototype

  IloSolution prototype(env);
  prototype.add(where);
  prototype.add(load);
  prototype.add(used);
  prototype.add(IloMinimize(env, used));

This code creates the prototype and adds the variables that are used during the execution of the genetic algorithm. The objective (minimize the number of bins used) is also added to allow solutions to be compared for quality.

You can now create the initial population. This population needs to be reasonably diverse and one simple way of doing this is to randomize it. Given the existing decreasing first fit packing goal, the IloRandomPerturbation goal modifier creates a randomized version which swaps the left and right branches of choice points with a certain probability.

Step 3   -  

Create the initial population

Add the following code after the comment // Create initial population

  IloSolutionPool population(env);
  IloGoal packGoal = Pack(env, load, where, weight);
  packGoal = IloRandomPerturbation(env, packGoal, 50.0 / (50 + n));
  IloPoolProc src = IloSource(env, packGoal, prototype);
  IloPoolProc create = src(popSize) >> population;
  IloSolver solver(mdl);
  solver.solve(IloExecuteProcessor(env, create));

A population is created, together with the decreasing first fit packing goal, which is then perturbed, each decision being inverted with probability 50/(50+n) where n is the number of items to pack. A probability based on the reciprocal of the number of items to pack ensures that the number of inversions is bounded. The extra constant on the denominator ensures that the probability is not too high for small n (that is, small problems). There is nothing particularly magic about this formula; many other formulas will work just as well.

The population is built using the IloSource operator which takes a goal and a solution prototype, invoking the goal to produce the solution. When cast to a pool processor (implicitly here), the prototype tells the resulting processor what type of solution to produce. Finally, popSize solutions are requested of IloSource through the parentheses operator, the resultant solutions being placed in population. The create processor is then cast to a goal via IloExecuteProcessor and executed by the solver, which is in turn created using the model.