简体   繁体   English

使用python scipy.optimize.minimize优化具有许多输出的函数

[英]Optimize a function that has many outputs with python scipy.optimize.minimize

I've been searching a little in the web and I did not found any solution to this question, so I decided to formally ask here. 我在网上进行了一些搜索,但没有找到解决该问题的任何方法,因此我决定在这里正式提出。

I have a function that has multiple outputs: 我有一个具有多个输出的函数:

f(input1,input2,... ,inputn) = output1,ouput2,output3

My wish is to minimize output1 constraining other outputs, for instance: 我的愿望是最小化output1约束其他输出,例如:

output2 >= 0 and output3 <= 7   (*constraints*)

My idea was to have the following program structure: 我的想法是要具有以下程序结构:

def function(input1,input2,...,inputn):
    #very secret part of code
    return(output1,ouput2,output3)

inputs=[1,31,22]
_res = optimize.minimize(function, inputs,  method='nelder-mead', 
options=options,*constraints*)

Now I believe that the answer should be in the documentation of the Scipy minimize function. 现在,我认为答案应该在Scipy minimize功能的文档中。

The fact is that, from what I've understood, I can only constrain my function inputs and not some of the outputs of my function. 事实是,据我了解,我只能约束函数输入,而不能约束函数的某些输出。 Am I missing something? 我想念什么吗?

Here is an example of optimization with constraint: 这是带有约束的优化的示例:

from scipy.optimize import minimize

def f(xy):
    x, y = xy 
    return x**2 + y**2

def constraint1(xy):
    x, y = xy 
    return x-1

def constraint2(xy):
    x, y = xy 
    return y-2

constraints = ({'type': 'ineq', 'fun': constraint1},
               {'type': 'ineq', 'fun': constraint2})

xy_start = [0, 0]
res = minimize(f, xy_start, method='SLSQP', constraints=constraints)

which gives: 这使:

     fun: 4.999999999999997
     jac: array([2., 4.])
 message: 'Optimization terminated successfully.'
    nfev: 13
     nit: 3
    njev: 3
  status: 0
 success: True
       x: array([1., 2.])

The idea is that your function you want to minimize is well scalar: the returned value have to be only output1 . 这个想法是,您要最小化的函数是纯量的:返回的值只能是output1 Additional functions have to be added in order to define each constraint: constraint1(inputs)->output2 , constraint2(inputs)->output3 ... etc 为了定义每个约束,必须添加其他功能: constraint1(inputs)->output2constraint2(inputs)->output3 ...等

Maybe you could separate the 'very secret part' of your code into different functions, which will be used both by the objective function and by the constraints functions 也许您可以将代码的“非常秘密的部分”分成不同的函数,这将由目标函数和约束函数使用

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

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