python - 变量 [t]-Pyomo 的未初始化 NumericValue 对象没有值
问题描述
我是 Pyomo 的新手,并试图找到一种方法来优化我的电池算法,其中包含未定义的电池容量 (Smax) 和逆变器尺寸 (Rmax)。
我的目标是找到最小化电池总充电成本的 Smax 和 Rmax 组合。
但我收到此错误:“未初始化的 NumericValue 对象 Ein[0] 没有值”
这是我的代码:
################################################# ###################
model = ConcreteModel()
# Define model parameters
model.T = Set(doc='hour of the period', initialize=df.hour.tolist(), ordered=True)
model.Rmax = Var( within=PositiveIntegers )
model.Smax = Var( within=PositiveIntegers )
model.Ein = Var(model.T, bounds = (0, None)) " Energy charged in battery at period t"
model.Eout = Var(model.T, bounds = (0, None)) "Energy discharged from battery at period t"
model.Z = Var(model.T, bounds = (0, None)) "Energy stored in battery at the end of the hour
model.L = Var(model.T) "Energy bought from the grid at every period t"
# DEFINE ALL CONSTRAINTS OF THE MODEL
"Initial storage state = 0 and constraint on SOC flow"
def storage_state(model, t):
if t == model.T.first():
return model.Z[t] == 0
else:
return (model.Z[t] == (model.Z[t-1] + (model.Ein[t]) - (model.Eout[t])))
model.charge_state = Constraint(model.T, rule=storage_state)
"Max SOC constraint"
def SOC_maxconstraint(model, t):
return model.Z[t] <= model.Smax
model.SOC_maxconstraint = Constraint(model.T, rule=SOC_maxconstraint)
"Min SOC constraint"
def SOC_minconstraint(model, t):
return model.Z[t] >= 0
model.SOC_minconstraint = Constraint(model.T, rule=SOC_minconstraint)
"Maximum dischage within a single hour"
def discharge_constraint(model, t):
return model.Eout[t] <= model.Rmax
model.discharge = Constraint(model.T, rule=discharge_constraint)
"Maximum charge within a single hour"
def charge_constraint(model, t):
return model.Ein[t] <= model.Rmax
model.charge = Constraint(model.T, rule=charge_constraint)
"Limit discharge to the amount of the charge in the battery"
def positive_charge(model, t):
return model.Eout[t] <= model.Z[t]
model.positive_charge = Constraint(model.T, rule=positive_charge)
"Demand of energy constraint"
def demand_constraint(model, t):
return (model.L[t] == (df.loc[t, 'MktDemand'] + (model.Ein[t]) - (model.Eout[t])))
model.demand_constraint = Constraint(model.T, rule=demand_constraint)
"Non-negativity of total demand"
def negativity_demand_constraint(model, t):
return model.L[t] >= 0
model.negativity_demand_constraint = Constraint(model.T, rule=negativity_demand_constraint)
## Objective function
original_costs = sum(df.loc[t, 'TOU'] * df.loc[t, 'MktDemand'] for t in model.T)
model.costs = sum(df.loc[t, 'TOU'] * (df.loc[t, 'MktDemand'] + (model.Ein[t]) - (model.Eout[t]))
for t in model.T)
model.objective = Objective(expr=model.costs, sense=minimize)
results = SolverFactory('glpk').solve(model)
print("Profits % =", round((model.objective() / original_costs ) * 100,2))
print("Smax:", model.Smax.get_values().values())
print("Rmax:", model.Rmax.get_values().values())
我得到:
pyomo\core\expr\numvalue.pyx in pyomo.core.expr.numvalue.value()
ValueError: No value for uninitialized NumericValue object Ein[0]
你们知道我做错了什么吗?
谢谢!
解决方案
通常这意味着您的求解器没有得到结果。所以基本上经常如果这是错误,你的模型是不可行的。
也许尝试使用 tee=True 查看求解器信息。
求解器 = SolverFactory('glpk')
结果=solver.solve(模型,三通=真)
如果它表明您的模型不可行,那么您的约束或界限或其他东西有问题(我没有看方程式)
推荐阅读
- c++ - `std::chrono` 时钟与`boost::xtime` 的比较
- python - Python for循环:执行第二行覆盖在第一行创建的对象
- android - 无法在 Intellij 上启动 android
- c# - 如何自定义 TabbedPage 标签栏?
- c# - 错误 CS0115: 'PopupField
.ExecuteDefaultAction(EventBase)':找不到合适的方法来覆盖 - powershell - 如何只允许某些 PowerShell 命令在 PowerShell 上运行?
- r - 本地图标未加载,r 传单中的空图像
- git - 是否可以使用其他私有存储库(bitbucket、github、gitlab)而不是 Azure API 管理器提供的一个?
- elasticsearch - ElasticSearch:查询是否需要专门使用后缀别名来处理特定字段?
- regex - 如果时间戳不存在,有没有办法将秒添加到时间戳中?