繁体   English   中英

Python 混合整数非线性规划

[英]Python Mixed Integer Nonlinear Programming

我正在为具有多个离散设备选项的制造设施解决设计优化问题(例如,选择0.5,1,2,5之一)。 成本M是固定的,但零件的数量β是另一个离散变量,总共需要建造n个单位。 演示该问题的简化的最小示例如下所示:

import numpy as np
from gekko import GEKKO
m = GEKKO()
n = 100
# select one of the Special Ordered Set
α = m.sos1([0.5, 1, 2, 5])
# integer variables
β = m.Array(m.Var,n,integer=True,lb=1,ub=5)
# log transform
δ = [m.log(b) for b in β] 
# matrix
M = np.random.rand(n,n)
# constraints
Mδ = M.dot(δ)
m.Equations([α*x>50 for x in Mδ])
# objective
m.Minimize(α)
# solve
m.solve()
print('α: ', α.value[0])
print('β: ', β)

对于5^100 x 4 = 3e70可能的可行解决方案,穷举搜索所有可行解决方案是不切实际的。 这个简化的问题在大约 30 秒内解决了大约 100 个整数变量。

 Number of state variables:            205
 Number of total equations: -          102
 Number of slack variables: -          100

 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :    33.4135999999999      sec
 Objective      :    1.00000000000000     
 Successful solution
 ---------------------------------------------------
 
α:  1.0
β:  [[3.0] [4.0] [4.0] [5.0] [4.0] [3.0] [3.0] [3.0] [3.0] [5.0] [4.0] [4.0]
 [4.0] [4.0] [3.0] [3.0] [4.0] [4.0] [3.0] [3.0] [4.0] [3.0] [3.0] [4.0]
 [4.0] [4.0] [5.0] [3.0] [1.0] [4.0] [4.0] [4.0] [3.0] [4.0] [4.0] [3.0]
 [3.0] [3.0] [4.0] [4.0] [3.0] [4.0] [1.0] [3.0] [4.0] [3.0] [3.0] [4.0]
 [3.0] [3.0] [4.0] [4.0] [3.0] [1.0] [3.0] [3.0] [3.0] [3.0] [3.0] [3.0]
 [4.0] [3.0] [3.0] [4.0] [4.0] [4.0] [2.0] [3.0] [1.0] [4.0] [4.0] [3.0]
 [3.0] [3.0] [4.0] [4.0] [3.0] [3.0] [4.0] [3.0] [3.0] [4.0] [3.0] [3.0]
 [3.0] [2.0] [4.0] [5.0] [4.0] [2.0] [3.0] [4.0] [3.0] [4.0] [1.0] [3.0]
 [5.0] [3.0] [4.0] [5.0]]

一个可能的解决方案是在第一个可行整数解决方案可用时终止求解器,而不是迭代直到满足间隙容差。 但是,这可能会返回次优解决方案。 提高求解速度的策略是什么?

求解器迭代总结可以就如何提高求解速度给出建议。 这里有一些建议:

初始化

如果分支定界(非整数解)的第一个 NLP 需要一段时间才能求解,请尝试使用IPOPT NLP 求解器进行初始化:

# solve
for i in [3,1]:
    m.options.SOLVER=i
    m.solve()

初始化将APOPT的第一次迭代时间从 1.57 秒减少到 0.1 秒,以获得迭代 1 的解:

 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------
Iter: 1 I:  0 Tm: 0.10 NLPi: 2 Dpth: 0 Lvs: 3 Obj: 7.60E-01 Gap: NaN
Iter: 2 I: -1 Tm: 0.05 NLPi: 0 Dpth: 1 Lvs: 2 Obj: 7.60E-01 Gap: NaN
Iter: 3 I: -1 Tm: 0.09 NLPi: 1 Dpth: 1 Lvs: 1 Obj: 7.60E-01 Gap: NaN

但是IPOPT需要3.09秒才能找到初始解决方案,因此没有节省时间。

APPT 求解器选项

有一些APPT 求解器选项可以帮助提高求解速度。 如果不可行的 NLP 问题占用了不成比例的求解时间,请将nlp_maximum_iterations设置为较低的数字以终止未快速收敛的试验解决方案。 minlp_branch_method=1对该问题的求解时间有积极影响,因为它是一种深度优先搜索,可快速识别初始整数解。 这有助于消除候选解决方案并修剪分支定界树。 另一个可以尝试的求解器选项是minlp_gap_tol ,它具有更大的容差以实现成功的解决方案。

m.solver_options = [# nlp sub-problem max iterations
                    'nlp_maximum_iterations 50', \
                    # 1 = depth first, 2 = breadth first
                    'minlp_branch_method 1', \
                    # covergence tolerance
                    'minlp_gap_tol 0.01']

设置随机种子,以便与矩阵M的相同随机数进行逐次直接比较:

# same random numbers
np.random.seed(0)

使用相同的随机数,这些选项将求解时间从7.86秒提高到4.87秒。

import numpy as np
from gekko import GEKKO
m = GEKKO()

# same random numbers
np.random.seed(0)

m.solver_options = [# nlp sub-problem max iterations
                    'nlp_maximum_iterations 50', \
                    # 1 = depth first, 2 = breadth first
                    'minlp_branch_method 1', \
                    # covergence tolerance
                    'minlp_gap_tol 0.01']

n = 100
# select one of the Special Ordered Set
α = m.sos1([0.5, 1, 2, 5])
# binary variables
β = m.Array(m.Var,n,integer=True,lb=1,ub=5)
# log transform
δ = [m.log(b) for b in β]
# matrix
M = np.random.rand(n,n)
# constraints
Mδ = M.dot(δ)
m.Equations([α*x>50 for x in Mδ])
# objective
m.Minimize(α)
# solve
m.solve()
print('α: ', α.value[0])
print('β: ', β)
 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------
Iter:  1 I:  0 Tm: 1.57 NLPi: 24 Dpth:  0 Lvs:  3 Obj: 7.60E-01 Gap: NaN
Iter:  2 I: -1 Tm: 0.05 NLPi:  0 Dpth:  1 Lvs:  2 Obj: 7.60E-01 Gap: NaN
Iter:  3 I:  0 Tm: 0.10 NLPi:  2 Dpth:  1 Lvs:  3 Obj: 7.60E-01 Gap: NaN
Iter:  4 I:  0 Tm: 0.30 NLPi:  5 Dpth:  2 Lvs:  5 Obj: 1.00E+00 Gap: NaN
Iter:  5 I:  0 Tm: 0.22 NLPi:  4 Dpth:  3 Lvs:  7 Obj: 1.00E+00 Gap: NaN
Iter:  6 I:  0 Tm: 0.16 NLPi:  3 Dpth:  4 Lvs:  9 Obj: 1.00E+00 Gap: NaN
Iter:  7 I:  0 Tm: 0.16 NLPi:  3 Dpth:  5 Lvs: 11 Obj: 1.00E+00 Gap: NaN
Iter:  8 I:  0 Tm: 0.17 NLPi:  3 Dpth:  6 Lvs: 13 Obj: 1.00E+00 Gap: NaN
Iter:  9 I:  0 Tm: 0.16 NLPi:  3 Dpth:  7 Lvs: 15 Obj: 1.00E+00 Gap: NaN
Iter: 10 I:  0 Tm: 0.17 NLPi:  3 Dpth:  8 Lvs: 17 Obj: 1.00E+00 Gap: NaN
Iter: 11 I:  0 Tm: 0.15 NLPi:  3 Dpth:  9 Lvs: 18 Obj: 1.00E+00 Gap: NaN
Iter: 12 I:  0 Tm: 0.16 NLPi:  3 Dpth: 10 Lvs: 20 Obj: 1.00E+00 Gap: NaN
Iter: 13 I:  0 Tm: 0.20 NLPi:  4 Dpth: 11 Lvs: 22 Obj: 1.00E+00 Gap: NaN
Iter: 14 I:  0 Tm: 0.25 NLPi:  5 Dpth: 12 Lvs: 24 Obj: 1.00E+00 Gap: NaN
Iter: 15 I:  0 Tm: 0.10 NLPi:  2 Dpth: 13 Lvs: 26 Obj: 1.00E+00 Gap: NaN
Iter: 16 I:  0 Tm: 0.10 NLPi:  2 Dpth: 14 Lvs: 28 Obj: 1.00E+00 Gap: NaN
Iter: 17 I:  0 Tm: 0.10 NLPi:  2 Dpth: 15 Lvs: 30 Obj: 1.00E+00 Gap: NaN
--Integer Solution:   1.00E+00 Lowest Leaf:   7.60E-01 Gap:   2.40E-01
Iter: 18 I:  0 Tm: 0.10 NLPi:  2 Dpth: 16 Lvs: 29 Obj: 1.00E+00 Gap: 2.40E-01
Iter: 19 I: -1 Tm: 0.09 NLPi:  1 Dpth:  2 Lvs:  1 Obj: 7.60E-01 Gap: 2.40E-01
Iter: 20 I:  0 Tm: 0.55 NLPi:  5 Dpth:  1 Lvs:  0 Obj: 5.00E+00 Gap: 2.40E-01
 No additional trial points, returning the best integer solution
 Successful solution
 
 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :    4.86900000000605      sec
 Objective      :    1.00000000000000     
 Successful solution
 ---------------------------------------------------

暂无
暂无

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

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