An error from the dynamic set
AnsweredI want to apply dynamically add decision variables and constraints to my model. But I get a key error from static set. I don't understand what is the problem. Any help will be appreciated.
Error:
Traceback (most recent call last):
File "d:\Github\docentlik_jcp\lagr_cutgen_dual_v1.py", line 468, in <module>
res, soltime=lagrange(pval,prod,7200, S)
File "d:\Github\docentlik_jcp\lagr_cutgen_dual_v1.py", line 355, in lagrange
rmpd_iter1[i,t,s]=rmpd.addConstr(sum(z_dual[i,t,s,iters] for iters in range(iter+1))==1)
File "d:\Github\docentlik_jcp\lagr_cutgen_dual_v1.py", line 355, in <genexpr>
rmpd_iter1[i,t,s]=rmpd.addConstr(sum(z_dual[i,t,s,iters] for iters in range(iter+1))==1)
KeyError: (0, 1, 0, 0)
The code part can cause this problem is:
for iter in tqdm(range(max_iter)):
z_dual={}
for s in range(S):
for i in range(n):
for t in range(1,T):
z_dual[i,t,s,iter] = rmpd.addVar(vtype=GRB.CONTINUOUS, lb=0, name="z_dual%s.%s.%s.%s" % (i,t,s,iter))
rmpd_iter1={}
#RMP constraints
for s in range(S):
for i in range(n):
for t in range(1,T):
rmpd_iter1[i,t,s]=rmpd.addConstr(sum(z_dual[i,t,s,iters] for iters in range(iter+1))==1)
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support.
-
Hi Nazmi,
KeyError: (0, 1, 0, 0) comes from python dict. It seems that the key (0, 1, 0, 0) does not exist in z_dual. You can run print(z_dual.keys()) and check what the keys of this dict. I suspect that the place in which z_dual is defined is wrong.
Thanks,
Ryuta1 -
Hi Ryuta,
I don't understand but this problem is vanished after adding print(z_dual.keys()) command. However, after get rid of this warning from the first iteration it again appears in the second iteration. I do not think the error is caused by the placement. The end of the each iteration I remove this constraint set, and I define the beginning of the each iteration with new decision variables. I appreciate your helps. Thank you for your helps,
Current form of error:0%| | 1/2000 [00:03<1:51:51, 3.36s/it]
Traceback (most recent call last):
File "d:\Github\docentlik_jcp\lagr_cutgen_dual_v1.py", line 479, in <module>
res, soltime=lagrange(pval,prod,7200, S)
File "d:\Github\docentlik_jcp\lagr_cutgen_dual_v1.py", line 357, in lagrange
rmpd_iter1[i,t,s]=rmpd.addConstr(sum(z_dual[i,t,s,iters] for iters in range(iter+1))==1, name=f"rmpd1_{s}_{i}_{t}")
File "d:\Github\docentlik_jcp\lagr_cutgen_dual_v1.py", line 357, in <genexpr>
rmpd_iter1[i,t,s]=rmpd.addConstr(sum(z_dual[i,t,s,iters] for iters in range(iter+1))==1, name=f"rmpd1_{s}_{i}_{t}")
KeyError: (0, 1, 0, 0)Source of error:for iter in tqdm(range(max_iter)):
z_dual={}
for s in range(S):
for i in range(n):
for t in range(1,T):
z_dual[i,t,s,iter] = rmpd.addVar(vtype=GRB.CONTINUOUS, lb=0, name="z_dual%s.%s.%s.%s" % (i,t,s,iter))
rmpd.update()
rmpd_iter1={}
#RMP constraints
for s in range(S):
for i in range(n):
for t in range(1,T):
rmpd_iter1[i,t,s]=rmpd.addConstr(sum(z_dual[i,t,s,iters] for iters in range(iter+1))==1, name=f"rmpd1_{s}_{i}_{t}")
rmpd_iter2={}
#RMP constraints
for i in range(n):
for t in range(1,T):
rmpd_iter2[i,t]=rmpd.addConstr(gp.quicksum(z_dual[i,t,s,iters]*x_val[i,t,s,iter] for iters in range(iter+1) for s in range(S))==cg_bar[i,t], name=f"rmpd2_{i}_{t}")
rmpd_obj=sum(objval[iters]*z_dual[i,t,s,iters] for i in range (n) for t in range(1,T) for s in range(S) for iters in range(iter+1))
rmpd.setObjective(rmpd_obj, GRB.MINIMIZE)0 -
Hi,
If I understand correctly, "z_dual" is redefined each time inside the loop of "for iter in tqdm(range(max_iter)):". In that case, the fourth index of z_dual is only "iter" each time. However, the part of error refers as the following:
rmpd_iter1[i,t,s]=rmpd.addConstr(sum(z_dual[i,t,s,iters] for iters in range(iter+1))==1, name=f"rmpd1_{s}_{i}_{t}")In the second iteration, the fourth index of z_dual is only "iter"=1, but "iters" will be 0 and 1. The key "iter"=0 doed not exist, therefore KeyError occurs.
Thanks
1
Post is closed for comments.
Comments
4 comments