I am trying to solve multiple optimization problems with various initial conditions using the Gekko suite. Initial conditions are assigned, run optimization using Gekko, and collect each solution. When I try to change parameters, objective function, or initial condition, Gekko often gives me 'Solution Not Found error: line 2130, in solve raise Exception(apm_error).' I am presenting some cases below hoping to get advice to resolve this problem I have. I had a similar question posted earlier, but I hope this question is more concise and clear. Thank you.
Case 1. runs well without error.
from gekko import GEKKO
import pandas as pd
import numpy as np
dat = {'A0': [23221, 2198, 4296, 104906, 691], 'h': [0.04, 0.25, 0.07, 0, 12.58],'emax': [23221, 2198, 4296, 104906, 691] }
dftemp = pd.DataFrame(data=dat)
na=len(dftemp)
# time points
n=51
year=50
# constants
Pa0 = 3.061
Pe0 = 10.603
C0 = 100
r = 0.05
k=50
shift=10000000 # to make positive inside log function
ll=0.15
for i in range(0,na):
# create GEKKO model
m = GEKKO(remote=False)
m.time = np.linspace(0,year,n)
t=m.time
A0=dftemp.loc[i][0]
h=dftemp.loc[i][1]
emax=dftemp.loc[i][2]
A = m.SV(value=A0, lb=0, ub=A0)
E = m.SV(value=0, lb=0, ub=A0)
u = m.MV(value=0, lb=-emax, ub=emax)
u.STATUS = 1
t = m.Param(value=m.time)
C = m.Var(value=C0)
d = m.Var(value=1)
l = m.Param(value=ll)
# Equation
m.Equation(A.dt()==-u)
m.Equation(E.dt()==u)
m.Equation(C.dt()==-C/k)
m.Equation(d==m.exp(-t*r))
# Objective (Utility)
J = m.Var(value=0)
# Final objective
Jf = m.FV()
Jf.STATUS = 1
m.Connection(Jf,J,pos2='end')
m.Equation(J.dt() == m.log((A+E*(1-l))*h*Pa0-C*u+E*Pe0+shift)*d)
# maximize U
m.Maximize(Jf)
# options
m.options.IMODE = 6 # optimal control
m.options.NODES = 3 # collocation nodes
m.options.SOLVER = 3 # solver (IPOPT)
# solve optimization problem
m.solve()
# print profit
print('Optimal Profit: ' + str(Jf.value[0]))
Case 2. change number of "time points" from n=51 to n=501 Error occurs
from gekko import GEKKO
import pandas as pd
import numpy as np
dat = {'A0': [23221, 2198, 4296, 104906, 691], 'h': [0.04, 0.25, 0.07, 0, 12.58],'emax': [23221, 2198, 4296, 104906, 691] }
dftemp = pd.DataFrame(data=dat)
na=len(dftemp)
# time points
n=501
year=50
# constants
Pa0 = 3.061
Pe0 = 10.603
C0 = 100
r = 0.05
k=50
shift=10000000 # to make positive inside log function
ll=0.15
for i in range(0,na):
# create GEKKO model
m = GEKKO(remote=False)
m.time = np.linspace(0,year,n)
t=m.time
A0=dftemp.loc[i][0]
h=dftemp.loc[i][1]
emax=dftemp.loc[i][2]
A = m.SV(value=A0, lb=0, ub=A0)
E = m.SV(value=0, lb=0, ub=A0)
u = m.MV(value=0, lb=-emax, ub=emax)
u.STATUS = 1
t = m.Param(value=m.time)
C = m.Var(value=C0)
d = m.Var(value=1)
l = m.Param(value=ll)
# Equation
m.Equation(A.dt()==-u)
m.Equation(E.dt()==u)
m.Equation(C.dt()==-C/k)
m.Equation(d==m.exp(-t*r))
# Objective (Utility)
J = m.Var(value=0)
# Final objective
Jf = m.FV()
Jf.STATUS = 1
m.Connection(Jf,J,pos2='end')
m.Equation(J.dt() == m.log((A+E*(1-l))*h*Pa0-C*u+E*Pe0+shift)*d)
# maximize U
m.Maximize(Jf)
# options
m.options.IMODE = 6 # optimal control
m.options.NODES = 3 # collocation nodes
m.options.SOLVER = 3 # solver (IPOPT)
# solve optimization problem
m.solve()
# print profit
print('Optimal Profit: ' + str(Jf.value[0]))
Case 3. change objective function from m.log to simple linear sum. Works well.
from gekko import GEKKO
import pandas as pd
import numpy as np
dat = {'A0': [23221, 2198, 4296, 104906, 691], 'h': [0.04, 0.25, 0.07, 0, 12.58],'emax': [23221, 2198, 4296, 104906, 691] }
dftemp = pd.DataFrame(data=dat)
na=len(dftemp)
# time points
n=51
year=50
# constants
Pa0 = 3.061
Pe0 = 10.603
C0 = 100
r = 0.05
k=50
shift=10000000 # to make positive inside log function
ll=0.15
for i in range(0,na):
# create GEKKO model
m = GEKKO(remote=False)
m.time = np.linspace(0,year,n)
t=m.time
A0=dftemp.loc[i][0]
h=dftemp.loc[i][1]
emax=dftemp.loc[i][2]
A = m.SV(value=A0, lb=0, ub=A0)
E = m.SV(value=0, lb=0, ub=A0)
u = m.MV(value=0, lb=-emax, ub=emax)
u.STATUS = 1
t = m.Param(value=m.time)
C = m.Var(value=C0)
d = m.Var(value=1)
l = m.Param(value=ll)
# Equation
m.Equation(A.dt()==-u)
m.Equation(E.dt()==u)
m.Equation(C.dt()==-C/k)
m.Equation(d==m.exp(-t*r))
# Objective (Utility)
J = m.Var(value=0)
# Final objective
Jf = m.FV()
Jf.STATUS = 1
m.Connection(Jf,J,pos2='end')
m.Equation(J.dt() == ((A+E*(1-l))*h*Pa0-C*u+E*Pe0+shift)*d)
# maximize U
m.Maximize(Jf)
# options
m.options.IMODE = 6 # optimal control
m.options.NODES = 3 # collocation nodes
m.options.SOLVER = 3 # solver (IPOPT)
# solve optimization problem
m.solve()
# print profit
print('Optimal Profit: ' + str(Jf.value[0]))
Case 4. change objective function from m.log to simple sum, and remove "variable" d from the objective function. Error occurs
from gekko import GEKKO
import pandas as pd
import numpy as np
dat = {'A0': [23221, 2198, 4296, 104906, 691], 'h': [0.04, 0.25, 0.07, 0, 12.58],'emax': [23221, 2198, 4296, 104906, 691] }
dftemp = pd.DataFrame(data=dat)
na=len(dftemp)
# time points
n=51
year=50
# constants
Pa0 = 3.061
Pe0 = 10.603
C0 = 100
r = 0.05
k=50
shift=10000000 # to make positive inside log function
ll=0.15
for i in range(0,na):
# create GEKKO model
m = GEKKO(remote=False)
m.time = np.linspace(0,year,n)
t=m.time
A0=dftemp.loc[i][0]
h=dftemp.loc[i][1]
emax=dftemp.loc[i][2]
A = m.SV(value=A0, lb=0, ub=A0)
E = m.SV(value=0, lb=0, ub=A0)
u = m.MV(value=0, lb=-emax, ub=emax)
u.STATUS = 1
t = m.Param(value=m.time)
C = m.Var(value=C0)
d = m.Var(value=1)
l = m.Param(value=ll)
# Equation
m.Equation(A.dt()==-u)
m.Equation(E.dt()==u)
m.Equation(C.dt()==-C/k)
m.Equation(d==m.exp(-t*r))
# Objective (Utility)
J = m.Var(value=0)
# Final objective
Jf = m.FV()
Jf.STATUS = 1
m.Connection(Jf,J,pos2='end')
m.Equation(J.dt() == ((A+E*(1-l))*h*Pa0-C*u+E*Pe0+shift))
# maximize U
m.Maximize(Jf)
# options
m.options.IMODE = 6 # optimal control
m.options.NODES = 3 # collocation nodes
m.options.SOLVER = 3 # solver (IPOPT)
# solve optimization problem
m.solve()
# print profit
print('Optimal Profit: ' + str(Jf.value[0]))
Case 5. change objective function from m.log to simple linear sum, and change shift to 0. Error occurs
from gekko import GEKKO
import pandas as pd
import numpy as np
dat = {'A0': [23221, 2198, 4296, 104906, 691], 'h': [0.04, 0.25, 0.07, 0, 12.58],'emax': [23221, 2198, 4296, 104906, 691] }
dftemp = pd.DataFrame(data=dat)
na=len(dftemp)
# time points
n=51
year=50
# constants
Pa0 = 3.061
Pe0 = 10.603
C0 = 100
r = 0.05
k=50
shift=0
ll=0.15
for i in range(0,na):
# create GEKKO model
m = GEKKO(remote=False)
m.time = np.linspace(0,year,n)
t=m.time
A0=dftemp.loc[i][0]
h=dftemp.loc[i][1]
emax=dftemp.loc[i][2]
A = m.SV(value=A0, lb=0, ub=A0)
E = m.SV(value=0, lb=0, ub=A0)
u = m.MV(value=0, lb=-emax, ub=emax)
u.STATUS = 1
t = m.Param(value=m.time)
C = m.Var(value=C0)
d = m.Var(value=1)
l = m.Param(value=ll)
# Equation
m.Equation(A.dt()==-u)
m.Equation(E.dt()==u)
m.Equation(C.dt()==-C/k)
m.Equation(d==m.exp(-t*r))
# Objective (Utility)
J = m.Var(value=0)
# Final objective
Jf = m.FV()
Jf.STATUS = 1
m.Connection(Jf,J,pos2='end')
m.Equation(J.dt() == ((A+E*(1-l))*h*Pa0-C*u+E*Pe0+shift)*d)
# maximize U
m.Maximize(Jf)
# options
m.options.IMODE = 6 # optimal control
m.options.NODES = 3 # collocation nodes
m.options.SOLVER = 3 # solver (IPOPT)
# solve optimization problem
m.solve()
# print profit
print('Optimal Profit: ' + str(Jf.value[0]))
