Cannot retrieve dual variables even after fixing all binary variables and resolving
Awaiting user inputI have a minimization problem with a quadratic (A[g]*p[g,t]^2 + B[g]*p[g,t]) objective function.
One of the constraints is the power balance (energy market) and I want to access the dual variables of it.
A[g] parameter is >=0 so in any case objective function is convex.
As explained in the PI section of Gurobi solver documentation, I firstly solved the model (succesfully done) and then I have fixed the noncontinuous (binary variables in my case) and resolved the problem so as to be able to retrieve the duals.
I'm using Pyomo as a modelling framework. So as explained in Pyomo documentation I have ficed the noncontinuous variable values , entered model.dual = pyo.Suffix(direction=pyo.Suffix.IMPORT)
when constructing the model again and before calling again the solver in order to be able to retrieve the dual variable values and then resolved the model (succesfully) and tried to get the dual variable values for constraint "power_balance".
I have of course firstly defined the below options.
solver2=SolverFactory("gurobi")
solver2.options['NonConvex'] = 2
solver2.options['MIPGap']=0
solver2.options["QCPDual"] = 1
solver2.solve(model,tee=True)
The model was solved again and I tried to get the out put of the duals as per below:
da= {'duals': model2.dual[model2.power_balance]}
duals = pd.DataFrame.from_dict([da]).T.rename(columns={0: "Marginal Price"})
print(duals)
and then i received the below error:
KeyError: "Component with id '2613873369360': power_balance"
can you please advise?
The problem is quadtratic and you can find the code below.
#Pyomo Model2Block1
#Model
model2=ConcreteModel(name="DayAhead Market Clearing Price Forecasting2")
model2.dual = Suffix(direction=Suffix.IMPORT)
#Decision Variables
model2.cgt=Var(G,T,within=NonNegativeReals)
model2.pgt=Var(G,T,within=NonNegativeReals)
#Other Variables
model2.ugt=Var(G,T,within=Binary)
#Parameters
model2.Pgmax=Param(G,initialize=Pmax)
model2.Pgmin=Param(G,initialize=Pmin)
model2.RESt=Param(T,initialize=RES_Load)
model2.BorderSchedule=Param(T,initialize=Border_schedule) # IMPORTS MINUS EXPORTS
model2.Loadt=Param(T,initialize=Load)
model2.Ag=Param(G,initialize=Ag)
model2.Bg=Param(G,initialize=Bg)
#fixing the noncontinuous variables as per the results of the first solution of the problem
for idx in Ugt_solution.keys():
model2.ugt[idx].fix(Ugt_solution[idx])
for idx in PgtFixed.keys():
model2.pgt[idx].fix(PgtFixed[idx])
#equations
def obj_rule(model2):
return sum(model2.cgt[g,t]*model2.pgt[g,t] for t in T for g in G)
model2.obj=Objective(rule=obj_rule,sense=minimize)
def power_limit_low(model2,g,t):
return model2.Pgmin[g]*model2.ugt[g,t]<= model2.pgt[g,t]
model2.power_limit_low=Constraint(G,T,rule=power_limit_low)
def power_limit_high(model2,g,t):
return model2.pgt[g,t]<= model2.Pgmax[g]*model2.ugt[g,t]
model2.power_limit_high=Constraint(G,T,rule=power_limit_high)
def power_balance(model2,t):
return sum(model2.pgt[g,t] for g in G)+model2.RESt[t]+model2.BorderSchedule[t]==model2.Loadt[t]
model2.power_balance=Constraint(T,rule=power_balance)
def linear_expressions(model2,g,t):
return model2.cgt[g,t]==model2.Ag[g]*model2.pgt[g,t]+model2.Bg[g]
model2.linear_expressions=Constraint(G,T,rule=linear_expressions)
Thank you in advance. Please note once again that the problem is optimized succesfully and the problem is the retrieval of the duals.

Hi Panagiotis,
The KeyError does not seem to originate from Pyomo or Gurobi, it seems to be come from the data structures you obtain (dicts, pandas dataframe). Did you check how the data structures (and their contents) look like?
Best regards,
Mario0 
Hello,
I am facing the same problem and can't the dual value that I get as None for all constraints, I am wondering after:
model.dual = Suffix(direction=Suffix.IMPORT)
How should I exactly extract these dual variables? I found the following code:
model.dual_val = model.dual.get(model.c)
which gives me an error for the constraint index!
I appreciate your comments.
0 
Hi Shahin,
The constraint indices are created and maintained by Pyomo, not by Gurobi.
From the Gurobi side, you need to ensure that the optimization result is "optimal" to obtain the dual values.Mario
0
Please sign in to leave a comment.
Comments
3 comments