我想使用scipy模块scipy.optimize最小化函数,并且我正在努力理解如何实现我的约束。我的解决方案的极值点必须是零和一,所以现在我强加
cons = ({'type': 'eq',
'fun' : lambda x: x[0]}, #the initial point is 0
{'type': 'eq',
'fun' : lambda x: x[-1]-1.}) #the final point is 1
但我还要求每个其他点严格高于0且小于1.如何为数组的所有其他点显式添加此进一步约束?
答案 0 :(得分:3)
如果x[0]
必须为0且x[-1]
必须为1,则表示您没有优化它们,因此您只需将它们设置为函数内的值即可,无需添加约束。对于其他点,使用bounds
keyword:
bounds=[(0, 1), (0, 1),...]