Try our new documentation site (beta).


Set a piecewise-linear objective function for a variable.

The arguments to this method specify a list of points that define a piecewise-linear objective function for a single variable. Specifically, the <span>$</span>x<span>$</span> and <span>$</span>y<span>$</span> arguments give coordinates for the vertices of the function.

For example, suppose we want to define the function <span>$</span>f(x)<span>$</span> shown below:

Image pwl

The vertices of the function occur at the points <span>$</span>(1, 1)<span>$</span>, <span>$</span>(3,2)<span>$</span> and <span>$</span>(5,4)<span>$</span>, so <span>$</span>x<span>$</span> is {1, 3, 5} and <span>$</span>y<span>$</span> is {1, 2, 4}. With these arguments we define <span>$</span>f(1) = 1<span>$</span>, <span>$</span>f(3) = 2<span>$</span> and <span>$</span>f(5) = 4<span>$</span>. Other objective values are linearly interpolated between neighboring points. The first pair and last pair of points each define a ray, so values outside the specified <span>$</span>x<span>$</span> values are extrapolated from these points. Thus, in our example, <span>$</span>f(-1)=0<span>$</span> and <span>$</span>f(6)=5<span>$</span>.

More formally, a set of <span>$</span>n<span>$</span> points

\mathtt{x} = \{x_1, \ldots, x_n\}, \quad \mathtt{y} = \{y_1, \ldots, y_n\}

define the following piecewise-linear function:

f(v) =
y_1 + \frac{y_2-y_1}{x_2-x_...
...- x_n), & \mathrm{if}\; v \ge x_n. \ [7pt]

The <span>$</span>x<span>$</span> entries must appear in non-decreasing order. Two points can have the same <span>$</span>x<span>$</span> coordinate -- this can be useful for specifying a discrete jump in the objective function.

Note that a piecewise-linear objective can change the type of a model. Specifically, including a non-convex piecewise linear objective function in a continuous model will transform that model into a MIP. This can significantly increase the cost of solving the model.

Setting a piecewise-linear objective for a variable will set the Obj attribute on that variable to 0. Similarly, setting the Obj attribute will delete the piecewise-linear objective on that variable.

Each variable can have its own piecewise-linear objective function. They must be specified individually, even if multiple variables share the same function.

void setPWLObj ( GRBVar var,
    double[] x,
    double[] y )
    Set the piecewise-linear objective function for a variable.


    var: The variable whose objective function is being set.

    x: The <span>$</span>x<span>$</span> values for the points that define the piecewise-linear function. Must be in non-decreasing order.

    y: The <span>$</span>y<span>$</span> values for the points that define the piecewise-linear function.

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.