• Gurobi Staff

Hi Param,

Could you please clarify what exactly the issue is? Is it about not getting the correct solution point? Do you get an error during model building?

Note that you can always write your model to a human-readable LP file via the write method

model.write("myModel.lp")

You can open the file $$\texttt{myModel.lp}$$ in any standard text editor and analyze whether the model looks as expected.

Maybe the Knowledge Base article How do I diagnose a wrong solution? might be helpful.

Moreover please note that your code snippet is not a minimal reproducible example.

Best regards,
Jaromił

The problem is with formulating the variables. Here,variable r[i,j] is not being assigned properly. Thus, the model always picks variable r as 0 at all timesteps.

def model_variables_and_constraints(model, tk, v1_t, v2_t, e1, e2, v, T_full_charge, T_top_up, delta_t):
r1t = {}
e1t = {}
# current_set = [8, 16, 24, 32, 48, 64]

# Add the charging variables r and e for full charge and top-up EV
for i in range(len(v1_t)):
for j in range(len(tk)):
r1t[i, j] = model.addVar(lb=0, ub=64, vtype=gb.GRB.INTEGER, name=f'EV_{i}_current_{j}')
e1t[i, j] = model.addVar(lb=0, ub=e1[i], vtype=gb.GRB.INTEGER, name=f'ChargingVariable_e1_{i}_{j}')
if tk[i] <= tk[j] <= T_full_charge[i]:

# Define constraints
for i in range(len(v1_t)):
for j in range(len(tk)):
res = model.addVar(lb=0, ub=50000, vtype=gb.GRB.INTEGER, name=f'volts*current_{i}_{j}')
final_res = model.addVar(lb=0, ub=50, vtype=gb.GRB.INTEGER, name=f'round_result_{i}_{j}')
model.addConstr(res == r1t[i, j] * v * delta_t)
# Constraint to update pending energy based on cumulative charging completed till that step
if tk[i]<=tk[j]<=T_full_charge[i]:
if j > 0:
model.addConstr(e1t[i, j] == e1[j-1] - final_res)
else:
model.addConstr(e1t[i, j] == e1[i] - final_res)
else:
return r1t, e1t
• Gurobi Staff

I do not see any variable $$\texttt{r[i,j]}$$ in your code snippet. Do you mean $$\texttt{r1t[i,j]}$$?

Sorry, yes I meant r1t[i,j]

• Gurobi Staff

Hi Param,

The constraints

model.addConstr(gb.quicksum([b1,b2,b3,b4,b5,b6])==1)...model.addConstr(r1t[i,j] == gb.quicksum([8*b1,16*b2,24*b3,32*b4,48*b5,64*b6]))

should make sure that each $$\texttt{r1t}$$ variable has at least value $$\texttt{8}$$.

Could you please write your model to an LP file and analyze whether it looks as expected? You can do so via

model.write("myModel.lp")

You can open the file $$\texttt{myModel.lp}$$ in any standard text editor.

if tk[i] <= tk[j] <= T_full_charge[i]:  model.addConstr(r1t[i, j] == 0)

Best regards,
Jaromił

Thankyou for your reply Jaromil. I changed the constraint which was incorrect. But the issue still persists. The thing is I also tried the same problem using PuLP solver, I can find an optimal solution using that. Also, when I write the model, there is a problem in variable initialization. You can see it here.

Maximize
- 119.5 EV_0_current_0 - 0.5 ChargingVariable_e1_0_0
- 119.5 EV_0_current_1 - 0.5 ChargingVariable_e1_0_1
- 119.5 EV_0_current_2 - 0.5 ChargingVariable_e1_0_2
- 120 EV_1_current_0 - 0.5 ChargingVariable_e1_1_0 - 120 EV_1_current_1
- 0.5 ChargingVariable_e1_1_1 - 120 EV_1_current_2
- 0.5 ChargingVariable_e1_1_2 - 120 EV_2_current_0
- 0.5 ChargingVariable_e1_2_0 - 120 EV_2_current_1
- 0.5 ChargingVariable_e1_2_1 - 120 EV_2_current_2
- 0.5 ChargingVariable_e1_2_2 - 119.5 EV_topup_0_current_0
- 0.5 ChargingVariable_e2_0_0 - 119.5 EV_topup_0_current_1
- 0.5 ChargingVariable_e2_0_1 - 119.5 EV_topup_0_current_2
- 0.5 ChargingVariable_e2_0_2 - 120 EV_topup_1_current_0
- 0.5 ChargingVariable_e2_1_0 - 120 EV_topup_1_current_1
- 0.5 ChargingVariable_e2_1_1 - 120 EV_topup_1_current_2
- 0.5 ChargingVariable_e2_1_2 - 120 EV_topup_2_current_0
- 0.5 ChargingVariable_e2_2_0 - 120 EV_topup_2_current_1
- 0.5 ChargingVariable_e2_2_1 - 120 EV_topup_2_current_2
- 0.5 ChargingVariable_e2_2_2
Subject To
R0: binary_var_current_8a + binary_var_current_16a
+ binary_var_current_24a + binary_var_current_32a
+ binary_var_current_48a + binary_var_current_64a = 1
R1: - 8 binary_var_current_8a - 16 binary_var_current_16a
- 24 binary_var_current_24a - 32 binary_var_current_32a
- 48 binary_var_current_48a - 64 binary_var_current_64a + EV_0_current_0
= 0.

I modified the constraints now:

# Add the charging variables r and e for full charge and top-up EV
for i in range(len(v1_t)):
for j in range(len(tk)):
r1t[i, j] = model.addVar(lb=0, ub=64, vtype=gb.GRB.INTEGER, name=f'EV_{i}_current_{j}')
e1t[i, j] = model.addVar(lb=0, ub=e1[i], vtype=gb.GRB.INTEGER, name=f'ChargingVariable_e1_{i}_{j}')
if tk[i] <= tk[j] <= T_full_charge[i]:
else:

for i in range(len(v2_t)):
for j in range(len(tk)):
r2t[i, j] = model.addVar(lb=0, ub=64, vtype=gb.GRB.INTEGER, name=f'EV_topup_{i}_current_{j}')
e2t[i, j] = model.addVar(lb=0, ub=e2[i], vtype=gb.GRB.INTEGER, name=f'ChargingVariable_e2_{i}_{j}')
if tk[i] <= tk[j] <= T_top_up[i]:
else: