I'm trying to schedule power of multiple prosumers in a microgrid. The problem includes a cost function with min and max statements since I want to distinguish between the prices for importing and exporting energy.

The following is a minimal example for the cost function without summing over all timesteps at the moment:

I have two decision variables $P_{charge}$ and $P_{discharge}$ for the battery storage (one of it is assumed to be always zero) and one other controllable load $P_{load}$. Consumption is denoted with positive and generation with negative values. $C_{import}$ and $C_{export}$ are the per unit prices for importing and exporting energy.

$C = max((P_{charge} + P_{discharge} + P_{load}) \cdot \Delta T,0) \cdot C_{import} + min((P_{charge} + P_{discharge} + P_{load}) \cdot \Delta T,0) \cdot C_{export}$

The min and max functions form if-else statements to check if the sum of power flows is negative or positive. Is it possible to introduce some binary decision variables to transform it into a MILP? I want to use a Matlab solver later.

Edit 1:

I have tried to introduce the proposed auxiliary variables but they do not match the desired values I computed from my simulation. So maybe I have to clarify the problem. At first, I simplify power flow of battery to one bidirectional power variable to reduce the problem further.

enter image description here

The image shows a part of my problem. My objective is to maximize $R - C$, where $R \ge 0$ is revenue and $C \ge 0$ is cost. Per unit prices are always greater than or equal to zero and $C_{import} > C_{export}$ is valid for now. The nonlinear version would be:

$$R = min(P_{load} + P_{BSS},0) \cdot (-1) \cdot C_{export}$$ $$C = max(P_{load} + P_{BSS},0) \cdot C_{import}$$

I have defined non-negative decision variables $X_{import}$ and $X_{export}$ with the line capacity (energy per timestep) as upper boundary. Additionally I've introduced following equality constraint:

$$(P_{load} + P_{BSS}) \cdot \Delta T == X_{import} - X_{export}$$

I neglected the injected PV power at this node for now. My goal is to be somehow able to use the auxiliary variables for an upper level node. Maybe I didn't fully understand the proposed answers which might led to the undesired result.


2 Answers 2


Taking a different tack from Rob, I'm going to assume that the original objective function $C$ is correct as stated and is being minimized, with $C_\text{export}>0$ being a per-unit compensation value for exported energy (i.e., you pay to import and get paid to export). In that case, splitting the energy expression into $X_\text{import} - X_\text{export}$ as Rob does will work if $C_\text{import} > C_\text{export}$ but will not work with $C_\text{import} < C_\text{export},$ since in the latter case the solver could add an arbitrary amount $\delta > 0$ to both $X_\text{import}$ and $X_\text{export},$ keeping the energy value the same while reaping an undeserved profit of $(C_\text{export} - C_\text{import})\delta.$ In this case, you have to introduce a binary variable $Y$ and constraints $$X_\text{import}\le M_\text{import}\cdot Y$$ and $$X_\text{export}\le M_\text{export}\cdot (1-Y),$$ where $M_\text{import}$ and $M_\text{export}$ are upper bounds on the amount of energy that could be imported or exported.

  • $\begingroup$ Thanks you for the hint! I tried to implement this as well. The binary variable seemed to be only zero for every timestep. Do I have to include Y somehow into the objective function? Is M a fixed boundary like the line capacity or is it the maximum for every timestep based on the current power flow? But this would involve some abs functions I guess. Maybe my edited questions helps to clarify the problem. $\endgroup$ Commented Jul 12, 2022 at 18:35
  • 1
    $\begingroup$ $M$ is a constant. It can be indexed by time if you know a priori bounds on imports and exports for that time, but it is not intended to be dependent on the variables in the model. $Y$ does not belong in the objective. I assume that you actually have a $P_{load}$ variable etc. for each combination of prosumer and time step. Whatever the indexing of $P_{load}$ and its siblings, the $X$ and $Y$ variables would have the same indexing. $\endgroup$
    – prubin
    Commented Jul 12, 2022 at 18:50
  • $\begingroup$ Alright, thank you very much! Now it works as intended. I assume that splitting $P_{BSS}$ into charging and discharging power can be treated the same way. $\endgroup$ Commented Jul 13, 2022 at 10:33

Assuming you meant $C=\max() - \min()$ instead of $C=\max() + \min()$, you can introduce nonnegative variables $X_\text{import}$ and $X_\text{export}$ and linear constraints $$(P_\text{charge} + P_\text{discharge} + P_\text{load}) \cdot \Delta T = X_\text{import} - X_\text{export}$$ and then minimize the linear function $$C_\text{import} X_\text{import} + C_\text{export} X_\text{export}.$$

  • 1
    $\begingroup$ +1 this. If the economics ($C_\text{import} > C_\text{export}$) are realistic, this will automatically lead to mutually exclusive charging/discharging decisions, completely without leaving the cosy LP space. $\endgroup$
    – ojdo
    Commented Jul 12, 2022 at 11:34
  • $\begingroup$ @RobPratt Thank you for your answer. I tried to implement it but the aux variables didn't match the desired values. I've edited my question to clarify the problem and my thoughts. I hope that helps! $\endgroup$ Commented Jul 12, 2022 at 15:27

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.