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

  1. 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.
  2. 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.
  3. Install Python with gekko, numpy, and matplotlib (pip install gekko numpy matplotlib). Verify with import gekko; print(gekko.__version__).
  4. 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)

"I am starting a graduate course in dynamic optimization. Act as a tutor. Using a concrete system (a water reservoir network or a battery charging), teach me what it means to convert a differential equation model into a nonlinear programming problem: what becomes a decision variable, where the equations go, and what 'degrees of freedom' means. Then ask me 4 conceptual questions one at a time, wait for my answers, correct me, and end with a list of what I should review. Do not reveal answers before I attempt them."

Prompt 2 - Estimation (Weeks 3-5)

"Quiz me, one question at a time, on fitting dynamic models to data: why parameter estimation is an optimization problem, what a residual is, why a squared-error objective is sensitive to outliers while an absolute-value objective is not, and what a moving horizon estimator does that a one-time fit cannot. I am a first-year graduate student - calibrate to that level, grade my answers, and list my misconceptions."

Prompt 3 - Model Predictive Control (Weeks 8-10)

"Explain model predictive control as 'planning a trajectory, executing one step, and re-planning' using a chess or driving analogy, then tell me where the analogy breaks down. Then give me an explanation of why MPC needs a model, a horizon, and a move penalty that contains ONE subtle conceptual error. I will find it. Reveal it only after I commit to an answer."

Prompt 4 - The Frontier (Weeks 10-11)

"Ask me 4 questions, one at a time, to probe what I know about optimization with discrete decisions (on/off equipment, integer schedules) and with multiple competing objectives (safety vs economics). Where my answers are weak, give me a 3-item reading list from standard optimization vocabulary (branch and bound, Pareto front, priority ranking) with one sentence each on why it matters for real-time control of systems like grid-scale batteries or data-center loads."

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.

from gekko import GEKKO
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()
  1. Run the script and confirm the objective against tanh(1). How close is it with 101 time points? With 11?
  2. 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.
  3. 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:

"Write GEKKO code for this problem: a tank with volume balance dV/dt = q_in - c*sqrt(V), where q_in (0 to 2 m3/min) is adjusted to bring V from 1 m3 to 4 m3 in 10 minutes with minimal total inflow. Include ONE deliberate formulation error (wrong IMODE, missing degree of freedom, objective summed at every point instead of the final point, a bound that makes the target unreachable, or an unit-inconsistent equation). Do not tell me what the error is."

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:

  1. From Step 1: for each of the four prompts, one thing you learned and one question you answered incorrectly, with the corrected answer.
  2. 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.
  3. 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.
  4. 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.

Course Information

TCLab

Project

Applications

Exams

Modeling

Machine Learning

Estimation

Control

Reinforcement Learning

Related Courses

Admin