Model a binary variable from two continous variables
AnsweredI want to model a binary value (b) from two continous variables (v_in, v_out). These variables are 0<=v_in, v_out <= v_max.
I want the following three conditions to apply on my binary variable
- v_in>0 & v_out=0 ==> b=1,
- v_in=0 & v_out>0 ==> b=0,
- v_in=0 & v_out=0 ==> b=0.
I have tried the following:
- v_in = b*v_max
- v_out = (1-b)v_max
The above solution works for 1. and 2. but it is nearly always wrong for 3.
Can you please help?
Thanks
-
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?
0 -
Thanks Jaromil,
v_in>0 and v_out>0 should not be possible. Only one of these values can be positive.
0 -
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?
0 -
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 = 0
x_vol_min = 0
x_vol_max = 10
phy_cost = 50
x_max = 5
x_max_mirror = -x_max
n_step = 3
df = pd.read_csv('input_data.csv', parse_dates=['ts'])
# Import Constants
const_out = df['input_out'].values
const_in = df['input_in'].values
m = gp.Model('model_1')
# Define continuous variables
x_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 variable
bln = m.addVars(n_step, vtype=GRB.BINARY)
# Define other variables
x_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.00001
m.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 constraints
m.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 constraints
m.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[0] == x_vol_init + x_in[0] - x_out[0])
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 fucntion
OF = 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()0 -
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.
0 -
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.
Thanks for your support Jaromil.
Best,
0
Please sign in to leave a comment.
Comments
6 comments