简体   繁体   English

Pyomo,TypeError:'numpy.ndarray' 对象不可调用

[英]Pyomo, TypeError: 'numpy.ndarray' object is not callable

I am trying to use scipy commands in Pyomo , an optimizer based in Python.我正在尝试在基于 Python 的优化器Pyomo 中使用 scipy 命令。

My goal is to develop an optimal trajectory for aircraft in the presence of a wind field.我的目标是在存在风场的情况下为飞机开发最佳轨迹。 I have data measurements at grid points.我在网格点有数据测量。 I have used scipy to generate the interpolation as我已经使用 scipy 生成插值作为

xi, yi = np.linspace(X2.astype('float').min(), X2.astype('float').max(), 100), np.linspace(Y2.astype('float').min(), Y2.astype('float').max(), 100)
xi, yi = np.meshgrid(xi, yi)

# Interpolate
rbfX = sp.interpolate.Rbf(X2, Y2, Wx2, function='multiquadric')
zXi = rbfX(xi, yi)

being sp how I call scipy.作为 sp 我如何称呼 scipy。 This is the way I have to calculate the speed of wind at any point.这就是我必须在任何时候计算风速的方式。 I am interpolating the windspeed.我正在插入风速。

Then, inside of the pyomo part, I write然后,在 pyomo 部分,我写

def Wind_lammda_definition1(model, i):
    return m.Wind_lammda[i,1] ==zXi(m.lammda[i,1], m.phi[i,1])
m.Wind_lammda_const1 = Constraint(m.N, rule = Wind_lammda_definition1)

being m.lammda and m.phi the position of the airplane. m.lammda 和 m.phi 是飞机的位置。

Unfortunately, once I run the code, I get rhe following error:不幸的是,一旦我运行代码,就会出现以下错误:

    return m.Wind_lammda[i,1] ==zXi(m.lammda[i,1], m.phi[i,1])

TypeError: 'numpy.ndarray' object is not callable

I have checked this and this threads and as far as I see, this is an error message which appears if there are syntax errors.我已经检查了这个这个线程,据我所知,这是一条错误消息,如果有语法错误就会出现。 However, I haven't found any and I think that this happens becasue I cannot port scipy into Pyomo.但是,我还没有找到,我认为这是因为我无法将 scipy 移植到 Pyomo 中。 Is this true or can it be fixed?这是真的还是可以修复?

Let's setup a Rbf object:让我们设置一个Rbf对象:

In [177]: X,Y = np.meshgrid(np.arange(4),np.arange(4))
In [178]: Z = np.sin(X-Y)
In [179]: fn = interpolate.Rbf(X,Y,Z)
In [180]: type(fn)
Out[180]: scipy.interpolate.rbf.Rbf

That's like your rbfX .这就像你的rbfX According to the docs it is callable , that is it has a __call__ method.根据文档,它是callable ,也就是说它有一个__call__方法。

If we call it with 2 scalars, we get a single element array:如果我们用 2 个标量调用它,我们会得到一个单元素数组:

In [181]: fn(1.5, 2.3)
Out[181]: array(-0.73094599)

Call it with a pair of arrays, we get an array with matching size:用一对数组调用它,我们得到一个大小匹配的数组:

In [184]: fn(np.arange(1,3,.5), np.arange(0,2,.5))
Out[184]: array([0.84147098, 0.85639014, 0.84147098, 0.85639014])

This is what your zXi .这就是你的zXi Look at its type , shape , and dtype .查看它的typeshapedtype It's an array.这是一个数组。 It cannot be "called", and there are specific rules as to how it can be indexed.它不能被“调用”,关于如何索引它有特定的规则。

zXi(m.lammda[i,1], m.phi[i,1]) is wrong because it tries to "call" a numpy array. zXi(m.lammda[i,1], m.phi[i,1])是错误的,因为它试图“调用”一个 numpy 数组。 But zXi[m.lammda[i,1], m.phi[i,1]] apparently has invalid indices, quite possibly floats.但是zXi[m.lammda[i,1], m.phi[i,1]]显然有无效的索引,很可能是浮动的。 I haven't examined what they are.我没有检查它们是什么。

rbfX(m.lammda[i,1], m.phi[i,1]) might work if the 2 arguments are meant to be interpolation points. rbfX(m.lammda[i,1], m.phi[i,1])如果这两个参数是插值点可能会起作用。

zXi = rbfX(xi, yi) creates an array (probably 2d (100,100)) that contains values interpolated at the xi,yi mesh. zXi = rbfX(xi, yi)创建一个数组(可能是 2d (100,100)),其中包含在xi,yi网格处内插的值。 You can pick an element, eg zXi[50,50] or array of values.您可以选择一个元素,例如zXi[50,50]或值数组。 But you can't treat it like the interpolation function, giving it float values.但是你不能把它当作插值函数,给它浮点值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM