Python 中 Excel 求解器和 OR Tools 求解器之间的差异

问题描述 投票:0回答:1

我正在使用 OR-Tools 来实现求解器

  • 版本:
    ortools-9.9.3963
  • 语言:
    Python

但是excel和OR Tools中的求解器结果不一样:

  • 求解器 Excel:1,191,892,387
  • 或工具:1,191,892,386

文件Python:https://drive.google.com/file/d/1bHjXuL2X2QEbHRI0BZ2YXDyqnC_EAiyt/view?usp=drive_link

Excel 文件:https://drive.google.com/drive/folders/1oJjXKEOQu0EtHCoUo9sMHmnBY9_12mZW?usp=drive_link

Python 代码

from ortools.linear_solver import pywraplp

def LinearProgrammingExample():

    # [START solver]
    solver = pywraplp.Solver.CreateSolver('CLP')
    # [END solver]

    # Create the two variables and let them take on any non-negative value.
    # [START variables]
    infinity = solver.infinity()
    for x in range(1, 32):
        globals()[f"x{x}"] = solver.NumVar(0.0, infinity, f"x{x}")

    print('Number of variables =', solver.NumVariables())
    # [END variables]

    # khai báo phân bổ hạn mức
    l1  =  sum([x1,x8,x15,x22,x29,x30])
    l1_dl  =  sum([x1,x8,x15,x22])
    l1_bds  =  sum([x8])
    l1_hq  =  sum([x29,x30])

    l2  =  sum([x2,x9,x16,x23])
    l2_dl  =  sum([x2,x9,x16,x23])
    l2_bds  =  sum([x9])

    l3  =  sum([x3,x10,x17,x24])
    l3_dl  =  sum([x3,x10,x17,x24])
    l3_bds  =  sum([x10])

    l4_2  =  sum([x4,x11,x18,x25])
    l4_2_dl  =  sum([x4,x11,x18,x25])
    l4_2_bds  =  sum([x11])
    
    l4_3  =  sum([x5,x12,x19,x26])
    l4_3_dl  =  sum([x5,x12,x19,x26])
    l4_3_bds  =  sum([x12])

    l5  =  sum([x6,x13,x20,x27])
    l5_dl  =  sum([x6,x13,x20,x27])
    l5_bds  =  sum([x13])

    l6  =  sum([x7,x14,x21,x28])
    l6_dl  =  sum([x7,x15,x23,x30])
    l6_bds  =  sum([x14])


    # khai báo phân bổ tài sản
    c1  =  sum([x1,x2,x3,x4,x5,x6,x7])
    c2  =  sum([x8,x9,x10,x11,x12,x13,x14])
    c3  =  sum([x15,x16,x17,x18,x19,x20,x21])
    c4  =  sum([x22,x23,x24,x25,x26,x27,x28])
    c5  =  sum([x29])
    c6  =  sum([x30])

    # tham số đầu vào
    pC11 = 0
    pJ3 = 0.5
    pK3 = 0
    pN3 = 0.5
    pM3 = 0
    pL3 = 0.5
    pAe3 = 0
    pAe4 = 0
    pAe5 = 0
    pAe6 = 0
    pAe7 = 0
    pAe8 =  0  
    pAe9 = 0
    pAe10 = 0
    pAe11 = 0


    # [START constraints]
    solver.Add(c1<=          2303087800            ) # c1
    solver.Add(c2<=         0 ) # c2
    solver.Add(c3<=   0) # c3
    solver.Add(c4<= 0) # c4
    solver.Add(c5<= 0) # c5
    solver.Add(c6<= 0) # c6

    solver.Add(x31 <=              3585716787                 ) # c7
    solver.Add(x31 <=   3585716787 ) # c8

    solver.Add(l1_dl - x31 *(pJ3-pC11) >=         1707141607       ) # c9
    solver.Add(l2_dl >=  0) # c10
    solver.Add(l3_dl >= 0) # c11
    solver.Add(l4_2_dl >=   0   ) # c12
    solver.Add(l4_3_dl >= 0) # c13
    solver.Add(l5_dl >= 0) # c14
    solver.Add(l6_dl >= 0) # c15

    solver.Add(l1_bds-x31* pK3   >=  0 ) # c16
    solver.Add(l2_bds   >= 0) # c17
    solver.Add(l3_bds   >= 0) # c18
    solver.Add(l4_2_bds   >= 0) # c19
    solver.Add(l4_3_bds   >= 0) # c20
    solver.Add(l5_bds   >= 0) # c21
    solver.Add(l6_bds   >= 0) # c22 

    solver.Add(l1_dl+pAe3-x31*(1-pL3-pC11) >=      1707141607 ) #c23

    solver.Add(l2_dl+ pAe4 >=   0) # c24
    solver.Add(l3_dl+ pAe5    >= 0) # c25
    solver.Add(l4_2_dl + pAe8 >=            0     ) # c26
    solver.Add(l4_3_dl + pAe9 >= 0) # c27
    solver.Add(l5_dl+ pAe10 >= 0) # c28
    solver.Add(l6_dl+ pAe11 >= 0) # c29

    solver.Add(l1_hq-x31* pM3 <= 0) # c30

    solver.Add(l1+ pAe3 -(1-pN3-pC11)*x31 >=        1707141607) # c31
    solver.Add(l2+ pAe4 >= 0) # c32
    solver.Add(l3+ pAe5 >= 0) # c33
    solver.Add(l4_2 + pAe8 >=      0  ) # c34
    solver.Add(l4_3+ pAe9 >= 0) # c35
    solver.Add(l5+ pAe10 >= 0) # c36
    solver.Add(l6+ pAe11 >= 0) # c37

    for x in range(1, 32):
        solver.Add(globals()[f"x{x}"] >=  0)


    print('Number of constraints =', solver.NumConstraints())
    # [END constraints]


    # [START objective]
    solver.Maximize(x31)
    # [END objective]

    # Solve the system.
    # [START solve]
    status = solver.Solve()
    # [END solve]


    # [START print_solution]
    if status == pywraplp.Solver.OPTIMAL:
        print('Solution:')
        for v in solver.variables():
            print(v, "=", v.solution_value())
        print('Objective value =', solver.Objective().Value())
    else:
        print('The problem does not have an optimal solution.')
    # [END print_solution]

    # [START advanced]
    print('\nAdvanced usage:')
    print('Problem solved in %f milliseconds' % solver.wall_time())
    print('Problem solved in %d iterations' % solver.iterations())
    # [END advanced]


LinearProgrammingExample()
# [END program]

为什么Excel求解器的结果与OR-Tools不同?

linear-programming or-tools
1个回答
0
投票

所有 LP 求解器都具有内部精度,通常在 1e-4 和 1e-6 之间。这是下面的,所以从 OR 的角度来看,这 2 个解决方案是等效的。

此外,即使是相同的代码,编译器和硬件也会导致这种差异。

© www.soinside.com 2019 - 2024. All rights reserved.