Solving Optimal Control with Fixed-end-point with Gekko

361 views Asked by At

I wanna solve an optimal control problem with the fixed-end-point condition on state variables in Gekko. I like to know how it is possible to define an optimal control problem with fixed-end-point as follows in Gekko:

$\min \int_{0}^{1} u^2+(u-x)^2 $

$\dot{x}=x+u$

$x(0)=1$

$x(1)=4$

Moreover, how can I retrieve the objective function value?

1

There are 1 answers

4
John Hedengren On

You can retrieve the objective function value with m.options.OBJFCNVAL. Here is a similar script to some of the Benchmark problems (see 1b in particular).

Optimization solution

import numpy as np
from gekko import GEKKO
m = GEKKO()
nt = 101; m.time = np.linspace(0,1,nt)
x = m.Var(1)
u = m.Var()
p = np.zeros(nt); p[-1] = 1.0; final = m.Param(value=p)
m.Equation(x.dt()==x+u)
m.Equation(final*(x-4)==0)
m.Minimize(m.integral(u**2+(u-x)**2)*final)

m.options.IMODE = 6; m.solve()
print('Objective: ' + str(m.options.OBJFCNVAL))

import matplotlib.pyplot as plt
plt.plot(m.time,x.value,'k:',LineWidth=2,label=r'$x$')
plt.plot(m.time,u.value,'b--',LineWidth=2,label=r'$u$')
plt.legend(loc='best'); plt.xlabel('Time')
plt.show()