# Paint production¶

The problem description in this section is taken from Section 7.5 Paint production of the book Applications of optimization with Xpress-MP. As a part of its weekly production a paint company produces five batches of paints, always the same, for some big clients who have a stable demand. Every paint batch is produced in a single production process, all in the same blender that needs to be cleaned between every two batches. The durations of blending paint batches 1 to 5 are respectively 40, 35, 45, 32, and 50 minutes. The cleaning times depend on the colors and the paint types. For example, a long cleaning period is required if an oil-based paint is produced after a water-based paint, or to produce white paint after a dark color. The times are given in minutes in the following table:

 CLEAN(i,j) 1 2 3 4 5 1 0 11 7 13 11 2 5 0 13 15 15 3 13 15 0 23 11 4 9 13 5 0 3 5 3 7 7 7 0

Since the company also has other activities, it wishes to deal with this weekly production in the shortest possible time (blending and cleaning). Which is the corresponding order of paint batches? The order will be applied every week, so the cleaning time between the last batch of one week and the first of the following week needs to be counted for the total duration of cleaning.

## Formulation of model 1¶

As for the problem in section 11.3 we are going to present two alternative model formulations. The first one is closer to the Mathematical Programming formulation in Applications of optimization with Xpress-MP, the second uses a two-dimensional element constraint. Let be the set of batches to produce, the processing time for batch , and the cleaning time between the consecutive batches and . We introduce decision variables taking their values in , to indicate the successor of every job, and variables for the duration of the cleaning after every job. The cleaning time after every job is obtained by indexing with the value of . We thus have the following problem formulation: The objective function sums up the processing and cleaning times of all batches. The last (all-different) constraint guarantees that every batch occurs exactly once in the production sequence.

Unfortunately, this model does not guarantee that the solution forms a single cycle. Solving it indeed results in a total duration of 239 with an invalid solution that contains two sub-cycles 1 3 2 1 and 4 5 4. A first possibility is to add a disjunction excluding this solution to our model and re-solve it iteratively until we reach a solution without sub-cycles. However, this procedure is likely to become impractical with larger data sets since it may potentially introduce an extremely large number of disjunctions. We therefore choose a different, a-priori formulation of the sub-cycle elimination constraints with a variable per batch and implication constraints. The variables correspond to the position of job in the production cycle. With these constraints, job 1 always takes the first position.

## Implementation of model 1¶

The implementation of the model formulated in the previous section is quite straightforward. The sub-cycle elimination constraints are implemented as logic relations with implies (a stronger formulation of these constraints is obtained by replacing the implications by equivalences, using KEquiv).

// Number of paint batches (=jobs)
int NJ = 5;
// Durations of jobs
int DUR[]  = {40, 35, 45, 32, 50};

// Cleaning times between jobs
int CLEAN = {
{  0, 11,  7, 13, 11 },
{  5,  0, 13, 15, 15 },
{ 13, 15,  0, 23, 11 },
{  9, 13,  5,  0,  3 },
{  3,  7,  7,  7,  0 } };

// Cleaning times after a batch
KIntArray CB;
// Successor of a batch
KIntVarArray succ;
// Cleaning time after batches
KIntVarArray clean;
// Variables for excluding subtours
KIntVarArray y;
// Objective variable
KIntVar * cycleTime;

// Creation of the problem in this session
KProblem problem(session,"B-5 Paint production");

// variables creation
char name;
int j,i;
for (j=0;j<NJ;j++) {
sprintf(name,"succ(%i)",j);
succ += (* new KIntVar( problem,name,0,NJ-1) );
succ[j].remVal(j);
sprintf(name,"y(%i)",j);
y += (* new KIntVar( problem,name,0,NJ-1) );
}

// initialization of CB array
for (j=0;j<NJ;j++) {
CB += 0;
}
// Cleaning time after every batch
for (j=0;j<NJ;j++) {
sprintf(name,"clean(%i)",j);
clean += (* new KIntVar( problem,name,0,1000) );
for (i=0;i<NJ;i++) {
CB[i] = CLEAN[j][i];
}
KEltTerm kelt(CB,succ[j]);
problem.post(new KElement(kelt,clean[j],"element"));
}

// objective variable creation
cycleTime = new KIntVar(problem,"cycleTime",0,1000);
// Objective: minimize the duration of a production cycle
KLinTerm cycleTerm;
for (j=0;j<NJ;j++) {
cycleTerm = cycleTerm + clean[j] + DUR[j];
}
problem.post(cycleTerm == *cycleTime);

// One successor and one predecessor per batch
problem.post(new KAllDifferent("alldiff",succ));

// Exclude subtours
for (i=0;i<NJ;i++) {
for (j=1;j<NJ;j++) {
if (i!=j) {
problem.post(new KGuard( succ[i] == j , y[j] == y[i] + 1  );                }
}
}
// propagating problem
if (problem.propagate()) {
printf("Problem is infeasible\n");
exit(1);
}

// Solve the problem
// creation of the solver
KSolver solver(problem);

// setting objective and sense of optimization
problem.setSense(KProblem::Minimize);
problem.setObjective(*cycleTime);

int result = solver.optimize();
// solution printing
KSolution * sol = &problem.getSolution();
// print solution resume
sol->printResume();

// Solution printing
printf("Minimum cycle time: %i\n", sol->getValue(*cycleTime));
printf("Sequence of batches:\nBatch Duration Cleaning\n");
int first=0;
do {
printf("%i\t%i\t%i\n", first, DUR[first],sol->getValue(clean[first]));
first = sol->getValue(succ[first]);
} while (first!=0);


## Formulation of model 2¶

We may choose to implement the paint production problem using rank variables similarly to the sequencing model in section 11.3. As before, let be the set of batches to produce, the processing time for batch , and the cleaning time between the consecutive batches and . We introduce decision variables taking their values in , for the number of the job in position . Variables with now denote the duration of the cleaning time. This duration is obtained by indexing with the values of two consecutive variables. We thus have the following problem formulation. As in model 1, the objective function sums up the processing and cleaning times of all batches. Although not strictly necessary from the mathematical point of view, we use different sum indices for durations and cleaning times to show the difference between summing over jobs or job positions. We now have an all-different constraint over the rank variables to guarantee that every batch occurs exactly once in the production sequence.

## Implementation of model 2¶

The implementation of the second model uses the 2-dimensional version of the KElement constraint in Artelys Kalis.

// Number of paint batches (=jobs)
int NJ = 5;
// Durations of jobs
int DUR[]  = {40, 35, 45 ,32 ,50};

// Cleaning times between jobs
KIntMatrix CLEAN(5,5,0,"CLEAN");

CLEAN = 0;CLEAN = 11;CLEAN = 7;CLEAN = 13;CLEAN = 11;
CLEAN = 5;CLEAN = 0;CLEAN = 13;CLEAN = 15;CLEAN = 15;
CLEAN = 13;CLEAN = 15;CLEAN = 0;CLEAN = 23;CLEAN = 11;
CLEAN = 9;CLEAN = 13;CLEAN = 5;CLEAN = 0;CLEAN = 3;
CLEAN = 3;CLEAN = 7;CLEAN = 7;CLEAN = 7;CLEAN = 0;

// Successor of a batch
KIntVarArray rank;
// Cleaning time after batches
KIntVarArray clean;
// Objective variable
KIntVar * cycleTime;

// Creation of the problem in this session
KProblem problem(session,"B-5 Paint production");

// variables creation
char name;
int k,j,i;
for (j=0;j<NJ;j++) {
sprintf(name,"rank(%i)",j);
rank += (* new KIntVar( problem,name,0,NJ-1) );
sprintf(name,"clean(%i)",j);
clean += (* new KIntVar( problem,name,0,1000) );
}

// Cleaning time after every batch
for (k=0;k<NJ;k++) {
if (k < NJ-1) {
KEltTerm2D kelt(CLEAN,rank[k],rank[k+1]);
problem.post(new KElement2D(kelt,clean[k],"element"));
} else {
KEltTerm2D kelt(CLEAN,rank[k],rank);
problem.post(new KElement2D(kelt,clean[k],"element"));
}
}

// objective variable creation
cycleTime = new KIntVar(problem,"cycleTime",0,1000);

// Objective: minimize the duration of a production cycle
KLinTerm cycleTerm;
for (j=0;j<NJ;j++) {
cycleTerm = cycleTerm + DUR[j];
}
KLinTerm cleanTerm;
for (k=0;k<NJ;k++) {
cleanTerm = cleanTerm + clean[k];
}
problem.post(cycleTerm + cleanTerm == *cycleTime);

// One position for every job
problem.post(new KAllDifferent("alldiff",rank));

// propagating problem
if (problem.propagate()) {
printf("Problem is infeasible\n");
exit(1);
}

// Solve the problem
// creation of the solver
KSolver solver(problem);

// setting objective and sense of optimization
problem.setSense(KProblem::Minimize);
problem.setObjective(*cycleTime);

int result = solver.optimize();
// solution printing
KSolution * sol = &problem.getSolution();
// print solution resume
sol->printResume();

// Solution printing
printf("Minimum cycle time: %i\n", sol->getValue(*cycleTime));
printf("Sequence of batches:\nBatch Duration Cleaning\n");
for (k=0;k<NJ;k++) {
printf("%i\t%i\t%i\n", sol->getValue(rank[k]), DUR[sol->getValue(rank[k])], sol->getValue(clean[k]));
}


## Results¶

The minimum cycle time for this problem is 243 minutes which is achieved with the following sequence of batches: 1 2 5 3 4 1. This time includes 202 minutes of (incompressible) processing time and 41 minutes of cleaning.