Python 双变量在Pyomo中不返回任何内容

Python 双变量在Pyomo中不返回任何内容,python,pyomo,Python,Pyomo,我有一个关于pyomo双变量检索的问题。我试图从约束“model.market\u clearing\u const”中检索双变量。但是,在运行以下代码后,我得到的错误是: id为“288675528520”的组件:市场结算成本[0] 所以,基本上,如果我查看模型下的双组件,我会看到后缀=0。这意味着没有双变量存储在后缀中,尽管我已经声明了它。我的解决方案是可行的 在此方面的任何投入都将不胜感激!多谢各位 import pandas as pd from pyomo.environ import

我有一个关于pyomo双变量检索的问题。我试图从约束“model.market\u clearing\u const”中检索双变量。但是,在运行以下代码后,我得到的错误是:

id为“288675528520”的组件:市场结算成本[0]

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

在此方面的任何投入都将不胜感激!多谢各位

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]]
将熊猫作为pd导入
从pyomo.environ导入*
从pyomo.opt导入SolverFactory
将numpy作为np导入
模型=具体模型(名称=“单位承诺”)
EGU_temp=pd.read_excel('cost_uc.xlsx','EGUs')
需求温度=pd.read\u excel('cost\u uc.xlsx','demand')
wind\u temp=pd.read\u excel('cost\u uc.xlsx','wind')
#计算总可变成本
EGU温度['tvc']=EGU温度['fuelcost']*EGU温度['heatrate']/1000+EGU温度['vom']
EGU=EGU温度到dict()
需求=需求温度到需求温度()
风=风的温度
#定义集:
U=列表(范围(10))#单位集
H=列表(范围(24))#小时集
#定义参数:
pmax=EGU['pmax']
pmin=EGU['pmin']
tvc=EGU['tvc']
启动成本=EGU[“启动成本”]
负载=需求[“负载”]
#定义变量:
模型x=Var(U,H,in=nonnegativereleases)
model.v=Var(U,H,in=Binary)
model.vU=Var(U,H,in=Binary)
model.vD=Var(U,H,in=Binary)
#定义目标函数:
def obj_功能(型号):
返回和(vU[u,h]型*u在u中的启动成本[u]h在h中的启动成本[u]\
+总和(x型[u,h]*tvc[u]表示u中u表示h中h)
model.obj_func=目标(规则=obj_函数)
#定义约束:
def上限(型号,u,h):
返回模型.x[u,h]=model.v[u,h]*pmin[u]
model.lb=约束(U,H,规则=下限)
#市场结算条件:
def市场清算(h型):
返回和(u中u的x型[u,h])=负载[h]
model.market\u clearing\u const=约束(H,规则=市场\u clearing)
#启用/关闭约束:
def on_off(型号,u,h):
如果h==0:
返回约束。跳过
其他:
返回model.v[u,h]==model.v[u,h-1]+model.vU[u,h]-model.vD[u,h]
model.onoff\U const=约束(U,H,rule=on\U off)
#求解模型并报告结果:
model.dual=后缀(方向=后缀.IMPORT\u EXPORT)
解算器=解算器工厂(“glpk”)
结果=解算器.solve(模型,T=True)
model.pprint()
如果(results.solver.status==SolverStatus.ok)和(results.solver.termination_condition==TerminationCondition.optimal):
打印('解决方案可行')
elif(results.solver.termination_condition==TerminationCondition.infactible):
打印('解决方案不可行')
其他:
#还有别的问题
打印('解算器状态:',results.Solver.Status)
打印('总成本:',值(model.obj_func))
#打印x结果:
对于u中的u:
对于h中的h:
打印(“:”,值(model.x[u,h]))
#打印影子价格(双变量):
价格=单位=np.0(24)
对于h中的h:
价格[h]=模型.双[模型.市场结算成本[h]]

对偶仅适用于纯连续LP模型。您有二进制变量,使模型成为MIP。MIP模型没有对偶