Solving your first business problem with Hexaly Modeler¶
The car sequencing problem is a well-known combinatorial optimization problem related to the organization of the production line of a car manufacturer. It involves scheduling a set of cars which are divided into classes by different sets of options. The assembly line has different stations where the various options (air-conditioning, sun-roof, etc.) are installed by teams. Each such station has a certain workload capacity given by the parameters P and Q: the station is able to handle P cars with the given option in each sequence of Q cars. The goal of the problem is to produce of a sequence of cars respecting the given demand for each class and such that overcapacities are minimized. In other words, for each option P/Q and for each window of length Q, a penalty max(0,N-P) is counted where N is the number of such options actually scheduled in this window.
In this section, we present a simple model for this problem. We illustrate how to read the input data from a file, we show how to model the Car Sequencing Problem by clearly defining the decisions, constraints, and objective of the problem, and we introduce some of the parameters that can be used to control the search.
Note
The full program for this problem is available among our code templates, together with its Python, Java, C++, and C# versions, plus a certain number of instance files.
Reading input data¶
Data files for this problem are available in folder
hexaly/examples/carsequencing and more files can be downloaded from
http://www.csplib.org (problem number 001). The format of these files is:
1st line: number of cars; number of options; number of classes.
2nd line: for each option, the maximum number of cars with this option in a block (parameter P).
3rd line: for each option, the block size to which the maximum number refers (parameter Q).
Then for each class: index of the class; number of cars in this class; for each option, whether or not this class requires it (0 or 1).
Below is the code of the input() function to read the input data. Note that
undefined variables are equal to nil. This allows checking the existence of
a variable (here, we throw an error if inFileName is not defined). Then, we
simply read integers one after the other in the file (regardless of line
breaks).
To simplify the modeling, we also define an initial sequence of cars, starting with all the cars belonging to the first class, followed by all the cars belonging to the second class, and so on:
use io;
/* Read instance data */
function input() {
local usage = "Usage: hexaly car_sequencing.hxm inFileName=inputFile"
+ "[solFileName=outputFile] [hxTimeLimit=timeLimit]";
if (inFileName == nil) throw usage;
local inFile = io.openRead(inFileName);
nbPositions = inFile.readInt();
nbOptions = inFile.readInt();
nbClasses = inFile.readInt();
maxCarsPerWindow[o in 0...nbOptions] = inFile.readInt();
windowSize[o in 0...nbOptions] = inFile.readInt();
local pos = 0;
for [c in 0...nbClasses] {
inFile.readInt(); // Note: index of class is read but not used
nbCars[c] = inFile.readInt();
options[c][o in 0...nbOptions] = inFile.readInt();
initialSequence[p in pos...pos+nbCars[c]] = c;
pos += nbCars[c];
}
}
Modeling the problem¶
One of the most important steps when modeling an optimization problem with
Hexaly is to define the decision variables. These decisions are such that
instantiating them allows evaluating all other expressions in your model
(in particular, the constraints and objectives). Hexaly Optimizer will try
to find the best possible decisions with respect to the given objectives and
constraints. Here, we want to decide the order of cars in the production line.
The best way to model an order with Hexaly is to define a
list decision variable. Here,
the list represents the sequence of cars: the i-th element of the list
corresponds to the index of the i-th car to build. In other words, if the i-th
element in the list is equal to j, then the i-th car to build is the one
described by initialSequence[j]:
sequence <- list(nbPositions);
Defining the constraints of your model is another important modeling task. Generally speaking, constraints should be limited to strictly necessary requirements. In the car sequencing problem, it is physically impossible to have two cars at the same position. On the contrary, satisfying all P/Q ratios is not always possible, which is why we define it as a first-priority objective instead. Limiting the use of constraints is generally a good practice, as Hexaly Optimizer tends to benefit from denser search spaces. Therefore, symmetry-breaking constraints and other types of non-necessary constraints should also be avoided.
To satisfy the assignment requirements, each car to be produced must be present
exactly once on the assembly line. Using the partition operator, we ensure
that every element between 0 and the total number of positions (excluded) is
present exactly once in the list, meaning that every car appears exactly once in
the sequence:
constraint partition(sequence);
Before writing the objective function, we introduce some intermediate expressions. From the value of the sequence, we can compute, for each option and each position in the line, the number of cars with this option in the window starting at this position:
nbCarsWindows[o in 0...nbOptions][j in 0...nbPositions-windowSize[o]+1]
<- sum[k in 0...windowSize[o]](options[initialSequence[sequence[j + k]]][o]);
Indeed, for each position p, the index of the car at position p is
sequence[p]. This car is described by the expression
initialSequence[sequence[p]]. To know whether this car includes any option
o, we check the value of options[initialSequence[sequence[p]]][o]. This
ability to define intermediate expressions makes the model more readable and
more efficient (if these intermediate expressions are used in several other
expressions).
We can then deduce the number of violations on overcapacities for each option and each window:
nbViolationsWindows[o in 0...nbOptions][p in 0...nbPositions-windowSize[o]+1]
<- max(nbCarsWindows[o][p] - maxCarsPerWindow[o], 0);
Note that we are using here a different way to express a sum. Indeed, you can
define a sum with the sum operator, but you can also use arithmetic
operators + and -. Ultimately, the last step consists in defining the
objective function, which is the sum of all violations:
totalViolations <- sum[o in 0...nbOptions][p in 0...nbPositions-windowSize[o]+1](
nbViolationsWindows[o][p]);
minimize totalViolations;
Parameterizing the optimizer¶
Some parameters can be defined in the param() function in order to control
the search:
function param() {
if (hxTimeLimit == nil) hxTimeLimit = 60;
}
The control parameters can also be set in command line, instead of being defined
in the HXM file. For instance, the following command line launches the
resolution for 30 seconds on the instance
hexaly/examples/carsequencing/instances/carseq_100_8_20_86.in.
> hexaly car_sequencing.hxm inFileName=instances/carseq_100_8_20_86.in hxTimeLimit=30
All control parameters can be found in the section Built-in variables and functions.
Setting an initial solution¶
The param function can also be used to
set an initial solution. Having an
initial solution is not necessary for Hexaly Optimizer to function properly,
but it can sometimes be useful if you wish to start the optimization from an
existing solution, or for debugging
purposes.
To initialize the value of a list decision variable, you must first make sure it is empty, then add its elements one by one:
sequence.value.clear();
for [p in 0...nbPositions]
sequence.value.add(p);
Launching the resolution¶
Having launched Hexaly Optimizer with the above command line, you should observe the following output:
Hexaly Optimizer 14.0.20250724-Linux64. All rights reserved.
Load car_sequencing.hxm...
Run input...
Run model...
Run param...
Run optimizer...
Model: expressions = 10929, decisions = 1, constraints = 1, objectives = 1
Param: time limit = 60 sec, no iteration limit
[objective direction ]: minimize
[ 0 sec, 0 itr]: 403
[ optimality gap ]: 100.00%
[ 1 sec, 32710 itr]: 6
[ 2 sec, 97525 itr]: 2
[ 3 sec, 167931 itr]: 1
[ 4 sec, 241127 itr]: 1
[ 4 sec, 241144 itr]: 0
[ optimality gap ]: 0%
241144 iterations performed in 4 seconds
Optimal solution:
obj = 0
gap = 0%
bounds = 0
Run output...
Each second (in accordance with parameter hxTimeBetweenDisplays, of default
value 1), a brief report of the current state of the search is displayed: the
number of elapsed seconds and the number of iterations performed, and the values
of the objective functions given in lexicographic order (separated with |).
You can disable all displays by setting parameter hxVerbosity to 0.
Writing solutions¶
You can define an output() function that will be called by Hexaly at
the end of the search. Here, we define a function writing the solution of the
problem in a file whose name must be assigned (for instance in the command line)
to some variable solFileName. It writes the solution in the following format:
First line: the objective value
Second line: for each position p, index of the class at position p
You can retrieve the value of any expression (decisions, intermediate
expressions) thanks to the value attribute. Writing in a file is done with
the usual print and println functions:
function output() {
if (solFileName == nil) return;
local solFile = io.openWrite(solFileName);
solFile.println(totalViolations.value);
local listSolution = sequence.value;
for [p in 0...nbPositions]
solFile.print(initialSequence[listSolution[p]], " ");
solFile.println();
}
Complete code¶
Here is the complete Hexaly Modeler code for the Car Sequencing Problem:
use io;
/* Read instance data */
function input() {
local usage = "Usage: hexaly car_sequencing.hxm "
+ "inFileName=inputFile [solFileName=outputFile] [hxTimeLimit=timeLimit]";
if (inFileName == nil) throw usage;
local inFile = io.openRead(inFileName);
nbPositions = inFile.readInt();
nbOptions = inFile.readInt();
nbClasses = inFile.readInt();
maxCarsPerWindow[o in 0...nbOptions] = inFile.readInt();
windowSize[o in 0...nbOptions] = inFile.readInt();
local pos = 0;
for [c in 0...nbClasses] {
inFile.readInt(); // Note: index of class is read but not used
nbCars[c] = inFile.readInt();
options[c][o in 0...nbOptions] = inFile.readInt();
initialSequence[p in pos...pos+nbCars[c]] = c;
pos += nbCars[c];
}
}
/* Declare the optimization model */
function model() {
// sequence[i] = j if class initially placed on position j is produced on position i
sequence <- list(nbPositions);
// sequence is a permutation of the initial production plan, all indexes must
// appear exactly once
constraint partition(sequence);
// Number of cars with option o in each window
nbCarsWindows[o in 0...nbOptions][j in 0...nbPositions-windowSize[o]+1]
<- sum[k in 0...windowSize[o]](options[initialSequence[sequence[j + k]]][o]);
// Number of violations of option o capacity in each window
nbViolationsWindows[o in 0...nbOptions][p in 0...nbPositions-windowSize[o]+1]
<- max(nbCarsWindows[o][p] - maxCarsPerWindow[o], 0);
// Minimize the sum of violations for all options and all windows
totalViolations <- sum[o in 0...nbOptions][p in 0...nbPositions-windowSize[o]+1](
nbViolationsWindows[o][p]);
minimize totalViolations;
}
/* Parametrize the solver */
function param() {
// Set the initial solution
sequence.value.clear();
for [p in 0...nbPositions]
sequence.value.add(p);
if (hxTimeLimit == nil) hxTimeLimit = 60;
}
/* Write the solution in a file with the following format:
* - 1st line: value of the objective;
* - 2nd line: for each position p, index of class at positions p. */
function output() {
if (solFileName == nil) return;
local solFile = io.openWrite(solFileName);
solFile.println(totalViolations.value);
local listSolution = sequence.value;
for [p in 0...nbPositions]
solFile.print(initialSequence[listSolution[p]], " ");
solFile.println();
}