声明对流域优化的约束

时间:2015-07-07 19:50:21

标签: python numpy optimization scipy constraints

我在使用scipy.optimize.basinhopping为约束创建字典时遇到问题。我能够运行我的代码(没有约束),但答案没有意义,因为我需要强制执行一些约束。目前,我只是试图让一个约束工作,但对于最终的解决方案,我需要弄清楚如何实现几个约束。我现在的代码是:

x0 = [f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11]
args = arg1,arg2,arg3,arg4
def func(x,*args)
     #Do some math
     return result

#This is where I need help most
cons = {'type':'ineq','fun': lambda x: x[5]-x[4]}

minimizer_kwargs = {"method":"COBYLA","args":"args","constraints":"cons"}
ret = scipy.optimize.basinhopping(func,x0,minimizer_kwargs=minimizer_kwargs)

但尝试运行时会出现此错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 601, in runfile
    execfile(filename, namespace)
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 66, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/Python27/Scripts/SpectralResearch/mainScripts/main.py", line 121, in <module>
    ret = scipy.optimize.basinhopping(func,x0,minimizer_kwargs=minimizer_kwargs)
  File "C:\Python27\lib\site-packages\scipy\optimize\_basinhopping.py", line 605, in basinhopping
    accept_tests, disp=disp)
  File "C:\Python27\lib\site-packages\scipy\optimize\_basinhopping.py", line 72, in __init__
    minres = minimizer(self.x)
  File "C:\Python27\lib\site-packages\scipy\optimize\_basinhopping.py", line 279, in __call__
    return self.minimizer(self.func, x0, **self.kwargs)
  File "C:\Python27\lib\site-packages\scipy\optimize\_minimize.py", line 432, in minimize
    return _minimize_cobyla(fun, x0, args, constraints, **options)
  File "C:\Python27\lib\site-packages\scipy\optimize\cobyla.py", line 218, in _minimize_cobyla
    raise TypeError('Constraints must be defined using a '
TypeError: Constraints must be defined using a dictionary.

基本上我需要强制执行某些变量大于其他变量的约束。我一直在查看文档([1],[2])和文章,但没有找到任何有用的文档。我有什么想法可能做错了吗?

1 个答案:

答案 0 :(得分:2)

minimizer_kwargs = {"method":"COBYLA","args":args,"constraints":cons}

你传递了字符串&#34; args&#34;和&#34;缺点&#34;而不是那些名字的变量。