Pyomo 能源系统模型:ValueError:未初始化的 NumericValue 对象没有值

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

我目前正在尝试使用 V2G 存储来建模能源系统模型,作为我第一次使用 pyomo。该模型的目标是在满足电力需求的同时最大限度地降低成本。这可以通过太阳能、陆上风能和海上风能的间歇性发电或通过电动汽车放电来实现。我附上了模型方程的图片。 Model equations 但是,我的代码遇到了一些问题并收到错误代码:

289093 lines were written
GLPK Simplex Optimizer 5.0
52562 rows, 35044 columns, 140160 non-zeros
Preprocessing...
26279 rows, 26280 columns, 105113 non-zeros
Scaling...
 A: min|aij| =  2.860e-05  max|aij| =  1.107e+00  ratio =  3.872e+04
GM: min|aij| =  7.727e-03  max|aij| =  1.294e+02  ratio =  1.675e+04
EQ: min|aij| =  5.971e-05  max|aij| =  1.000e+00  ratio =  1.675e+04
Constructing initial basis...
Size of triangular part is 26279
      0: obj =   8.061601191e+09 inf =   1.219e+07 (23830)
   7587: obj =   5.758397626e+09 inf =   6.047e+06 (16900) 69
  14917: obj =   5.758397626e+09 inf =   3.688e+06 (11839) 29
LP HAS NO PRIMAL FEASIBLE SOLUTION
glp_simplex: unable to recover undefined or non-optimal solution
If you need actual output for non-optimal solution, use --nopresol
...
N_of = 0
ERROR: evaluating object as numeric value: V_discharge[0]
        (object: <class 'pyomo.core.base.var._GeneralVarData'>)
    No value for uninitialized NumericValue object V_discharge[0]
Output exceeds the size limit. Open the full output data in a text editor---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_15212\450560137.py in 
     80 print("N_on =", model.N_on())
     81 print("N_of =", model.N_of())
---> 82 print("Z =", model.Z())
     83 

c:\Users\emok\Anaconda3\lib\site-packages\pyomo\core\base\expression.py in __call__(self, exception)
     59         if self.expr is None:
     60             return None
---> 61         return self.expr(exception=exception)
     62 
     63     def is_named_expression_type(self):

c:\Users\emok\Anaconda3\lib\site-packages\pyomo\core\expr\base.py in __call__(self, exception)
    113 
    114         """
--> 115         return evaluate_expression(self, exception)
    116 
    117     def __str__(self):

c:\Users\emok\Anaconda3\lib\site-packages\pyomo\core\expr\visitor.py in evaluate_expression(exp, exception, constant)
   1240 
   1241     try:
...
pyomo\core\expr\numvalue.pyx in pyomo.core.expr.numvalue.value()

pyomo\core\expr\numvalue.pyx in pyomo.core.expr.numvalue.value()

ValueError: No value for uninitialized NumericValue object V_discharge[0]

当我初始化 V_discharge 时,我的所有结果都只是 0。我感觉问题与模型的动态方面有关,就好像它没有正确更新变量 V(电动汽车中的能量存储)一样。此外,我已经成功解决了没有存储选项的静态模型。

因此,我有以下疑问:

  1. 我对于问题根源的结论是否正确?
  2. 如何解决这个问题?
  3. 我的代码还有其他问题吗?

我们将不胜感激所有帮助。

import pandas as pd
from pyomo.environ import \*
from pyomo.opt import SolverFactory
from pyomo.core import Constraint

# Load data into a Pandas DataFrame
data = pd.read_csv(r'D:\data\data.csv')

# Create a Pyomo model
model = ConcreteModel()

# Define the sets
model.hours = Set(initialize=data.index, doc='Set of hours')

# Define the parameters
model.k_ipv = Param(default=40)
model.k_ion = Param(default=30)
model.k_iof = Param(default=46)
model.k_vv2g = Param(default=226.56)
model.eta_charge = Param (default=0.924)
model.eta_discharge = Param(default=0.903)
model.E_s = Param(default=160425)
model.alpha = Param(default=0.5)

#Define data
model.CF_pv = Param(model.hours, initialize=data['CF_pv'].to_dict())
model.CF_on = Param(model.hours, initialize=data['CF_on'].to_dict())
model.CF_of = Param(model.hours, initialize=data['CF_of'].to_dict())
model.pv2g = Param(model.hours, initialize=data['pv2g'].to_dict())
model.d_ev = Param(model.hours, initialize=data['d_ev'].to_dict())

# Define the variables
model.N_pv = Var(domain=NonNegativeReals, initialize=0)
model.N_on = Var(domain=NonNegativeReals, initialize=0)
model.N_of = Var(domain=NonNegativeReals, initialize=0)
model.V = Var(model.hours, domain=NonNegativeReals)
model.V_charge = Var(model.hours, domain=NonNegativeReals)
model.V_discharge = Var(model.hours, domain=NonNegativeReals)
model.C = Var(model.hours, domain=NonNegativeReals)

# Market balance constraints
model.d1 = Constraint(model.hours, rule=lambda model, h: model.CF_pv[h] * model.N_pv +
                                                   model.CF_on[h] * model.N_on +
                                                   model.CF_of[h] * model.N_of + model.V_discharge[h] == data['d'][h])

model.d2 = Constraint(model.hours, rule=lambda model, h: model.CF_pv[h] * model.N_pv +
                                                   model.CF_on[h] * model.N_on +
                                                   model.CF_of[h] * model.N_of == data['d'][h] + model.V_charge[h] + model.C[h])


#State of charge constraint
def s1_constraint(model,h):
    if h == model.hours.first():
        return model.V[h] == model.alpha*model.E_s / 2
    else: 
        return model.V[h] == model.V[h-1] + model.eta_charge * model.V_charge[h] - model.V_discharge[h]/model.eta_discharge - data['d_ev'][h]
model.s1 = Constraint(model.hours, rule = s1_constraint)

"""No free lunch"""
model.s2 = Constraint(rule=lambda model:  model.V[model.hours.first()] == model.V[model.hours.last()])

"""Maximum energy storage must be less or equal to capacity"""
model.s3 = Constraint(model.hours, rule=lambda model, h: model.V[h] <= model.alpha*model.E_s)

# Charging and discharging constraints
""" Maximum discharge rate within a single hour """
model.disc1 = Constraint(model.hours, rule=lambda model, h: model.V_discharge[h] <= model.alpha*data['pv2g'][h])

""" Maximum charge rate within a single hour """
model.char1 = Constraint(model.hours, rule=lambda model, h: model.V_charge[h] <= model.alpha*data['pv2g'][h])

# Define objective function
def obj_expression(model):
    return model.k_ipv * model.N_pv + model.k_ion * model.N_on + model.k_iof * model.N_of + model.k_vv2g * sum(model.V_discharge[h] for h in model.hours)

model.Z = Objective(expr=obj_expression(model), sense=minimize) # Use the defined objective expression with the 'h' variable

# Solve the optimization problem
solver = SolverFactory('glpk')
#solver.solve(model)
results = solver.solve(model,tee=True)

# Access the solution
print("N_pv =", model.N_pv())
print("N_on =", model.N_on())
print("N_of =", model.N_of())
print("Z =", model.Z())
python optimization model pyomo energy
2个回答
0
投票

您收到的错误在报告中显示“LP HAS NO PRIMAL FEASIBLE SOLUTION”,这是关键问题。你所写的模型是不可行的。我认为你的表述在几个方面都值得怀疑。需要考虑的一些事项:

  1. 在约束 d1、d2...如果你从其中一个方程中减去另一个方程,你会得到:

    v_discharge = -v_charge - c

只有当它们都为零时才能为真,因为您将它们定义为非负数。所以你的充放电符号约定有问题。

    我不确定你为什么需要 d2。通常你只需要覆盖流量平衡的需求方,我认为:
  1. pv + solar + discharge - charge + outside_source >= demand

    
    

  2. 你的 d1 约束有问题。通常,这应该是大于或等于约束,而不是相等。如果任何特定时间的需求为零会发生什么?然后,您将所有其他变量强制为零......
  3. 不清楚
model.C

是什么。您应该评论

所有
您的变量... 如果您在处理公式后仍然遇到困难,您应该使用重新公式编辑您的帖子,并剪切并粘贴 .csv 文件的大约 10 行左右,以便可以重现您的错误。


0
投票

© www.soinside.com 2019 - 2024. All rights reserved.