本文介绍了使用 scipy.optimize 动态选择参数以最小化 python 中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将变量列表作为参数的函数,我想使用 scipy.optimize.minimize 最小化这个函数.
问题是它是在运行时决定参数列表中的哪个变量应该进行最小化.所有其他变量将获得一个固定值.

I have a function which takes a list of variables as an argument and I would like to minimize this function using scipy.optimize.minimize.
The problem is that it is decided on runtime for which variable in the argument list the minimization should be done. All other variables will get a fixed value.

举个例子来说明:

a = 1
c = 1.1
d = -1.2

def func( b ):
    return function_to_minimize( array=[a,b,c,d] )

sol = scipy.optimize.minimize( func, [b0], args=(a,c,d) )

这可行,但是,可能是 bcd 是已知的,我想优化 a 找到最小的解决方案.

This works, however, it could be that b, c and d are known and I want to optimize a to find the minimum solution.

更复杂的是,列表的长度也不知道.这意味着可能有一个变量 efg 等等.

To make it even more complicated, the length of the list is not known either. That means there could be a variabel e, f, g, ... and so on.

实际符号如下.None 元素是应该优化的元素.

The actual notation is as follows. The element which is None is the one which should be optimized for.

array = [1, 1.1, None, -0.5, 4]

def func(arr):
    return function_to_minimize(arr)

startvalue = 1.0
sol = scipy.optimize.minimize( func, [startvalue], args='Array without None' )

有没有办法告诉 scipy.optimize.minimize 优化哪个元素?也许我可以做一个聪明的 lambda 技巧?

Is there a way to tell scipy.optimize.minimize for which element to optimize for? Is there perhaps a smart lambda trick which I could do?

非常感谢您的帮助!

推荐答案

如您所知,要最小化的函数会根据哪些参数而变化给出.所以我们需要编写一些动态定义函数的代码.一种方法是定义一个模板字符串,做一些字符串格式化根据给定的参数修改模板,然后使用exec定义函数.这有一些优先级 - 标准库使用这个 定义命名元组的技术.

As you know, the function to be minimized changes depending on what parametersare given. So we need to write some code which defines the function dynamically.One way to do this is to define a template string, do some string formatting tomodify the template based on which parameters are given, and then use exec todefine the function. There is some precedence for this -- the standard library uses this technique to define namedtuples.

例如,如果我们希望最小化的表达式是

So, for example, if the expression we wish to minimize is

4*(b-a)**2 + 5*(c-d)**2

然后你可以使用

import textwrap
import scipy.optimize as optimize

def make_model(*fixed):
    template = textwrap.dedent("""
        def func(variable, {fixed}):
            {variable} = variable
            return 4*(b-a)**2 + 5*(c-d)**2
        """)
    variable = set(('a', 'b', 'c', 'd')).difference(fixed)
    ns = dict()
    funcstr = template.format(variable=', '.join(variable), fixed=', '.join(fixed))
    print(funcstr)  # comment out if you don't want to see the function
    exec funcstr in ns
    return ns['func']

def solve(initial_guess, **givens):
    fixed = tuple(givens.keys())
    vals = tuple(givens.values())
    sol = optimize.minimize(make_model(*fixed), initial_guess, args=vals)
    return sol

print(solve(initial_guess=1, a=1, c=1.1, d=-1.2))

产生的结果

def func(variable, a, c, d):
    b = variable
    return 4*(b-a)**2 + 5*(c-d)**2

   status: 0
  success: True
     njev: 1
     nfev: 3
 hess_inv: array([[1]])
      fun: array([ 26.45])
        x: array([ 1.])
  message: 'Optimization terminated successfully.'
      jac: array([ 0.])
      nit: 0
print(solve(initial_guess=(1, 1), a=1, c=1.1))

收益

def func(variable, a, c):
    b, d = variable
    return 4*(b-a)**2 + 5*(c-d)**2

   status: 0
  success: True
     njev: 3
     nfev: 12
 hess_inv: array([[1, 0],
       [0, 1]])
      fun: 2.4611848645596973e-16
        x: array([ 0.99999999,  1.1       ])
  message: 'Optimization terminated successfully.'
      jac: array([  1.19209279e-08,   2.88966118e-08])
      nit: 1

这篇关于使用 scipy.optimize 动态选择参数以最小化 python 中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 12:29