Gurobi Optimizer: Technical Handbook/Ch 1. Introduction to Mathematical Optimization and Gurobi

Chapter 1

Introduction to Mathematical Optimization and Gurobi

15 min read

0:00
1x

Chapter Summary

# Chapter 1: Introduction to Mathematical Optimization and Gurobi Welcome to the world of Mathematical Optimization. In modern enterprise computing, machine learning often gets the lion's share of attention for predicting the future: predicting customer churn, estimating demand, or classifying imag...

Chapter 1: Introduction to Mathematical Optimization and Gurobi

Welcome to the world of Mathematical Optimization. In modern enterprise computing, machine learning often gets the lion's share of attention for predicting the future: predicting customer churn, estimating demand, or classifying images. But prediction is only half the battle.

Once you know what will likely happen, what decision should you make?

This is where mathematical optimization steps in. Instead of guessing, running simple heuristics, or using trial-and-error simulations, optimization mathematically proves which decision is the absolute best among millions—or billions—of possibilities.

In this handbook, we will master Gurobi, the industry-standard, state-of-the-art mathematical programming solver designed to tackle the world's most complex computational decision problems.


1. What is Mathematical Optimization?

At its core, mathematical optimization (often called Mathematical Programming) is a declarative approach to problem-solving. Rather than programming an explicit step-by-step algorithm to find a solution, you formulate the problem as a set of mathematical relationships:

  1. Decision Variables: The choices you need to make (e.g., How many units of Product A should we ship from Warehouse 1 to Store B?).
  2. Objective Function: The metric you want to maximize or minimize (e.g., Minimize total logistics cost or Maximize factory throughput).
  3. Constraints: The real-world limitations you must respect (e.g., Truck capacities, labor laws, factory machine availability, or customer demand).
text
   +-------------------------------------------------------+
   |                The Optimization Model                 |
   |                                                       |
   |   MINIMIZE / MAXIMIZE  -->  Objective Function        |
   |   SUBJECT TO           -->  Business Constraints      |
   |   BY ADJUSTING         -->  Decision Variables        |
   +-------------------------------------------------------+

Once formulated, you feed this model into an optimization engine—such as Gurobi—which navigates the solution space using advanced algorithms (Simplex, Barrier, Branch-and-Bound, and Cutting Planes) to find the mathematically optimal answer.

Key Insight: Machine learning answers the question: "What is likely to happen?" Mathematical optimization answers the far more valuable operational question: "Given our limitations, what is the best possible action to take?"


2. A Taxonomy of Optimization Problems

Before writing code, it is vital to identify the mathematical category of your problem. Gurobi solves several classes of problems, but the primary ones you will encounter are:

Linear Programming (LP)

In an LP, the objective function and all constraints are strictly linear relationships, and all decision variables are continuous (can take any fractional value). LPs are computationally "easy" and can solve with millions of variables in seconds.

Mixed-Integer Linear Programming (MIP or MILP)

In the real world, you cannot hire $2.4$ workers, build $0.7$ warehouses, or fly half an airplane. When some or all variables must be integers (or binary: 0 or 1), the problem becomes a Mixed-Integer Linear Program. MIPs are NP-hard, making them computationally challenging, but Gurobi is specifically engineered to be the fastest MIP solver in the world.

Quadratic Programming (QP / MIQP)

If your objective includes squared terms (such as minimizing risk variance in financial portfolios), your problem is a Quadratic Program. Gurobi natively handles convex and non-convex quadratic objectives and constraints.


3. Real-World Example: The Agile Cloud Microservice Allocator

To see Gurobi in action, let’s consider a real-world IT infrastructure problem: Cloud Resource Allocation.

The Scenario

You manage a platform running microservices. You have incoming client requests that require computation. You can deploy these services across two types of cloud instances:

  • On-Demand Instances: Cost $$0.10$ per hour, provide $4\text{ GB}$ of RAM, can scale freely.
  • Spot Instances: Cost $$0.04$ per hour, provide $2\text{ GB}$ of RAM, but availability is capped by the provider at a maximum of $10$ instances.

Your Objective: Minimize total hosting cost.
Your Demand Requirement: The platform requires at least $50\text{ GB}$ of total RAM to keep services responsive.
Constraint: You can only provision whole (integer) servers.


4. Building Your First Model with Gurobi Python (gurobipy)

Let's implement this problem using Python and the

text
gurobipy
API.

Installation

If you haven't installed Gurobi yet, run:

bash
pip install gurobipy

(Note: Without a license, Gurobi runs in restricted trial mode, which is sufficient for this introductory example.)

The Code

python
import gurobipy as gp
from gurobipy import GRB

# 1. Initialize the Environment and Model
model = gp.Model("Cloud_Resource_Allocation")

# 2. Add Decision Variables
# Integer variables for server counts
on_demand = model.addVar(vtype=GRB.INTEGER, name="OnDemand_Instances")
spot = model.addVar(vtype=GRB.INTEGER, name="Spot_Instances")

# 3. Define the Objective Function (Minimize Cost)
# On-Demand: $0.10, Spot: $0.04
model.setObjective(0.10 * on_demand + 0.04 * spot, GRB.MINIMIZE)

# 4. Add Constraints
# Demand constraint: Total RAM >= 50 GB
model.addConstr((4 * on_demand) + (2 * spot) >= 50, name="Min_RAM_Requirement")

# Availability constraint: Maximum 10 Spot instances available
model.addConstr(spot <= 10, name="Max_Spot_Availability")

# 5. Optimize the Model
model.optimize()

# 6. Display the Results
if model.status == GRB.OPTIMAL:
    print("\n--- Optimal Provisioning Plan ---")
    print(f"Provision {int(on_demand.X)} On-Demand instances.")
    print(f"Provision {int(spot.X)} Spot instances.")
    print(f"Minimum Hourly Cost: ${model.ObjVal:.2f}")
else:
    print("No optimal solution found.")

Understanding the Output

When you run this script, Gurobi displays a solve log detailing the Presolve phase, the Branch-and-Bound iterations, and finally outputs:

text
--- Optimal Provisioning Plan ---
Provision 8 On-Demand instances.
Provision 10 Spot instances.
Minimum Hourly Cost: $1.20

Gurobi used every available cheap Spot instance ($10 \times 2\text{ GB} = 20\text{ GB}$), and satisfied the remaining $30\text{ GB}$ requirement using On-Demand instances ($8 \times 4\text{ GB} = 32\text{ GB}$, since $7.5$ instances is not allowed).


5. Practical Tips and Best Practices

As you advance from toy scripts to production-grade enterprise optimization pipelines, keep these critical guidelines in mind:

1. Vectorize and Batch Using
text
addVars

Avoid calling

text
addVar
in long, nested Python
text
for
loops. For large problems, use Gurobi's high-level tupledicts via
text
model.addVars()
:

python
# Instead of iterating, create collections in one call
servers = ["web", "api", "db"]
instances = model.addVars(servers, vtype=GRB.INTEGER, name="instances")

2. Guard Against Infeasibility

A model is infeasible if its constraints conflict such that no solution is mathematically possible (e.g., requiring $100\text{ GB}$ RAM but capping total instances to zero).

  • Best Practice: Always check
    text
    model.status == GRB.OPTIMAL
    before querying variable values like
    text
    var.X
    . If a model is infeasible, explore Gurobi's
    text
    model.computeIIS()
    (Irreducible Inconsistent Subsystem) tool to identify which business rules are contradicting one another.

3. Normalize Your Numerical Scales

Gurobi uses floating-point arithmetic. If your model contains variables measuring both milligrams ($10^{-3}$) and company valuation in dollars ($10^{9}$), numerical instability can occur.

  • Rule of Thumb: Try to keep all matrix coefficients, bounds, and objective values within the range of $[10^{-3}, 10^{6}]$.

4. Separate Data from Model Formulation

Never hardcode business constants inside your constraint definitions. Structure your code to intake standard Python data structures,

text
pandas
DataFrames, or database records, keeping the optimization mathematics modular and testable.


Summary & What’s Next

In this chapter, you learned:

  • How mathematical optimization differs fundamentally from predictive algorithms.
  • The foundational components of an optimization model: Variables, Objectives, and Constraints.
  • How to define and solve a Mixed-Integer Linear Program using
    text
    gurobipy
    .

In Chapter 2: Advanced Architecture and Model Formulation, we will leave simple scripts behind. We will explore multi-index modeling, matrix-based formulations, and design models that scale gracefully to millions of variables and constraints.

What You Learned

  • 1

    Mathematical optimization moves beyond prediction by using mathematical models to determine the single best decision from millions of potential options.

  • 2

    The core framework for formulating problems requires defining clear decision variables, a specific objective function to optimize, and the operational constraints involved.

  • 3

    Gurobi serves as the industry-standard, state-of-the-art solver designed to solve complex mathematical programming problems efficiently.

📖 Key Terms

Mathematical Optimization

The process of selecting the best element from some set of available alternatives based on a specific objective function and defined constraints.

MIPGap

A parameter representing the relative difference between the best known feasible solution and the best theoretical bound, used to determine when to stop the search.

Feasible Solution

A set of variable values that satisfies all constraints defined within the optimization model.

Objective Function

A mathematical expression that defines the goal of the optimization, such as maximizing profit or minimizing costs.

IIS (Irreducible Inconsistent Subsystem)

A subset of the constraints and variable bounds of a model that is infeasible, but where removing any single constraint renders the subsystem feasible.

Quiz requires login

Sign in to take the quiz and track your progress.