无法使用 Gurobi 获取 Pyomo 中约束的对偶变量

问题描述 投票:0回答:0

我有一个二次非凸目标函数的最小化问题。

约束之一是电力平衡(能源市场),我想访问它的对偶变量。

如 Gurobi 求解器文档的 PI 部分所述,我首先求解了模型(成功完成),然后修复了非连续变量(在我的案例中为二进制变量)并解决了问题,以便能够检索对偶.

我正在使用 Pyomo 作为建模框架。因此,正如 Pyomo 文档中所解释的那样,我已经确定了非连续变量值,在再次构建模型时输入

model.dual = pyo.Suffix(direction=pyo.Suffix.IMPORT)
,然后再次调用求解器以便能够检索对偶变量值然后解析模型(成功)并尝试获取约束“power_balance”的对偶变量值。

我当然首先定义了以下选项。

solver2=SolverFactory("gurobi")

solver2.options['NonConvex'] = 2

solver2.options['MIPGap']=0

solver2.options["QCPDual"] = 1

solver2.solve(model,tee=True)

模型再次求解,我尝试按照以下方式获得对偶的输出:

da= {'duals': model2.dual[model2.power_balance]}

duals = pd.DataFrame.from_dict([da]).T.rename(columns={0: "Marginal Price"})

print(duals)

然后我收到以下错误:

KeyError:“ID 为‘2613873369360’的组件:power_balance”

你能给我建议吗?

问题是二次的,非凸的,你可以在下面找到代码。

#Pyomo Model2-Block1

#Model
model2=ConcreteModel(name="Day-Ahead 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 non-continuous 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)

提前谢谢你。 请再次注意,问题优化成功,问题是对偶的检索。

python pyomo gurobi
© www.soinside.com 2019 - 2024. All rights reserved.