scipy.优化.最小化python2.x3.x的结果不同

2024-04-26 02:40:58 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我有一个非线性约束问题使用SLSQP解算器scipy.优化.最小化. 不幸的是,这个问题(相同的文件,相同的代码)在不同的计算机(一个Windows,一个Linux)上返回不同的结果。scipy版本是相同的(1.2.1)。这是我的密码:

import numpy as np
from scipy.optimize import minimize

class OptimalAcc():
    def __init__(self, v0, tg, tr, D0, sgr, l, t0, a0, b0, 
                       rho_t=0.5, rho_u=0.5, vM=15, vm=2.78, aM=2.5, am=-2.9):

        # Problem constants
        self.v0 = v0
        self.D0 = D0
        self.sgr = sgr
        self.l = l
        self.T = tg + tr
        self.D = tg / self.T
        self.t0 = t0
        self.a0 = a0
        self.b0 = b0        
        self.rho_t = rho_t
        self.rho_u = rho_u
        self.vM = vM
        self.vm = vm
        self.aM = aM
        self.am = am

    def cost_fn(self, x):

        # Acceleration profile variables
        t = x[:1]
        a = x[1:2]
        b = x[2:3]

        # Objective function
        f = self.rho_t*x[:1]+self.rho_u*(a**2*t**3/3 +
                                         a*b*t**2 +
                                         b**2*t)
        return f

    def solve(self):

        # Inequality constraints
        ineq = ({'type':'ineq',
             'fun':lambda x: np.array([self.aM - x[2],
                                       x[2]-self.am,
                                       x[0],
                                       self.vM - (self.v0 + x[2]*x[0] + 0.5*x[1]*x[0]**2),
                                       self.v0 + x[2]*x[0] + 0.5*x[1]*x[0]**2 - self.vm,
                                       np.sin(np.pi*self.D - np.pi/2)-
                                       np.sin(2*np.pi*(x[0] -((self.D0*self.T)/abs(self.sgr-2)))/self.T + 3*np.pi/2 - np.pi*self.D)])})

        # Equality constraints
        eq = ({'type':'eq',
               'fun':lambda x: np.array([x[1]*x[0] + x[2],
                                         self.v0*x[0] + 0.5*x[2]*x[0]**2 + x[1]*x[0]**3/6 - self.l])})

        # Starting points
        x0 = np.array([self.t0, self.a0, self.b0])

        # Solve optimization problem
        res = minimize(self.cost_fn, 
                       x0=x0,
                       constraints=[ineq,eq],
                       options={'disp': True})

        return res

if __name__== "__main__":
    v0 = 1
    tg = 20
    tr = 20
    D0 = 1
    sgr = 1
    l = 70
    t0 = 10
    a0 = -0.1
    b0 = 1.5

    # Create instance of optimization problem class
    obj = OptimalAcc(v0, tg, tr, D0, sgr, l, t0, a0, b0)

    # Solve problem and return optimal profile
    u_t = obj.solve().x
    print('x_1:',u_t[0])
    print('x_2:',u_t[1])
    print('x_3:',u_t[2])

Windows计算机产生:

Optimization terminated successfully.    (Exit mode 0)
            Current function value: 8.696191258640086
            Iterations: 7
            Function evaluations: 35
            Gradient evaluations: 7
x_1: 13.508645429307041
x_2: -0.06874922875473621
x_3: 0.9287089606820067

我相信这些结果是局部最优的,我可以用MATLAB中的fmincon验证相同的输出。你知道吗

但是,Linux机器会产生:

Positive directional derivative for linesearch    (Exit mode 8)
            Current function value: 14.4116342889
            Iterations: 17
            Function evaluations: 147
            Gradient evaluations: 13
x_1: 7.65875894797259
x_2: -0.241800477348664
x_3: 2.5000000000000053

显然,优化器被困在Linux计算机中。是什么原因造成的?我唯一的猜测是numpy中有一些精确的数字。你知道吗


Tags: selfnppivmtgb0ama0
1条回答
网友
1楼 · 发布于 2024-04-26 02:40:58

正如在评论中所讨论的,这个问题很可能与Windows和Linux无关,而更像是python2和python3。例如

a**2*t**3/3

在python2和python3之间可能看起来有所不同,因为可能只涉及整数(在代码中有更多类似的示例)。你知道吗

一个简单的解决办法可能是包括

from __future__ import division

在脚本的顶部,它将处理在python2和python3中如何执行除法的差异。你知道吗

相关问题 更多 >