Specifying Multiple Objectives
Let us first discuss the interface for managing multiple objectives.
An empty model starts with one objective function, which is initially
just 0.0. We'll refer to this as the primary objective. You
can modify it in a few ways: you can set the
Obj attribute, or you can use the
setObjective
method from your language API (e.g.,
Model.setObjective in
Python).
Note that for a multi-objective model, all objective functions must be linear,
including the primary objective.
In general, attributes and methods that aren't specific to
multi-objective optimization will work with the primary objective
function.
If you'd like to input additional objectives, your first step is to
tell Gurobi how many objectives you'd like to have in your model. You
do this by setting the NumObj attribute on
the Gurobi model. Objectives are numbered 0
through
NumObj-1
. Once you've done this, you then modify or query
objective in two steps. The first is to set parameter
ObjNumber to the desired objective
number; the second is to use the ObjN
variable attribute to access the corresponding objective entry. The
primary objective is always objective
, so
ObjN and Obj are
equivalent when ObjNumber is 0.
Additional objectives are always linear.
To give an example, after you set NumObj
to 3, then
ObjNumber
can be set to ,
, or
. This parameter
setting will determine the effect of setting attribute
ObjN
on
a variable. Thus, for example, if you set ObjN
to 5 for a variable
x
when ObjNumber
is 2, then the objective coefficient in
objective 2 for variable x
will be set to 5.
You can also set an objective constant for each objective, using
ObjNCon. Again,
ObjNCon and
ObjCon are equivalent when
ObjNumber is . You can also give
each objective a name through ObjNName.
Note that a model has a single objective sense (controlled by the ModelSense attribute). This means that you can't maximize the first objective and minimize the second. However, you can achieve the same result with a simple trick. Each objective has a weight, and these weights are allowed to be negative. Minimizing an objective function is equivalent to maximizing the negation of that function.
You can change the number of objectives in your model as many times as you like. When you increase the objective count, the new objectives and their associated attributes are set to 0. When you decrease the count, objectives beyond the new count are discarded. If you set the number of objectives to zero, the model becomes a pure feasibility problem.
We have extended the LP and MPS file formats, so writing a model with multiple objectives to a file will capture those objectives. Similarly, if you read a model file that contains multiple objectives, then NumObj and ObjN will capture the objectives stored in the file. See the file format section for details.