Optimization formulation with LLM assistance

Optimization formulation with LLM assistance

  • 60-minute LLM formulation activity
  • 120-minute examination preparation
  • 2105623 5 October 2026

Learning targets

  • Translate a process description into variables, an objective, and constraints.
  • Explain the units and physical meaning of every equation.
  • Audit an LLM proposal with counterexamples and independent checks.
  • Defend the model before interpreting its optimum.

Three-hour session

Minutes Activity
0–15 Independent formulation of a blending LP
15–40 LLM draft, counterexamples, and Pyomo checks
40–60 MILP pair activity and discussion
60–70 Break
70–120 LP and MILP examination review
120–160 Individual mock examination
160–180 Solutions and final checklist

Case A: the production assignment

  • A chemical plant blends two liquid feeds, A and B, to make a solvent mixture for a customer.
  • The customer requires exactly 100 metric tonnes of product per day. The plant must supply the full amount each day.
  • The product may contain at most 5 wt% of an impurity. This means no more than 5 kg of impurity per 100 kg of product.
  • As the process engineer, choose the daily amount of each feed to minimize the total daily feed purchase cost.

Case A: feed data and units

Feed Cost (USD/t) Impurity (wt%) Availability (t/day)
A 100 2 100
B 60 10 80

Prices apply to total feed mass, including impurity. Availability is an upper limit on daily purchases.

Case A: operating conditions

  • The feeds mix completely. No reaction, evaporation, separation, or mass loss occurs. All feed mass becomes product.
  • The plant consumes all purchased feed that day. It holds no inventory and allows no shortage, extra production, or disposal.
  • Feed quantities are continuous. Either feed may be unused. The only feed limits are the stated daily availabilities.
  • Mixing capacity is sufficient. Other operating costs are constant, and neither supplier charges a fixed fee in Case A.

Case A: independent formulation

  • Write each decision variable and its unit.
  • Write the cost objective and the total mass balance.
  • Write a contaminant balance inequality without a variable denominator.
  • State the variable bounds and classify the model.

Case A: variables and objective

x_A, x_B = feed mass flow rates (t/day)

minimize z = 100 x_A + 60 x_B

0 ≤ x_A ≤ 100 and 0 ≤ x_B ≤ 80

Objective units: (USD/t) × (t/day) = USD/day.

Case A: balances and quality

x_A + x_B = 100

0.02 x_A + 0.10 x_B ≤ 0.05 × 100

Both sides of the quality inequality have units of impurity t/day.

Case A: an analytic check

x_A = 100 − x_B

2 + 0.08 x_B ≤ 5, so x_B ≤ 37.5

z = 10,000 − 40 x_B

The cheapest feasible blend uses A = 62.5 and B = 37.5 t/day. Cost = 8,500 USD/day.

LLM assistance in the modeling workflow

  • Draft: extract data, decisions, and unresolved ambiguities.
  • Review: link every equation to a sentence in the problem.
  • Challenge: seek a counterexample and inspect units.
  • Implement: translate the accepted equations into Pyomo.

Prompt 1: formulation before code

Act as an optimization modeling tutor.
List sets, parameters, variables, domains, and units.
Identify missing information. Do not invent data.
Write the objective and constraints.
Map each equation to a sentence in the problem.
Check units and classify the model.
Do not write code yet.

Ambiguity needs an explicit assumption

  • “Meet demand” could mean exactly the demand or at least that amount.
  • A stated capacity might apply per hour, per shift, or per day.
  • “Use feed B” might mean purchasing, consuming, or activating a supplier.
  • The modeler resolves these choices and records the assumption.

When the problem leaves a material choice unclear, ask before inserting a number.

Audit exercise: a flawed draft

minimize 100 x_A + 60 x_B

x_A + x_B ≤ 100

0.02 x_A + 0.10 x_B ≤ 0.05

x_A ≥ 0, x_B ≥ 0

Find the missing or incorrect requirements. This is a deliberately constructed draft, not a recorded LLM response.

Counterexamples expose semantic errors

Test Flawed draft Correct Case A
A = 0, B = 0 Allowed Fails the 100 t/day requirement
A = 62.5, B = 37.5 Rejected by wrong quality RHS Feasible, impurity = 5 wt%
A = 20, B = 80 Rejected Rejected, impurity = 8.4 wt%

A model audit checks what the equations allow and exclude.

Prompt 2: adversarial model review

Review this formulation without rewriting it first.
For each constraint, identify its physical meaning and units.
Find a point that the equations allow but the problem forbids.
Find a point that the problem allows but the equations exclude.
Check variable bounds and missing balances.
State what you could not verify.

Pyomo mirrors the accepted equations

import pyomo.environ as p
m = p.ConcreteModel()
m.A = p.Var(bounds=(0, 100))
m.B = p.Var(bounds=(0, 80))
m.mass = p.Constraint(expr=m.A + m.B == 100)
m.quality = p.Constraint(
    expr=0.02*m.A + 0.10*m.B <= 5)
m.obj = p.Objective(expr=100*m.A + 60*m.B)

Verification after solving

solver = p.SolverFactory("appsi_highs")
result = solver.solve(m, load_solutions=False)
assert p.check_optimal_termination(result)
m.solutions.load_from(result)
a, b = p.value(m.A), p.value(m.B)
assert abs(a + b - 100) < 1e-6
assert 0.02*a + 0.10*b <= 5 + 1e-6

Also check availability, domains, and the objective with an independent calculation.

Case B: the supplier contract

  • The plant still makes exactly 100 t/day with at most 5 wt% impurity. Feed prices, compositions, and operating conditions remain as in Case A.
  • Supplier B now offers an optional daily contract. Activating it costs 1,200 USD for that day, in addition to 60 USD per tonne of B.
  • An active contract requires the plant to purchase and consume between 20 and 80 t/day of B. With no contract, B supply is zero.
  • Supplier A remains available at 100 USD/t, up to 100 t/day, with no fixed charge or minimum purchase.

Case B: the engineering decision

  • Decide whether to activate supplier B and how many tonnes per day to obtain from each supplier.
  • Minimize daily feed purchases plus the daily activation fee. The product quantity and impurity specification must still hold.
  • First describe what an inactive and an active contract permit. Then choose variable domains and write the MILP formulation.
  • After solving, report the contract decision, both feed quantities, product impurity, and total daily cost. Compare with using only A.

Activation and minimum-lot constraints

y_B ∈ {0, 1}

20 y_B ≤ x_B ≤ 80 y_B

minimize 100 x_A + 60 x_B + 1,200 y_B

Keep the mass balance, quality inequality, and feed bounds from Case A.

Pair exercise: logic and an AI audit

  • Supplier B raises its minimum daily order from 20 to 40 t. The maximum remains 80 t/day and the fixed fee remains 1,200 USD/day.
  • Keep all other Case B data, including exactly 100 t/day of product and at most 5 wt% impurity. Revise the formulation.
  • Before solving, decide whether an active B contract can meet product quality. Ask an LLM to audit your logic and upper bound.
  • Submit the revised constraints and a numerical test of an active contract. State the optimal purchasing plan and daily cost.

10 minutes in pairs. Your explanation must stand on its own.

Common logical mistakes

Claim What the equation actually says
x_B ≤ M y_B means active implies flow Active permits zero flow unless a lower bound applies
y_B ≤ y_A means A requires B It means B requires A
x_B y_B is needed to charge activation A fixed charge uses F y_B
An arbitrary large M is harmless It can weaken the relaxation and numerical behavior

Activity reflection

  • What did the LLM help you express or notice?
  • Which proposed equation needed correction?
  • What numerical evidence supports your correction?

Keep your original formulation, AI draft, and corrected version.

Break

  • 10 minutes
  • Next: examination preparation

Diagnostic: five modeling decisions

    1. Is production a parameter or a decision variable?
    1. Does “at most 80 t/day” require ≤, =, or ≥?
    1. What are the units of concentration × flow?
    1. Does x ≤ My force production when y = 1?
    1. What does an optimal solver result establish?

Diagnostic: reasoning

  • Production is a decision when the planner can choose it.
  • “At most” gives an upper bound. A flow cap has units of flow.
  • Mass fraction × mass flow gives contaminant mass flow.
  • x ≤ My forces x = 0 when y = 0, if x ≥ 0.
  • Optimality concerns the encoded model under the solver’s criteria.

A formulation has a traceable structure

  • Sets and indices define what the model distinguishes.
  • Parameters contain supplied data with units.
  • Decision variables describe controllable quantities and their domains.
  • The objective measures the stated goal. Each constraint traces to a requirement.

Case A: the feasible blends

Quality changes the cheapest recipe

Maximum impurity A (t/day) B (t/day) Cost (USD/day)
4 wt% 75 25 9,000
5 wt% 62.5 37.5 8,500
6 wt% 50 50 8,000

Predict the direction before reading the numbers. Which constraint sets the recipe?

A tighter upper bound improves the relaxation

Quality and mass balance imply x_B ≤ 37.5

Therefore: 20 y_B ≤ x_B ≤ 37.5 y_B

Formulation LP relaxation cost (USD/day)
Availability bound M = 80 9,062.50
Quality bound M = 37.5 9,700.00
Integer optimum 9,700.00

The activation fee changes the preferred plan

B activation fee A (t/day) B (t/day) Total cost (USD/day)
1,200 USD/day 62.5 37.5 9,700
1,800 USD/day 100 0 10,000

Maximum variable-cost saving = 40 × 37.5 = 1,500 USD/day.

Mock examination: production with inventory

  • 40 minutes, including reading time.
  • Write a complete formulation and justify the decisions.
  • Use an independent attempt before consulting any tool.
  • The instructor will specify permitted tools for this practice.

The actual examination follows the course’s announced rules.

Mock case: two production periods

Parameter Period 1 Period 2
Demand (t) 40 60
Production cost (USD/t) 5 8
Production capacity (t/period) 80 80
Setup cost (USD/period) 100 100

Initial and final inventory are zero. Carrying 1 t from period 1 to period 2 costs 1 USD. Meet every demand on time. No backlog or disposal.

Mock case: tasks and marks

Task Marks
Variables, domains, and units 3
Objective with all cost terms 4
Period balances and boundary conditions 4
Production/setup linkage 3
Model class and assumptions 2
Optimal plan and capacity interpretation 4

Find the optimal plan. Then predict the effect of increasing period-1 capacity from 80 to 100 t.

Mock solution: formulation

x_t ≥ 0, s_t ≥ 0, y_t ∈ {0,1}, t = 1,2

minimize 5x₁ + 8x₂ + s₁ + 100(y₁ + y₂)

s_(t−1) + x_t = d_t + s_t

x_t ≤ 80 y_t, s₀ = 0, s₂ = 0

x and s are tonnes. Each y indicates whether the period incurs setup cost.

Mock solution: the optimal plan

Quantity Period 1 Period 2
Production (t) 80 20
Closing inventory (t) 40 0
Setup indicator 1 1

Production: 560 USD. Holding: 40 USD. Setups: 200 USD. Total: 800 USD.

Capacity can remove an entire setup

Period-1 capacity Production (t) Setups Total cost (USD)
80 t 80, 20 Both periods 800
100 t 100, 0 Period 1 only 660

The 140 USD saving combines cheaper early production with removal of period-2 setup.

A final model audit

  • Every variable has a meaning, unit, and domain.
  • Every required balance includes its boundary conditions.
  • Every implication works in both binary states.
  • Every big-M follows from a valid bound.
  • One feasible point and one rejected point agree with the problem.
  • The reported optimum passes physical and numerical checks.

References and teaching materials

  • Pyomo documentation: www.pyomo.org/documentation
  • NL4Opt competition report: arxiv.org/abs/2303.08233
  • Course books and Interactive Lab: www.skhgroup.net/teaching/2105623/
  • The blending and mock-exam data in this deck are synthetic.