使用优化模块的Scipy错误.无法将数组转换为fortran [英] Scipy error using optimization module. Failure converting array to fortran

查看:99
本文介绍了使用优化模块的Scipy错误.无法将数组转换为fortran的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用scipy的优化模块来查找使用slsqp的功能的最小值,而我遇到了一些问题.调用该函数的实际代码如下所示:

Trying to use scipy's optimize module to find the minimum of a function using slsqp and I'm running into some issues. The actual code calling the function is shown below:

def minimizeWebEnergyLost(x, parameters):
    """values = [theta, velocity]"""
    firstTerm = lambda values: (x * values[1]**2 / 2.0)
    sqrtTerm = lambda values: np.sqrt((parameters.gravity**2 * x**2) / (4 * values[1]**4 * np.cos(values[0])**4) + 1)
    secondTerm = lambda values: (values[1]**4 * np.cos(values[0])**2) / parameters.gravity
    arcsinhTerm = lambda values: np.arcsinh((parameters.gravity * x) / (2 * values[1]**2 * np.cos(values[0])**2))
    costFunction = lambda values: firstTerm(values)*sqrtTerm(values)+secondTerm(values)*arcsinhTerm(values)
    bounds = ((-math.pi/2,math.pi/2),(0,parameters.maxSlingSpeed))
    minimum = minimize(costFunction, (pi/4, 20), method="SLSQP", bounds=bounds)
    return minimum

由于某种原因,我得到的错误如下:

For some reason the error I'm getting is as follows:

_slsqp.error: failed in converting 8th argument `g' of _slsqp.slsqp to C/Fortran array

不太确定这里发生了什么,但是我可以提出更多可能有用的代码.

Not quite sure what's going on hre, but I can put up any more code that might be useful if it helps.

推荐答案

当用户指定的目标函数未返回标量时,将发生此错误.现在, b-carter

This cryptic error occurs when the user specified objective function does not returning a scalar. A clear error message is now returned in a patch by b-carter,

"Objective function must return a scalar" 

并且文档已更新,请参见线程进行讨论.

and the documentation is updated, see this thread for discussion.

这篇关于使用优化模块的Scipy错误.无法将数组转换为fortran的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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