Generative AI for Solving Optimization Problems – Is it helpful?

Mario Ruthmair

EURO 2025, Leeds

Where can GenAI “reasonably” help?

  • Identifying optimization problems
  • Translating business optimization problems to mathematical formulations (?)
  • Generating prototype implementations
  • Interpreting and visualizing solutions
  • Debugging

What about the model solving process?

  • Solvers are currently used by GenAI as black-box tools
  • Can GenAI create a solution algorithm for a specific problem?

What does Gurobi offer?

  • OpenAI Custom GPTs:
    • Prompt Engineer
    • Modeling Assistant
    • Gurobot (discontinued)
  • Guide for using custom GPTs
  • New agentic framework Gurobot

Gurobi AI Modeling Prompt Engineer

  • OpenAI custom GPT
  • Identify optimization problem
  • Create reasonable prompt for Modeling Assistant

Gurobi AI Modeling Prompt Engineer

System prompt to give context and define reponse structure:

I want you to act as an expert in Operations Research, helping 
users identify and describe their optimization problem clearly.

I likely have an optimization problem on my hands but, I need help 
with identifying and formulating it.
Your response will be depending on whether we are in Step 1 or 2.

If we are in Step 1, we should have a discussion to make sure we 
both understand the context, variables and constraints.
You need to ask me questions so that we can together identify a 
proper optimization problem and its components.
This discussion will continue until we have a good understanding of 
the problem. After we have that, we move on to Step 2.

Formulate the problem in a structured manner. In the end, the 
problem description will be given for review to upper management. 
This means that the context needs to be stated explicitly, and the 
constraints are stated explicitly rather than implicitly. Your 
response should ONLY follow the following template:

## 📝 Problem description
Context: what is the problem on a high level, what industry and 
role are we in, etc.

Objective: 
objective description

Constraints:
constraints in an itemized list

Data:
necessary data in an itemized list, if applicable, specify which 
columns are expected

## ⏯️ Next steps
Now that you have a problem description:
1. review the details and, if necessary, either discuss here or 
adjust manually.
2. prepare your data files (real or dummy data).
3. copy-paste the Problem Description and paste it together with 
the data files into [Gurobi AI Modeling Assistant]
(https://chatgpt.com/g/g-g69cy3XAp-gurobi-ai-modeling-assistant).

Gurobi AI Modeling Assistant

  • OpenAI custom GPT
  • Translate to math formulation
  • Generate gurobipy code and run it
  • Size-limited license

Gurobi AI Modeling Assistant

System prompt:

I want you to act as an expert in optimization and Python 
programming, specifically using gurobipy.

I will write you a problem description and you guide me to convert
this problem into an optimization model, solve it and explain the 
results.

Your response will follow the template:

Install gurobipy using Code Interpreter: 
`!pip install /mnt/data/gurobipy-12.0.1-cp311-cp311-manylinux2014_
x86_64.manylinux_2_17_x86_64.whl`

If the user provides any data files, read them and make sure you 
understand the complete context before moving on.

## 🔢 Problem definition
Convert the problem description into a mathematical model. Use 
proper mathematical notations

## 🐍 Build and solve model
Create and solve the model using Code Interpreter.  ANY TIME YOU 
WRITE CODE YOU DO THIS IN A Code Interpreter & Data Analysis BLOCK 
THAT YOU EXECUTE, YOU NEVER WRITE CODE AS TEXT. 
Model non-divisible items as integer variables.

## 👩‍🏫 Results
Display solution, objective values and decision variables. Note any 
unaddressed aspects.

List the gurobipy version used (infer from wheel gurobipy wheel name)

Close with the following sentence: "For more guidance, see the 
[documentation](https://gurobi-ai-modeling.readthedocs.io/) for this 
Custom GPT"

Hints for Prompt Engineering

  • Assume a persona: “You are a seasoned expert in mathematical optimization and Python programming, with extensive experience using the gurobipy library for solving complex optimization problems.”
  • Answer engineering: Break down query in smaller steps (Chain-of-Thought), define answer template
  • Ask for mathematical model with variables, constraints, objective
  • Provide data with description
  • Ask to generate and run the code, use the correct API

Guide for Using Custom GPTs

Gurobot

  • Launch Date Today!
  • Agentic framework in our Gurobi User Portal: https://portal.gurobi.com/iam/chat
  • Based on Anthropic Claude Sonnet 4 (reasoning)
  • Includes links to Gurobi documentation
  • Can access your user account + licenses
  • Provides Python code and runs it to validate syntax
  • Easy escalation to Experts team
  • Daily usage limits

Gurobot Architecture

Gurobot: 2D Bin Packing

Prompt

Assume there is a large rectangle.

I need to pack a given set of smaller rectangles into the large one.

Write a linear mathematical problem formulation for this feasibility problem.

Response

Here’s a comprehensive linear mathematical formulation for the 2D rectangle packing feasibility problem:

Context

We have a large rectangle (container) with dimensions \(W \times H\) and a set of smaller rectangles that need to be packed into it without overlapping. This is a feasibility problem to determine if all rectangles can fit.

Mathematical Formulation

Sets and Indices
  • \(I = \{1, 2, \ldots, n\}\): set of rectangles to be packed
  • \(i, j \in I\): indices for rectangles
Parameters
  • \(W, H\): width and height of the large container rectangle
  • \(w_i, h_i\): width and height of rectangle \(i \in I\)
  • \(M\): a large constant (big-M parameter), typically \(M = \max(W, H)\)
Decision Variables
  • \(x_i, y_i \geq 0\): coordinates of the bottom-left corner of rectangle \(i\)
  • \(r_i \in \{0, 1\}\): rotation variable (1 if rectangle \(i\) is rotated 90°, 0 otherwise)
  • \(\alpha_{ij}, \beta_{ij}, \gamma_{ij}, \delta_{ij} \in \{0, 1\}\): binary variables for non-overlapping constraints between rectangles \(i\) and \(j\)
Objective Function

Since this is a feasibility problem, we can use a dummy objective: \[\text{minimize } \quad 0\]

Constraints

1. Container Bounds: For each rectangle \(i \in I\): \[x_i + w_i (1 - r_i) + h_i r_i \leq W\] \[y_i + h_i (1 - r_i) + w_i r_i \leq H\]

2. Non-overlapping Constraints: For each pair of rectangles \(i, j \in I\) where \(i < j\):

\[x_i + w_i (1 - r_i) + h_i r_i \leq x_j + M(1 - \alpha_{ij})\] \[x_j + w_j (1 - r_j) + h_j r_j \leq x_i + M(1 - \beta_{ij})\] \[y_i + h_i (1 - r_i) + w_i r_i \leq y_j + M(1 - \gamma_{ij})\] \[y_j + h_j (1 - r_j) + w_j r_j \leq y_i + M(1 - \delta_{ij})\]

3. Logical Constraints: For each pair \(i, j \in I\) where \(i < j\): \[\alpha_{ij} + \beta_{ij} + \gamma_{ij} + \delta_{ij} \geq 1\]

4. Variable Domains: \[x_i, y_i \geq 0 \quad \forall i \in I\] \[r_i, \alpha_{ij}, \beta_{ij}, \gamma_{ij}, \delta_{ij} \in \{0, 1\} \quad \forall i, j \in I, i < j\]

Explanation of Constraints

  • Container bounds ensure each rectangle fits within the container boundaries, accounting for possible rotation
  • Non-overlapping constraints use the “big-M” technique where exactly one of the four relative position constraints must be active for each pair of rectangles
  • Logical constraints ensure that for each pair of rectangles, at least one spatial relationship (left, right, below, above) is enforced
  • The rotation variable \(r_i\) allows rectangles to be rotated 90° if beneficial

Alternative Formulation (Without Rotation)

If rotation is not allowed, the formulation simplifies by removing \(r_i\) variables:

Container Bounds: \[x_i + w_i \leq W \quad \forall i \in I\] \[y_i + h_i \leq H \quad \forall i \in I\]

Non-overlapping Constraints: \[x_i + w_i \leq x_j + M(1 - \alpha_{ij}) \quad \forall i, j \in I, i < j\] \[x_j + w_j \leq x_i + M(1 - \beta_{ij}) \quad \forall i, j \in I, i < j\] \[y_i + h_i \leq y_j + M(1 - \gamma_{ij}) \quad \forall i, j \in I, i < j\] \[y_j + h_j \leq y_i + M(1 - \delta_{ij}) \quad \forall i, j \in I, i < j\]

This formulation creates a mixed-integer linear programming (MILP) problem that can be solved efficiently with Gurobi.

References:

Gurobot: 2D Bin Packing (cont.)

Prompt

Provide Python code based on gurobipy for the problem without rotation.

Response

Here’s a comprehensive Python implementation using gurobipy for the 2D rectangle packing problem without rotation:

Key Implementation Features

  • Object-oriented design with separate methods for data creation, model building, and solution display
  • Context manager usage for proper Gurobi environment handling
  • Big-M formulation for non-overlapping constraints
  • Comprehensive constraint modeling following the mathematical formulation
  • Solution verification and visualization

Important Code Snippets

Decision Variables
# Position variables (bottom-left corner coordinates)
self.x_vars = self.model.addVars(n, vtype=GRB.CONTINUOUS, lb=0, name="x")
self.y_vars = self.model.addVars(n, vtype=GRB.CONTINUOUS, lb=0, name="y")

# Binary variables for non-overlapping constraints
self.alpha_vars = self.model.addVars(n, n, vtype=GRB.BINARY, name="alpha")
self.beta_vars = self.model.addVars(n, n, vtype=GRB.BINARY, name="beta")
self.gamma_vars = self.model.addVars(n, n, vtype=GRB.BINARY, name="gamma")
self.delta_vars = self.model.addVars(n, n, vtype=GRB.BINARY, name="delta")
Non-overlapping Constraints
# For each pair of rectangles (i,j), exactly one spatial relationship must hold
for i in range(n):
    for j in range(i + 1, n):
        w_i, h_i = self.rectangles[i]
        w_j, h_j = self.rectangles[j]
        
        # Four possible relative positions using big-M
        self.model.addConstr(
            self.x_vars[i] + w_i <= self.x_vars[j] + M * (1 - self.alpha_vars[i, j])
        )
        # ... (similar for other three directions)
        
        # Logical constraint: at least one position must be enforced
        self.model.addConstr(
            self.alpha_vars[i, j] + self.beta_vars[i, j] + 
            self.gamma_vars[i, j] + self.delta_vars[i, j] >= 1
        )

Test Results

The implementation successfully:

  • Solves feasible instances: Found optimal packing for 5 rectangles in a 10×8 container
  • Detects infeasibility: Correctly identifies when rectangles cannot fit (e.g., total area exceeds container area)
  • Provides detailed solution: Shows exact coordinates and verification metrics

Key Gurobi Best Practices Applied

  1. Context managers for environment handling
  2. Variable lower bounds: Variables have lb=0 by default in Gurobi
  3. Efficient constraint generation using loops and proper indexing
  4. Big-M parameter selection: M = max(container_width, container_height)
  5. Proper variable types: Continuous for positions, binary for logical constraints

The model generates approximately O(n²) binary variables and constraints for n rectangles, making it suitable for small to medium-sized instances (typically up to 20-50 rectangles depending on computational resources).

Attachments:

  • script.py
  • script.log

Gurobot: Capacitated Vehicle Routing Problem

Prompt

Show a single-commodity flow formulation for the capacitated vehicle routing problem.

Response

I’ll present the single-commodity flow formulation for the Capacitated Vehicle Routing Problem (CVRP). This formulation treats the problem as a network flow problem where a single commodity (representing vehicle capacity) flows through the network.

Problem Context

The CVRP involves finding optimal routes for a fleet of identical vehicles with limited capacity \(Q\) to serve a set of customers with known demands, starting and ending at a central depot, while minimizing total travel cost.

Mathematical Formulation

Sets and Parameters
  • \(V = \{0, 1, 2, \ldots, n\}\): Set of nodes (0 = depot, \(1, \ldots, n\) = customers)
  • \(A\): Set of arcs \((i,j)\) where \(i, j \in V\)
  • \(c_{ij}\): Travel cost from node \(i\) to node \(j\)
  • \(d_i\): Demand of customer \(i\) (with \(d_0 = 0\) for the depot)
  • \(Q\): Vehicle capacity
  • \(K\): Number of available vehicles
Decision Variables
  • \(x_{ij} \in \{0,1\}\): Binary variable equal to 1 if arc \((i,j)\) is traversed, 0 otherwise
  • \(f_{ij} \geq 0\): Flow of commodity on arc \((i,j)\) (representing cumulative demand served)
Objective Function

\[\min \sum_{(i,j) \in A} c_{ij} x_{ij}\]

Constraints

1. Flow Conservation at Depot: \[\sum_{j \in V \setminus \{0\}} f_{0j} = \sum_{i=1}^{n} d_i\] \[\sum_{i \in V \setminus \{0\}} f_{i0} = 0\]

2. Flow Conservation at Customer Nodes: \[\sum_{j \in V \setminus \{i\}} f_{ij} - \sum_{j \in V \setminus \{i\}} f_{ji} = d_i \quad \forall i \in \{1, \ldots, n\}\]

3. Vehicle Flow Constraints: \[\sum_{j \in V \setminus \{0\}} x_{0j} \leq K\] \[\sum_{i \in V \setminus \{0\}} x_{i0} = \sum_{j \in V \setminus \{0\}} x_{0j}\]

4. Customer Visit Constraints: \[\sum_{i \in V \setminus \{j\}} x_{ij} = 1 \quad \forall j \in \{1, \ldots, n\}\] \[\sum_{j \in V \setminus \{i\}} x_{ij} = 1 \quad \forall i \in \{1, \ldots, n\}\]

5. Flow-Arc Relationship: \[f_{ij} \leq Q \cdot x_{ij} \quad \forall (i,j) \in A\]

6. Capacity Constraints: \[f_{ij} \leq Q \quad \forall (i,j) \in A\]

7. Non-negativity and Binary Constraints: \[x_{ij} \in \{0,1\} \quad \forall (i,j) \in A\] \[f_{ij} \geq 0 \quad \forall (i,j) \in A\]

Key Features of This Formulation

  • Single Commodity: The flow variables \(f_{ij}\) represent the cumulative demand being carried on each arc
  • Capacity Enforcement: Constraint 5 ensures that flow can only occur on selected arcs and respects vehicle capacity
  • Subtour Elimination: The flow conservation constraints naturally eliminate subtours that don’t include the depot
  • Polynomial Size: This formulation has \(O(n^2)\) variables and constraints, making it more compact than some alternative formulations

Advantages and Limitations

Advantages:

  • Compact formulation with polynomial number of variables
  • Natural subtour elimination through flow constraints
  • Strong LP relaxation bounds

Limitations:

  • Can be computationally challenging for large instances
  • May require additional valid inequalities for tighter bounds
  • Flow variables are continuous, which may lead to fractional solutions in the LP relaxation

Learn more:

Gurobot: Capacitated Vehicle Routing Problem – Try 2

Prompt

Show a single-commodity flow formulation for the capacitated vehicle routing problem.

Response

I’ll present a comprehensive single-commodity flow formulation for the Capacitated Vehicle Routing Problem (CVRP). This formulation treats the problem as a network flow problem where each vehicle route corresponds to a flow of a single commodity.

Problem Context

The CVRP involves finding optimal routes for a fleet of vehicles with limited capacity to serve a set of customers from a central depot, minimizing total travel cost while ensuring all customers are visited exactly once.

Mathematical Formulation

Sets and Parameters
  • \(V = \{0, 1, 2, \ldots, n\}\): Set of nodes where \(0\) is the depot and \(\{1, 2, \ldots, n\}\) are customers
  • \(A = \{(i,j) : i, j \in V, i \neq j\}\): Set of arcs
  • \(K\): Number of available vehicles
  • \(Q\): Vehicle capacity
  • \(d_i\): Demand of customer \(i\) (with \(d_0 = 0\) for the depot)
  • \(c_{ij}\): Cost of traveling from node \(i\) to node \(j\)
Decision Variables
  • \(x_{ij}^k \in \{0,1\}\): Binary variable equal to 1 if vehicle \(k\) travels directly from node \(i\) to node \(j\), 0 otherwise
  • \(f_{ij}^k \geq 0\): Flow of commodity (load) on arc \((i,j)\) for vehicle \(k\)
Objective Function

\[\min \sum_{k=1}^{K} \sum_{(i,j) \in A} c_{ij} x_{ij}^k\]

Constraints

1. Customer Visit Constraints: Each customer must be visited exactly once by exactly one vehicle: \[\sum_{k=1}^{K} \sum_{j \in V} x_{ij}^k = 1 \quad \forall i \in V \setminus \{0\}\]

2. Flow Conservation at Customers: For each customer, inflow equals outflow for each vehicle: \[\sum_{j \in V} x_{ji}^k = \sum_{j \in V} x_{ij}^k \quad \forall i \in V, \forall k \in K\]

3. Depot Constraints: Each vehicle can leave and return to the depot at most once: \[\sum_{j \in V \setminus \{0\}} x_{0j}^k \leq 1 \quad \forall k \in K\] \[\sum_{i \in V \setminus \{0\}} x_{i0}^k \leq 1 \quad \forall k \in K\]

4. Commodity Flow Conservation: At each customer node, the flow decreases by the customer’s demand: \[\sum_{j \in V} f_{ji}^k - \sum_{j \in V} f_{ij}^k = d_i \sum_{j \in V} x_{ji}^k \quad \forall i \in V \setminus \{0\}, \forall k \in K\]

At the depot, the outgoing flow equals the total demand served by each vehicle: \[\sum_{j \in V \setminus \{0\}} f_{0j}^k - \sum_{i \in V \setminus \{0\}} f_{i0}^k = -\sum_{i \in V \setminus \{0\}} d_i \sum_{j \in V} x_{ij}^k \quad \forall k \in K\]

5. Flow-Arc Relationship: Flow can only exist on arcs that are traversed: \[f_{ij}^k \leq Q \cdot x_{ij}^k \quad \forall (i,j) \in A, \forall k \in K\]

6. Capacity Constraints: The flow on any arc cannot exceed vehicle capacity: \[f_{ij}^k \leq Q \quad \forall (i,j) \in A, \forall k \in K\]

7. Non-negativity and Binary Constraints: \[x_{ij}^k \in \{0,1\} \quad \forall (i,j) \in A, \forall k \in K\] \[f_{ij}^k \geq 0 \quad \forall (i,j) \in A, \forall k \in K\]

Key Features of this Formulation

  • Single-commodity interpretation: Each vehicle carries a single type of “commodity” (the goods to be delivered)
  • Flow variables: The \(f_{ij}^k\) variables track the load carried by each vehicle on each arc
  • Subtour elimination: The flow conservation constraints naturally eliminate subtours without requiring additional subtour elimination constraints
  • Capacity enforcement: The flow constraints ensure that vehicle capacity is never exceeded

This formulation is particularly effective for medium-sized instances and provides a strong linear programming relaxation compared to other CVRP formulations.

Learn more:

Gurobot: Capacitated Vehicle Routing Problem – Try 2 (cont.)

Prompt

Do we need the vehicle index k?

Response

Excellent question! You’re absolutely right to question this. In a true single-commodity flow formulation, we typically do not need the vehicle index \(k\). The formulation I provided was actually a vehicle-indexed formulation. Let me present the cleaner single-commodity flow formulation without the vehicle index:

Single-Commodity Flow Formulation (Without Vehicle Index)

Decision Variables
  • \(x_{ij} \geq 0\): Number of times arc \((i,j)\) is traversed (can be fractional in LP relaxation, integer in optimal solution)
  • \(f_{ij} \geq 0\): Flow of commodity on arc \((i,j)\)
Objective Function

\[\min \sum_{(i,j) \in A} c_{ij} x_{ij}\]

Constraints

1. Customer Visit Constraints: Each customer must be visited exactly once: \[\sum_{j \in V} x_{ij} = 1 \quad \forall i \in V \setminus \{0\}\]

2. Flow Conservation: \[\sum_{j \in V} x_{ji} = \sum_{j \in V} x_{ij} \quad \forall i \in V\]

3. Commodity Flow Conservation: At each customer, flow decreases by the customer’s demand: \[\sum_{j \in V} f_{ji} - \sum_{j \in V} f_{ij} = -d_i \quad \forall i \in V \setminus \{0\}\]

At the depot, net outflow equals total demand: \[\sum_{j \in V \setminus \{0\}} f_{0j} - \sum_{i \in V \setminus \{0\}} f_{i0} = \sum_{i=1}^{n} d_i\]

4. Flow-Arc Relationship: \[f_{ij} \leq Q \cdot x_{ij} \quad \forall (i,j) \in A\]

5. Vehicle Count Constraint: \[\sum_{j \in V \setminus \{0\}} x_{0j} \leq K\]

6. Integrality (for exact solution): \[x_{ij} \in \mathbb{Z}_+ \quad \forall (i,j) \in A\]

Key Advantages of Removing Vehicle Index

  1. Fewer variables: \(O(n^2)\) instead of \(O(Kn^2)\) variables
  2. Simpler formulation: No need to track which specific vehicle uses each arc
  3. Better LP relaxation: Often provides tighter bounds
  4. Natural interpretation: Focuses on the network flow structure rather than individual vehicle assignments

The vehicle assignment can be recovered from the solution during post-processing if needed, but the optimization doesn’t require explicit vehicle indexing.

Gurobot: Capacitated Vehicle Routing Problem – Try 2 (cont.)

Prompt

Create Python code for this formulation based on gurobipy. Generate a small test instance with 3 vehicles and 5 customers.

Response

Perfect! The model is now working correctly. Here’s the complete Python implementation of the single-commodity flow formulation for CVRP using gurobipy:

Key Implementation Highlights

The code successfully demonstrates several important aspects:

1. Object-Oriented Design
  • Separates data creation, model building, and solution display into different methods
  • Uses proper initialization with dict() constructor to avoid potential issues
2. Gurobipy Best Practices
  • Uses addVars() for efficient variable creation
  • Employs quicksum() for constraint building
  • Implements proper variable bounds and types
3. Mathematical Formulation Implementation

The code implements the key constraints:

  • Customer visit constraints: Each customer visited exactly once
  • Flow conservation: Inflow equals outflow at each node
  • Commodity flow conservation: Flow decreases by demand at each customer
  • Flow-arc relationship: Flow only exists on traversed arcs
  • Vehicle count constraint: Limits number of vehicles used
  • Capacity constraints: Flow cannot exceed vehicle capacity
4. Solution Results

The optimal solution found:

  • Single route: 0 → 5 → 2 → 4 → 3 → 1 → 0
  • Total demand served: 17 units (within capacity of 20)
  • Total distance: 24.45 units
  • Vehicles used: 1 out of 3 available
5. Flow Interpretation

The flow variables show how the load decreases along the route:

  • Starts with 17 units leaving depot (0 → 5)
  • Reduces to 14 units after serving customer 5 (5 → 2)
  • Continues decreasing as each customer is served
  • Ends with 3 units before serving the last customer

This demonstrates the power of the single-commodity flow formulation in naturally handling capacity constraints and subtour elimination through flow conservation.

Would you like me to explain any specific part of the formulation in more detail, or would you like to see how to extend this model to handle additional constraints like time windows or multiple depots?

Attachments:

  • script.py
  • script.log

Gurobi Intelligence Vision

Conclusions

  • GenAI can indeed be helpful in the problem solving workflow
    • Formal problem definition
    • Mathematical formulation \(\rightarrow\) Be careful!
    • Code implementation (using a solver as black-box)
    • Solution presentation
  • If you are not an OR expert, can you evaluate whether an AI-generated formulation is correct?



Thank you for your attention!