简体   繁体   English

高度依赖初始猜测的非线性系统的解

[英]Solution of a nonlinear system highly dependent on initial guess

I have a system of nonlinear equations我有一个非线性方程组

  1. xF - xA * exp(-(wAA * xA + wBB * xB)) = 0 xF - xA * exp(-(wAA * xA + wBB * xB)) = 0
  2. xF - xB * exp(-(wBB * xB + wAB * xA)) = 0 xF - xB * exp(-(wBB * xB + wAB * xA)) = 0
  3. xA + xB + xF -1 = 0 xA + xB + xF -1 = 0

where the variables are xA, xB and xF, while wAA, wBB and wAB are given parameters.其中变量是 xA、xB 和 xF,而 wAA、wBB 和 wAB 是给定参数。 The variables are non negative and in the interval [0,1] Here there is a minimal example code变量是非负的并且在 [0,1] 区间内 这里有一个最小的示例代码

import math
from scipy.optimize import least_squares

coupleInteractions = [10000., 10., 0.]

def equations(p):
    xA, xB, xF = p
    wAA, wBB, wAB = coupleInteractions

    eq1 = xF - xA * math.exp(-(wAA * xA + wBB * xB))
    eq2 = xF - xB * math.exp(-(wBB * xB + wAB * xA))
    eq3 = xA + xB + xF -1

    return (eq1, eq2, eq3)

guess = [0., 1., 0.]
bounds = ([0., 0., 0.], [1., 1., 1.])
root = least_squares(equations, x0=guess, bounds=bounds, xtol=1.e-15, gtol = 1.e-15, ftol = 1.e-15, loss="cauchy")

print(root)
print(root.x)

I want to test the code in some limit cases.我想在一些极限情况下测试代码。 For example if all the parameters are zero, wAA = wAB = wBB = 0, I have a simple linear system, with xA = xB = xF = 1./3.例如,如果所有参数都为零,wAA = wAB = wBB = 0,我有一个简单的线性系统,其中 xA = xB = xF = 1./3。 In this case the code works perfectly without dependence on the guess values.在这种情况下,代码可以完美运行而不依赖于猜测值。

I want to test, like in the code posted, the case wAA >> wBB and wAA >> wAB.我想像发布的代码一样测试 wAA >> wBB 和 wAA >> wAB 的情况。 From a mathematical point of view I obtain xA = 1, xB = 0 and xF = 0. But with the least_square function I obtain the wrong answer.从数学的角度来看,我获得了 xA = 1、xB = 0 和 xF = 0。但是对于least_square function,我得到了错误的答案。 I deliberately use a "wrong" guess to test the method, because in the intermediate cases I do not want that the results higly depends on the initial choice.我故意使用“错误”的猜测来测试该方法,因为在中间情况下我不希望结果高度依赖于初始选择。 Is there an error in my code?我的代码有错误吗?

I have performed a new test to see if a global optimization could work.我进行了一项新测试,看看全局优化是否可行。 I have defined the function:我已经定义了 function:

def scalarEquations(p):
    xA, xB, xF = p
    wAA, wBB, wAB = coupleInteractions

    eq1 = xF - xA * math.exp(-(wAA * xA + wBB * xB))
    eq2 = xF - xB * math.exp(-(wBB * xB + wAB * xA))
    eq3 = xA + xB + xF -1

    return (eq1**2 + eq2**2 + eq3 **2)

It returns the sum of the squares for the 3 equations.它返回 3 个方程的平方和。 Then I performed a brute force approach.然后我执行了蛮力方法。

from scipy.optimize import brute
rranges = (slice(0,1, 0.05), slice(0,1, 0.005), slice(0,1, 0.005))
t = brute(scalarEquations, rranges)
print(t)

I obtain the same "wrong" solution.我得到了同样的“错误”解决方案。 I know that a global optimizer should find the absolute minimum.我知道全局优化器应该找到绝对最小值。 I do not understand.我不明白。

Your code looks ok.您的代码看起来不错。 Looks like there is a local minimum near [0,1,0] for wAA large and wBB greater than e and the optimization is converging to it.对于大于 e 的 wAA 和大于 e 的 wBB,看起来在 [0,1,0] 附近有一个局部最小值,并且优化正在向它收敛。

Probably I found a solution.可能我找到了解决方案。 I have applied at both sides of my equation system log function. Also, I define a new loss function with this idea.我在我的方程系统日志function的两边都应用了。另外,我用这个想法定义了一个新的损失function。 I change the lower bound to 1.e-15 to avoid domain error.我将下限更改为1.e-15以避免域错误。 Finally, I perform a brute optimization.最后,我进行了粗略的优化。 Here is my new piece of code这是我的新代码

def scalarEquations(p):
    xA, xB, xF = p
    wAA, wBB, wAB = coupleInteractions

    eq1 = math.log(xF) - math.log(xA * math.exp(-(wAA * xA + wAB * xB)))
    eq2 = math.log(xF) - math.log(xB * math.exp(-(wBB * xB + wAB * xA)))
    eq3 = math.log(xA + xB + xF) -math.log(1.)

    return (eq1**2 + eq2**2 + eq3 **2)

rranges = ((1.e-15, 1.), (1.e-15, 1.), (1.e-15, 1.) )
t = brute(scalarEquations, rranges, Ns = 100)
print(t)

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

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