I'm using gurobi in an python enviroment and would like to Model a varying efficiency for a Battery charge/discharge without the Model losing it's MIPL characteristics.

Example Code:

eta_Batt = 0.9     # Battery Efficiency

E_Batt_StandBy = 0   # Constant Dischsarge in [W]


m = Model()

m.setParam("MIPGap", 0.1)


x_E_Batt_discharge  = m.addVars(Horizont, name="x_E_Batt_discharge" ,vtype=GRB.CONTINUOUS, lb = 0, ub = E_Batt_discharge_max)

x_E_Batt_charge     = m.addVars(Horizont, name="x_E_Batt_charge"    ,vtype=GRB.CONTINUOUS, lb = 0, ub = E_Batt_charge_max)


m.addConstrs(x_SoC_Batt[t]   == x_SoC_Batt[t-1] + (x_E_Batt_charge[t] * eta_Batt - x_E_Batt_discharge[t] / eta_Batt - E_Batt_StandBy)  * (100* (delta_t/60) / Cap_Batt)  for t in T1)

m.addConstrs(x_SoC_Batt[t]   == SoC_Batt_start + (x_E_Batt_charge[t] * eta_Batt - x_E_Batt_discharge[t] / eta_Batt - E_Batt_StandBy)  * (100* (delta_t/60) / Cap_Batt)  for t in range(1))

I'm searching for a way so that eta_Batt is not a constant but decreases with x_E_Batt_charge or x_E_Batt_discharge without having it directly dependent on these variables and therefore making the contrstaints quadratic.

Thank you in advance

  • 1
    $\begingroup$ Could you provide a description of your problem (rather than code) ? $\endgroup$
    – Kuifje
    Commented Jul 26, 2022 at 8:02
  • $\begingroup$ Maybe I'm missing something, but this looks like a MILP to me, no? $\endgroup$
    – Richard
    Commented Jul 26, 2022 at 8:40
  • $\begingroup$ Hi, thanks for responding so quickly Yes it's an MILP approach right now, but I want to change the eta_Batt constant so that it changes with the amount of energy that is charged or discharged into the Battery. So eta_Batt needs to be dependent on current E_Batt_charge/discharge without making the last two constraints quadratic. Hope that's clarifying my problem $\endgroup$
    – Johanna
    Commented Jul 26, 2022 at 9:04
  • $\begingroup$ @JohannaSchoofs, suppose the problem has been quadratic. What's happened? Many of the quadratic programs can be linearized. $\endgroup$
    – A.Omidi
    Commented Jul 26, 2022 at 9:16
  • $\begingroup$ Yeah that's true but then the optimizer takes a very long time to solve the problem. I want to avoid that because I want to simulate a hole year in a rolling simulation. $\endgroup$
    – Johanna
    Commented Jul 26, 2022 at 9:44

1 Answer 1


An alternative to quadratic constraints to would be to replace x_E_Batt_charge[t] * eta_Batt with a new variable z[t] and define z[t] as a piecewise linear function of x_E_Batt_charge[t]. Recent versions of Gurobi provide support for piecewise linear constraints.

  • $\begingroup$ So simple but it works. Thank you! $\endgroup$
    – Johanna
    Commented Jul 28, 2022 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.