IBM ILOG Solver User's Manual > Extending the Library > Writing a Goal: Car Sequencing > Writing a goal: Car sequencing > Adding the distribute and sequence constraints

The number of cars required for each configuration can be expressed by means of the Solver constraint IloDistribute. This function takes four parameters: an environment, an array of constrained variables, an array of constrained values, and an array to count those constrained values. The following code actually implements the specific details of our example.

    model.add(IloDistribute(env, cards, confs, cars));

For more information about the distribute constraint, see Chapter 6, Using the Distribute Constraint: Car Sequencing.

At first glance, it seems more difficult to implement the constraints representing the capacities of options in the problem, though it is easy to express such a constraint in natural language. However, this is possible using the IloSequence constraint. You use this constraint to define constraints on sequences of cars. As a sequence constraint, it groups several constraints created by IloDistribute in order to reduce the domains of constrained variables more efficiently. More specifically, an instance of this class enables you to constrain:

So here for each option opt, you define a sequence constraint and add it to the model, like this:

    IloInt opt;
    for (opt = 0; opt < nbOptions; opt++) {
      IloIntVarArray ncard(env,optConf[opt].getSize());
      for(i = 0; i < optConf[opt].getSize(); i++){
        ncard[i] = cards[(IloInt)optConf[opt][i]];
      }
      model.add(IloSequence(env,
                            0,
                            (IloInt)nbMax[opt],
                            (IloInt)seqWidth[opt],
                            cars,
                            optConf[opt],
                            ncard));
    }
    cout << "Pass 3 " << endl;