Try our new documentation site (beta).
Model.setPWLObj()
setPWLObj ( var, x, y )
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 and
arguments give coordinates for the
vertices of the function.
For example, suppose we want to define the function shown below:
![Image pwl](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/./pwl.png?x70725)
![<span>$</span>(1, 1)<span>$</span>](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img30.png?x70725)
![<span>$</span>(3,2)<span>$</span>](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img31.png?x70725)
![<span>$</span>(5,4)<span>$</span>](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img32.png?x70725)
![<span>$</span>x<span>$</span>](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img9.png?x70725)
![<span>$</span>y<span>$</span>](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img26.png?x70725)
![<span>$</span>f(1) = 1<span>$</span>](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img33.png?x70725)
![<span>$</span>f(3) = 2<span>$</span>](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img34.png?x70725)
![<span>$</span>f(5) = 4<span>$</span>](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img35.png?x70725)
![<span>$</span>x<span>$</span>](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img9.png?x70725)
![<span>$</span>f(-1)=0<span>$</span>](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img36.png?x70725)
![<span>$</span>f(6)=5<span>$</span>](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img37.png?x70725)
More formally, a set of points
![\begin{displaymath}
\mathtt{x} = [x_1, \ldots, x_n], \quad \mathtt{y} = [y_1, \ldots, y_n]
\end{displaymath}](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img53.png?x70725)
define the following piecewise-linear function:
![\begin{displaymath}
f(v) =
\left\{
\begin{array}{ll}
y_1 + \frac{y_2-y_1}{x_2-x_...
...- x_n), & \mathrm{if}\; v \ge x_n. \ [7pt]
\end{array}\right.
\end{displaymath}](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img39.png?x70725)
The entries must appear in non-decreasing order. Two points can
have the same
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.
Arguments:
var: A Var object that gives the variable whose objective function is being set.
x: The values for the points that define the piecewise-linear function. Must be in non-decreasing order.
y: The values for the points that define the piecewise-linear function.
Example usage:
model.setPWLObj(var, [1, 3, 5], [1, 2, 4])
![](/wp-content/plugins/hd_documentations/content/images/documentation-ampl-guide.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-cloud.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-example-tour.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-quick-start.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-reference-manuals.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-remote-services.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/quickstart-os-linux.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/quickstart-os-mac-osx.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/quickstart-os-windows.webp?x70725)