双变量在 Pyomo 中没有任何回报 [英] Dual Variable Returns Nothing in Pyomo

查看:43
本文介绍了双变量在 Pyomo 中没有任何回报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 pyomo 双变量检索的问题.我正在尝试从约束model.market_clearing_const"中检索对偶变量.但是,在运行这些代码之后,我得到的错误是:

I have a question regarding to pyomo dual variable retrieval. I'm trying to retrieve the dual variable from the constraint "model.market_clearing_const". However, after running these codes folowing, the error I got is:

ID 为2886755528520"的组件:market_clearing_const[0]"

"Component with id '2886755528520': market_clearing_const[0]"

所以基本上,如果我查看模型下的双组件,我会看到后缀=0.所以这意味着没有双重变量存储在后缀中,尽管我已经声明了它.我的解决方案是可行的.

So basically if I look into the dual component under model, I see suffix=0. So this means there's no dual variables are stored in suffix despite the fact that I already declare it. And my solution is feasible.

对此的任何输入表示赞赏!谢谢.

Any inputs on this are appreciated! Thank you.

import pandas as pd
from pyomo.environ import *
from pyomo.opt import SolverFactory
import numpy as np

model = ConcreteModel(name="Unit_Commitment")

EGU_temp = pd.read_excel('cost_uc.xlsx','EGUs')
demand_temp = pd.read_excel('cost_uc.xlsx','demand')
wind_temp = pd.read_excel('cost_uc.xlsx','wind')

# Calculate total variable cost cost
EGU_temp['tvc'] = EGU_temp['fuelcost']*EGU_temp['heatrate']/1000 + EGU_temp['vom']

EGU = EGU_temp.to_dict()
demand = demand_temp.to_dict()
wind = wind_temp.to_dict()

# Define set:
U = list(range(10))  # Set of Units
H = list(range(24))  # Set of Hours

# Define parameters:
pmax = EGU['pmax']
pmin = EGU['pmin']
tvc = EGU['tvc']
startup_cost = EGU['startupcost']
load = demand['load']


# Define variables:
model.x = Var(U, H, within=NonNegativeReals)
model.v = Var(U, H, within=Binary)
model.vU = Var(U, H, within=Binary)
model.vD = Var(U, H, within=Binary)

# Define Objective function:
def obj_function(model):
    return sum(model.vU[u, h]*startup_cost[u] for u in U for h in H)\
           + sum(model.x[u, h]*tvc[u] for u in U for h in H)


model.obj_func = Objective(rule=obj_function)


# Define constraints:
def upperbound(model,u,h):
    return model.x[u,h] <= model.v[u, h]*pmax[u]


model.ub = Constraint(U,H, rule=upperbound)

def lowerbound(model,u,h):
    return model.x[u,h] >= model.v[u,h]*pmin[u]


model.lb = Constraint(U,H, rule=lowerbound)

# Market Clearing condition:
def market_clearing(model, h):
    return sum(model.x[u, h] for u in U) == load[h]


model.market_clearing_const = Constraint(H, rule=market_clearing)

# Turn on/shut down constraints:
def on_off(model,u,h):
    if h == 0:
        return Constraint.Skip
    else:
        return model.v[u, h] == model.v[u, h - 1] + model.vU[u, h] - model.vD[u, h]

model.onoff_const = Constraint(U,H, rule=on_off)

# Solve the model and report results:
model.dual = Suffix(direction=Suffix.IMPORT_EXPORT)
solver = SolverFactory('glpk')
results = solver.solve(model, tee=True)
model.pprint()


if (results.solver.status == SolverStatus.ok) and (results.solver.termination_condition == TerminationCondition.optimal):
    print('Solution is feasible')
elif (results.solver.termination_condition == TerminationCondition.infeasible):
    print('Solution is infeasible')
else:
    # Something else is wrong
    print('Solver Status: ',  results.solver.status)


print('Total Cost:', value(model.obj_func))

# Print x results:
for u in U:
    for h in H:
        print('<unit '+str(u+1), 'in hour '+str(h+1)+'>:', value(model.x[u, h]))

# Print shadow price (dual variable):
price = unit_1 = np.zeros(24)
for h in H:
    price[h] = model.dual[model.market_clearing_const[h]]

推荐答案

对偶只存在于纯连续 LP 模型中.您有二进制变量,使模型成为 MIP.MIP 模型没有对偶.

Duals only exist for pure continuous LP models. You have binary variables, making the model a MIP. MIP models don't have duals.

这篇关于双变量在 Pyomo 中没有任何回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆