IBM ILOG Solver User's Manual > Evolutionary Algorithms > Car Sequencing Using EA > Problem solving: Creating the initial population

After creating the decoding goal, you will move on to create the initial population of solutions to start the genetic algorithm. Before doing this, you will first put in place an operator factory which will be used to generate and parameterize genetic operators, including an operator which creates a random member to be used in population construction.

Step 9   -  

Set up an operator factory

Add the following code after the comment // Set up an operator factory

  IloEAOperatorFactory factory(env, prio);
  factory.setAfterOperate(decode);
  factory.setSearchLimit(IloFailLimit(env, 100));
  factory.setPrototype(prototype);

The operator factory is specified as operating over the prio array (that is, the indirect representation). After any genetic operation on the priorities, it is stated that the decoding goal must be invoked. In addition, this entire process (genetic operator + decoding) is limited to 100 fails in order to avoid excessive exploration times when a solution is difficult to produce. Finally, it is indicated to the factory that for production of new solutions by genetic operators, prototype should be used as a template. This prototype will be cloned internally and the clone used to contain the new solution.

You are now in a position to create the initial population.

Step 10   -  

Make the initial population

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

  const IloInt popSize = 30;
  IloSolutionPool pop(env);
  IloPoolProc create = factory.randomize();
  solver.solve(IloExecuteProcessor(env, create(popSize) >> pop));

  // Sort the population wrt. quality and display.
  pop.sort();
  env.out() << "Initial population: "; 
  DisplayCosts(pop);

Here, a pool processor is retrieved from the factory which is used to create a population member. This processor randomizes the values of the variables specified to the factory at construction time (in this case, the prio array). As you specified that the decoding goal should be invoked after each genetic operator produced by factory, then the create processor will also decode these randomized priorities to a car sequence. The desired population size is specified to the population creation operator via the () operator, which will ensure that at least popSize solutions are created. As these solutions are created randomly each time, then popSize different solutions should result, which are placed into the population solution pool pop using the >> operator. The operation of pool processors is such that if a solution failed to be produced at any invocation of the processor (due to the exhaustion of a search limit, for example), the processor is called again and again until the desired number of solutions is produced.

The resulting pool processor is invoked by the IloExecuteProcessor function which converts the processor to an instance of IloGoal.

The population is sorted in decreasing order of quality, which is possible as the objective was added to the solution prototype earlier; thus each solution knows how it should be compared with another.