我试图了解gekko及其不同类型的自定义变量是如何工作的.所以我写了一个非常简单的优化问题,但它不会找到最优的解决方案,至少我认为这就是错误消息的意思.

代码是一个简单的设置switch 组合(braco\u gas和braco\u eh,均为二进制)乘以一些权重(vazao和volume,均为continuos).

我想找出哪个switch 和权重组合会产生最大的目标值.参见以下目标:

目标=vazao\u gas*braco\u gas\u 1+volume\u gas*braco\u gas\u 2+vazao\u eh*braco\u eh\u 1+volume\u eh*braco\u eh\u 2

请参见以下代码:

from gekko import GEKKO
import numpy as np

m = GEKKO(remote=False)

# binary variables
braco_gas_1 = m.Var(integer=True, lb=0,ub=1)
braco_eh_1 = m.Var(integer=True, lb=0,ub=1)

braco_gas_2 = m.Var(integer=True, lb=0,ub=1)
braco_eh_2 = m.Var(integer=True, lb=0,ub=1)

# continuous variables
vazao_gas = m.Var(value=100,lb=0,ub=150)
vazao_eh = m.Var(value=100,lb=0,ub=150)

#constants
volume_gas = 1000
volume_eh = 2000

# I want to see each parcel of the objective
tempo_b1 = m.MV(vazao_gas*braco_gas_1 + volume_gas*braco_gas_2)
tempo_b1.STATUS=1
tempo_b2 = m.MV(vazao_eh*braco_eh_1 + volume_eh*braco_eh_2)
tempo_b2.STATUS=1

# that is supposed to be the objective
tempo_total = m.MV(tempo_b1+tempo_b2, lb=0, ub = 4000)
tempo_total.STATUS=1

# Only of binary variable of each group can be true
m.Equation (braco_gas_1+braco_gas_2 == 1)
m.Equation (braco_eh_1+braco_eh_2 == 1)

# I want to maximize the objective
m.Maximize(tempo_b1+tempo_b2)

m.options.SOLVER = 1

m.solve()    # solve

print('Braco_gas_1:'+str(braco_gas_1.value))
print('Braco_gas_2:'+str(braco_gas_2.value))
print('Braco_eh_1:'+str(braco_eh_1.value))
print('Braco_eh_2:'+str(braco_eh_2.value))
print('vazao_gas:'+str(vazao_gas.value))
print('vazao_eh:'+str(vazao_eh.value))
print('volume_gas:'+str(volume_gas))
print('volume_eh:'+str(volume_eh))
print('tempo_b1:'+str(tempo_b1.value))
print('tempo_b2:'+str(tempo_b2.value))

print('tempo_total:'+str(tempo_total.value))

错误消息之后:

 ----------------------------------------------------------------
 APMonitor, Version 1.0.0
 APMonitor Optimization Suite
 ----------------------------------------------------------------
 
 
 --------- APM Model Size ------------
 Each time step contains
   Objects      :  0
   Constants    :  0
   Variables    :  9
   Intermediates:  0
   Connections  :  0
   Equations    :  3
   Residuals    :  3
 
 Number of state variables:    9
 Number of total equations: -  2
 Number of slack variables: -  0
 ---------------------------------------
 Degrees of freedom       :    7
 
 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------
Iter:     1 I:  0 Tm:      0.00 NLPi:    4 Dpth:    0 Lvs:    3 Obj: -1.00E+15 Gap:       NaN
Iter:     2 I: -1 Tm:      0.00 NLPi:    0 Dpth:    1 Lvs:    2 Obj: -1.00E+15 Gap:       NaN
Iter:     3 I: -2 Tm:      0.00 NLPi:    2 Dpth:    1 Lvs:    1 Obj: -1.00E+15 Gap:       NaN
Iter:     4 I: -2 Tm:      0.00 NLPi:    2 Dpth:    1 Lvs:    0 Obj: -1.00E+15 Gap:       NaN
 Warning: no more possible trial points and no integer solution
 Maximum iterations
 
 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :  0.0208 sec
 Objective      :  -1.E+15
 Unsuccessful with error code  0
 ---------------------------------------------------
 
 Creating file: infeasibilities.txt
 Use command apm_get(server,app,'infeasibilities.txt') to retrieve file
 @error: Solution Not Found

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10100/3307325789.py in <module>
     32 m.options.SOLVER = 1
     33 
---> 34 m.solve()    # solve
     35 
     36 print('Braco_gas_1:'+str(braco_gas_1.value))

~\Anaconda3\lib\site-packages\gekko\gekko.py in solve(self, disp, debug, GUI, **kwargs)
   2138                 print("Error:", errs)
   2139             if (debug >= 1) and record_error:
-> 2140                 raise Exception(apm_error)
   2141 
   2142         else: #solve on APM server

Exception: @error: Solution Not Found

推荐答案

该问题目前是无限的(请参见Objective: -1.E+15).

使用m.Intermediate()而不是m.MV().MV(操纵变量)是一个自由度,优化器可以使用该自由度在所有可行解决方案中实现最佳目标.因为tempo_b1tempo_b2tempo_total都有与求解它们相关的方程,所以它们需要是:

  • 具有m.Var()和相应m.Equation()定义的正则变量
  • 中间变量为m.Intermediate(),用一行定义变量和方程.

这里是简单混合整数线性规划(MINLP)优化问题的解决方案.

 ----------------------------------------------------------------
 APMonitor, Version 1.0.1
 APMonitor Optimization Suite
 ----------------------------------------------------------------
 
 --------- APM Model Size ------------
 Each time step contains
   Objects      :            0
   Constants    :            0
   Variables    :            7
   Intermediates:            2
   Connections  :            0
   Equations    :            6
   Residuals    :            4
 
 Number of state variables:              7
 Number of total equations: -            3
 Number of slack variables: -            0
 ---------------------------------------
 Degrees of freedom       :              4
 
 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------
Iter: 1 I:  0 Tm: 0.00 NLPi: 4 Dpth: 0 Lvs: 0 Obj: -3.00E+03 Gap: 0.00E+00
 Successful solution
 
 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :   1.529999999911524E-002 sec
 Objective      :   -3000.00000000000     
 Successful solution
 ---------------------------------------------------
Braco_gas_1:[0.0]
Braco_gas_2:[1.0]
Braco_eh_1:[0.0]
Braco_eh_2:[1.0]
vazao_gas:[150.0]
vazao_eh:[150.0]
volume_gas:1000
volume_eh:2000
tempo_b1:[1000.0]
tempo_b2:[2000.0]
tempo_total:[3000.0]

完整的脚本:

from gekko import GEKKO
import numpy as np

m = GEKKO(remote=False)

# binary variables
braco_gas_1 = m.Var(integer=True, lb=0,ub=1)
braco_eh_1 = m.Var(integer=True, lb=0,ub=1)

braco_gas_2 = m.Var(integer=True, lb=0,ub=1)
braco_eh_2 = m.Var(integer=True, lb=0,ub=1)

# continuous variables
vazao_gas = m.Var(value=100,lb=0,ub=150)
vazao_eh = m.Var(value=100,lb=0,ub=150)

#constants
volume_gas = 1000
volume_eh = 2000

# I want to see each parcel of the objective
tempo_b1 = m.Intermediate(vazao_gas*braco_gas_1 + volume_gas*braco_gas_2)
tempo_b2 = m.Intermediate(vazao_eh*braco_eh_1 + volume_eh*braco_eh_2)

# that is supposed to be the objective
tempo_total = m.Var(lb=0, ub = 4000)
m.Equation(tempo_total==tempo_b1+tempo_b2)

# Only of binary variable of each group can be true
m.Equation (braco_gas_1+braco_gas_2 == 1)
m.Equation (braco_eh_1+braco_eh_2 == 1)

# I want to maximize the objective
m.Maximize(tempo_b1+tempo_b2)

m.options.SOLVER = 1
m.solve()    # solve

print('Braco_gas_1:'+str(braco_gas_1.value))
print('Braco_gas_2:'+str(braco_gas_2.value))
print('Braco_eh_1:'+str(braco_eh_1.value))
print('Braco_eh_2:'+str(braco_eh_2.value))
print('vazao_gas:'+str(vazao_gas.value))
print('vazao_eh:'+str(vazao_eh.value))
print('volume_gas:'+str(volume_gas))
print('volume_eh:'+str(volume_eh))
print('tempo_b1:'+str(tempo_b1.value))
print('tempo_b2:'+str(tempo_b2.value))

print('tempo_total:'+str(tempo_total.value))

documentation18 example problems (with videos)中提供了其他教程.

Python相关问答推荐

Pystata:从Python并行运行stata实例

连接两个具有不同标题的收件箱

如何检测背景有噪的图像中的正方形

Pandas 有条件轮班操作

如何使用数组的最小条目拆分数组

如何请求使用Python将文件下载到带有登录名的门户网站?

对所有子图应用相同的轴格式

ODE集成中如何终止solve_ivp的无限运行

有没有一种方法可以从python的pussompy比较结果中提取文本?

为什么NumPy的向量化计算在将向量存储为类属性时较慢?'

从Windows Python脚本在WSL上运行Linux应用程序

LocaleError:模块keras._' tf_keras. keras没有属性__internal_'''

Cython无法识别Numpy类型

将CSS链接到HTML文件的问题

计算机找不到已安装的库'

提取数组每行的非零元素

Python日志(log)库如何有效地获取lineno和funcName?

普洛特利express 发布的人口普查数据失败

对当前的鼹鼠进行编码,并且我的按键获得了注册

根据两个lambda条件筛选组并根据条件创建新列的最佳方式是什么?