简体   繁体   English

GEKKO 的二进制优化

[英]Binary Optimization for GEKKO

I am trying to solve a quadratic binary optimization problem with Gekko with qobj , by defining a NxN symmetric matrix and an N-dimensional bias vector at random.我试图通过随机定义一个 NxN 对称矩阵和一个 N 维偏置向量来解决带有qobj的 Gekko 的二次二元优化问题。 However I observed computational times suspiciously low: 2e-2s for solving with N=20 and 0.05s to solve a 200 dimensional problem.然而,我观察到计算时间非常低:2e-2s 用于解决 N=20 和 0.05s 以解决 200 维问题。 Also i never get more than 3-4 iterations.此外,我的迭代次数永远不会超过 3-4 次。 Am i missing something here?我在这里错过了什么吗?

import numpy as np
N = 20
#create square symmetric matrix for the quadratic term
b = np.random.normal(0,1,(N,N))
Q = (b + b.T)/2
#bias vector for linear term
c=np.random.normal(0,1,N)
from gekko import GEKKO
m = GEKKO(remote=False)
z = m.Array(m.Var,N,integer=True,lb=0,ub=1,value=1)
m.qobj(c,A=Q,x=z,otype='min')
m.solve(disp=True)

Any suggestion is appreciated!任何建议表示赞赏!

Switch to APOPT solver for Mixed Integer solution.切换到混合 Integer 解决方案的 APOPT 求解器。

m.options.SOLVER=1

Here is the complete script.这是完整的脚本。 MIQP problems are generally very fast. MIQP 问题通常非常快。 It slows down significantly if the problem is nonlinear (NLP) with mixed integer elements (MINLP).如果问题是非线性 (NLP) 与混合 integer 元素 (MINLP),它会显着减慢。

import numpy as np
N = 200
#create square symmetric matrix for the quadratic term
b = np.random.normal(0,1,(N,N))
Q = (b + b.T)/2
#bias vector for linear term
c=np.random.normal(0,1,N)
from gekko import GEKKO
m = GEKKO(remote=False)
z = m.Array(m.Var,N,integer=True,lb=0,ub=1,value=1)
m.qobj(c,A=Q,x=z,otype='min')
m.options.SOLVER=1
m.solve(disp=True)
print(z)
[[0.0] [1.0] [1.0] [0.0] [1.0] [1.0] [1.0] [1.0] [1.0] [0.0] [1.0] [1.0]
 [1.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0] [0.0] [1.0] [1.0] [1.0]
 [1.0] [1.0] [1.0] [1.0] [0.0] [1.0] [0.0] [1.0] [1.0] [0.0] [0.0] [0.0]
 [0.0] [1.0] [1.0] [0.0] [0.0] [1.0] [1.0] [1.0] [1.0] [1.0] [0.0] [1.0]
 [1.0] [1.0] [0.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0] [0.0]
 [0.0] [1.0] [0.0] [0.0] [0.0] [0.0] [1.0] [1.0] [0.0] [0.0] [0.0] [0.0]
 [1.0] [1.0] [0.0] [0.0] [1.0] [1.0] [0.0] [1.0] [1.0] [0.0] [1.0] [1.0]
 [0.0] [1.0] [0.0] [0.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0]
 [0.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0]
 [1.0] [1.0] [1.0] [0.0] [0.0] [0.0] [1.0] [1.0] [1.0] [1.0] [1.0] [1.0]
 [0.0] [0.0] [1.0] [1.0] [0.0] [1.0] [0.0] [1.0] [0.0] [1.0] [1.0] [1.0]
 [0.0] [0.0] [0.0] [0.0] [1.0] [0.0] [0.0] [1.0] [1.0] [0.0] [0.0] [1.0]
 [1.0] [1.0] [0.0] [1.0] [0.0] [0.0] [1.0] [1.0] [1.0] [1.0] [0.0] [1.0]
 [0.0] [0.0] [0.0] [0.0] [0.0] [0.0] [1.0] [1.0] [1.0] [1.0] [1.0] [0.0]
 [0.0] [0.0] [1.0] [1.0] [1.0] [1.0] [0.0] [1.0] [0.0] [0.0] [1.0] [1.0]
 [0.0] [0.0] [0.0] [1.0] [1.0] [1.0] [0.0] [1.0] [0.0] [1.0] [0.0] [0.0]
 [0.0] [1.0] [1.0] [1.0] [0.0] [1.0] [1.0] [0.0]]

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

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