The original question can be found here.
I am a beginner at using this package, and most of the code given below has been borrowed from the examples given in the official documentation for GEKKO.
Usual imports:
from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
Initializing the model and defining the time points:
m = GEKKO() # initialize gekko
nt = 501
m.time = np.linspace(0.0,1.0,nt)
t = m.Param(value=m.time)
p = np.zeros(nt) # mark final time point
p[-1] = 1.0
final = m.Param(value=p)
Defining the state and control variables:
x = m.Var(value=0.0)
y = m.Var(value=0.0)
z = m.Var(value=0.0)
V = m.FV(value=2.0, lb=0.0, ub=100.0)
V.STATUS = 1
theta = m.CV(1.0)
theta.STATUS = 1
theta.SPHI = 0
theta.SPLO = 6.28
V was chosen as a Fixed Variable since the problem statement mentions that:
V is a constant scalar speed.
theta has been assumed to be in [0,2*pi] since it hasn't been directly mentioned in the problem statement.
z is used as a proxy variable in order to implement the integral objective.
The constraint equations are defined:
m.Equation(x.dt()==V*m.cos(theta))
m.Equation(y.dt()==V*m.sin(theta))
m.Equation(x*final==1.2)
m.Equation(y*final==1.6)
m.Equation(((x-0.4)**2)+((y-0.5)**2)>=0.1)
m.Equation(((x-0.8)**2)+((y-1.5)**2)>=0.1)
Defining the variables needed in order to obtain 2nd order derivatives:
dx = m.Var(value = 0.0)
dy = m.Var(value = 0.0)
ddx = m.Var(value = 0.0)
ddy = m.Var(value = 0.0)
Corresponding equations to make this work:
m.Equation(dx==x.dt())
m.Equation(dy==y.dt())
m.Equation(ddx==dx.dt())
m.Equation(ddy==dy.dt())
Setting the proxy variable's derivatives as the objective inside the integral and solving:
m.Equation(z.dt()==(ddx**2)+(ddy**2))
m.Obj(z*final)
m.options.IMODE = 6 # optimal control mode
m.solve(disp=True)
I am not completely sure if the way I have coded the given question is correct. As the given link shows, a solution to the problem exists. I have tried using APOPT and IPOPT to solve this problem, and both solvers are unable to find a solution. COLD_START cannot be used since DOF < 0.
Any help with this would be greatly appreciated!
