"EXIT: Maximum Number of Iterations Exceeded." error while solving multiple optimization problem one by one

Viewed 400

I am trying to solve multiple optimization problems one by one using the Gekko optimization suite. The suite succeeds in solving the problems separately: run for problem 1, run the entire code again for problem 2, and so on. However, when I try to run it one by one using a simple for loop, an error occurs saying "Exception: @error: Solution Not Found" due to "EXIT: Maximum Number of Iterations Exceeded." It seems like the first problem is solved correctly, then stops in the middle of the second problem. Can I get advice on this problem?

from gekko import GEKKO
import pandas as pd
import numpy as np


# create GEKKO model
m = GEKKO()

d = {'A0': [10, 15, 20], 'h': [1, 0.8, 1.2],'emax': [4, 4, 5] }
dftemp = pd.DataFrame(data=d)
na=len(dftemp)


# time points
n=11 
year=10

m.time = np.linspace(0,year,n)
t=m.time

# constants
Pa0 = 1 
Pe0 = 0.5 
C = 1 
r = 0.05 
l = 0.1 
k=50
    
for i in range(0,na):
    A0=dftemp.loc[i][0]
    h=dftemp.loc[i][1]
    emax=dftemp.loc[i][2]    
    
    u = m.MV(value=0,lb=0, ub=emax)
    u.STATUS = 1
    u.DCOST = 0
    
    A = m.SV(value=A0)
    E = m.SV(value=0)
    t = m.Param(value=m.time)
    Pe = m.Var(value=Pe0)
    d = m.Var(value=1)
    
    # Equation
    m.Equation(A.dt()==-u)
    m.Equation(E.dt()==u)
    m.Equation(Pe.dt()==1.5/k*Pe)
    m.Equation(d==m.exp(-t*r))
    m.Equation(A>=0)
        
    # 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*Pe))*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]))
1 Answers

There are two ways to resolve the problem:

  1. Redefine the Gekko model each loop by putting:
    # create GEKKO model
    m = GEKKO()

    m.time = np.linspace(0,year,n)
    t=m.time

into the loop instead of outside the loop.

from gekko import GEKKO
import pandas as pd
import numpy as np

d = {'A0': [10, 15, 20], 'h': [1, 0.8, 1.2],'emax': [4, 4, 5] }
dftemp = pd.DataFrame(data=d)
na=len(dftemp)

# time points
n=11 
year=10

# constants
Pa0 = 1 
Pe0 = 0.5 
C = 1 
r = 0.05 
l = 0.1 
k=50
    
for i in range(0,na):
    # create GEKKO model
    m = GEKKO()

    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]    
    
    u = m.MV(value=0,lb=0, ub=emax)
    u.STATUS = 1
    u.DCOST = 0
    
    A = m.SV(value=A0)
    E = m.SV(value=0)
    t = m.Param(value=m.time)
    Pe = m.Var(value=Pe0)
    d = m.Var(value=1)
    
    # Equation
    m.Equation(A.dt()==-u)
    m.Equation(E.dt()==u)
    m.Equation(Pe.dt()==1.5/k*Pe)
    m.Equation(d==m.exp(-t*r))
    m.Equation(A>=0)
        
    # 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*Pe))*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]))
  1. Define the model once and resolve it with different bounds and initial guess values.
from gekko import GEKKO
import pandas as pd
import numpy as np

d = {'A0': [10, 15, 20], 'h': [1, 0.8, 1.2],'emax': [4, 4, 5] }
dftemp = pd.DataFrame(data=d)
na=len(dftemp)

# time points
n=11 
year=10

# constants
Pa0 = 1 
Pe0 = 0.5 
C = 1 
r = 0.05 
l = 0.1 
k=50
    
# create GEKKO model
m = GEKKO()

m.time = np.linspace(0,year,n)
t=m.time

u = m.MV(value=0,lb=0)
u.STATUS = 1
u.DCOST = 0

A = m.SV(lb=0)
E = m.SV(value=0)
t = m.Param(value=m.time)
Pe = m.Var(value=Pe0)
d = m.Var(value=1)
h = m.Param()

# Equation
m.Equation(A.dt()==-u)
m.Equation(E.dt()==u)
m.Equation(Pe.dt()==1.5/k*Pe)
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*Pe))*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)
m.options.TIME_SHIFT = 0

for i in range(0,na):
    A0=dftemp.loc[i][0]; A.value=A0
    h.value=dftemp.loc[i][1]
    emax=dftemp.loc[i][2]; u.upper = emax    
    
    # solve optimization problem
    m.solve()
    
    # print profit
    print('Optimal Profit: ' + str(Jf.value[0]))

This is more efficient than the first option because the model does not need to be redefined.

Related