.. _constraints-examples: ******************** Constraints examples ******************** This chapter contains a collection of examples demonstrating the use of Artelys Kalis for solving different types of (optimization) problems. The first section shows different ways of defining and posting constraints for simple linear constraints. The following sections each introduce a new constraint type. Constraint all-different : Sudoku ================================= Sudoku puzzles, originating from Japan, have recently made their appearance in many western newspapers. The idea of these puzzles is to complete a given, partially filled 9 × 9 board with the numbers 1 to 9 in such a way that no line, column, or 3 × 3 subsquare contains a number more than once. The figures :numref:`fig11_Sudoku1` and :numref:`fig11_Sudoku2` show two instances of such puzzles. Whilst sometimes tricky to solve for a human, these puzzles lend themselves to solving by a constraint programming approach. .. _fig11_Sudoku1: .. figure:: images/11_Sudoku1.png :scale: 60% :align: center Sudoku (‘The Times’, 26 January, 2005) .. _fig11_Sudoku2: .. figure:: images/11_Sudoku2.png :scale: 60% :align: center Sudoku (‘The Guardian’, 29 July, 2005) Model formulation ----------------- As in the examples, we denote the columns of the board by the set :math:`XS = \{A, B, ..., I\}` and the rows by :math:`YS = \{0, 1, ..., 8\}`. For every :math:`x` in :math:`XS` and :math:`y` in :math:`YS` we define a decision variable :math:`V_{xy}` taking as its value the number at the position :math:`(x, y)`. The only constraints in this problem are: * all numbers in a row must be different, * all numbers in a column must be different, * all numbers in a 3 × 3 subsquare must be different. These constraints can be stated with the ``KAllDifferent`` constraint that ensures that all variables in the relation take different values. .. math:: &\forall x \in XS, y \in YS : V_{xy} \in \{1, ..., 9\} \\ &\forall x \in XS : \text{all-different}(V_{x1}, ..., V_{x9}) \\ &\forall y \in YS : \text{all-different}(V_{Ay}, ..., V_{Iy}) \\ &\text{all-different}(V_{A1}, ..., V_{C3}) \\ &\text{all-different}(V_{A4}, ..., V_{C6}) \\ &\text{all-different}(V_{A7}, ..., V_{C9}) \\ &\text{all-different}(V_{D1}, ..., V_{F3}) \\ &\text{all-different}(V_{D4}, ..., V_{F6}) \\ &\text{all-different}(V_{D7}, ..., V_{F9}) \\ &\text{all-different}(V_{G1}, ..., V_{I3}) \\ &\text{all-different}(V_{G4}, ..., V_{I6}) \\ &\text{all-different}(V_{G7}, ..., V_{I9}) In addition, certain variables :math:`V_{xy}` are fixed to the given values. Implementation -------------- The implementation for the Sudoku puzzle in figure :numref:`fig11_Sudoku2` looks as follows: .. tabs:: .. code-tab:: c++ // index variables int indexX,indexY; // Creation of the problem in this session KProblem problem(session,"Sudoku"); // creation of a 9x9 matrix of KintVar // we use the following bijection: A,B,..,I <-> 0,1,..,8 char name[80]; KIntVar *** vars = new KIntVar **[9]; for (indexX = 0;indexX < 9 ; indexX++) { vars[indexX] = new KIntVar * [9]; for (indexY = 0;indexY < 9 ; indexY++) { sprintf(name,"v[%i,%i]",indexX,indexY); vars[indexX][indexY] = new KIntVar(problem,name,1,9); } } // Data from "The Guardian", 29 July, 2005. http://www.guardian.co.uk/sudoku vars[0][0]->instantiate(8); vars[5][0]->instantiate(3); vars[1][1]->instantiate(5); vars[6][1]->instantiate(4); vars[0][2]->instantiate(2); vars[4][2]->instantiate(7); vars[7][2]->instantiate(6); vars[3][3]->instantiate(1); vars[8][3]->instantiate(5); vars[2][4]->instantiate(3); vars[6][4]->instantiate(9); vars[0][5]->instantiate(6); vars[5][5]->instantiate(4); vars[1][6]->instantiate(7); vars[4][6]->instantiate(2); vars[8][6]->instantiate(3); vars[2][7]->instantiate(4); vars[7][7]->instantiate(1); vars[3][8]->instantiate(9); vars[8][8]->instantiate(8); // All-different values in rows for (indexX = 0;indexX < 9 ; indexX++) { KIntVarArray tmpY; for (indexY = 0;indexY < 9 ; indexY++) { tmpY += *vars[indexX][indexY]; } problem.post(new KAllDifferent("alldiffVert",tmpY,KAllDifferent::GENERALIZED_ARC_CONSISTENCY)); } // All-different values in columns for (indexY = 0;indexY < 9 ; indexY++) { KIntVarArray tmpX; for (indexX = 0;indexX < 9 ; indexX++) { tmpX += *vars[indexX][indexY]; } problem.post(new KAllDifferent("alldiffHoriz",tmpX,KAllDifferent::GENERALIZED_ARC_CONSISTENCY)); } // All-different values in 3x3 squares int i,j; for (j=0;j<3;j++) { for (i=0;i<3;i++) { KIntVarArray tmpXY; for (indexY = i*3;indexY < i*3+3 ; indexY++) { for (indexX = j*3;indexX < j*3+3 ; indexX++) { tmpXY += *vars[indexX][indexY]; } } problem.post(new KAllDifferent("alldiff3x3",tmpXY,KAllDifferent::GENERALIZED_ARC_CONSISTENCY)); } } // propagating problem if (problem.propagate()) { printf("Problem is infeasible\n"); exit(1); } // creation of the solver KSolver solver(problem); // look for all solutions int result = solver.findAllSolutions(); // solution printing KSolution * sol = &problem.getSolution(); // print solution resume sol->printResume(); printf("|-------|-------|-------|\n"); for (indexY = 0;indexY < 9 ; indexY++) { printf("| %i %i %i | %i %i %i | %i %i %i |\n", sol->getValue(*vars[0][indexY]), sol->getValue(*vars[1][indexY]), sol->getValue(*vars[2][indexY]), sol->getValue(*vars[3][indexY]), sol->getValue(*vars[4][indexY]), sol->getValue(*vars[5][indexY]), sol->getValue(*vars[6][indexY]), sol->getValue(*vars[7][indexY]), sol->getValue(*vars[8][indexY]) ); if (indexY % 3 == 2) { printf("|-------|-------|-------|\n"); } } // memory desallocation for (indexX = 0;indexX < 9 ; indexX++) { for (indexY = 0;indexY < 9 ; indexY++) { delete vars[indexX][indexY]; } delete[] vars[indexX]; } delete[] vars; .. code-tab:: py import sys, os from kalis import * # Creation of the session session = KSession() # Creation of the problem in this session problem = KProblem(session, "Sudoku") # Creation of a 9x9 matrix of KintVar vars = [ [ KIntVar(problem, "v[{},{}]".format(i,j), 1, 9) for j in xrange(9) ] for i in xrange(9) ] pb = raw_input("Enter problem to be solved (1 or 2): ") # Data from "The Guardian", 29 July, 2005. http://www.guardian.co.uk/sudoku if pb == "1": vars[0][0].instantiate(8); vars[5][0].instantiate(3) vars[1][1].instantiate(5); vars[6][1].instantiate(4) vars[0][2].instantiate(2); vars[4][2].instantiate(7); vars[7][2].instantiate(6) vars[3][3].instantiate(1); vars[8][3].instantiate(5) vars[2][4].instantiate(3); vars[6][4].instantiate(9) vars[0][5].instantiate(6); vars[5][5].instantiate(4) vars[1][6].instantiate(7); vars[4][6].instantiate(2); vars[8][6].instantiate(3) vars[2][7].instantiate(4); vars[7][7].instantiate(1) vars[3][8].instantiate(9); vars[8][8].instantiate(8) elif pb == "2": vars[0][3].instantiate(3); vars[0][6].instantiate(5) vars[1][4].instantiate(4); vars[1][7].instantiate(8) vars[2][1].instantiate(1); vars[2][3].instantiate(5) vars[2][4].instantiate(7); vars[2][5].instantiate(9) vars[2][6].instantiate(4); vars[2][8].instantiate(3) vars[3][0].instantiate(5); vars[3][2].instantiate(2) vars[4][2].instantiate(4); vars[4][6].instantiate(1) vars[5][6].instantiate(9); vars[5][8].instantiate(7) vars[6][0].instantiate(4); vars[6][2].instantiate(7) vars[6][3].instantiate(9); vars[6][4].instantiate(1) vars[6][5].instantiate(5); vars[6][7].instantiate(3) vars[7][1].instantiate(6); vars[7][4].instantiate(8) vars[8][2].instantiate(1); vars[8][5].instantiate(3) else: print "problem must be 1 or 2, not {}".format(pb) del session exit(1) # All-different values in rows for i in xrange(9): tmpI = KIntVarArray() for j in xrange(9): tmpI.add(vars[i][j]) problem.post(KAllDifferent("alldiffVert {}".format(i), tmpI, KAllDifferent.GENERALIZED_ARC_CONSISTENCY)) # All-different values in columns for j in xrange(9): tmpJ = KIntVarArray() for i in xrange(9): tmpJ.add(vars[i][j]) problem.post(KAllDifferent("alldiffHoriz {}".format(j), tmpJ, KAllDifferent.GENERALIZED_ARC_CONSISTENCY)) # All-different values in 3x3 squares for offsetI in xrange(0,9,3): for offsetJ in xrange(0,9,3): tmpIJ = KIntVarArray() for i in xrange(3): for j in xrange(3): tmpIJ.add(vars[i + offsetI][j + offsetJ]) problem.post(KAllDifferent("alldiff3x3 {},{}".format(offsetI, offsetJ), tmpIJ, KAllDifferent.GENERALIZED_ARC_CONSISTENCY)) # Propagate problem if problem.propagate(): print "Problem is infeasible" exit(1) # Solve the problem solver = KSolver(problem) solver.findAllSolutions() # Get solution sol = problem.getSolution() # Print solution resume sol.printResume(); print "|-------|-------|-------|" for j in xrange(9): print "| {} {} {} | {} {} {} | {} {} {} |".format( sol.getValue(vars[0][j]), sol.getValue(vars[1][j]), sol.getValue(vars[2][j]), sol.getValue(vars[3][j]), sol.getValue(vars[4][j]), sol.getValue(vars[5][j]), sol.getValue(vars[6][j]), sol.getValue(vars[7][j]), sol.getValue(vars[8][j]) ) if j % 3 == 2: print "|-------|-------|-------|" del session .. code-tab:: java import com.artelys.kalis.*; import java.io.*; public class Sudoku { public static void main(String[] args) { try { System.loadLibrary("Kalis"); // uses java option -Djava.library.path=path to find Kalis.dll System.loadLibrary("KalisJava"); // uses java option -Djava.library.path=path to find KalisJava.dll KSession session = new KSession(); // Creation of the problem in this session KProblem problem = new KProblem(session, "Sudoku", 4); KIntVar[][] vars = new KIntVar[9][9]; int i, j; for (i = 0; i < 9; i++) { for (j = 0; j < 9; j++) { vars[i][j] = new KIntVar(problem, String.format("v[%d,%d]", i, j), 1, 9); } } System.out.print("Enter problem to be solved (1 or 2): "); BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); String pb = br.readLine(); if (pb.equals("1")) { vars[0][0].instantiate(8); vars[5][0].instantiate(3); vars[1][1].instantiate(5); vars[6][1].instantiate(4); vars[0][2].instantiate(2); vars[4][2].instantiate(7); vars[7][2].instantiate(6); vars[3][3].instantiate(1); vars[8][3].instantiate(5); vars[2][4].instantiate(3); vars[6][4].instantiate(9); vars[0][5].instantiate(6); vars[5][5].instantiate(4); vars[1][6].instantiate(7); vars[4][6].instantiate(2); vars[8][6].instantiate(3); vars[2][7].instantiate(4); vars[7][7].instantiate(1); vars[3][8].instantiate(9); vars[8][8].instantiate(8); } else if (pb.equals("2")) { vars[0][3].instantiate(3); vars[0][6].instantiate(5); vars[1][4].instantiate(4); vars[1][7].instantiate(8); vars[2][1].instantiate(1); vars[2][3].instantiate(5); vars[2][4].instantiate(7); vars[2][5].instantiate(9); vars[2][6].instantiate(4); vars[2][8].instantiate(3); vars[3][0].instantiate(5); vars[3][2].instantiate(2); vars[4][2].instantiate(4); vars[4][6].instantiate(1); vars[5][6].instantiate(9); vars[5][8].instantiate(7); vars[6][0].instantiate(4); vars[6][2].instantiate(7); vars[6][3].instantiate(9); vars[6][4].instantiate(1); vars[6][5].instantiate(5); vars[6][7].instantiate(3); vars[7][1].instantiate(6); vars[7][4].instantiate(8); vars[8][2].instantiate(1); vars[8][5].instantiate(3); } else { throw new RuntimeException(String.format( "problem must be 1 or 2, not '%s'", pb)); } // *** Modeling of the problem i = 0; for (KIntVar[] row : vars) { KIntVarArray tmpRow = new KIntVarArray(); for (KIntVar cell : row) { tmpRow.add(cell); } problem.post(new KAllDifferent( String.format("alldiffHoriz#%d", i++), tmpRow, KAllDifferent.PropagationLevel.GENERALIZED_ARC_CONSISTENCY .swigValue())); } for(i = 0; i < 9; i++) { KIntVarArray tmpCol = new KIntVarArray(); for(j = 0; j < 9; j++) { tmpCol.add(vars[j][i]); } problem.post(new KAllDifferent( String.format("alldiffVert#%d", i), tmpCol, KAllDifferent.PropagationLevel.GENERALIZED_ARC_CONSISTENCY .swigValue())); } int[] offsets = {0,3,6}; for(int offsetI : offsets) { for(int offsetJ : offsets) { KIntVarArray tmpSquare = new KIntVarArray(); for(i = offsetI; i < offsetI+3; i++) { for(j = offsetJ; j < offsetJ+3; j++) { tmpSquare.add(vars[j][i]); } } problem.post(new KAllDifferent( String.format("alldiff%dx%d", offsetI, offsetJ), tmpSquare, KAllDifferent.PropagationLevel.GENERALIZED_ARC_CONSISTENCY .swigValue())); } } if (problem.propagate()) { throw new RuntimeException("Problem is infeasible"); } KSolver solver = new KSolver(problem); solver.findAllSolutions(); KSolution sol = problem.getSolution(); sol.printResume(); System.out.println("|-------|-------|-------|"); for(j = 0; j < 9; j++) { System.out.println(String.format("| %d %d %d | %d %d %d | %d %d %d |", sol.getValue(vars[0][j]), sol.getValue(vars[1][j]), sol.getValue(vars[2][j]), sol.getValue(vars[3][j]), sol.getValue(vars[4][j]), sol.getValue(vars[5][j]), sol.getValue(vars[6][j]), sol.getValue(vars[7][j]), sol.getValue(vars[8][j]))); if (j % 3 == 2) System.out.println("|-------|-------|-------|"); } } catch (Exception e) { e.printStackTrace(); } } } Results ------- The model shown above generates the following output; this puzzle has only one solution, as is usually the case for Sudoku puzzles. .. _fig11_Sudoku_result: .. figure:: images/11_Sudoku_result.png :align: center Result of the Sudoku described in :numref:`fig11_Sudoku2` The ``KAllDifferent`` constructor takes an optional third argument that allows the user to specify the propagation algorithm to be used for evaluating the constraint. If we change from the default setting (``KAllDifferent::FORWARD_CHECKING``) to the more aggressive strategy ``KAllDifferent::GENERALIZED_ARC_CONSISTENCY`` by adding this choice as the third argument of the constructor of ``KAllDifferent``, we observe that the number of nodes is reduced to a single node the problem is solved by simply posting the constraints. Whereas the time spent in the search is down to zero, the constraint posting now takes 4-5 times longer (still just a fraction of a second) due to the larger computational overhead of the generalized arc consistency algorithm. Allover, the time for problem definition and solving is reduced to less than a tenth of the previous time. As a general rule, the generalized arc consistency algorithm achieves stronger pruning (i.e., it removes more values from the domains of the variables). However, due to the increase in computation time its use is not always justified. The reader is therefore encouraged to try both algorithm settings in his models. .. _kAbs_distance: Constraint distance: Frequency assignment ========================================= The area of telecommunications, and in particular mobile telecommunications, gives rise to many different variants of frequency assignment problems. We are given a network of cells (nodes) with requirements of discrete frequency bands. Each cell has a given demand for a number of frequencies (bands). Figure 11.2.1 shows the structure of the network. Nodes linked by an edge are considered as neighbors. They must not be assigned the same frequencies to avoid interference. Furthermore, if a cell uses several frequencies they must all be different by at least 2. The objective is to minimize the total number of frequencies used in the network. .. _fig11_telecommunications_network: .. figure:: images/11_telecommunications_network.png :align: center :scale: 60% Telecommunications network The following table lists the number of frequency demands for every cell: +--------+---+---+---+---+---+---+---+---+---+----+ | Cell | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | +--------+---+---+---+---+---+---+---+---+---+----+ | Demand | 4 | 5 | 2 | 3 | 2 | 4 | 3 | 4 | 3 | 2 | +--------+---+---+---+---+---+---+---+---+---+----+ Model formulation ----------------- Let :math:`NODES` be the set of all nodes in the network and :math:`DEM_n` the demand of frequencies at node :math:`n \in NODES`. The network is given as a set of edges :math:`LINKS`. Furthermore, let :math:`DEMANDS = \{1, 2, . . . ,NUMDEM\}` be the set of frequencies, numbered consecutively across all nodes where the upper bound :math:`NUMDEM` is given by the total number of demands. The auxiliary array :math:`INDEX_n` indicates the starting index in :math:`DEMANDS` for node :math:`n`. For representing the frequency assigned to every demand :math:`d \in DEMANDS` we introduce the variables :math:`use` that take their values from the set :math:`\{1, 2, . . . ,NUMDEM\}`. The two sets of constraints (different frequencies assigned to neighboring nodes and minimum distance between frequencies within a node) can then be modeled as follows. .. math:: &\forall (n,m) \in LINKS : \text{all-different}(\bigcup\limits_{d=INDEX_n}^{INDEX_n+DEM_n-1} use_d \cup \bigcup\limits_{d=INDEX_m}^{INDEX_m+DEM_m-1} use_d) \\ &\forall n \in NODES, \forall c < d \in \{INDEX_n, ...,INDEX_n + DEM_n - 1\} : |use_c - use_d| \geq 2 \\ The objective function is to minimize to the number of frequencies used. We formulate this by minimizing the largest frequency number that occurs for the :math:`use` variables: .. math:: &\text{minimize} \text{ } \text{maximum}_{d \in DEMANDS}(use_d)\\ Implementation ----------------- The edges forming the telecommunications network are modeled as a list :math:`LINK`, where edge :math:`l` is given as :math:`(LINK(l,1),LINK(l,2))`. For the implementation of the constraints on the values of frequencies assigned to the same node we have two equivalent choices with Kalis, namely using ``abs`` or ``distance`` constraints. .. tabs:: .. code-tab:: c++ const int NODES = 10; // Range of links between nodes const int LINKS = 18 ; // Demand of nodes int DEM[] = {4, 5, 2, 3, 2, 4, 3, 4, 3, 2}; // Neighboring nodes int LINK[18][2] = {{1, 3}, {1, 4}, {1, 6},{2, 4}, {2, 7},{3, 4}, {3, 6}, {3, 8}, {3, 9},{4, 7}, {4, 9}, {4,10},{5, 7}, {5, 8}, {5, 9},{6, 9}, {7, 8}, {8,10}}; // Start index in 'use' int INDEX[10]; // Upper bound on no. of freq. int NUMDEM; int n; NUMDEM = 0; for (n=0;ngetObjectiveValue(); numfreq.setSup(nsol-1); } printf("Number of frequencies: %f\n", problem.getSolution().getObjectiveValue()); printf("Frequency assignment: \n"); for (n=0;n 0) { solver.printStats(); KSolution sol = problem.getSolution(); nsol = (int) sol.getObjectiveValue(); numfreq.setSup(nsol - 1); } System.out.println("Number of frequencies: " + problem.getSolution().getObjectiveValue()); System.out.println("Frequency assignment: "); for (indexNode=0; indexNode 0) { solver2.printStats(); KSolution sol = problem.getSolution(); } System.out.println("Number of frequencies: " + problem.getSolution().getObjectiveValue()); System.out.println("Frequency assignment: "); for (indexNode=0; indexNodegetSolution().printResume(); return 0; } .. code-tab:: py class SolutionListener(KSolverEventListener): def __init__(self, problem): KSolverEventListener.__init__(self, problem) def solutionFound(self, solution, thread_id): solution.printResume() .. code-tab:: java class MySolverEventListener extends KSolverEventListener { public void nodeExplored() { System.out.println("Node explored"); } public void branchGoDown() { System.out.println("Branch go down"); } public void branchGoUp() { System.out.println("Branch go up"); } public void branchingScheme() { } public boolean stopComputations() { return false; } public void solutionFound() { System.out.println("A solution has been found!"); } } Improving the problem formulation --------------------------------- We may observe that in our problem formulation all demand variables within a node and the constraints on these variables are entirely symmetric. In the absence of other constraints, we may reduce these symmetries by imposing an order on the :math:`use` variables, :math:`use_d + 1 \leq use_{d+1}` for demands :math:`d` and :math:`d+1` belonging to the same cell. Doing so, the problem is solved to optimality within less than 40 nodes using just the default strategy. We may take this a step further by writing: :math:`use_d + 2 \leq use_{d+1}`. The addition of these constraints shortens the search by yet a few more nodes. They can even be used simply in replacement of the ``abs`` or ``distance`` constraints. Results ----------------- An optimal solution to this problem uses 11 different frequencies. The model shown in the program listing prints out the following assignment of frequencies to nodes: .. _fig11_frequence_assignment: .. figure:: images/11_Frequencies_assignment.png :align: center Assignment of frequencies result .. _kElement: Constraint element: Sequencing jobs on a single machine ======================================================= The problem described in this section is taken from Section 7.4 *Sequencing jobs on a bottleneck machine* of the book *Applications of optimization with Xpress-MP*. The aim of this problem is to provide a model that may be used with different objective functions for scheduling operations on a single (bottleneck) machine. We shall see here how to minimize the total processing time, the average processing time, and the total tardiness. A set of tasks (or jobs) is to be processed on a single machine. The execution of tasks is nonpreemptive (that is, an operation may not be interrupted before its completion). For every task :math:`i` its release date, duration, and due date are given in the following table : +---------------+----+----+----+----+---+----+----+ | Job | 1 | 2 | 3 | 4 | 5 | 6 | 7 | +---------------+----+----+----+----+---+----+----+ | Release date | 2 | 5 | 4 | 0 | 0 | 8 | 9 | +---------------+----+----+----+----+---+----+----+ | Duration | 5 | 6 | 8 | 4 | 2 | 4 | 2 | +---------------+----+----+----+----+---+----+----+ | Due date | 10 | 21 | 15 | 10 | 5 | 15 | 22 | +---------------+----+----+----+----+---+----+----+ What is the optimal value for each of the objectives: minimizing the total duration of the schedule (*makespan*), the mean processing time or the total tardiness (that is, the amount of time by which the completion of jobs exceeds their respective due dates) ? Model formulation ----------------- We are going to present a model formulation that is close to the Mathematical Programming formulation in *Applications of optimization with Xpress-MP*. In model formulation we are going to deal with the different objective functions in sequence, but the body of the models will remain the same. To represent the sequence of jobs we introduce variables :math:`rank_k` with :math:`k \in JOBS = \{1, . . . , NJ\}` that take as value the number of the job in position (rank) :math:`k`. Every job :math:`j` takes a single position. This constraint can be represented by a ``KAllDifferent`` on the :math:`rank_k` variables: .. math:: &\text{all-different}(rank_1,..., rank_{NJ})\\ The processing time :math:`dur_k` for the job in position :math:`k` is given by :math:`DUR_{rank_k}` (where :math:`DUR_j` denotes the duration given in the table in the previous section). Similarly, the release time :math:`rel_k` is given by :math:`REL_{rank_k}` (where :math:`REL_j` denotes the given release date): .. math:: &\forall k \in JOBS : dur_k = DUR_{rank_k}\\ &\forall k \in JOBS : rel_k = REL_{rank_k}\\ If :math:`start_k` is the start time of the job at position :math:`k`, this value must be at least as great as the release date of the job assigned to this position. The completion time :math:`comp_k` of this job is the sum of its start time plus its duration: .. math:: &\forall k \in JOBS : start_k \geq rel_k\\ &\forall k \in JOBS : comp_k = start_k + dur_k\\ Another constraint is needed to specify that two jobs cannot be processed simultaneously. The job in position :math:`k+1` must start after the job in position :math:`k` has finished, hence the following constraints: .. math:: &\forall k \in \{1, ..., NJ-1 \} : start_{k+1} \geq start_k + dur_k\\ **Objective 1:** The first objective is to minimize the makespan (completion time of the schedule), or, equivalently, to minimize the completion time of the last job (job with rank :math:`NJ`). The complete model is then given by the following (where :math:`MAXTIME` is a sufficiently large value, such as the sum of all release dates and all durations): .. math:: &\text{minimize } comp_{NJ}\\ &\forall k \in JOBS : rank_k \in JOBS\\ &\forall k \in JOBS : start_k,comp_k \in \{0, ..., MAXTIME\}\\ &\forall k \in JOBS : dur_k \in \{\min_{j\in JOBS}DUR_j, ..., \max_{j \in JOBS}DUR_j\}\\ &\forall k \in JOBS : rel_k \in \{\min_{j\in JOBS}REL_j, ..., \max_{j \in JOBS}REL_j\}\\ &\text{all-different}(rank_1, ..., rank_{NJ})\\ &\forall k \in JOBS : dur_{k} = DUR_{rank_k}\\ &\forall k \in JOBS : rel_{k} = REL_{rank_k}\\ &\forall k \in JOBS : start_k \geq rel_k\\ &\forall k \in JOBS : comp_k = start_k + dur_k\\ &\forall k \in \{1, ..., NJ-1 \} : start_{k+1} \geq start_k + dur_k\\ **Objective 2:** For minimizing the average processing time, we introduce an additional variable :math:`totComp` representing the sum of the completion times of all jobs. We add the following constraint to the problem to calculate :math:`totComp`: .. math:: &totComp = \sum_{k \in JOBS} comp_{k}\\ The new objective consists of minimizing the average processing time, or equivalently, minimizing the sum of the job completion times: :math:`\text{minimize }totComp` **Objective 3:** If we now aim to minimize the total tardiness, we again introduce new variables this time to measure the amount of time that jobs finish after their due date. We write :math:`late_k` for the variable that corresponds to the tardiness of the job with rank :math:`k`. Its value is the difference between the completion time of a job :math:`j` and its due date :math:`DUE_j`. If the job finishes before its due date, the value must be zero. We thus obtain the following constraints: .. math:: &\forall k \in JOBS : due_k = DUE_{rank_k}\\ &\forall k \in JOBS : late_k \geq comp_k - due_k\\ For the formulation of the new objective function we introduce the variable :math:`totLate` representing the total tardiness of all jobs. The objective now is to minimize the value of this variable: .. math:: &\text{minimize }totLate\\ &totLate = \sum_{k \in JOBS} late_k\\ Implementation ----------------- The implementation below (BottleneckSequencing.cpp) solves the same problem three times, each time with a different objective, and prints the resulting solutions. .. tabs:: .. code-tab:: c++ // Number of tasks int NTASKS = 7; // Release date of tasks int REL[] = { 2, 5, 4, 0, 0, 8, 9}; // Duration of tasks int DUR[] = { 5, 6, 8, 4, 2, 4, 2}; // Due date of tasks int DUE[] = {10, 21, 15, 10, 5, 15, 22}; // Number of job at position k KIntVarArray rank; // Start time of job at position k KIntVarArray start; // Duration of job at position k KIntVarArray dur; // Completion time of job at position k KIntVarArray comp; // Release date of job at position k KIntVarArray rel; // Creation of the problem in this session KProblem problem(session,"B-4 Sequencing"); // compute some statistics int indexTask; int MAXTIME = 0; int MINDUR = MAX_INT; int MAXDUR = 0; int MINREL = MAX_INT; int MAXREL = 0; int MINDUE = MAX_INT; int MAXDUE = 0; int SDUR = 0; for (indexTask = 0;indexTask < NTASKS; indexTask++) { if (MINDUR > DUR[indexTask]) { MINDUR = DUR[indexTask]; } if (MAXDUR < DUR[indexTask]) { MAXDUR = DUR[indexTask]; } if (MINREL > REL[indexTask]) { MINREL = REL[indexTask]; } if (MAXREL < REL[indexTask]) { MAXREL = REL[indexTask]; } if (MINDUE > DUE[indexTask]) { MINDUE = DUE[indexTask]; } if (MAXDUE < DUE[indexTask]) { MAXDUE = DUE[indexTask]; } SDUR += DUR[indexTask]; } MAXTIME = MAXREL + SDUR; char name[80]; for (indexTask = 0;indexTask < NTASKS; indexTask++) { sprintf(name,"rank(%i)",indexTask); rank += (* new KIntVar(problem,name,0,NTASKS-1)); } for (indexTask = 0;indexTask < NTASKS; indexTask++) { sprintf(name,"start(%i)",indexTask); start += (* new KIntVar(problem,name,0,MAXTIME)); } for (indexTask = 0;indexTask < NTASKS; indexTask++) { sprintf(name,"dur(%i)",indexTask); dur += (* new KIntVar(problem,name,MINDUR,MAXDUR)); } for (indexTask = 0;indexTask < NTASKS; indexTask++) { sprintf(name,"comp(%i)",indexTask); comp += (* new KIntVar(problem,name,0,MAXTIME)); } for (indexTask = 0;indexTask < NTASKS; indexTask++) { sprintf(name,"rel(%i)",indexTask); rel += (* new KIntVar(problem,name,MINREL,MAXREL)); } // One position per job problem.post(KAllDifferent("alldiff(rank)",rank)); // Duration of job at position k KIntArray idur; KIntArray irel; KIntArray idue; for (indexTask = 0;indexTask < NTASKS; indexTask++) { idur += DUR[indexTask]; irel += REL[indexTask]; idue += DUE[indexTask]; } for (indexTask = 0;indexTask < NTASKS; indexTask++) { KEltTerm kelt(idur,rank[indexTask]); problem.post(kelt == dur[indexTask]); // Release date of job at position k KEltTerm keltrel(irel,rank[indexTask]); problem.post(keltrel == rel[indexTask]); } for (indexTask = 0;indexTask < NTASKS-1; indexTask++) { // Sequence of jobs problem.post(start[indexTask+1] >= start[indexTask] + dur[indexTask]); } for (indexTask = 0;indexTask < NTASKS; indexTask++) { // Start times problem.post(start[indexTask] >= rel[indexTask]); // Completion times problem.post(comp[indexTask] == start[indexTask] + dur[indexTask]); } // propagating problem if (problem.propagate()) { printf("Problem is infeasible\n"); exit(1); } // Set the branching strategy KBranchingSchemeArray myBa; myBa += KSplitDomain(KSmallestDomain(),KMinToMax()); // creation of the solver KSolver solver(problem,myBa); // ********************** // Objective 1: Makespan // ********************** problem.setObjective(comp[NTASKS-1]); problem.setSense(KProblem::Minimize); // look for all solutions int result = solver.optimize(); // solution printing KSolution * sol = &problem.getSolution(); // print solution resume sol->printResume(); // solution printing printf("Completion time: %i\n",problem.getSolution().getValue(comp[NTASKS-1])); printf("Rel\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",REL[indexTask]); } printf("\nDur\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",DUR[indexTask]); } printf("\nStart\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",problem.getSolution().getValue(start[indexTask])); } printf("\nEnd\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",problem.getSolution().getValue(comp[indexTask])); } printf("\nDue\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",DUE[indexTask]); } printf("\n"); // *************************************** // Objective 2: Average completion time: // *************************************** KLinTerm totalCompletionTerm; for (indexTask = 0;indexTask < NTASKS; indexTask++) { totalCompletionTerm = totalCompletionTerm + comp[indexTask]; } KIntVar averageCompletion(problem,"average completion",0,1000); problem.post(averageCompletion == totalCompletionTerm); problem.setObjective(averageCompletion); result = solver.optimize(); // solution printing printf("Completion time: %i\n",problem.getSolution().getValue(comp[NTASKS-1])); printf("average: %i\n",problem.getSolution().getValue(averageCompletion)); printf("Rel\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",REL[indexTask]); } printf("\nDur\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",DUR[indexTask]); } printf("\nStart\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",problem.getSolution().getValue(start[indexTask])); } printf("\nEnd\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",problem.getSolution().getValue(comp[indexTask])); } printf("\nDue\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",DUE[indexTask]); } printf("\n"); // ***************************** // Objective 3: total lateness: // ***************************** // Lateness of job at position k KIntVarArray late; // Due date of job at position k KIntVarArray due; for (indexTask = 0;indexTask < NTASKS; indexTask++) { sprintf(name,"due(%i)",indexTask); due += (* new KIntVar(problem,name,MINDUE,MAXDUE)); sprintf(name,"late(%i)",indexTask); late += (* new KIntVar(problem,name,0,MAXTIME)); } // Due date of job at position k for (indexTask = 0;indexTask < NTASKS; indexTask++) { KEltTerm keltdue(idue,rank[indexTask]); problem.post(keltdue == due[indexTask]); } KLinTerm totLatTerm; // building each tasks with fixed time horizon (0..HORIZON) for (indexTask = 0;indexTask < NTASKS; indexTask++) { // Late jobs: completion time exceeds the due date problem.post(late[indexTask] >= (comp[indexTask]) - due[indexTask]); totLatTerm = totLatTerm + late[indexTask]; } KIntVar totLate(problem,"total lateness",0,1000); problem.post(totLate == totLatTerm); problem.setObjective(totLate); result = solver.optimize(); // solution printing printf("Completion time: %i\n",problem.getSolution().getValue(comp[NTASKS-1])); printf("average: %i\n",problem.getSolution().getValue(averageCompletion)); printf("Tardiness: %i\n",problem.getSolution().getValue(totLate)); printf("Rel\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",REL[indexTask]); } printf("\nDur\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",DUR[indexTask]); } printf("\nStart\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",problem.getSolution().getValue(start[indexTask])); } printf("\nEnd\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",problem.getSolution().getValue(comp[indexTask])); } printf("\nDue\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",DUE[indexTask]); } printf("\nLate\t"); for (indexTask = 0;indexTask < NTASKS; indexTask++) { printf("%i\t",problem.getSolution().getValue(late[indexTask])); } printf("\n"); .. code-tab:: py import sys from kalis import * ### Data creation nb_tasks = 7 release_dates = [2, 5, 4, 0, 0, 8, 9] durations = [5, 6, 8, 4, 2, 4, 2] due_dates = [10, 21, 15, 10, 5, 15, 22] ### Variable creation # Tasks positions rank = KIntVarArray() # Jobs starting dates jobs_start = KIntVarArray() # Duration of job for each position jobs_durations = KIntVarArray() # Completion time for each position jobs_completions = KIntVarArray() # Release date of job for each postion jobs_release_dates = KIntVarArray() ### Creation of the problem # Creation of the Kalis session session = KSession() # Creation of the optimization problem problem = KProblem(session, "B-4 Sequencing") # Compute some statistics min_duration = min(durations) max_duration = max(durations) min_release_date = min(release_dates) max_realease_date = max(release_dates) min_due_date = min(due_dates) max_due_date = max(due_dates) max_time = max_realease_date + sum(durations) for task_index in range(nb_tasks): rank += KIntVar(problem, "rank(%d)" % task_index, 0, nb_tasks - 1) jobs_start += KIntVar(problem, "start(%d)" % task_index, 0, max_time) jobs_durations += KIntVar(problem, "dur(%d)" % task_index, min_duration, max_duration) jobs_completions += KIntVar(problem, "comp(%d)" % task_index, 0, max_time) jobs_release_dates += KIntVar(problem, "rel(%d)" % task_index, min_release_date, max_realease_date) ### Creation of the constraints # One position per job problem.post(KAllDifferent("alldiff(rank)", rank)) # Convert python lists data to KIntArray K_durations = KIntArray() K_release_dates = KIntArray() for task_index in range(nb_tasks): res = K_durations.add(durations[task_index]) res = K_release_dates.add(release_dates[task_index]) # Corresponding durations and jobs durations variables with a KElement constraint # i.e. "durations[rank[task_index]] == jobs_durations[task_index]" for task_index in range(nb_tasks): duration_kelt = KEltTerm(K_durations, rank[task_index]) problem.post(duration_kelt == jobs_durations[task_index]) release_date_kelt = KEltTerm(K_release_dates, rank[task_index]) problem.post(release_date_kelt == jobs_release_dates[task_index]) # Ordering of starting dates between jobs for task_index in range(nb_tasks - 1): problem.post(jobs_start[task_index + 1] >= jobs_start[task_index] + jobs_durations[task_index]) # Ordering start times and release dates for task_index in range(nb_tasks): problem.post(jobs_start[task_index] >= jobs_release_dates[task_index]) # Job completion date is equal to its start date plus its duration for task_index in range(nb_tasks): problem.post(jobs_completions[task_index] == jobs_start[task_index] + jobs_durations[task_index]) # First propagation of the problem if problem.propagate(): print("Problem is infeasible") sys.exit(1) ### Solve the problem # Set the branching scheme my_branching_array = KBranchingSchemeArray() my_branching_array += KSplitDomain(KSmallestDomain(), KMinToMax()) # Creation of the solver solver = KSolver(problem, my_branching_array) ### Objective 1 : minimize the makespan problem.setObjective(jobs_completions[nb_tasks - 1]) problem.setSense(KProblem.Minimize) # Look for all feasible solutions result = solver.optimize() # Printing the solution def printSequencingSolution(solution): print("Completion time: ", solution.getObjectiveValue()) print("Release dates: ", end='\t') for task_index in range(nb_tasks): print(release_dates[task_index], end="\t") print("\nDurations: ", end='\t') for task_index in range(nb_tasks): print(durations[task_index], end="\t") print("\nStart dates: ", end='\t') for task_index in range(nb_tasks): print(solution.getValue(jobs_start[task_index]), end="\t") print("\nEnd dates: ", end='\t') for task_index in range(nb_tasks): print(solution.getValue(jobs_completions[task_index]), end="\t") print("\nDue dates: ", end='\t') for task_index in range(nb_tasks): print(due_dates[task_index], end="\t") print("") if result: solution = problem.getSolution() solution.printResume() printSequencingSolution(solution) ### Objective 2: minimize the average completion time jobs_completions_sum = 0 for task_index in range(nb_tasks): jobs_completions_sum += jobs_completions[task_index] # The average completion time is defined as equal to the sum of the completion times since it # is equivalent for the optimization phase. average_completion = KIntVar(problem, "average completion", 0, 1000) problem.post(average_completion == jobs_completions_sum) problem.setObjective(average_completion) result = solver.optimize() if result: solution = problem.getSolution() solution.printResume() printSequencingSolution(solution) ### Objective 3: minimize the average completion time # Declare lateness of each job as a variable jobs_lateness = KIntVarArray() for task_index in range(nb_tasks): jobs_lateness += KIntVar(problem, "late(%d)" % task_index, 0, max_time) # Declare due date of each job as a variable jobs_due_dates = KIntVarArray() for task_index in range(nb_tasks): jobs_due_dates += KIntVar(problem, "due(%d)" % task_index, min_due_date, max_due_date) # Convert python lists to KIntArray K_due_dates = KIntArray() for task_index in range(nb_tasks): res = K_due_dates.add(due_dates[task_index]) # Set due date for each job (i.e. "jobs_due_dates[rank[task_index]] == due_dates[task_index]") for task_index in range(nb_tasks): due_date_kelt = KEltTerm(K_due_dates, rank[task_index]) problem.post(due_date_kelt == jobs_due_dates[task_index]) # Adding lateness constraint lateness_sum = 0 for task_index in range(nb_tasks): problem.post(jobs_lateness[task_index] >= jobs_completions[task_index] - jobs_due_dates[task_index]) lateness_sum += jobs_lateness[task_index] total_lateness = KIntVar(problem, "total lateness", 0, nb_tasks * max_time) problem.post(total_lateness == lateness_sum) problem.setObjective(total_lateness) result = solver.optimize() if result: solution = problem.getSolution() print("Completion time: %d" % solution.getValue(jobs_completions[nb_tasks-1])) print("average: %f" % (solution.getValue(average_completion) / nb_tasks)) print("Tardiness: %d" % solution.getValue(total_lateness)) printSequencingSolution(solution) .. code-tab:: java // Number of tasks int NTASKS = 7; // Release date of tasks int REL[] = { 2, 5, 4, 0, 0, 8, 9}; // Duration of tasks int DUR[] = { 5, 6, 8, 4, 2, 4, 2}; // Due date of tasks int DUE[] = {10, 21, 15, 10, 5, 15, 22}; System.loadLibrary("KalisJava"); try { // Number of job at position k KIntVarArray rank = new KIntVarArray(); // Start time of job at position k KIntVarArray start = new KIntVarArray(); // Duration of job at position k KIntVarArray dur = new KIntVarArray(); // Completion time of job at position k KIntVarArray comp = new KIntVarArray(); // Release date of job at position k KIntVarArray rel = new KIntVarArray(); KSession session = new KSession(); // Creation of the problem in this session KProblem problem = new KProblem(session, "B-4 Sequencing"); // compute some statistics int indexTask; int MAXTIME = 0; int MINDUR = Integer.MAX_VALUE; int MAXDUR = 0; int MINREL = Integer.MAX_VALUE; int MAXREL = 0; int MINDUE = Integer.MAX_VALUE; int MAXDUE = 0; int SDUR = 0; for (indexTask = 0; indexTask < NTASKS; indexTask++) { if (MINDUR > DUR[indexTask]) { MINDUR = DUR[indexTask]; } if (MAXDUR < DUR[indexTask]) { MAXDUR = DUR[indexTask]; } if (MINREL > REL[indexTask]) { MINREL = REL[indexTask]; } if (MAXREL < REL[indexTask]) { MAXREL = REL[indexTask]; } if (MINDUE > DUE[indexTask]) { MINDUE = DUE[indexTask]; } if (MAXDUE < DUE[indexTask]) { MAXDUE = DUE[indexTask]; } SDUR += DUR[indexTask]; } MAXTIME = MAXREL + SDUR; for (indexTask = 0; indexTask < NTASKS; indexTask++) { rank.add(new KIntVar(problem, "use(" + indexTask + ")", 0, NTASKS - 1)); } for (indexTask = 0; indexTask < NTASKS; indexTask++) { start.add(new KIntVar(problem, "start(" + indexTask + ")", 0, MAXTIME)); } for (indexTask = 0; indexTask < NTASKS; indexTask++) { dur.add(new KIntVar(problem, "dur(" + indexTask + ")", MINDUR, MAXDUR)); } for (indexTask = 0; indexTask < NTASKS; indexTask++) { comp.add(new KIntVar(problem, "comp(" + indexTask + ")", 0, MAXTIME)); } for (indexTask = 0; indexTask < NTASKS; indexTask++) { rel.add(new KIntVar(problem, "rel(" + indexTask + ")", MINREL, MAXREL)); } // One position per job problem.post(new KAllDifferent("alldiff(rank)", rank)); // Duration of job at position k KIntArray idur = new KIntArray(); KIntArray irel = new KIntArray(); KIntArray idue = new KIntArray(); for (indexTask = 0; indexTask < NTASKS; indexTask++) { idur.add(DUR[indexTask]); irel.add(REL[indexTask]); idue.add(DUE[indexTask]); } for (indexTask = 0; indexTask < NTASKS; indexTask++) { KEltTerm kelt = new KEltTerm(idur, rank.getElt(indexTask)); problem.post(new KElement(kelt, dur.getElt(indexTask))); // Release date of job at position k KEltTerm keltrel = new KEltTerm(irel, rank.getElt(indexTask)); problem.post(new KElement(keltrel, rel.getElt(indexTask))); } for (indexTask = 0; indexTask < NTASKS - 1; indexTask++) { // Sequence of jobs // Create the linear combination start.getElt(indexTask+1) - start.getElt(indexTask) - dur.getElt(indexTask)) KLinTerm linearTerm = new KLinTerm(); linearTerm.add(start.getElt(indexTask+1),1); linearTerm.add(start.getElt(indexTask),-1); linearTerm.add(dur.getElt(indexTask),-1); // add the linear combination equality startDates[3] - 1 * startDates[0] - varObj == 0 KNumVarArray intVarArrayToSet = linearTerm.getLvars(); KDoubleArray coeffsToSet = linearTerm.getCoeffs(); problem.post(new KNumLinComb("",coeffsToSet,intVarArrayToSet,0,KNumLinComb.LinCombOperator.GreaterOrEqual)); } for (indexTask = 0; indexTask < NTASKS; indexTask++) { // Start times problem.post(new KGreaterOrEqualXyc(start.getElt(indexTask), rel.getElt(indexTask), 0)); // Completion times // Create the linear combination comp.getElt(indexTask) - start.getElt(indexTask) - dur.getElt(indexTask) KLinTerm linearTerm = new KLinTerm(); linearTerm.add(comp.getElt(indexTask),1); linearTerm.add(start.getElt(indexTask),-1); linearTerm.add(dur.getElt(indexTask),-1); // add the linear combination equality startDates[3] - 1 * startDates[0] - varObj == 0 KNumVarArray intVarArrayToSet = linearTerm.getLvars(); KDoubleArray coeffsToSet = linearTerm.getCoeffs(); problem.post(new KNumLinComb("",coeffsToSet,intVarArrayToSet,0,KNumLinComb.LinCombOperator.Equal)); } // propagating problem if (problem.propagate()) { System.out.println("Problem is infeasible"); exit(1); } // Set the branching strategy KBranchingSchemeArray myBa = new KBranchingSchemeArray(); myBa.add(new KSplitDomain(new KSmallestDomain(), new KMinToMax())); // creation of the solver KSolver solver = new KSolver(problem, myBa); // ********************** // Objective 1: Makespan // ********************** problem.setObjective(comp.getElt(NTASKS - 1)); problem.setSense(KProblem.Sense.Minimize); // look for all solutions int result = solver.optimize(); // solution printing KSolution sol = problem.getSolution(); // print solution resume sol.printResume(); // solution printing System.out.println("Completion time: " + problem.getSolution().getValue(comp.getElt(NTASKS - 1))); System.out.print("Rel\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(REL[indexTask] + "\t"); } System.out.print("\nDur\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(DUR[indexTask] + "\t"); } System.out.print("\nStart\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(problem.getSolution().getValue(start.getElt(indexTask)) + "\t"); } System.out.print("\nEnd\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(problem.getSolution().getValue(comp.getElt(indexTask)) + "\t"); } System.out.print("\nDue\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(DUE[indexTask] + "\t"); } System.out.print("\n"); // *************************************** // Objective 2: Average completion time: // *************************************** KLinTerm totalCompletionTerm = new KLinTerm(); for (indexTask = 0; indexTask < NTASKS; indexTask++) { totalCompletionTerm.add(comp.getElt(indexTask), 1); } KIntVar averageCompletion = new KIntVar(problem, "average completion", 0, 1000); // Create the linear combination averageCompletion - totalCompletionTerm totalCompletionTerm.add(averageCompletion,-1); KNumVarArray intVarArrayToSet = totalCompletionTerm.getLvars(); KDoubleArray coeffsToSet = totalCompletionTerm.getCoeffs(); problem.post(new KNumLinComb("",coeffsToSet,intVarArrayToSet,0,KNumLinComb.LinCombOperator.Equal)); problem.setObjective(averageCompletion); result = solver.optimize(); // solution printing System.out.println("Completion time: " + problem.getSolution().getValue(comp.getElt(NTASKS - 1))); System.out.println("average: " + problem.getSolution().getValue(averageCompletion)); System.out.print("\nRel\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(REL[indexTask] + "\t"); } System.out.print("\nDur\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(DUR[indexTask] + "\t"); } System.out.print("\nStart\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(problem.getSolution().getValue(start.getElt(indexTask)) + "\t"); } System.out.print("\nEnd\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(problem.getSolution().getValue(comp.getElt(indexTask)) + "\t"); } System.out.print("\nDue\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(DUE[indexTask] + "\t"); } System.out.print("\n"); // ***************************** // Objective 3: total lateness: // ***************************** // Lateness of job at position k KIntVarArray late = new KIntVarArray(); // Due date of job at position k KIntVarArray due = new KIntVarArray(); for (indexTask = 0; indexTask < NTASKS; indexTask++) { due.add(new KIntVar(problem, "due(" + indexTask + ")", MINDUE, MAXDUE)); late.add(new KIntVar(problem, "late(" + indexTask + ")", 0, MAXTIME)); } // Due date of job at position k for (indexTask = 0; indexTask < NTASKS; indexTask++) { KEltTerm keltdue = new KEltTerm(idue, rank.getElt(indexTask)); problem.post(new KElement(keltdue, due.getElt(indexTask))); } KLinTerm totLatTerm = new KLinTerm(); // building each tasks with fixed time horizon (0..HORIZON) for (indexTask = 0; indexTask < NTASKS; indexTask++) { // Late jobs: completion time exceeds the due date // Create the linear combination late.getElt(indexTask) - comp.getElt(indexTask) + due.getElt(indexTask) KLinTerm linearTerm = new KLinTerm(); linearTerm.add(late.getElt(indexTask),1); linearTerm.add(comp.getElt(indexTask),-1); linearTerm.add(due.getElt(indexTask),+1); // add the linear combination equality startDates[3] - 1 * startDates[0] - varObj == 0 KNumVarArray intVarArrayToSet_linearTerm = linearTerm.getLvars(); KDoubleArray coeffsToSet_linearTerm = linearTerm.getCoeffs(); problem.post(new KNumLinComb("",coeffsToSet_linearTerm,intVarArrayToSet_linearTerm,0,KNumLinComb.LinCombOperator.GreaterOrEqual)); totLatTerm.add(late.getElt(indexTask), 1); } KIntVar totLate = new KIntVar(problem, "total lateness", 0, 1000); totLatTerm.add(totLate,-1); // add the linear combination equality startDates[3] - 1 * startDates[0] - varObj == 0 KNumVarArray intVarArrayToSet_totLatTerm = totLatTerm.getLvars(); KDoubleArray coeffsToSet_totLatTerm = totLatTerm.getCoeffs(); problem.post(new KNumLinComb("",coeffsToSet_totLatTerm,intVarArrayToSet_totLatTerm,0,KNumLinComb.LinCombOperator.Equal)); problem.setObjective(totLate); result = solver.optimize(); // solution printing System.out.println("Completion time: " + problem.getSolution().getValue(comp.getElt(NTASKS - 1))); System.out.println("average: " + problem.getSolution().getValue(averageCompletion)); System.out.println("Tardiness: " + problem.getSolution().getValue(totLate)); System.out.print("\nRel\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(REL[indexTask] + "\t"); } System.out.print("\nDur\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(DUR[indexTask] + "\t"); } System.out.print("\nStart\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(problem.getSolution().getValue(start.getElt(indexTask)) + "\t"); } System.out.print("\nEnd\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(problem.getSolution().getValue(comp.getElt(indexTask)) + "\t"); } System.out.print("\nDue\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(DUE[indexTask] + "\t"); } System.out.print("\nLate\t"); for (indexTask = 0; indexTask < NTASKS; indexTask++) { System.out.print(problem.getSolution().getValue(late.getElt(indexTask)) + "\t"); } System.out.print("\n"); } catch (Exception e) { e.printStackTrace(); } Results ----------------- The minimum makespan of the schedule is 31, the minimum sum of completion times is 103 (which gives an average of 103 / 7 = 14. 71). A schedule with this objective value is 5 :math:`\rightarrow` 4 :math:`\rightarrow` 1 :math:`\rightarrow` 6 :math:`\rightarrow` 2 :math:`\rightarrow` 3. If we compare the completion times with the due dates we see that jobs 1, 2, 3, and 6 finish late (with a total tardiness of 21). The minimum tardiness is 18. A schedule with this tardiness is 5 :math:`\rightarrow` 1 :math:`\rightarrow` 4 :math:`\rightarrow` 6 :math:`\rightarrow` 2 :math:`\rightarrow` 7 :math:`\rightarrow` 3 where jobs 4 and 7 finish one time unit late and job 3 is late by 16 time units, and it terminates at time 31 instead of being ready at its due date, time 15. This schedule has an average completion time of 15.71. .. _kOccurrence: Constraint occurrence: Sugar production ======================================= The problem description in this section is taken from Section 6.4 *Cane sugar production* of the book *Applications of optimization with Xpress-MP*. The harvest of cane sugar in Australia is highly mechanized. The sugar cane is immediately transported to a sugarhouse in wagons that run on a network of small rail tracks. The sugar content of a wagonload depends on the field it has been harvested from and on the maturity of the sugar cane. Once harvested, the sugar content decreases rapidly through fermentation and the wagonload will entirely lose its value after a certain time. At this moment, eleven wagons loaded with the same quantity have arrived at the sugarhouse. They have been examined to find out the hourly loss and the remaining life span (in hours) of every wagon, these data are summarized in the following table: +---------------+----+----+----+----+----+----+----+----+----+----+----+ | Lot | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | +---------------+----+----+----+----+----+----+----+----+----+----+----+ | Loss (kg/h) | 43 | 26 | 37 | 28 | 13 | 54 | 62 | 49 | 19 | 28 | 30 | +---------------+----+----+----+----+----+----+----+----+----+----+----+ | Life span (h) | 8 | 8 | 2 | 8 | 4 | 8 | 8 | 8 | 8 | 8 | 8 | +---------------+----+----+----+----+----+----+----+----+----+----+----+ Every lot may be processed by any of the three, fully equivalent production lines of the sugarhouse. The processing of a lot takes two hours. It must be finished at the latest at the end of the life span of the wagonload. The manager of the sugarhouse wishes to determine a production schedule for the currently available lots that minimizes the total loss of sugar. Model formulation ----------------- Let :math:`WAGONS = {1, ..., NW}` be the set of wagons, :math:`NL` the number of production lines and :math:`DUR` the duration of the production process for every lot. The hourly loss for every wagon :math:`w` is given by :math:`LOSS_w` and its life span by :math:`LIFE_w`. We observe that, in an optimal solution, the production lines need to work without any break otherwise we could reduce the loss in sugar by advancing the start of the lot that follows the break. This means that the completion time of every lot is of the form :math:`s \times DUR`, with :math:`s > 0` and is an integer. The maximum value of :math:`s` is the number of time slots (of length :math:`DUR`) that the sugarhouse will work, namely :math:`NS = \text{ceil}(NW / NL)`, where *ceil* stands for *rounded to the next largest integer*. If :math:`NW / NL` is an integer, every line will process exactly :math:`NS` lots. Otherwise, some lines will process :math:`NS - 1` lots, but at least one line processes :math:`NS` lots. In all cases, the length of the optimal schedule is :math:`NS \times DUR` hours. We call :math:`SLOTS = \{1 ,... ,NS\}` the set of time slots. Every lot needs to be assigned to a time slot. We define variables :math:`process_w` for the time slot assigned to wagon :math:`w` and variables :math:`loss_w` for the loss incurred by this wagonload. Every time slot may take up to :math:`NL` lots because there are :math:`NL` parallel lines; therefore, we limit the number of *occurrences* of time slot values among the :math:`process_w` variables (this constraint relation is often called *cardinality constraint*) : .. math:: &\forall s \in SLOTS : |process_w = s|_{w \in WAGONS} \leq NL\\ The loss of sugar per wagonload :math:`w` and time slot :math:`s` is :math:`COST_{w,s} = s \times DUR \times LOSS_w`. Let variables :math:`loss_w` denote the loss incurred by wagon load :math:`w`: .. math:: &\forall w \in WAGONS : loss_w = COST_{w,proces_w}\\ The objective function (total loss of sugar) is then given as the sum of all losses: .. math:: &\text{minimize }\sum_{w \in WAGONS} loss_w\\ Implementation ----------------- The following model is the implementation of this problem. It uses the function ceil to calculate the maximum number of time slots. The constraints on the processing variables are expressed by occurrence relations and the losses are obtained via element constraints. The branching strategy uses the variable selection criterion ``KLargestMin``, that is, choosing the variable with the largest lower bound. .. tabs:: .. code-tab:: c++ // Number of wagon loads of sugar int NW = 11; // Number of production lines int NL = 3; // Time slots for production int NS = ceil(NW/((float)NL)); // Loss in kg/hour int LOSS[] = { 43 ,26, 37, 28, 13, 54, 62, 49, 19, 28 ,30}; // Remaining time per lot (in hours) int LIFE[] = { 8 , 8, 2, 8 , 4 , 8 , 8, 8, 6, 8 , 8}; // Cost per wagon KIntArray COST; // Duration of the production int DUR = 2; // Loss per wagon KIntVarArray loss; // Time slots for wagon loads KIntVarArray process; // Objective variable KIntVar * totalLoss; // Creation of the problem in this session KProblem problem(session,"A-4 Cane sugar production 1"); int w; // Variables creation char name[80]; for (w=0;wprintResume(); // Solution printing printf("Total loss: %i\n", sol->getValue(*totalLoss)); for (s = 0; s < NS; s++) { printf("Slot %i: ", s); for (w=0;wgetValue(process[w]) == s) { printf("wagon %i (%i) ",w,(s+1)*DUR*LOSS[w]); } } printf("\n"); } .. code-tab:: py from kalis import * import math ### Data nb_wagons = 11 nb_production_lines = 3 nb_time_slots = math.ceil(nb_wagons / float(nb_production_lines)) # Loss in kg/hour wagons_loss_rate = [43, 26, 37, 28, 13, 54, 62, 49, 19, 28, 30] wagons_life_span = [8, 8, 2, 8, 4, 8, 8, 8, 6, 8, 8] # Duration of the production production_duration = 2 ### Creation of the problem # Creation of the Kalis session session = KSession() # Creation of the optimization problem problem = KProblem(session, "A-4 Cane sugar production 1") ### Variables creation # Loss per wagon wagon_loss = KIntVarArray() for w in range(nb_wagons): wagon_loss += KIntVar(problem, "loss(%d)" % w, 0 , 10000) # Time slots for wagon loads wagon_process = KIntVarArray() for w in range(nb_wagons): wagon_process += KIntVar(problem, "process(%d)" % w, 0, nb_time_slots - 1) ### Set constraints # Set the cardinality constraint for s in range(nb_time_slots): oc = KOccurTerm(s, wagon_process) problem.post(KOccurrence(oc, nb_production_lines, False, True)) # limit on raw product life: for w in range(nb_wagons): wagon_process[w].setSup(math.floor(wagons_life_span[w] / float(production_duration)) - 1) # Set the each wagon loss according to its processing order for w in range(nb_wagons): # Set cost per wagon cost = KIntArray() for t in range(nb_time_slots): res = cost.add( (t + 1) * production_duration * wagons_loss_rate[w] ) # Add KElement constraint kelt = KEltTerm(cost, wagon_process[w]) problem.post(KElement(kelt, wagon_loss[w], "element(%d)" % w)) # Objective value wagons_loss_sum = 0 for w in range(nb_wagons): wagons_loss_sum += wagon_loss[w] total_loss = KIntVar(problem, "totalLoss", 0, 10000) problem.post(total_loss == wagons_loss_sum) # First propagation to check inconsistency if problem.propagate(): print("Problem is infeasible") sys.exit(1) ### Solve the problem # Search strategy customization my_branching_scheme = KBranchingSchemeArray() my_branching_scheme += KAssignVar(KSmallestMax(), KMinToMax(), wagon_process) # Set the solver solver = KSolver(problem, my_branching_scheme) # Setting objective and sense of optimization problem.setSense(KProblem.Minimize) problem.setObjective(total_loss) # Run optimization result = solver.optimize() if result: solution = problem.getSolution() solution.printResume() print("Total loss:", solution.getValue(total_loss)) for t in range(nb_time_slots): print("Slot %d:" % t, end='') for w in range(nb_wagons): if solution.getValue(wagon_process[w]) == t: print(" wagon %d (%d)" % (w, (t + 1) * production_duration * wagons_loss_rate[w]), end='') print("") .. code-tab:: java // Number of wagon loads of sugar int NW = 11; // Number of production lines int NL = 3; // Time slots for production int NS = (int) Math.ceil(NW / (float) NL); // Loss in kg/hour int[] LOSS = {43, 26, 37, 28, 13, 54, 62, 49, 19, 28, 30}; // Remaining time per lot (in hours) int[] LIFE = {8, 8, 2, 8, 4, 8, 8, 8, 6, 8, 8}; // Cost per wagon KIntArray COST = new KIntArray(); // Duration of the production int DUR = 2; // Loss per wagon KIntVarArray loss = new KIntVarArray(); // Time slots for wagon loads KIntVarArray process = new KIntVarArray(); // Objective variable KIntVar totalLoss = new KIntVar(); // Creation of the session KSession session = new KSession(); // Creation of the problem in this session KProblem problem = new KProblem(session, "A-4 Cane sugar production 1"); int w; // Variables creation for (w = 0; w < NW; w++) { process.add(new KIntVar(problem, "process(" + w + ")", 0, NS - 1)); } for (w = 0; w < NW; w++) { loss.add(new KIntVar(problem, "loss(" + w + ")", 0, 10000)); } int s; // Wagon loads per time slot for (s = 0; s < NS; s++) { KOccurTerm oc = new KOccurTerm(s, process); problem.post(new KOccurrence(oc, NL, false, true)); } // Limit on raw product life for (w = 0; w < NW; w++) { process.getElt(w).setSup(Math.floor(LIFE[w] / DUR) - 1); } KLinTerm totLossTerm = new KLinTerm(); // Objective function: total loss for (w = 0; w < NW; w++) { for (s = 0; s < NS; s++) { COST.add((s + 1) * DUR * LOSS[w]); } KEltTerm kelt = new KEltTerm(COST,process.getElt(w)); problem.post(new KElement(kelt, loss.getElt(w),"element")); totLossTerm.add(loss.getElt(w),1); } totalLoss = new KIntVar(problem,"totalLoss",0,10000); KNumVarArray intVarArrayToSet = totLossTerm.getLvars(); KDoubleArray coeffsToSet = totLossTerm.getCoeffs(); intVarArrayToSet.add(totalLoss); coeffsToSet.add(-1); problem.post(new KNumLinComb("",coeffsToSet,intVarArrayToSet,0,KNumLinComb.LinCombOperator.Equal)); // propagating problem if (problem.propagate()) { System.out.println("Problem is infeasible"); exit(1); } // search strategy customization KBranchingSchemeArray myBa = new KBranchingSchemeArray(); myBa.add(new KAssignVar(new KSmallestMax(), new KMinToMax(), process)); // Solve the problem // creation of the solver KSolver solver = new KSolver(problem, myBa); // setting objective and sense of optimization problem.setSense(KProblem.Sense.Minimize); problem.setObjective(totalLoss); int result = solver.optimize(); // solution printing KSolution sol = problem.getSolution(); // print solution resume sol.printResume(); // Solution printing System.out.println("Total loss: " + sol.getValue(totalLoss)); for (s = 0; s < NS; s++) { System.out.print("Slot " + s + " :"); for (w = 0; w < NW; w++) { if (sol.getValue(process.getElt(w)) == s) { System.out.print("wagon " + w + " (" + (s + 1) * DUR * LOSS[w] + ")"); } } System.out.println(); } An alternative formulation of the constraints on the processing variables is to replace the ``KOccurrence`` constraints by a single ``KGlobalCardinalityConstraint``, indicating for every time slot the minimum and maximum number (:math:`MINUSE_s = 0` and :math:`MAXUSEs = NL`) of production lines that may be used. .. tabs:: .. code-tab:: c++ int * vals = new int[NS]; int * low = new int[NS]; int * upp = new int[NS]; for (s=0;s` 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 :math:`JOBS = \{1, ..., NJ\}` be the set of batches to produce, :math:`DUR_j` the processing time for batch :math:`j`, and :math:`CLEAN_{i,j}` the cleaning time between the consecutive batches :math:`i` and :math:`j`. We introduce decision variables :math:`succ_j` taking their values in :math:`JOBS`, to indicate the successor of every job, and variables :math:`clean_j` for the duration of the cleaning after every job. The cleaning time after every job is obtained by indexing :math:`CLEAN_{i,j}` with the value of :math:`succ_j`. We thus have the following problem formulation: .. math:: &\text{minimize } \sum_{j \in JOBS}(DUR_j + clean_j)\\ &\forall j \in JOBS : succ_j \in {JOBS} \backslash \{j\}\\ &\forall j \in JOBS : clean_j = CLEAN_{j,succ_j}\\ &\text{all-different}(\bigcup\limits_{j \in JOBS}^{} succ_j)\\ 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 :math:`\rightarrow` 3 :math:`\rightarrow` 2 :math:`\rightarrow` 1 and 4 :math:`\rightarrow` 5 :math:`\rightarrow` 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. .. math:: &succ_1 \ne 3 \vee succ_3 \ne 2 \vee succ_2 \ne 1 \vee succ_1 \ne 5 \vee succ_5 \ne 4\\ 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 :math:`y_j` per batch and :math:`(NJ-1)^2` implication constraints. .. math:: &\forall j \in JOBS : rank_j \in \{1, ..., NJ\}\\ &\forall i \in JOBS, \forall j \in \{2, ..., NJ\}, i \ne j : succ_i = j \Rightarrow y_j = y_i + 1\\ &\forall j \in JOBS : rank_j \in \{1, ..., NJ\}\\ The variables :math:`y_j` correspond to the position of job :math:`j` 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``). .. tabs:: .. code-tab:: c++ // Number of paint batches (=jobs) int NJ = 5; // Durations of jobs int DUR[] = {40, 35, 45, 32, 50}; // Cleaning times between jobs int CLEAN[5][5] = { { 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[80]; int j,i; for (j=0;jprintResume(); // 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); .. code-tab:: py from kalis import * ### Data # Number of paint batches (=jobs) nb_jobs = 5 # Durations of jobs jobs_durations = [40, 35, 45, 32, 50] # Cleaning times between jobs jobs_cleaning_times = [[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]] max_cleaning_time = max(max(jobs_cleaning_times[j]) for j in range(nb_jobs)) ### Creation of the problem # Creation of the Kalis session session = KSession() # Creation of the optimization problem problem = KProblem(session, "B-5 Paint production") ### Variables creation # Successor of a batch batch_successor = KIntVarArray() for j in range(nb_jobs): batch_successor += KIntVar(problem, "succ(%d)" % j, 0, nb_jobs - 1) batch_successor[j].remVal(j) # Cleaning times after a batch batch_cleaning_time = KIntVarArray() for j in range(nb_jobs): batch_cleaning_time += KIntVar(problem, "clean(%d)" % j, 0, max_cleaning_time) # Variables for excluding subtours job_position_in_cycle = KIntVarArray() # i.e. y_j variables for j in range(nb_jobs): job_position_in_cycle += KIntVar(problem, "y(%d)" % j, 0, nb_jobs - 1) ### Constraints creation # Cleaning time after every batch: # i.e. "batch_cleaning_time[j] == jobs_cleaning_times[batch_succesor[j]]" for j in range(nb_jobs): K_cleaning_time = KIntArray() # convert Python list to KIntArray for i in range(nb_jobs): res = K_cleaning_time.add(jobs_cleaning_times[j][i]) kelt = KEltTerm(K_cleaning_time, batch_successor[j]) problem.post(kelt == batch_cleaning_time[j]) # One successor and on predecessor per batch problem.post(KAllDifferent("alldiff", batch_successor)) # Exclude subtours for i in range(nb_jobs): for j in range(1, nb_jobs): if i != j: problem.post( KGuard(batch_successor[i] == j, job_position_in_cycle[j] == (job_position_in_cycle[i] + 1))) # Set objective cycle_time = KIntVar(problem, "cycle_time", 0, 1000) batch_cleaning_times_sum = 0 for j in range(nb_jobs): batch_cleaning_times_sum += batch_cleaning_time[j] problem.post(cycle_time == (batch_cleaning_times_sum + sum(jobs_durations))) # First propagation to check inconsistency if problem.propagate(): print("Problem is infeasible") sys.exit(1) ### Solve the problem # Set the solver solver = KSolver(problem) # Setting objective and sense of optimization problem.setSense(KProblem.Minimize) problem.setObjective(cycle_time) # Run optimization result = solver.optimize() # Solution printing if result: solution = problem.getSolution() solution.printResume() print("Minimum cycle time: %d" % solution.getValue(cycle_time)) print("Sequence of batches:") print("Batch Duration Cleaning") j = solution.getValue(batch_successor[0]) while j != 0: print("%d\t%d\t%d" % (j, jobs_durations[j], solution.getValue(batch_cleaning_time[j]))) j = solution.getValue(batch_successor[j]) .. code-tab:: java // 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 = new KIntArray(); // Successor of a batch KIntVarArray succ = new KIntVarArray(); // Cleaning time after batches KIntVarArray clean = new KIntVarArray(); // Variables for excluding subtours KIntVarArray y = new KIntVarArray(); // Objective variable KIntVar cycleTime = new KIntVar(); // Creation of the problem in this session KProblem problem = new KProblem(session,"B-5 Paint production"); // variables creation int j,i; for (j=0;j`. As before, let :math:`JOBS = \{1, ..., NJ\}` be the set of batches to produce, :math:`DUR_j` the processing time for batch :math:`j`, and :math:`CLEAN_{i,j}` the cleaning time between the consecutive batches :math:`i` and :math:`j`. We introduce decision variables :math:`rank_k` taking their values in :math:`JOBS`, for the number of the job in position :math:`k`. Variables :math:`clean_k` with :math:`k \in JOBS` now denote the duration of the :math:`k^{\text{th}}` cleaning time. This duration is obtained by indexing :math:`CLEAN_{i,j}` with the values of two consecutive :math:`rank_k` variables. We thus have the following problem formulation. .. math:: &\text{minimize } \sum_{j \in JOBS}DUR_j + \sum_{k \in JOBS}clean_k\\ &\forall k \in JOBS : rank_k \in {JOBS} \backslash \{k\}\\ &\forall k \in \{1, ..., NJ-1\} : clean_k = CLEAN_{rank_k,rank_{k+1}}\\ & clean_{NJ} = CLEAN_{rank_{NJ},rank_{1}}\\ &\text{all-different}(\bigcup\limits_{k \in JOBS}^{} rank_k)\\ 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. .. tabs:: .. code-tab:: c++ // 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][0] = 0;CLEAN[1][0] = 11;CLEAN[2][0] = 7;CLEAN[3][0] = 13;CLEAN[4][0] = 11; CLEAN[0][1] = 5;CLEAN[1][1] = 0;CLEAN[2][1] = 13;CLEAN[3][1] = 15;CLEAN[4][1] = 15; CLEAN[0][2] = 13;CLEAN[1][2] = 15;CLEAN[2][2] = 0;CLEAN[3][2] = 23;CLEAN[4][2] = 11; CLEAN[0][3] = 9;CLEAN[1][3] = 13;CLEAN[2][3] = 5;CLEAN[3][3] = 0;CLEAN[4][3] = 3; CLEAN[0][4] = 3;CLEAN[1][4] = 7;CLEAN[2][4] = 7;CLEAN[3][4] = 7;CLEAN[4][4] = 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[80]; int k,j,i; for (j=0;jprintResume(); // Solution printing printf("Minimum cycle time: %i\n", sol->getValue(*cycleTime)); printf("Sequence of batches:\nBatch Duration Cleaning\n"); for (k=0;kgetValue(rank[k]), DUR[sol->getValue(rank[k])], sol->getValue(clean[k])); } .. code-tab:: py ### Data # Number of paint batches (=jobs) nb_jobs = 5 # Durations of jobs jobs_durations = [40, 35, 45, 32, 50] # Cleaning times between jobs jobs_cleaning_times = [[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]] max_cleaning_time = max(max(jobs_cleaning_times[j]) for j in range(nb_jobs)) # Setting data as a KIntMatrix K_cleaning_times_matrix = KIntMatrix(5, 5, 0, "CLEAN") for i in range(nb_jobs): for j in range(nb_jobs): K_cleaning_times_matrix.setMatrix(i, j, jobs_cleaning_times[i][j]) ### Creation of the problem # Creation of the Kalis session session = KSession() # Creation of the problem in this session problem = KProblem(session, "B-5 Paint production") ### Variable creation # Successor of a batch batch_rank = KIntVarArray() for j in range(nb_jobs): batch_rank += KIntVar(problem, "rank(%d)" % j, 0, nb_jobs - 1) # Cleaning time after batches batch_cleaning_time = KIntVarArray() for j in range(nb_jobs): batch_cleaning_time += KIntVar(problem, "clean(%d)" % j, 0, max_cleaning_time) ### Constraints creation # Set cleaning times after every batch for k in range(nb_jobs): if k < nb_jobs - 1: kelt = KEltTerm2D(K_cleaning_times_matrix, batch_rank[k], batch_rank[k + 1]) problem.post(KElement2D(kelt, batch_cleaning_time[k], "element")) else: kelt = KEltTerm2D(K_cleaning_times_matrix, batch_rank[k], batch_rank[0]) problem.post(KElement2D(kelt, batch_cleaning_time[k], "element")) # One position for every job problem.post(KAllDifferent("alldiff", batch_rank)) # Set objective cycle_time = KIntVar(problem, "cycle_time", 0, 1000) batch_cleaning_times_sum = 0 for j in range(nb_jobs): batch_cleaning_times_sum += batch_cleaning_time[j] problem.post(cycle_time == (batch_cleaning_times_sum + sum(jobs_durations))) # First propagation to check inconsistency if problem.propagate(): print("Problem is infeasible") sys.exit(1) ### Solve the problem # Set the solver solver = KSolver(problem) # Setting objective and sense of optimization problem.setSense(KProblem.Minimize) problem.setObjective(cycle_time) # Run optimization result = solver.optimize() # Solution printing if result: solution = problem.getSolution() solution.printResume() print("Minimum cycle time: %d" % solution.getValue(cycle_time)) print("Sequence of batches:") print("Batch Duration Cleaning") for k in range(nb_jobs): job_id = solution.getValue(batch_rank[k]) print("%d\t%d\t%d" % (job_id, jobs_durations[job_id], solution.getValue(batch_cleaning_time[k]))) .. code-tab:: java // Cleaning times between jobs KIntMatrix CLEAN = new KIntMatrix(5, 5, 0, "CLEAN"); CLEAN.setMatrix(0, 0, 0); CLEAN.setMatrix(1,0,11); CLEAN.setMatrix(2,0,7); CLEAN.setMatrix(3,0,13); CLEAN.setMatrix(4,0,11); CLEAN.setMatrix(0,1,5); CLEAN.setMatrix(1,1,0); CLEAN.setMatrix(2,1,13); CLEAN.setMatrix(3,1,15); CLEAN.setMatrix(4,1,15); CLEAN.setMatrix(0,2,13); CLEAN.setMatrix(1,2,15); CLEAN.setMatrix(2,2,0); CLEAN.setMatrix(3,2,23); CLEAN.setMatrix(4,2,11); CLEAN.setMatrix(0,3,9); CLEAN.setMatrix(1,3,13); CLEAN.setMatrix(2,3,5); CLEAN.setMatrix(3,3,0); CLEAN.setMatrix(4,3,3); CLEAN.setMatrix(0,4,3); CLEAN.setMatrix(1,4,7); CLEAN.setMatrix(2,4,7); CLEAN.setMatrix(3,4,7); CLEAN.setMatrix(4,4,0); // Successor of a batch KIntVarArray rank = new KIntVarArray(); // Cleaning time after batches KIntVarArray clean = new KIntVarArray(); // Objective variable KIntVar cycleTime = new KIntVar(); // Creation of the problem in this session KProblem problem = new KProblem(session, "B-5 Paint production"); // variables creation int k,j; for (j=0; j DIST[c][b]+DIST[b][d]) { DIST[c][d] = DIST[c][b]+DIST[b][d]; DIST[d][c] = DIST[c][b]+DIST[b][d]; } } } } } // total popuplation int sumPop=0; char name[80]; // building variables for (c=0;c dmax) { dmax = DIST[c][d]; } } sprintf(name,"depdist(%i)",c); depdist += (* new KIntVar(problem,name,dmin,dmax) ); sprintf(name,"numdep(%i)",c); numdep += (* new KIntVar(problem,name,0,NC) ); // compute total popuplation for solution printing routines sumPop += POP[c]; } // Distance from cities to tax offices for (c=0;c= 1)); } // Limit total number of offices KLinTerm sbuild; for (c=0;c 0) { printf("Office in %i: ",c); for (d=0;d distances[c][b] + distances[b][d]: distances[c][d] = distances[c][b] + distances[b][d] distances[d][c] = distances[c][b] + distances[b][d] # Population of cities populations = [15, 10, 12, 18, 5, 24, 11, 16, 13, 22, 19, 20] total_population = sum(populations) # Desired number of tax offices nb_offices = 3 ### Variables creation # Creation of the Kalis session and of the optimization problem session = KSession() problem = KProblem(session, "J-5 Tax office location") # Building variables: 1 if office in city, 0 otherwise build = KIntVarArray() for c in range(nb_cities): build += KIntVar(problem, "build(%d)" % c, 0, 1) # Office on which city depends depend = KIntVarArray() for c in range(nb_cities): depend += KIntVar(problem, "depend(%d)" % c, 0, nb_cities - 1) # Distance to tax office dep_dist = KIntVarArray() for c in range(nb_cities): min_dist = min(distances[c]) max_dist = max(distances[c]) dep_dist += KIntVar(problem, "depdist(%d)" % c, min_dist, max_dist) # Number of depending cities per off num_dep = KIntVarArray() for c in range(nb_cities): num_dep += KIntVar(problem, "numdep(%d)" % c, 0, nb_cities) ### Constraints creation # Set distances variables to their corresponding data for c in range(nb_cities): # auxiliary array used to set up the constraint K_dist = KIntArray() for d in range(nb_cities): res = K_dist.add(distances[c][d]) # set KElement constraint: "dep_dist[c] == distances[depend[c]]" kelt = KEltTerm(K_dist, depend[c]) problem.post(kelt == dep_dist[c]) # Set the number of cities depending for each office for c in range(nb_cities): koc = KOccurTerm(c, depend) problem.post(koc == num_dep[c]) # Relations between dependencies and offices built for c in range(nb_cities): problem.post(KEquiv(build[c] == 1, num_dep[c] >= 1)) # Limit total number of offices build_sum = 0 for c in range(nb_cities): build_sum += build[c] problem.post(build_sum <= nb_offices) # Set objective total_distance = KIntVar(problem, "totDist", 0, 10000) populations_distance_product = 0 for c in range(nb_cities): populations_distance_product += populations[c] * dep_dist[c] problem.post(populations_distance_product == total_distance) ### Solve the problem # First propagation to check inconsistency if problem.propagate(): print("Problem is infeasible") sys.exit(1) # Setting objective and sense of optimization problem.setSense(KProblem.Minimize) problem.setObjective(total_distance) # Set the branching strategy myBranchingArray = KBranchingSchemeArray() # KAssignAndForbid for the "build" variables myBranchingArray += KAssignAndForbid(KMaxDegree(), KMaxToMin(), build) # KSplit domain for the "dep_dist" variables ('True' stand for exploring the lower part first, # '5' stand for the minimum domain size where the domain is not split anymore) myBranchingArray += KSplitDomain(KSmallestDomain(), KMinToMax(), dep_dist, True, 5) # Set the solver solver = KSolver(problem, myBranchingArray) # Run optimization result = solver.optimize() # Solution printing if result: solution = problem.getSolution() solution.printResume() total_distance_found = solution.getValue(total_distance) print("Total weighted distance: %f (average per inhabitant: %f)" % (total_distance_found, total_distance_found / float(total_population))) for c in range(nb_cities): if solution.getValue(build[c]) > 0: print("Office in %d: " % c) for d in range(nb_cities): if solution.getValue(depend[d]) == c: print(d, end=" ") print("") .. code-tab:: java // *** Creation of the session KSession session = new KSession(); // Number of cities int NC = 12; // Distance matrix int DIST[][]; // Population of cities int POP[] = {15, 10, 12 ,18 ,5 ,24, 11, 16, 13, 22, 19 ,20}; // Desired number of tax offices int NUMLOC = 3; // 1 if office in city, 0 otherwise KIntVarArray build = new KIntVarArray(); // Office on which city depends KIntVarArray depend = new KIntVarArray(); // Distance to tax office KIntVarArray depdist = new KIntVarArray(); // Number of depending cities per off. KIntVarArray numdep = new KIntVarArray(); // Objective function variable KIntVar totDist = new KIntVar(); // Creation of the problem in this session KProblem problem = new KProblem(session,"J-5 Tax office location"); // index variables int b,c,d; // Calculate the distance matrix // Initialize all distance labels with a sufficiently large value DIST = new int[NC][NC]; for (c=0;c DIST[c][b]+DIST[b][d]) { DIST[c][d] = DIST[c][b]+DIST[b][d]; DIST[d][c] = DIST[c][b]+DIST[b][d]; } } } } } // total popuplation int sumPop=0; // building variables for (c=0;c dmax) { dmax = DIST[c][d]; } //System.out.println("dmin="+dmin); //System.out.println("dmax="+dmax); } depdist.add(new KIntVar(problem,"depdist("+c+")",dmin,dmax) ); numdep.add(new KIntVar(problem,"numdep("+c+")",0,NC) ); // compute total popuplation for solution printing routines sumPop += POP[c]; } // Distance from cities to tax offices for (c=0;c 0) { System.out.print("Office in "+c+" :"); for (d=0;d` uses ``KAllDifferent`` constraints to ensure that every job occurs once only, calculates the duration of cleaning times with ``KElement`` constraints, and introduces auxiliary variables and constraints to prevent subcycles in the production sequence. All these constraints can be expressed by a single constraint relation, the ``KCycle`` constraint. Let :math:`JOBS = \{1, ..., NJ\}` be the set of batches to produce, :math:`DUR_j` the processing time for batch :math:`j`, and :math:`CLEAN_{i,j}` the cleaning time between the consecutive batches :math:`i` and :math:`j`. As before we define decision variables :math:`succ_j` taking their values in :math:`JOBS`, to indicate the successor of every job. The complete model formulation is the following, .. math :: &\text{minimize } \sum_{j \in JOBS} DUR_j + cleantime\\ &\forall j \in JOBS : succ_j \in JOBS \backslash \{ j \}\\ &cleantime = cycle((succ_j)_{j \in JOBS}, (CLEAN_{i,j})_{i,j \in JOBS \times JOBS})\\ where *cycle* stands for the relation *sequence into a single cycle without subcycles or repetitions*. The variable cleantime equals the total duration of the cycle. Implementation ----------------- The model using the ``KCycle`` constraint looks as follows. .. tabs:: .. code-tab:: c++ // 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][0] = 0;CLEAN[1][0] = 11;CLEAN[2][0] = 7;CLEAN[3][0] = 13;CLEAN[4][0] = 11; CLEAN[0][1] = 5;CLEAN[1][1] = 0;CLEAN[2][1] = 13;CLEAN[3][1] = 15;CLEAN[4][1] = 15; CLEAN[0][2] = 13;CLEAN[1][2] = 15;CLEAN[2][2] = 0;CLEAN[3][2] = 23;CLEAN[4][2] = 11; CLEAN[0][3] = 9;CLEAN[1][3] = 13;CLEAN[2][3] = 5;CLEAN[3][3] = 0;CLEAN[4][3] = 3; CLEAN[0][4] = 3;CLEAN[1][4] = 7;CLEAN[2][4] = 7;CLEAN[3][4] = 7;CLEAN[4][4] = 0; // Cleaning times after a batch KIntArray CB; // Successor of a batch KIntVarArray succ; // Objective variable KIntVar * cycleTime; // Objective variable KIntVar * cleanTime; // Creation of the problem in this session KProblem problem(session,"B-5 Paint production"); // variables creation char name[80]; int j,i; for (j=0;jprintResume(); // 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],CLEAN[first][sol->getValue(succ[first])]); first = sol->getValue(succ[first]); } while (first!=0); .. code-tab:: py from kalis import * ### Data # Number of paint batches (=jobs) nb_jobs = 5 # Durations of jobs jobs_durations = [40, 35, 45, 32, 50] # Cleaning times between jobs jobs_cleaning_times = [[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]] ### Creation of the problem # Creation of the Kalis session session = KSession() # Creation of the problem in this session problem = KProblem(session, "B-5 Paint production") # Setting data as a KIntMatrix K_cleaning_times_matrix = KIntMatrix(5, 5, 0, "CLEAN") for i in range(nb_jobs): for j in range(nb_jobs): K_cleaning_times_matrix.setMatrix(i, j, jobs_cleaning_times[i][j]) ### Variables creation # Successor of a batch batch_successor = KIntVarArray() for j in range(nb_jobs): batch_successor += KIntVar(problem, "succ(%d)" % j, 0, nb_jobs - 1) batch_successor[j].remVal(j) # Cycle time monitoring clean_time = KIntVar(problem, "cleanTime", 0, 1000) # Assign values to 'batch_successor' variables as to obtain a single cycle # 'clean_time' is the sum of the cleaning times problem.post(KCycle(batch_successor, None, clean_time, K_cleaning_times_matrix)) # Objective variable cycle_time = KIntVar(problem, "cycleTime", 0, 1000) problem.post(sum(jobs_durations) + clean_time == cycle_time) ### Solve the problem # First propagation to check inconsistency if problem.propagate(): print("Problem is infeasible") sys.exit(1) # Set the solver solver = KSolver(problem) # Setting objective and sense of optimization problem.setSense(KProblem.Minimize) problem.setObjective(cycle_time) # Run optimization result = solver.optimize() # Solution printing if result: solution = problem.getSolution() solution.printResume() print("Minimum cycle time: %d" % solution.getValue(cycle_time)) print("Sequence of batches:") print("Batch Duration Cleaning") j = solution.getValue(batch_successor[0]) while j != 0: next_job = solution.getValue(batch_successor[j]) print("%d\t%d\t%d" % (j, jobs_durations[j], jobs_cleaning_times[j][next_job])) j = next_job .. code-tab:: java // Cleaning times between jobs KIntMatrix CLEAN = new KIntMatrix(5,5,0,"CLEAN"); CLEAN.setMatrix(0,0,0); CLEAN.setMatrix(1,0,11); CLEAN.setMatrix(2,0,7); CLEAN.setMatrix(3,0,13); CLEAN.setMatrix(4,0,11); CLEAN.setMatrix(0,1,5); CLEAN.setMatrix(1,1,0); CLEAN.setMatrix(2,1,13); CLEAN.setMatrix(3,1,15); CLEAN.setMatrix(4,1,15); CLEAN.setMatrix(0,2,13); CLEAN.setMatrix(1,2,15); CLEAN.setMatrix(2,2,0); CLEAN.setMatrix(3,2,23); CLEAN.setMatrix(4,2,11); CLEAN.setMatrix(0,3,9); CLEAN.setMatrix(1,3,13); CLEAN.setMatrix(2,3,5); CLEAN.setMatrix(3,3,0); CLEAN.setMatrix(4,3,3); CLEAN.setMatrix(0,4,3); CLEAN.setMatrix(1,4,7); CLEAN.setMatrix(2,4,7); CLEAN.setMatrix(3,4,7); CLEAN.setMatrix(4,4,0); // Successor of a batch KIntVarArray succ = new KIntVarArray(); // Objective variable KIntVar cycleTime = new KIntVar(); // Objective variable KIntVar cleanTime = new KIntVar(); // Creation of the problem in this session KProblem problem = new KProblem(session,"B-5 Paint production"); // variables creation int j; for (j=0; j bool: xa = a // nb_rows ya = a % nb_rows xb = b // nb_rows yb = b % nb_rows delta_x = abs(xa - xb) delta_y = abs(ya - yb) return (delta_x <= 2) and (delta_y <= 2) and (delta_x + delta_y <= 3) .. code-tab:: java public boolean KnightMoveOk(int a,int b, int S) { int xa = a / S; int ya = a % S; int xb = b / S; int yb = b % S; int delta_x = (xa>xb)? xa-xb : xb - xa; int delta_y = (ya>yb)? ya-yb : yb - ya; return delta_x<=2 && delta_y<=2 && delta_x+delta_y==3; } The following model defines the user constraint function valid *KnightMoveOk* as the implementation of the new binary constraints on pairs of :math:`move_p` variables (the constraints are established with ``KACBinTableConstraint``). .. tabs:: .. code-tab:: c++ // Number of rows/columns int S = 8; // Total number of cells int N = S * S; // Cell at position p in the tour KIntVarArray pos; // Creation of the problem in this session KProblem problem(session,"Euler Knight"); // variables creation char name[80]; int j,i; for (j=0;jprintResume(); for (j=0;j ",sol->getValue(pos[j])); } printf("0\n"); .. code-tab:: py from kalis import * ### Data # Number of rows/columns nb_rows = 8 # Total number of cells N = nb_rows * nb_rows ### Variables creation # Creation of the Kalis session and of the optimization problem session = KSession() problem = KProblem(session, "Euler Knight") # Cell at position p in the tour positions = KIntVarArray() for p in range(N): positions += KIntVar(problem, "pos(%d)" % p, 0, N-1) ### Constraints creation # Fix the start position positions[0].instantiate(0) # Each cell is visited once problem.post(KAllDifferent("alldiff", positions, KAllDifferent.GENERALIZED_ARC_CONSISTENCY)) # The path of the knight obeys the chess rules for valid knight moves allowed_moves_table = [[KnightMoveOk(v1, v2, nb_rows) for v2 in range(N)] for v1 in range(N)] for i in range(N - 1): problem.post(KACBinTableConstraint(positions[i], positions[i + 1], allowed_moves_table, KACBinTableConstraint.ALGORITHM_AC2001, "KnightMove")) # The Knight must return to its first position problem.post(KACBinTableConstraint(positions[N - 1], positions[0], allowed_moves_table, KACBinTableConstraint.ALGORITHM_AC2001, "KnightMove")) ### Solve the problem # First propagation to check inconsistency if problem.propagate(): print("Problem is infeasible") sys.exit(1) # Setting enumeration parameters my_branching_array = KBranchingSchemeArray() my_branching_array += KProbe(KSmallestMin(), KMaxToMin(), positions, 14) # Set the solver solver = KSolver(problem, my_branching_array) # Run optimization result = solver.solve() # Solution printing if result: solution = problem.getSolution() solution.printResume() for i in range(N): print(solution.getValue(positions[i]), end=" ") print("0") .. code-tab:: java //todo The branching scheme used in this model is the ``KProbe`` heuristic, in combination with the variable selection ``KSmallestMin`` (choose variable with smallest lower bound) and the value selection criterion ``KMaxToMin`` (from largest to smallest domain value). Our model defines one parameter. It is thus possible to change the size of the chessboard (S) when executing the model without having to modify the model source. Results ----------------- The first solution printed out by our model is the following tour: .. math:: 0 & \rightarrow 17 \rightarrow 34 \rightarrow 51 \rightarrow 36 \rightarrow 30 \rightarrow 47 \rightarrow 62 \rightarrow 45 \rightarrow 39 \\ & \rightarrow 54 \rightarrow 60 \rightarrow 43 \rightarrow 33 \rightarrow 48 \rightarrow 58 \rightarrow 52 \rightarrow 35 \rightarrow 41 \\ & \rightarrow 56 \rightarrow 50 \rightarrow 44 \rightarrow 38 \rightarrow 55 \rightarrow 61 \rightarrow 46 \rightarrow 63 \rightarrow 53 \\ & \rightarrow 59 \rightarrow 49 \rightarrow 32 \rightarrow 42 \rightarrow 57 \rightarrow 40 \rightarrow 25 \rightarrow 8 \rightarrow 2 \\ & \rightarrow 19 \rightarrow 4 \rightarrow 14 \rightarrow 31 \rightarrow 37 \rightarrow 22 \rightarrow 7 \rightarrow 13 \rightarrow 28 \\ & \rightarrow 18 \rightarrow 24 \rightarrow 9 \rightarrow 3 \rightarrow 20 \rightarrow 26 \rightarrow 16 \rightarrow 1 \rightarrow 11 \\ & \rightarrow 5 \rightarrow 15 \rightarrow 21 \rightarrow 6 \rightarrow 23 \rightarrow 29 \rightarrow 12 \rightarrow 27 \rightarrow 10 \\ & \rightarrow 0 \\ Alternative implementation -------------------------- Whereas the aim of the model above is to give an example of the definition of user constraints, it is possible to implement this problem in a more efficient way using the model developed for the previous cyclic scheduling problem. The set of successors (domains of variables :math:`succ_p`) can be calculated using the same algorithm that we have used above in the implementation of the user-defined binary constraints. Without repeating the complete model definition at this place, we simply display the model formulation, including the calculation of the sets of possible successors (procedure *calculate_successors*, and the modified procedure *print_sol* for solution printout). We use a simpler version of the *cycle* constraints than the one we have seen in previous section, its only argument is the set of successor variables as there are no weights to the arcs in this problem. .. tabs:: .. code-tab:: c++ // Total number of cells int N = S * S; // Cell at position p in the tour KIntVarArray succ; // Creation of the problem in this session KProblem problem(session,"Euler Knight 2"); // variables creation char name[80]; int j,i; for (j=0;jprintResume(); int thispos=0; int nextpos=sol->getValue(succ[0]); while (nextpos!=0) { printf("%i -> ",thispos); thispos=nextpos; nextpos=sol->getValue(succ[thispos]); } printf("0\n"); .. code-tab:: py #todo .. code-tab:: java // Number of rows/columns int S = 8; // Total number of cells int N = S * S; // Cell at position p in the tour KIntVarArray succ = new KIntVarArray(); // variables creation int j, i; for (j = 0; j < N; j++) { succ.add(new KIntVar(problem, "succ(" + j + ")", 0, N - 1)); } // Calculate set of possible successors for (j = 0; j < N; j++) { for (i = 0; i < N; i++) { if (!KnightMoveOk(j, i, S)) { // i is not a possible successor for j succ.getElt(j).remVal(i); } } } // Each cell is visited once, no subtours problem.post(new KCycle(succ, null, null, null)); // Solve the problem // creation of the solver KSolver solver = new KSolver(problem); // propagating problem if (problem.propagate()) { System.out.println("Problem is infeasible"); exit(1); } int result = solver.solve(); // solution printing KSolution sol = problem.getSolution(); // print solution resume sol.printResume(); int thispos = 0; int nextpos = sol.getValue(succ.getElt(0)); while (nextpos != 0) { System.out.print(thispos + " -> "); thispos = nextpos; nextpos = sol.getValue(succ.getElt(thispos)); } System.out.println("0\n"); The calculation of the domains for the :math:`succ_p` variables reduces these to 2-8 elements (as compared to the :math:`N=64` values for every :math:`pos_p` variables), which clearly reduces the search space for this problem. This second model finds the first solution to the problem after 131 nodes taking just a fraction of a second to execute on a standard PC whereas the first model requires several thousand nodes and considerably longer running times. It is possible to reduce the number of branch-and-bound nodes even further by using yet another version of the *cycle* constraint that works with successor and predecessor variables. This version of *cycle* performs a larger amount of propagation, at the expense of (slightly) slower execution times for our problem when S < 8. For S > 8, the computation expense due to the stronger propagation pays. This compromise strength of propagation / nodes explored is typical of hard combinatorial problems where the need of stronger propagation arise for a certain size of the problem. Below this size, a simpler but faster propagation is the best choice. Above this size, the stronger propagation scheme gives the best results and is sometime necessary to find solution in a reasonnable time. As the limit of size for this behavior is problem dependent, the user is therefore encouraged to try both algorithms. .. _fig11_Euler_knight_tour_chessboard: .. figure:: images/11_Euler_knight_tour_chessboard.png :scale: 60% :align: center Graphical representation of a knight's tour with on a 24x24 chessboard Generalized arc-consistency constraint : Task assignment problem ================================================================ The problem consists in finding a sequence of starting times of various activities, namely :math:`A,B,C,D,E`, of a given plant. Each activity got 5 different time slots for its starting time, represented by the set :math:`\{1,2,3,4\}`. Due to some plant requirements, two-by-two constraints exists upon the starting time activities : * B can't begin at 3 ; * C can't begin at 2 ; * A and B can't begin at the same starting time ; * B and C can't begin at the same starting time ; * C must begin after D ; * E must begin after A ; * E must begin after B ; * E must begin after C ; * E must begin after D ; * B and D can't begin at the same starting time ; * A and D must begin at the same time. Model formulation ----------------- To represent the activities starting time we defined as many variables as activities :math:`tasks_A, tasks_B, tasks_C, tasks_D, tasks_E`, with a domain of integer from :math:`\{1,2,3,4\}`. Constraints can be expressed as implicit constraints but also through a global constraint : a *generic arc-consistency (GAC) constraint*. It mainly proceeds by examining the arcs between tuples of variables and removes those values from its domain which aren't consistent with the constraint. The implicit constraints can be written as : .. math:: & tasks_B \neq 3 \\ & tasks_C \neq 2 \\ & tasks_A \neq tasks_B \\ & tasks_B \neq tasks_C \\ & tasks_C < tasks_D \\ & tasks_A = tasks_D \\ & tasks_E < tasks_A \\ & tasks_E < tasks_B \\ & tasks_E < tasks_C \\ & tasks_E < tasks_D \\ & tasks_B \neq tasks_D \\ And they can be expressed through a *test function* that validate or not a given starting time combination: .. tabs:: .. code-tab:: c++ bool StartingTimeCombinationOk(int a, int b, int c, int d, int e) { return (b!=3) && (c!=2) && (a!=b) && (b!=c) && (cprintResume(); cout << "### Computation time = " << solver.getDblAttrib(KSolver::ComputationTime) << endl; cout << "### Number of nodes = " << solver.getIntAttrib(KSolver::NumberOfNodes) << endl; cout << "### Depth = " << solver.getIntAttrib(KSolver::Depth) << endl; return 0; } .. code-tab:: py #todo .. code-tab:: java // todo Implementation of the GAC Table constraint ------------------------------------------ In the following GAC constraint (``KGeneralizedArcConsistencyTableConstraint`` version), end-user needs this time to enumerate the complete list of valid tuples that defined the valid support of the variables with respect to the constraint. This list of tuple will be used by the propagation engine to filter the domain variables, as it is done with :cpp:func:`testIfSatisfied` in ``KGeneralizedArcConsistencyConstraint``. This implementation is identified as the one to be preferred when the list of valid tuples is greatly inferior to the overall combinatorial of the variables. The implementation is: .. tabs:: .. code-tab:: c++ ///////////////////////////////////////////// // Task activities starting time ///////////////////////////////////////////// enum TASKS {A,B,C,D,E}; const char * TaskName[] = {"A","B","C","D","E"}; int N=5; KIntVarArray tasks; ///////////////////////////////////////////// // Create the task activities variables ///////////////////////////////////////////// char name[80]; int indexTask; for (indexTask = A; indexTask <= E; indexTask++) { sprintf(name,"task[%s]",TaskName[indexTask]); tasks += KIntVar(problem,name,1,4); } /////////////////////////////////////////////// // the task activities should follow the combination rules /////////////////////////////////////////////// KTupleArray tuple; int _a, _b, _c, _d, _e; for (_a = 0; _a <= 4; _a++) for (_b = 0; _b <= 4; _b++) for (_c = 0; _c <= 4; _c++) for (_d = 0; _d <= 4; _d++) for (_e = 0; _e <= 4; _e++) { if (StartingTimeCombinationOk(_a, _b, _c, _d, _e)) { KIntArray tuple_elem(N); tuple_elem[A] = _a; tuple_elem[B] = _b; tuple_elem[C] = _c; tuple_elem[D] = _d; tuple_elem[E] = _e; tuple.add(tuple_elem); } } KGeneralizedArcConsistencyTableConstraint gacTableConstraint(tasks, tuple,"StartingTimeCombinationGACTable"); problem.post(gacTableConstraint); ///////////////////////////////////////////// // Solve the problem // creation of the solver ///////////////////////////////////////////// KSolver solver(problem); ///////////////////////////////////////////// // propagating problem ///////////////////////////////////////////// if (problem.propagate()) { printf("Problem is infeasible\n"); return 1; } solver.solve(); ///////////////////////////////////////////// // solution printing ///////////////////////////////////////////// KSolution * sol = &problem.getSolution(); ///////////////////////////////////////////// // print solution resume ///////////////////////////////////////////// sol->printResume(); cout << "### Computation time = " << solver.getDblAttrib(KSolver::ComputationTime) << endl; cout << "### Number of nodes = " << solver.getIntAttrib(KSolver::NumberOfNodes) << endl; cout << "### Depth = " << solver.getIntAttrib(KSolver::Depth) << endl; return 0; .. code-tab:: py #todo .. code-tab:: java // todo