Declaring the objective
An objective function is used to express the cost of each potential solution.
An objective function is used to express the cost of each potential solution. When an objective is added to a model, the problem is considered to be an optimization problem. The CP Optimizer search handles models with at most one objective function, though the modeling API provided by Concert Technology does not impose this restriction.
In the C++ API and the Java™ API, objective functions
are represented by IloObjective. In the C# API, objectives are represented by IObjective.
An objective function can be specified by creating an objective instance
directly; however, it is common to use the functions which return
an instance of IloObjective. In the C++ API, the functions are IloMinimize and IloMaximize. In the Java API, these functions are IloModeler.minimize and IloModeler.maximize. In the C# API,
these functions are IModeler.Minimize and IModeler.Maximize. For example, in the C++
API, the following code defines an objective to minimize the expression x[1] + 2*x[2] + 3*x[3]:
IloObjective obj
= IloMinimize(env,
x[1] + 2*x[2] + 3*x[3]);
Using the Java API,
the same objective could be written using the method IloCP.minimize:
IloObjective obj
= cp.minimize(cp.sum(cp.sum(x[1],cp.prod(2,x[2])),cp.prod(3,x[3])));
Using the C# API, the same objective could
be written using the method CP.Minimize:
IObjective obj
= cp.Minimize(cp.Sum(cp.Sum(x[1],cp.Prod(2,x[2])),cp.Prod(3,x[3])));
Objective
An objective expresses the cost of possible solutions. The optimal solution to an optimization problem is the feasible solution that, depending on the problem type, minimizes or maximizes the cost.
After you create an objective, you must explicitly add it to the model in order for it to be taken into account by the optimizer.