KeyError, Attribute Error.
AnsweredHello! I want to model a bidirectional EV charging & discharging scheduling problem. Right now, I am trying to implement some constraints on the power flow such that
1. the power at the EV charging station is balanced with the power drawn from the utility grid i.e. chargingPower - dischargingPower = gridPower, where gridPower is the amount of power drawn from the grid to the EV charging station load.
2. charging and discharging of power cannot happen within an overlapping time step i.e. an EV cannot be charging and discharging simultaneously.
The following implementation returns a KeyError "Missing Constraint Index". However, when I debug using Pycharm, by inspecting p_ch, p-dch and u, I see that there are multiple key and attribute errors (shown in screenshot). Can someone explain what is happening, and how I may correct this?
p_ch = {}
p_dch = {}
p_grid = {}
p_grid_max = utilityLine_capacity #240kW
u = m.addVars(
ev_index, time_intervalStart, vtype=gp.GRB.BINARY, name="u"
)
# charge_val[i, t], discharge_val[i, t] are binary decision variables that capture the charging/discharging instances per time step, t for each ev i
for i in ev_index:
for t in time_intervalStart:
p_ch[i,t] = charge_val[i, t] * batt_spec["ch_rate"]*batt_spec["ch_eff"] / batt_spec["max_cap"]
p_dch[i, t] = discharge_val[i, t] * batt_spec["dch_rate"] * batt_spec["dch_eff"] / batt_spec["max_cap"]
for i in ev_index:
for t in time_intervalStart:
m.addConstrs((u[i,t] == 0) >> (p_ch[i,t] == 0))
m.addConstrs((u[i, t] == 1) >> (p_dch[i, t] == 0))
for t in time_intervalStart:
p_grid[t] = gp.LinExpr()
for i in ev_index:
p_grid[t] = gp.quicksum(p_ch[i, t] for i in ev_index for t in time_intervalStart)-gp.quicksum(p_dch[i, t] for i in ev_index for t in time_intervalStart)
m.addConstr(p_grid[t] <= p_grid_max)
m.addConstr(p_grid[t] >= -p_grid_max)
The following screenshots show that u, p_ch, p_dch, p_grid are all of the same length, and I checked that they all had the same keys before the error message ocurred.


-
Hi Myat,
The error "KeyError: 'Missing constraint index'" occurs when you try to add a single constraint using the method Model.addConstrs().
Please use Model.addConstr() instead of using Model.addConstrs() to add the single indicator constraint in the following calls in your code
m.addConstr((u[i,t] == 0) >> (p_ch[i,t] == 0))
m.addConstr((u[i, t] == 1) >> (p_dch[i, t] == 0))Regards,
Simran
1
Please sign in to leave a comment.
Comments
1 comment