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

In this section, you will learn how to:

Make a copy of the example file YourSolverHome/examples/src/tutorial/ea1max_init_partial.cpp and open this copy in your development environment. This file is a program that is only partially completed. You will fill in the blanks in each step in this section. At the end of the section, you will have completed the example and you can compile and run the program.

The main routine starts by parsing command line parameters for the problem size and the population size. You declare an IloEnv object used for allocation followed by a "try/catch" block to trap possible exceptions which may occur within the function body execution.

Step 1   -  

Parse command line parameters

Add the following code after the comment
//Parse command line parameters

    IloInt problemSize = 50;     // Bits per solution
    IloInt populationSize = 50;  // Number of solutions in population
    if (argc > 1)
      problemSize = atoi(argv[1]);
    if (argc > 2)
      populationSize = atoi(argv[2]);
    env.out() << "Problem size: " << problemSize << endl;
    env.out() << "Population size: " << populationSize << endl;

Next, you build a Concert model of the "one max" problem. You declare an instance of IloModel. Then, you declare an IloBoolVarArray of bits. You constrain the count integer variable to be the sum of the binary digits in the bits array. The count variable has a lower bound of 0 for an array composed entirely of 0's and an upper bound of 50 for an array composed entirely of 1's.

Step 2   -  

Create the model

Add the following code after the comment //Create the model

    IloModel model(env);
    IloBoolVarArray bits(env, problemSize);     // The bits to count 
    for (IloInt v = 0; v < problemSize; v++) {
      char name[10];
      sprintf(name, "%02ld", v + 1);
      bits[v].setName(name);                         // Name the variable
    }
    IloIntVar count(env, 0, problemSize, "count");   // The bit count
    model.add(count == IloSum(bits));                // Count the bits

As all generated solutions must contain the variable array you just modeled, you define a solution prototype. This prototype is a template for all solutions that will be placed in the initial population. Internally, unique solutions will be generated by cloning this prototype and storing the solver's state. You also define the objective function, which is to maximize the count variable. This addition is useful when solutions are to be compared or sorted.

Step 3   -  

Create the solution prototype

Add the following code after the comment //Create the solution prototype

    IloSolution prototype(env);
    prototype.add(bits);
    IloObjective obj = IloMaximize(env, count, "Obj");
    prototype.setObjective(obj);

You will make use of a factory of operators which operate over an array of decision variables specified to the factory. This factory process simplifies the definition of standard operators as the factory has access to certain common parameters, for example, the solution prototype.

Here, you use an operator which randomizes the values of the variables specified in the constructor of the factory, in order to generate a random population. This operator will be called until the number of solutions is equal to the desired population size. Solver verifies the legality of each solution by generating the solutions in a goal search which is backtracked each time a solution is produced. The fail limit specified is applied to the generation of each solution, meaning that the effort to generate each solution is limited. If a solution is not generated within this limit, this small subsearch is abandoned, and the generation loop reiterated.

Step 4   -  

Initialize the population

Add the following code after the comment //Initialize the population

    IloSolver solver(model);
    IloSolutionPool population(env);
    IloEAOperatorFactory factory(env, bits);
    factory.setPrototype(prototype);
    factory.setSearchLimit(IloFailLimit(env, problemSize));
    solver.solve(IloExecuteProcessor(
      env, factory.randomize("rand")(populationSize) >> population
    ));

You can then display the solution pool's content.

Step 5   -  

Display the population

Add the following code after the comment //Display the population

    env.out() << "INITIAL POPULATION " << population << endl;

Finally, the routine's "try/catch" block as well as its body are closed. This code is provided for you:

  } catch(IloException ex) {
    env.out() << "Caught: " << ex << endl;
  }
  env.end();
  return 0;
}

You have now completed the model of the "one max" problem and created the initial solution pool. In the next section, you will learn how to write a simple generation loop.

Step 6   -  

Compile and run the program

Compile and run the program. You should get the following results:

Problem size: 50
Population size: 50
INITIAL POPULATION IloSolutionPoolI (no name)
    [0] IloSolution[ 01[0] 02[1] 03[1] 04[1] 05[0] 06[0] 07[1] 08[1] 09[1] 
10[0] 11[1] 12[1] 13[0] 14[1] 15[0] 16[0] 17[1] 18[1] 19[1] 20[1] 21[0] 22[1] 
23[1] 24[1] 25[0] 26[0] 27[1] 28[0] 29[1] 30[1] 31[1] 32[1] 33[1] 34[1] 35[1] 
36[0] 37[0] 38[1] 39[0] 40[1] 41[1] 42[1] 43[1] 44[1] 45[1] 46[0] 47[1] 48[1] 
49[0] 50[1] Obj{Max} [34] ] (Obj = 34)
    [1] IloSolution[ 01[0] 02[1] 03[1] 04[1] 05[0] 06[0] 07[0] 08[1] 09[1] 
10[1] 11[1] 12[1] 13[0] 14[1] 15[1] 16[1] 17[0] 18[0] 19[1] 20[1] 21[1] 22[0] 
23[0] 24[1] 25[0] 26[1] 27[1] 28[0] 29[0] 30[0] 31[1] 32[1] 33[1] 34[0] 35[0] 
36[1] 37[0] 38[0] 39[0] 40[1] 41[1] 42[0] 43[1] 44[0] 45[1] 46[0] 47[0] 48[0] 
49[0] 50[1] Obj{Max} [26] ] (Obj = 26)
    [2] IloSolution[ 01[1] 02[1] 03[1] 04[0] 05[0] 06[1] 07[1] 08[1] 09[0] 
10[0] 11[1] 12[0] 13[1] 14[0] 15[1] 16[0] 17[0] 18[1] 19[0] 20[0] 21[0] 22[0] 
23[0] 24[1] 25[0] 26[1] 27[1] 28[0] 29[0] 30[0] 31[1] 32[1] 33[1] 34[1] 35[0] 
36[1] 37[0] 38[1] 39[0] 40[0] 41[0] 42[0] 43[1] 44[0] 45[1] 46[0] 47[0] 48[0] 
49[1] 50[0] Obj{Max} [22] ] (Obj = 22)
    [3] IloSolution[ 01[0] 02[0] 03[1] 04[0] 05[1] 06[1] 07[1] 08[0] 09[0] 
10[0] 11[1] 12[0] 13[0] 14[1] 15[0] 16[1] 17[1] 18[0] 19[0] 20[1] 21[0] 22[0] 
23[1] 24[1] 25[0] 26[0] 27[1] 28[0] 29[0] 30[1] 31[0] 32[1] 33[1] 34[0] 35[0] 
36[1] 37[0] 38[1] 39[1] 40[1] 41[0] 42[1] 43[1] 44[0] 45[1] 46[1] 47[1] 48[1] 
49[1] 50[1] Obj{Max} [27] ] (Obj = 27)
    [4] IloSolution[ 01[1] 02[0] 03[1] 04[0] 05[0] 06[0] 07[1] 08[1] 09[0] 
10[0] 11[0] 12[1] 13[1] 14[0] 15[0] 16[1] 17[0] 18[1] 19[0] 20[1] 21[0] 22[1] 
23[0] 24[1] 25[0] 26[1] 27[0] 28[1] 29[1] 30[1] 31[1] 32[0] 33[0] 34[1] 35[1] 
36[1] 37[1] 38[0] 39[0] 40[0] 41[0] 42[0] 43[1] 44[0] 45[1] 46[0] 47[1] 48[0] 
49[1] 50[0] Obj{Max} [24] ] (Obj = 24)

                              ( abridged )

The complete program is available in the YourSolverHome/examples/src/ea1max_init.cpp file.