# OR-Tools: Nonlinear constraints?

I have inherited a reasonably simple ortools-based optimizer (Python) with linear relationships that I need to non-linear-ize, and I have no idea how to do that.

The relevant part of my problem looks like this:

solver = pywraplp.Solver("B", pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
...
# charging a battery
## cap_prev: capacity before the current timeslot
## cap: ditto afterwards
## b_chg: charging power
## all are NumVar(0,some_hardware_limit)

solver.Add(cap_prev + 0.9 * b_chg - b_dis == cap)


The problem is that I need to replace the 0.9 with something that depends on the value of b_chg. The Naïve solution looks like this:

solver.Add(cap_prev + b_chg_eff - b_dis == cap)



except that this is no longer linear, which kindof points to the assumption that I need a different solver.

• This is piecewise quadratic. And in addition, non-convex (all quadratic equality constraints are non-convex). I would look into adding more segments and using a piecewise linear approximation. Standard formulations are available for piecewise linear functions. Commented Feb 13, 2023 at 15:59
• You can use CP-SAT (your example borrowed some of its API). You will need to scale all floating points to integer, use integer variables, and read github.com/google/or-tools/blob/stable/ortools/sat/docs/… for the OnlyEnforceIf part. You will also need to use AddMultiplicationEquality() and intermediate variables. Commented Feb 13, 2023 at 16:22
• Well, sure, I can auto-generate something piecewise-linear from my piecewise-quadratic formula … but won't that result in a suboptimal solution? seems to me that a stepwise linearization would add a heap of local maxima (of unknown magnitude) to the problem. Commented Feb 14, 2023 at 18:07
• What is "stepwise linearization"? You mean a step function? No, piecewise linearization is different. Error bounds on the approximation are known a priori. "local maxima"? How so? MIP solvers are global solvers. I suggest discussing with your teacher, as there is some confusion here. Commented Feb 14, 2023 at 21:02
• What teacher?? Yes I can approximate with arbitrary error bounds, nice, do I write my own code to assemble the approximation or are there solvers that include appropriate support code? Also, well, nice to know that MIP solvers in general do global optimization, but I was kindof hoping there's some resource out there that explains using concrete code, not abstract math, how to go from what I have to where I need to go with this. I can't possibly be the only person who builds a linear model and then gets confronted with nonlinear requirements. Commented Feb 16, 2023 at 17:52

$$200\delta_3+ 100\delta_2 \le$$ b_chng $$\le (200+e)\delta_2 +(100+e)\delta_1$$
$$\sum_{i=1}^3\delta_i = 1$$ where $$\delta$$ are binary and e is a very small number (like $$1e-3$$).
b_chng_eff= 1st Constr1$$\delta_1$$+2nd_Constr$$\delta_2$$+3rd_Constr$$\delta_3$$
• Multiplying by $\delta$ makes things cubic. This is not how I would formulate piecewise functions. Commented Feb 14, 2023 at 2:49