简体   繁体   English

在Python中使用limfit.minimize最小化目标函数

[英]Minimize objective function using limfit.minimize in Python

I am having a problem with package lmfit.minimize minimization procedure. 我遇到了包lmfit.minimize最小化过程的问题。 Actually, I could not create a correct objective function for my problem. 实际上,我无法为我的问题创建正确的目标函数。

Problem definition 问题定义

  • My function: yn = a_11*x1**2 + a_12*x2**2 + ... + a_m*xn**2 ,where xn - unknowns, a_m - coefficients. 我的函数: yn = a_11*x1**2 + a_12*x2**2 + ... + a_m*xn**2 ,其中xn - 未知数, a_m - 系数。 n = 1..N, m = 1..M n = 1..N,m = 1..M
  • In my case, N=5 for x1,..,x5 and M=3 for y1, y2, y3 . 在我的例子中,对于y1, y2, y3 x1,..,x5对于x1,..,x5M=3N=5

I need to find the optimum: x1, x2,...,x5 so that it can satisfy the y 我需要找到optimum: x1, x2,...,x5以便它可以满足y

My question: 我的问题:

  • Error: ValueError: operands could not be broadcast together with shapes (3,) (3,5) . 错误: ValueError: operands could not be broadcast together with shapes (3,) (3,5)
  • Did I create the objective function of my problem properly in Python? 我是否在Python中正确创建了问题的目标函数?

My code: 我的代码:

import numpy as np
from lmfit import Parameters, minimize

def func(x,a):
    return np.dot(a, x**2)

def residual(pars, a, y):
    vals = pars.valuesdict()
    x = vals['x']
    model = func(x,a)
    return y - model

def main():
    # simple one: a(M,N) = a(3,5)
    a = np.array([ [ 0, 0, 1, 1, 1 ],
                   [ 1, 0, 1, 0, 1 ],
                   [ 0, 1, 0, 1, 0 ] ])
    # true values of x
    x_true = np.array([10, 13, 5, 8, 40])
    # data without noise
    y = func(x_true,a)

    #************************************
    # Apriori x0
    x0 = np.array([2, 3, 1, 4, 20])
    fit_params = Parameters()
    fit_params.add('x', value=x0)

    out = minimize(residual, fit_params, args=(a, y))
    print out
if __name__ == '__main__':
    main()

Directly using scipy.optimize.minimize() the code below solves this problem. 直接使用scipy.optimize.minimize() ,下面的代码解决了这个问题。 Note that with more points yn you will tend to get the same result as x_true , otherwise more than one solution exists. 请注意,更多的积分yn你往往会得到相同的结果x_true ,否则不止一个解决方案的存在。 You can minimize the effect of the ill-constrained optimization by adding boundaries (see the bounds parameter used below). 您可以通过添加边界来最小化不受约束的优化的影响(请参阅下面使用的bounds参数)。

import numpy as np
from scipy.optimize import minimize

def residual(x, a, y):
    s = ((y - a.dot(x**2))**2).sum()
    return s

def main():
    M = 3
    N = 5
    a = np.random.random((M, N))
    x_true = np.array([10, 13, 5, 8, 40])
    y = a.dot(x_true**2)

    x0 = np.array([2, 3, 1, 4, 20])
    bounds = [[0, None] for x in x0]
    out = minimize(residual, x0=x0, args=(a, y), method='L-BFGS-B', bounds=bounds)
    print(out.x)

If M>=N you could also use scipy.optimize.leastsq for this task: 如果M>=N您还可以使用scipy.optimize.leastsq执行此任务:

import numpy as np
from scipy.optimize import leastsq

def residual(x, a, y):
    return y - a.dot(x**2)

def main():
    M = 5
    N = 5
    a = np.random.random((M, N))
    x_true = np.array([10, 13, 5, 8, 40])
    y = a.dot(x_true**2)

    x0 = np.array([2, 3, 1, 4, 20])
    out = leastsq(residual, x0=x0, args=(a, y))
    print(out[0])

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

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