我正在研究一个整数优化问题,并想尝试 GEKKO。
问题描述:x1,x2,x3,...,x9,x10,x11(它们是 1-16 范围内的整数)是目标函数的十一个整数参数。我想找到一组 x 值来最小化目标函数的输出。然而,目标值是通过运行另一个 C/C++ 程序获得的,因为该问题不能用数学表示。像你的例子一样的公式。
我怀疑目标函数的返回值 j 是否与规范不匹配。在这种情况下 j python float 可以吗?但从日志和复查来看,init 状态的客观值是 0.06,这是正确的。我不知道为什么它只运行一次迭代然后停止。
def f_per_particle(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11):
inputs_list = [x1.value,x2.value,x3.value,x4.value,x5.value,x6.value,x7.value,x8.value,x9.value,x10.value,x11.value]
#Use the inputs_list as input to run another SW program
#The SW program will return an objective value j which is float value
#The goal is to find min. objective value with corresponding x1-x11 value
return j
m = GEKKO() # Initialize gekko
m.options.SOLVER=1 # APOPT is an MINLP solver
# optional solver settings with APOPT
m.solver_options = ['minlp_maximum_iterations 500', \
# minlp iterations with integer solution
'minlp_max_iter_with_int_sol 10', \
# treat minlp as nlp
'minlp_as_nlp 0', \
# nlp sub-problem max iterations
'nlp_maximum_iterations 50', \
# 1 = depth first, 2 = breadth first
'minlp_branch_method 1', \
# maximum deviation from whole number
'minlp_integer_tol 0.05', \
# covergence tolerance
'minlp_gap_tol 0.01']
# Initialize variables
# Integer constraints for x1 to x11
x1 = m.Var(value=8,lb=1,ub=16,integer=True)
x2 = m.Var(value=8,lb=1,ub=16,integer=True)
x3 = m.Var(value=8,lb=1,ub=16,integer=True)
x4 = m.Var(value=8,lb=1,ub=16,integer=True)
x5 = m.Var(value=8,lb=1,ub=16,integer=True)
x6 = m.Var(value=8,lb=1,ub=16,integer=True)
x7 = m.Var(value=8,lb=1,ub=16,integer=True)
x8 = m.Var(value=8,lb=1,ub=16,integer=True)
x9 = m.Var(value=8,lb=1,ub=16,integer=True)
x10 = m.Var(value=8,lb=1,ub=16,integer=True)
x11 = m.Var(value=8,lb=1,ub=16,integer=True)
m.Obj(f_per_particle(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11)) # Objective
m.solve() # Solve
Execution result as follows :
apm 130.226.87.11_gk_model0 <br><pre> ----------------------------------------------------------------
APMonitor, Version 0.9.1
APMonitor Optimization Suite
----------------------------------------------------------------
--------- APM Model Size ------------
Each time step contains
Objects : 0
Constants : 0
Variables : 11
Intermediates: 0
Connections : 0
Equations : 1
Residuals : 1
________________________________________________
WARNING: objective equation 1 has no variables
ss.Eqn(1)
0 = 0.06
________________________________________________
Number of state variables: 11
Number of total equations: - 0
Number of slack variables: - 0
---------------------------------------
Degrees of freedom : 11
----------------------------------------------
Steady State Optimization with APOPT Solver
----------------------------------------------
Iter: 1 I: 0 Tm: 0.00 NLPi: 1 Dpth: 0 Lvs: 0 Obj: 6.00E-02 Gap: 0.00E+00
Successful solution
---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 1.410000000032596E-002 sec
Objective : 6.000000000000000E-002
Successful solution
---------------------------------------------------
Results
x1: [8.0]
x2: [8.0]
x3: [8.0]
x4: [8.0]
x5: [8.0]
x6: [8.0]
x7: [8.0]
x8: [8.0]
x9: [8.0]
x10: [8.0]
x11: [8.0]
Objective: 0.06
(virtual-env) nan@TEK-CB-NAN-01:~/Swarm_Sandbox/GEKKO/mlp_fi_fwl/run$