如何在 MILP 优化问题中使用目标函数中的函数?

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

感谢您的精美包裹!非常好用而且方便。

最近,我正在尝试解决一个 MILP 问题,其中两个状态变量是下边界和上边界内的整数,目标函数是返回实数的独立函数。

这个想法看起来很简单,但我的 Gekko 只计算最初的一步,然后停止优化过程。请告诉我在 Gekko 中实现这个问题的正确方法。非常感谢!

我的代码如下所示:

# Initialize gekko

m = GEKKO()

# APOPT is an MINLP solver
m.options.SOLVER=1

# Solver settings (copied from an example)
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'\]

# Two state variables
m.x1 = m.Var(value=20,  lb=1,   ub=90,  integer=True)
m.x2 = m.Var(value=60,  lb=2,   ub=91,  integer=True)

# Objective function with a independent function that returns a real number
m.Obj(K_func(data, m.x1, m.x2))

# Solve the Gekko optimization
m.solve(disp=True) # Solve
optimization gekko mixed-integer-programming
1个回答
0
投票

以下是如何使用 Gekko 包含外部

K_func
目标函数的示例:

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

# sample data
data = np.random.rand(10)*20

# example function
def K_func(data,x1,x2):
    obj = sum([(x1-d)**2 + (x2-d-3)**2 for d in data])
    return obj

# APOPT is an MINLP solver
m.options.SOLVER=1

# Solver settings (copied from an example)
m.solver_options = ['minlp_maximum_iterations 500', \
                    'minlp_max_iter_with_int_sol 10', \
                    'minlp_as_nlp 0', \
                    'nlp_maximum_iterations 50', \
                    'minlp_branch_method 1', \
                    'minlp_integer_tol 0.05', \
                    'minlp_gap_tol 0.01']

# Two state variables
m.x1 = m.Var(value=20,  lb=1,   ub=90,  integer=True)
m.x2 = m.Var(value=60,  lb=2,   ub=91,  integer=True)

# Objective function with a independent function that returns a real number
m.Obj(K_func(data, m.x1, m.x2))

# Solve the Gekko optimization
m.solve(disp=True) # Solve
print(f'x1: {m.x1.value[0]}')
print(f'x2: {m.x2.value[0]}')

结果随着不同的随机数而变化,但这里有一个输出:

 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------
Iter: 1 I: 0 Tm: 0.00 NLPi: 4 Dpth: 0 Lvs: 3 Obj: 5.14E+02 Gap: NaN
--Integer Solution: 5.14E+02 Lowest Leaf: 5.14E+02 Gap: 5.42E-04
Iter: 2 I: 0 Tm: 0.00 NLPi: 1 Dpth: 1 Lvs: 3 Obj: 5.14E+02 Gap: 5.42E-04
 Successful solution

 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :   3.770000001532026E-002 sec
 Objective      :    513.977527789434
 Successful solution
 ---------------------------------------------------

x1: 6.0
x2: 9.0

任何外部函数都需要根据 Gekko 变量进行定义,否则它就是黑盒函数,需要使用不同的策略,例如使用 1D cspline、2D bspline 或更高版本创建函数的代理模型维度机器学习模型。以下是黑盒模型的相关问题:

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