简体   繁体   English

具有多个约束的Scipy最小化

[英]Scipy minimization with multiple constraints

I want to minimize a functional using the scipy module scipy.optimize and I'm struggling to understand how to implement my constraints. 我想使用scipy模块scipy.optimize最小化功能,而我正努力了解如何实现约束。 The extremal points of my solution must be exactly zero and one, so right now I'm imposing 我的解决方案的极值点必须正好为零和一,所以现在我要施加

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

but I also need every other point to be strictly higher than 0 and smaller than 1. How can I explicitly add this further constraint for all the other points of my array? 但是我还需要其他所有点都严格大于0且小于1。如何为数组的所有其他点显式添加此进一步约束?

If x[0] must be 0 and x[-1] must be 1, you are not optimising them, so you can just set them to those values inside your function, no need to add constraints. 如果x[0]必须为0, x[-1]必须为1,则您没有对其进行优化,因此您可以将它们设置为函数内部的那些值,而无需添加约束。 For the other points, set bounds between 0 and 1 with bounds keyword : 对于其他点,请使用bounds 关键字在0和1之间设置范围:

bounds=[(0, 1), (0, 1),...]

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

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