•  Gurobi Staff

Your 3 conditions can be reformulated as

v_in > 0 => b=1 and b=0 else.

This is only if you are OK with the fact that if v_in > 0 & v_out > 0 then b=1.

Modeling such a conditional statement is discussed in the Knowledge Base article How do I model conditional statements in Gurobi?

•    Thanks Jaromil,

v_in>0 and v_out>0 should not be possible. Only one of these values can be positive.

•  Gurobi Staff

If v_in>0 and v_out>0 is not possible then your 3 conditions can indeed be formulated as

v_in > 0 => b=1 and b=0 else and you can follow the instructions in Knowledge Base article How do I model conditional statements in Gurobi?

•    I read the article and I followed it. I still don't get the correct answer. The issue might arise from other parts of the model.

I have prepared the following minimal example. It is a basic storage model built mostly on the power generation example on Gurobi case study.

Can you please have a look and let me know if you would change it.

x_vol_init = 0x_vol_min = 0x_vol_max = 10phy_cost = 50x_max = 5x_max_mirror = -x_maxn_step = 3df = pd.read_csv('input_data.csv', parse_dates=['ts'])# Import Constantsconst_out = df['input_out'].valuesconst_in = df['input_in'].valuesm = gp.Model('model_1')# Define continuous variablesx_out = m.addVars(n_step, vtype=GRB.CONTINUOUS, ub=x_max)x_in = m.addVars(n_step, vtype=GRB.CONTINUOUS, ub=x_max)# Define boolean variablebln = m.addVars(n_step, vtype=GRB.BINARY)# Define other variablesx_vol = m.addVars(n_step, vtype=GRB.CONTINUOUS, ub=x_vol_max, lb=x_vol_min)x_net = m.addVars(n_step, vtype=GRB.CONTINUOUS, ub=x_max, lb=x_max_mirror)# From https://support.gurobi.com/hc/en-us/articles/4414392016529-How-do-I-model-conditional-statements-in-Gurobi-eps = 0.00001m.addConstrs(x_out[t] >= x_in[t] + eps - x_max * (1 - bln[t]) for t in range(n_step))m.addConstrs(x_out[t] <= x_in[t] + x_max * bln[t] for t in range(n_step))# Other constraintsm.addConstrs(x_net[t] <= x_max for t in range(n_step))m.addConstrs(x_net[t] >= x_max_mirror for t in range(n_step))m.addConstrs(x_net[t] == x_out[t] - x_in[t] for t in range(n_step))# Volume related constraintsm.addConstrs(x_vol[t] <= x_vol_max for t in range(n_step))m.addConstrs(x_vol[t] >= x_vol_min for t in range(n_step))m.addConstrs(x_vol == x_vol_init + x_in - x_out)m.addConstrs(x_vol[t] == x_vol[t - 1] + x_in[t] - x_out[t] for t in range(1, n_step))# Calculate the objective fucntionOF = gp.quicksum(    (x_out[t] * const_out[t]) - (x_in[t] * const_in[t]) - (bln[t] * (x_net[t] * phy_cost)) for t in range(n_step))m.setObjective(OF, GRB.MAXIMIZE)m.optimize()
•  Gurobi Staff

It is not possible to reproduce your example due to missing input_data.csv file. You could try hard coding a small data set which still shows the wrong behavior. Please then also elaborate on what exactly is going wrong with the model and what you would expect to happen.

•    I found the issue. It has to do with including another continous variable in the objective function (x_net) which results in forcing incorrect values on b_in and b_out.