【问题标题】:Problems regarding Pyomo-provided math functions关于 Pyomo 提供的数学函数的问题
【发布时间】:2021-05-25 15:02:01
【问题描述】:

我正在尝试在 Python 中使用 Pyomo 解决双层问题。但是,当我尝试运行代码时,出现以下错误:

“Pyomo NumericValue 类型‘mon’到浮点数的隐式转换被禁用。这个错误通常是使用 Pyomo 组件作为 Python 内置数学模块函数之一的参数的结果,当 定义表达式。通过使用 Pyomo 提供的数学函数来避免这个错误。”

在 Pyomo 的文档中没有提到 Pyomo 提供的函数。我想知道如何修改显示的倒数第二行代码,使 model.rn[i,j] 满足整数要求?

以下是我的代码:

import random
import matplotlib.pyplot as plt
import numpy as np
from pyomo.environ import *
from pyomo.bilevel import *
from pyomo.bilevel.components import SubModel
from pyomo.opt import SolverFactory

capacity =[150,80, 65]

model = ConcreteModel()
model.sub = SubModel()

model.M=RangeSet(1,3)
model.N=RangeSet(1,12)
model.f= Param(model.M,model.N,within=NonNegativeIntegers,initialize=20)      
model.v= Param(model.M,model.N,within=NonNegativeIntegers)   

model.sub.x = Param(within=Binary)

model.r= Var(model.M,model.N,within=PercentFraction)                     
model.rp= Var(model.M,model.N,within=NonNegativeReals,bounds=(0, 10))     
model.rn = Var(model.M, model.N, within=NonNegativeIntegers)            
model.un= Var(model.M,model.N,within=NonNegativeIntegers)                

for j in range(1,13):
   model.v[1,j] = capacity[0]-model.f[1,j]
   model.v[2,j] = capacity[1]-model.f[2,j]
   model.v[3,j] = capacity[2]-model.f[3,j]

for j in range(1,13):
  for i in range(1,4):
     model.rn[i,j]=floor(model.v[i,j]*model.r[i,j])
     model.un[i,j]=model.v[i,j]-model.rn[i,j]

【问题讨论】:

    标签: python pyomo


    【解决方案1】:

    这很难做到。据我所知,它可能只适用于该 pyomo 对象的值,因为 model.r 是一个 pyomo 对象。这不是参数的问题,而是变量的问题。 您可能想写出模拟 python 'floor' 函数的约束。

    【讨论】:

    • 感谢您的 cmets。我通过定义一个函数解决了这个问题。
    猜你喜欢
    • 2018-06-18
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多