'Max Equation Length
I read most of the questions with the same error, but most of them were answered the same which is not the case with my problem. I am solving a very easy model and the only issue is the conditional statement that I correctly managed. But I got this error and now I don't know how to fix it! "APM model error: string > 15000 characters Consider breaking up the line into multiple equations
The may also be due to only using newline character CR instead of CR LF (for Windows) or LF (for MacOS/Linux) To fix this problem, save APM file with appropriate newline characters
STOPPING... " it will be great if you help me.
Solution 1:[1]
There is no need to do the summation with IMODE=2. Each data row and associated calculation is added to the objective function with m.Minimize() or m.Obj().
Try using the m.if3() function instead of an if statement. This is required to provide the gradient-based solvers with continuous first and second derivatives. The m.if2() can also be used if m.if3() takes too long. There is more information on logical conditions in optimization problems.
from gekko import GEKKO
import random
m = GEKKO()
# random numbers between -0.5 and 0.5
r = [30*(random.random()+1.0) for i in range(744)]
# power
Pw = m.Param(value=r)
eta = m.Const(0.6)
Pre = m.Var(value=25,lb=20,ub=30)
def f(Pw,Pre):
D = m.if3(Pw-Pre,Pw-Pre,eta*(Pw-Pre))
return D
# constraint
#m.Equation(f(Pw,Pre)>=0)
# objective
m.Minimize(f(Pw,Pre))
# set global option and solve
m.options.IMODE = 2
m.solve()
The constraint m.Equation(f(Pw,Pre)>=0) is not needed because the absolute value is positive. This script gives the solution:
APMonitor Optimization Suite
----------------------------------------------------------------
--------- APM Model Size ------------
Each time step contains
Objects : 0
Constants : 1
Variables : 6
Intermediates: 0
Connections : 0
Equations : 4
Residuals : 4
Number of state variables: 3720
Number of total equations: - 2232
Number of slack variables: - 1488
---------------------------------------
Degrees of freedom : 0
----------------------------------------------
Model Parameter Estimation with APOPT Solver
----------------------------------------------
Iter: 1 I: 0 Tm: 1.95 NLPi: 4 Dpth: 0 Lvs: 0 Obj: 7.00E+03 Gap: 0.00E+00
Successful solution
---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 1.99689999999828 sec
Objective : 7001.86498359946
Successful solution
---------------------------------------------------
Don't forget to post your code, not an image of your code next time you ask a question. It makes it easier to provide more specific suggestions.
Solution 2:[2]
Thanks for the time you put into it. I corrected my own model and now it works. but I have another problem. with different initial values for the variable, it gives a different answer! not the optimal one! but this should have one answer.
I do not understand why!
try:
from pip import main as pipmain
except:
from pip._internal import main as pipmain
pipmain(['install','gekko'])
from gekko import GEKKO
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#Initialize Model
m = GEKKO(remote=False)
#define parameter
df=pd.read_excel (r'C:\Users\....')
Pw=pd.DataFrame(df).values
eta = m.Const(value=0.6)
Pre=m.Var(lb=20, ub=30)
Pre.value=23
def f(Pw,Pre):
Dplus=m.Var(value=0)
Dminus=m.Var(value=0)
for i in range(744):
D=float(Pw[i])-Pre.value
if D>=0:
Dplus.value=Dplus.value+D*eta.value
elif D<0:
Dminus.value=Dminus.value+D
return Dplus+Dminus
#constraint:
m.Equation(f(Pw,Pre)>=0)
#Objective:
m.Minimize(f(Pw,Pre))
#Set global options:
m.options.IMODE = 2 #steady state optimization
#Solve simulation:
m.solve()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Hedengren |
| Solution 2 | Leila Eyni |

