我试图了解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