我有一个优化问题需要在python中解决。总体结构是
def foo(a, b, c, d, e):
# do something and return one value
def bar(a, b, c, d, e, f, g, h, i, j):
# do something and return one value
def func():
return foo(a, b, c, d, e) - bar(a, b, c, d, e, f, g, h, i, j)
我想使用least_squares
最小化并返回{foo
和{least_squares
。在
我试过了:
^{pr2}$如何使x
成为f, g, h, i and j
最小值列表的返回值?在
当前定义问题的方式相当于最大化
bar
(假设您将func
传递给一个最小化函数)。由于不改变参数a
到e
,func
基本上是常数与bar
的结果之间的差异;由于负号,它将被尝试最大化,因为这样会最小化整个函数。在我想你想要最小化的是两个函数的绝对值或平方差。我用一个简单的例子来说明,假设函数只返回参数之和:
然后你得到:
^{pr2}$以及
^{3}$如上所述,所有参数都将到达上限,以最大化}。在
bar
,从而最小化{对于自适应函数
func2
,您将收到:因此,正如预期的那样,对于这个简单的例子,我们可以选择参数,使这两个函数之间的差值变为0。显然,参数的结果不是唯一的,它们也可能都是0。在
我希望这有助于使您的实际功能发挥作用。在
编辑:
正如您所要求的
least_square
,这也很好(使用上面的函数定义);那么总的差别就可以了:然后您将得到与上述相同的结果:
由于5个参数在这个问题中不会改变,所以我会将它们固定到一个特定的值,并且不会将它们传递给优化调用。在
相关问题 更多 >
编程相关推荐