Dynamic Optimization with AI Assistants
Objective: Preview the major topics of the course with Generative AI tutor prompts, set up a working GEKKO toolchain, solve and modify a classic optimal control problem, and practice auditing an AI-generated formulation. Estimated time: 1-2 hours.
Graduate research and industrial practice now assume fluency with AI assistants that derive equations, write GEKKO/Python code, and draft reports in seconds. This course uses that fluency deliberately: the AI is your tutor, your adversarial reviewer, and your debugging partner - never your engineer of record. Every assignment ends with a short report where you supply the correct formulations, numbers, and justifications. This first assignment sets that pattern.
Step 0: Set Up Your Tools
- Choose the AI assistant you will use this semester (e.g., ChatGPT, Claude, Gemini, or a coding agent). A free tier may not be sufficient for a full semester of use.
- Install the course TA skill in your assistant (instructions in the archive for Claude, ChatGPT/Codex, and Gemini). It makes the AI course-aware: the schedule, the TCLab labs, GEKKO IMODE conventions, the MHE and MPC objective forms used on this site, and the course AI policy.
- Install Python with gekko, numpy, and matplotlib (pip install gekko numpy matplotlib). Verify with import gekko; print(gekko.__version__).
- Bookmark the GEKKO documentation and the course schedule.
Step 1: Preview the Course with Tutor Prompts
Work through the four prompts below one at a time with your AI assistant. Answer its questions yourself before asking for explanations - the point is to locate what you already know.
Prompt 1 - Formulation (Weeks 1-2)
Prompt 2 - Estimation (Weeks 3-5)
Prompt 3 - Model Predictive Control (Weeks 8-10)
Prompt 4 - The Frontier (Weeks 10-11)
Step 2: Run and Modify a Classic Problem
This is the classic linear-quadratic benchmark (Example 1a of the course benchmark collection): find the control u(t) that minimizes the accumulated cost of state deviation and control effort,
$$\min_{u(t)} \; x_2(t_f) \quad \mathrm{with} \quad \frac{dx_1}{dt}=u, \quad \frac{dx_2}{dt}=x_1^2+u^2, \quad x_1(0)=1, \; x_2(0)=0, \; t_f=1$$
It has a known analytic solution $$x_1(t) = \cosh(1-t)/\cosh(1)$$ with optimal objective $$x_2(t_f)=\tanh(1)\approx 0.7616$$ - which makes it the right first problem: you can verify the optimizer instead of trusting it.
import numpy as np
import matplotlib.pyplot as plt
m = GEKKO()
nt = 101
m.time = np.linspace(0,1,nt)
x1 = m.Var(value=1)
x2 = m.Var(value=0)
u = m.Var(value=0)
p = np.zeros(nt); p[-1] = 1.0
final = m.Param(value=p)
m.Equation(x1.dt()==u)
m.Equation(x2.dt()==x1**2 + u**2)
m.Minimize(x2*final)
m.options.IMODE = 6 # simultaneous dynamic optimization
m.solve()
print(f'x2(tf) = {x2.value[-1]:.4f} (analytic: tanh(1) = {np.tanh(1):.4f})')
plt.plot(m.time, x1.value, label='x1')
plt.plot(m.time, u.value, label='u')
plt.plot(m.time, np.cosh(1-np.array(m.time))/np.cosh(1),'k--',label='x1 analytic')
plt.legend(); plt.xlabel('time'); plt.grid()
plt.show()
- Run the script and confirm the objective against tanh(1). How close is it with 101 time points? With 11?
- Modify it (pick two): (a) add the control bound -0.5 <= u <= 0.5 and explain what the trajectory does when the unconstrained optimum is cut off; (b) add the terminal constraint x1(tf) = 0 (m.fix_final(x1,0)) and report the new objective; (c) change the horizon to tf = 2 and predict, before running, whether the objective grows or shrinks.
- For each modification, write one sentence on why the solution changed the way it did. If you cannot explain it, ask the AI to interrogate you until you can.
Step 3: Audit an AI-Generated Formulation
AI assistants produce plausible GEKKO code with confidently wrong formulations. Practice catching them:
Audit the code before running it: check the IMODE against the task, count degrees of freedom, check how the objective is imposed in time, check bounds against the target, and check units. Commit to a diagnosis in writing, then run the code and see whether the symptom matches. Ask the AI to reveal the planted error only after your verdict.
What to Turn In
Submit a report (PDF, about 2 pages) that curates what you learned. You may use Generative AI to help write and format the report, but you are responsible for every claim in it. Answer these questions:
- From Step 1: for each of the four prompts, one thing you learned and one question you answered incorrectly, with the corrected answer.
- From Step 2: your objective values vs tanh(1) for two discretizations, plots for your two modifications, and the one-sentence physical explanation of each change.
- From Step 3: the planted error, whether your pre-run audit caught it, and the audit checklist item you will use all semester because of it.
- Which AI assistant and setup (with or without the TA skill) you will use this semester, and one observed difference in answer quality that a better prompt produced.