查找Pyomo提供的数学函数 [英] Finding Pyomo provided math functions

查看:301
本文介绍了查找Pyomo提供的数学函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Pyomo中,我想解决以下最小化问题,该问题与两个音频文件的播放速率和定时相匹配:

In Pyomo I want to solve the following minimization problem, which matches the playback rate and timing of two audio files:

import pyomo
from pyomo.environ import *
from pyomo.opt import SolverFactory
import soundfile as sf
import math
import numpy

#Create Model:
model = ConcreteModel()

#Declare Variables:
model.P = Var(initialize = 1, within=PositiveReals, bounds = (0.5,2))
model.T = Var(initialize = 0, within=Reals, bounds = (-500,500))


#Objective function:
def shift(data,rate,t,point):
    Y=numpy.zeros(len(data))
    for i in range(len(data)):
        for j in range(point):
            if math.floor(i*rate+t-j+point/2)>=0 and math.floor(i*rate+t-j+point/2)<=len(data)-1:
                Y[i]+=data[math.floor(i*rate+t-j+point/2)]*numpy.sinc((math.floor(i*rate+t)-i*rate-t)-(j-point/2))
    return Y

def obj_fun(model):
#Read data from audiofile:
    Audio1, samplerate = sf.read('C:/Audio1.wav')
    Audio2, samplerate = sf.read('C:/Audio2.wav')
    Audio1=Audio1[:,0]
    Audio2=Audio2[:,0]

    Audio2= numpy.pad(Audio2,(0,len(Audio1)-len(Audio2)),'constant', constant_values=(0, 0))
    return -1*numpy.sum(shift(Audio2,model.P,model.T,4)*Audio1)




#Create obj function:
model.obj = Objective(rule=obj_fun, sense=1)

问题是以下错误:

    TypeError: Implicit conversion of Pyomo NumericValue type `<class 'pyomo.core.base.expr_coopr3._SumExpression'>' to a float is
disabled. This error is often the result of using Pyomo components as
arguments to one of the Python built-in math module functions when
defining expressions. Avoid this error by using Pyomo-provided math
functions.

除非更改了numpy/math的正弦和底函数,否则求解器将无法运行.但是,在Pyomo的文档中,没有引用Pyomo提供的功能.为了使创建的模型对象没有错误,我该如何更改标准的sinc/floor函数?

The solver can't function unless numpy's/math's sinc and floor functions are changed. However, in Pyomo's documentation there is no reference to Pyomo-provided functions. What do I change the standard sinc/floor functions with in order to get the model object created without errors?

我也想知道是否存在一种更智能的方式来将数据读取到模型中.到目前为止,在目标函数的每次遍历中都不必要读取它.

Also I am wondering whether there is a smarter way to read the data into the model. As of now, it's unnecessarily read on every pass of the objective function.

推荐答案

Pyomo提供的内在函数(sin,log等)会自动通过行from pyomo.environ import *导入.问题是您要在该行之后导入数学,该行会用数学库中的内在函数覆盖Pyomo的内在函数.解决方案是完全删除数学导入语句,或者将数学导入语句移到pyomo导入语句上方.

Pyomo provided intrinsic functions (sin, log, etc.) are automatically imported with the line from pyomo.environ import *. The problem is that you are importing math after that line which overwrites Pyomo's intrinsic functions with the ones from the math library. The solution is to remove the math import statement entirely or to move the math import statement above the pyomo import statement.

这篇关于查找Pyomo提供的数学函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆