【问题标题】:Error TypeError loop of ufunc does not support argument 0 pyomo [closed]ufunc的错误类型错误循环不支持参数0 pyomo [关闭]
【发布时间】:2021-12-16 00:47:33
【问题描述】:

创建于 2021 年 10 月 3 日星期日 19:48:52

Intento resolver un problema de optimizacion para obtener la nota media en 3 asignaturas, en el que g=estimacion de nota por asignatura, x=horas dedicadas y s=coeficiente

model = pyo.ConcreteModel()
model.g= pyo.Var(range(3),domain=pyo.NonNegativeReals)
model.x=pyo.Var(range(3),domain=pyo.NonNegativeReals)
model.s=pyo.Var(range(3),domain=pyo.NonNegativeReals)

# Define objective function
model.obj = pyo.Objective(expr= (model.g[0]+model.g[1]+model.g[2])/3, sense=pyo.maximize)

"""# Define constraints"""

model.con1 = pyo.Constraint( expr= model.g[0]==-10*np.log(1+model.s[0]*model.x[0]))

model.con2 = pyo.Constraint( expr= model.g[1]==-10*np.log(1+model.s[1]*model.x[1]))

model.con3 = pyo.Constraint( expr= model.g[2]==-10*np.log(1+model.s[2]*model.x[2]))

model.con4 = pyo.Constraint( expr=model.g[0]<=10)

model.con5 = pyo.Constraint( expr=model.g[1]<=10)

model.con6 = pyo.Constraint( expr=model.g[2]<=10)

model.con7 = pyo.Constraint( expr=model.g[2]<=10)

model.con8 = pyo.Constraint( expr=model.s[0]==0.01)

model.con9= pyo.Constraint( expr=model.s[1]==0.015)

model.con10= pyo.Constraint( expr=model.s[2]==0.02)

model.con11 = pyo.Constraint( expr=model.x[0]+model.x[1]+model.x[2]<=48.0)

Me da el siguiente 错误:

Traceback (most recent call last):

  File "/home/alumno/anaconda3/envs/optimizacion2/Optimizacion_practica2.py", line 22, in <module>
    model.con1 = pyo.Constraint( expr= model.g[0]==-10*np.log(1+model.s[0]*model.x[0]))

  File "pyomo/core/expr/numvalue.pyx", line 909, in pyomo.core.expr.numvalue.NumericValue.__array_ufunc__

  File "pyomo/core/expr/numvalue.pyx", line 1004, in pyomo.core.expr.numvalue.NumericNDArray.__array_ufunc__

TypeError: loop of ufunc does not support argument 0 of type SumExpression which has no callable log method

¿SABE ALGUIEN COMO PODRIA SOLUCIONARLO PORFAVOR?

【问题讨论】:

  • 请将您的问题翻译成英文!

标签: python numpy optimization pyomo logarithm


【解决方案1】:

Pyomo 依靠运算符重载和标准(数学)函数的自定义实现来构建表达式树,以表示每个约束和目标的符号表达式。因此,您需要使用函数的 Pyomo 实现(可通过 pyomo.environ 获得),例如:

model.con1 = pyo.Constraint( expr= model.g[0]==-10*pyo.log(1+model.s[0]*model.x[0]))

使用 numpy 或 math 中的函数将尝试立即计算数值表达式,并且通常会导致 Pyomo 引发异常:如果立即计算表达式,则约束看到的唯一内容就是结果,并且约束需要 symbolic 表达式发送到优化求解器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-29
    • 2021-12-07
    • 2021-02-14
    • 2022-12-17
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 2014-03-25
    相关资源
    最近更新 更多