
This course includes our updated coding exercises so you can practice your skills as you learn.
See a demo
What is Google OR-Tools?
An open-source software suite from Google for solving optimization problems: finding the best decision among a huge number of possible ones.
Free: open source under the Apache 2.0 license.
Multi-language: Python, C++, Java and C#.
Easy to install: pip install ortools.
Proven: CP-SAT regularly wins top places at the MiniZinc Challenge.
Typical uses: scheduling staff and machines, routing vehicles, assigning tasks, packing and selecting items.
W1. Hello CP-SAT: Find Two Numbers That Sum to 10
Data: target sum 10; both numbers between 0 and 10.
Variables: x, y ∈ {0, …, 10}, integers.
Objective: none (any solution).
Constraint: x + y = 10.
CP-SAT: CpModel(), NewIntVar(0, 10, "x"), Add(x + y == 10), Solve(model).
Key idea: every CP-SAT program has the same four steps: model, variables, constraints, solve.
W2. Maximize Profit with Two Products
Data: Chair: $30 profit, 2 h wood, 3 h labor. Table: $50 profit, 4 h wood, 2 h labor. Available: 40 h wood, 36 h labor.
Variables: c, t ≥ 0, integers (chairs, tables).
Objective: max 30c + 50t.
Constraints: 2c + 4t ≤ 40 (wood); 3c + 2t ≤ 36 (labor).
CP-SAT: Add(...), Maximize(30*c + 50*t), ObjectiveValue().
Key idea: one Maximize line turns "any solution" into "the best solution".
Verified answer: 8 chairs and 6 tables, $540 profit.
W3. Chickens and Rabbits: Count Heads and Legs
Data: 35 heads and 94 legs in total; a chicken has 2 legs, a rabbit has 4.
Variables: ch, rb ∈ {0, …, 35}.
Objective: none.
Constraints: ch + rb = 35 (heads); 2ch + 4rb = 94 (legs).
Key idea: name the unknowns, then write each sentence of the story as one constraint.
Verified answer: 23 chickens and 12 rabbits.
W4. SEND + MORE = MONEY
Data: eight letters S, E, N, D, M, O, R, Y; leading letters S and M cannot be zero.
Variables: one digit per letter ∈ {0, …, 9}; S, M ∈ {1, …, 9}.
Constraints: AllDifferent(S, E, N, D, M, O, R, Y); 1000S + 100E + 10N + D + 1000M + 100O + 10R + E = 10000M + 1000O + 100N + 10E + Y.
CP-SAT: AddAllDifferent(letters), Add(send + more == money).
Key idea: one global constraint replaces 28 separate "not equal" rules.
Verified answer: 9567 + 1085 = 10652.
W5. Assign 3 Workers to 3 Tasks
Data: workers Ali, Sara, Omid; tasks Report, Testing, Design. Hours c[i,j]:
Ali: Report 9, Testing 2, Design 7
Sara: Report 6, Testing 4, Design 3
Omid: Report 5, Testing 8, Design 1
Variables: x[i,j] ∈ {0,1} (worker i does task j).
Objective: min Σ c[i,j]·x[i,j].
Constraints: Σ_j x[i,j] = 1 (one task per worker); Σ_i x[i,j] = 1 (one worker per task).
CP-SAT: NewBoolVar, AddExactlyOne, Minimize.
Key idea: boolean "who does what" variables, the same idea behind Project 01.
Verified answer: the best assignment takes 9 hours.
Project 01. Weekly Employee Shift Scheduler
The problem: A small shop runs three shifts a day, seven days a week. Six employees must cover every shift, respect rest rules and weekly limits, and get the shifts they prefer as often as possible.
The question: Who should work which shift on each day?
You will learn: boolean decision variables, coverage constraints, at-most-one rules.
Data:
Employees: Anna, Ben, Carla, David, Elena, Farid. Days: Mon to Sun. Shifts: morning, evening, night.
Demand per shift: Mon to Fri: morning 2, evening 2, night 1. Sat and Sun: 1, 1, 1.
Max shifts per employee per week: 6.
Shift preference (morning / evening / night): Anna 3/2/1, Ben 1/3/2, Carla 2/1/3, David 3/1/1, Elena 2/3/1, Farid 1/2/3.
Days-off requests (working that day scores −5): Anna Sat, Ben Mon, Carla Sun, Elena Wed.
Project 02. Simple Job Shop: Scheduling 3 Machines
The problem: A small factory has four jobs to finish. Each job passes through cutting, drilling and painting in its own order, and each machine can work on only one job at a time.
The question: In what order should each machine process the jobs so everything finishes as early as possible?
You will learn: interval variables, NoOverlap, minimizing the makespan.
Data: Machines: 0 Cutting, 1 Drilling, 2 Painting. Operations as (machine, time):
Job 0: (0,3), (1,2), (2,2)
Job 1: (0,2), (2,1), (1,4)
Job 2: (1,4), (2,3), (0,1)
Job 3: (2,2), (0,3), (1,2)
Horizon = sum of all times = 29.
Project 03. School Class Timetable
The problem: A school needs a weekly timetable for two classes. Five teachers share three rooms, one of them a science lab, across 20 periods of the week.
The question: When and where should every lesson take place with no clashes?
You will learn: clash constraints, room suitability, pure feasibility problems.
Data:
Days Mon to Fri, 4 periods per day, so 20 slots (slot = day × 4 + period).
Rooms: Room 1 (normal), Room 2 (normal), Lab (lab).
Lessons per week, identical for classes 7A and 7B: Math (Mr. Smith) 4, Science (Ms. Jones, needs Lab) 3, English (Mr. Brown) 4, History (Ms. Lee) 2, Art (Mr. Garcia) 2.
Project 04. Packing Boxes into Trucks
The problem: A warehouse has 12 boxes of different weights to ship. Each truck carries at most 100 kg, and every truck sent out costs money.
The question: What is the smallest number of trucks that can carry every box?
You will learn: bin packing, linking variables, symmetry breaking.
Data: Box weights (kg): 48, 30, 19, 36, 36, 27, 42, 42, 36, 24, 30, 15 (total 385). Truck capacity 100 kg; 6 trucks available. Extension: volumes (m³) 0.8, 0.5, 0.3, 0.6, 0.7, 0.4, 0.9, 0.6, 0.5, 0.4, 0.6, 0.2 with truck volume 2.0.
Project 05. Choosing Projects on a Budget
The problem: A company has $100k to invest and ten candidate projects, each with a cost and an expected benefit. Some projects depend on others, and some cannot run together.
The question: Which set of projects gives the highest total value within the budget?
You will learn: knapsack, implications, business rules as constraints.
Data: Budget $100k. Projects (cost $k / value):
New website 12/20
CRM system 25/40
Mobile app 30/45
Staff training 8/15
Solar panels 22/30
Warehouse robot 35/50
Office move 28/25
Data dashboard 15/28
Security audit 10/22
Marketing campaign 18/26
Rules: Mobile app requires New website; Warehouse robot and Office move cannot both be chosen.
Project 06. Tourist Route Through a City's Landmarks
The problem: A tourist starts at the hotel, wants to see all seven landmarks in the city, and then return to the hotel, walking as little as possible.
The question: In what order should the landmarks be visited?
You will learn: the traveling salesman problem, AddCircuit.
Data: Node (x, y) in km:
0 Hotel (0, 0)
1 Old Cathedral (1.2, 0.8)
2 City Museum (2.5, 0.3)
3 Central Park (3.1, 1.9)
4 River Bridge (1.8, 2.6)
5 Market Square (0.4, 2.2)
6 Castle (2.9, 3.4)
7 Art Gallery (0.9, 3.5)
Walking distance d[i,j] in meters = round(Euclidean distance × 1000).
Project 07. Best Sightseeing Tour with a Time Limit
The problem: Same city, but now the tourist has only four hours. Each landmark takes time to visit and gives a different amount of enjoyment, so not everything fits.
The question: Which landmarks should be visited, and in what order, for the most enjoyment?
You will learn: optional visits with self-loops, budget constraints.
Data: Project 06 landmarks and distances. Walking speed 80 m/min; walking time t[i,j] = round(d[i,j] / 80). Time budget T = 240 min. Per landmark (visit min / enjoyment):
Hotel 0/0
Old Cathedral 45/7
City Museum 90/9
Central Park 30/5
River Bridge 15/3
Market Square 40/6
Castle 75/10
Art Gallery 60/8
Project 08. Delivery Routes for Multiple Vehicles
The problem: A delivery company serves 12 customers from one depot with up to three vans. Every customer must be visited exactly once, and every van returns to the depot.
The question: Which van visits which customers, and in what order, to minimize total distance?
You will learn: AddMultipleCircuit, routes that start and end at a depot.
Formulation:
Variables: x[i,j] ∈ {0,1}.
Objective: min Σ d[i,j]·x[i,j].
Constraints: AddMultipleCircuit({(i, j, x[i,j])}); Σ_j x[0,j] ≤ K.
Key idea: the depot may be visited many times; every customer exactly once.
Verified result: OPTIMAL, 64 km using just 1 vehicle. Without capacity limits a single vehicle is cheapest, which motivates Project 09.
Project 09. Delivery Trucks with Capacity Limits
The problem: Same customers, but now each order has a size and every truck can carry only 15 units. A single truck can no longer do the whole job.
The question: How should the routes change so that no truck is overloaded?
You will learn: load variables, conditional constraints with OnlyEnforceIf.
Data: Demand (C1 to C12): 3, 4, 2, 5, 3, 4, 2, 3, 4, 3, 2, 5 (total 40). Capacity Q = 15 per truck.
Formulation:
Variables: x[i,j] ∈ {0,1}; L[i] ∈ [q[i], Q] (load on arrival).
Objective: min Σ d·x.
Constraints: AddMultipleCircuit(arcs); x[i,j] = 1 ⇒ L[j] ≥ L[i] + q[j], for j ≠ 0.
CP-SAT: model.Add(L[j] >= L[i] + q[j]).OnlyEnforceIf(x[i, j]).
Key idea: OnlyEnforceIf makes a constraint conditional on an arc, with no big-M needed.
Verified result: OPTIMAL, 85 km with 3 trucks.
Project 10. Deliveries with Time Windows
The problem: Same customers again, but each one can receive deliveries only within a set time window, and every stop takes 10 minutes of service.
The question: Which routes reach every customer on time with the least driving?
You will learn: time variables, waiting, time-window constraints.
Data:
Speed 30 km/h, so travel time t[i,j] = 2 × d[i,j] minutes.
Service time: 10 min at each customer, 0 at the depot.
Time windows in minutes after 08:00:
NodeWindowNodeWindowDepot[0, 480]C7[300, 420]C1[60, 180]C8[60, 200]C2[0, 120]C9[0, 150]C3[120, 240]C10[200, 330]C4[30, 150]C11[90, 210]C5[180, 300]C12[0, 90]C6[240, 360]
Formulation:
Variables: x[i,j] ∈ {0,1}; T[i] ∈ [a[i], b[i]] (arrival time).
Objective: min Σ d·x.
Constraints: AddMultipleCircuit(arcs); x[i,j] = 1 ⇒ T[j] ≥ T[i] + s[i] + t[i,j], for j ≠ 0.
CP-SAT: model.Add(T[j] >= T[i] + s[i] + t[i][j]).OnlyEnforceIf(x[i, j]).
Key idea: same pattern as capacity, applied to a new resource. Waiting is allowed because T[j] can be later than the earliest arrival.
Verified result: OPTIMAL, 83 km with 2 trucks.
Learn how to control how long CP-SAT searches by setting a time limit with max_time_in_seconds.
See what the solver returns when time runs out and how to judge a FEASIBLE solution using the bound and gap.
Includes a full 60-job scheduling example, related parameters, common mistakes and hands-on "what if" exercises.
Learn how num_workers lets CP-SAT run several search strategies in parallel, and why the default already uses all your CPU cores.
See a real experiment on a 60-job scheduling model: one worker gives the same answer every run, while more workers find far better schedules but vary from run to run.
Includes guidance on choosing a value, common mistakes and hands-on exercises to test parallel search on your own computer.
Learn how to stop CP-SAT as soon as the solution is “good enough” using relative_gap_limit and absolute_gap_limit.
See how a 5% gap limit cut solve time from 29 seconds to a quarter of a second on a 150-task assignment problem, and why the status still says OPTIMAL.
Includes the gap formula, when gap limits fail, how to choose a value and hands-on exercises.
Learn how to turn on CP-SAT’s search log with log_search_progress, including in Jupyter and Colab.
Read a real log line by line: model size, presolve, which worker found each solution, and how the bound moves.
Includes a diagnosis table for slow or infeasible models, common mistakes and hands-on exercises.
Have you ever looked at a messy real-world decision and thought, "There must be a smarter way to do this"?
Who should work which shift? Which boxes go on which truck?
What's the best route for three delivery vans?
These questions show up everywhere, in factories, hospitals, schools, warehouses and offices, and most people still answer them with guesswork and spreadsheets.
I'm Alireza Soroudi, and I've spent years solving optimization problems in operations research, energy systems and supply chains. In this course I want to share the tool I reach for most often: Google OR-Tools, and in particular its CP-SAT solver. It's free, it's powerful, and with Python it's surprisingly approachable, once someone shows you how to think about problems the right way.
How the course works
We start gently. First, you'll learn what OR-Tools is and the four building blocks of every optimization model: data, decision variables, constraints and an objective. Then we warm up with five small toy problems, such as finding two numbers, counting chickens and rabbits, and solving the famous SEND + MORE = MONEY puzzle. They're short and a little playful, but each one teaches a skill you'll use again and again.
Then we build. You'll work through 10 hands-on projects, each introducing one new idea:
Scheduling: build a weekly staff roster, schedule jobs on factory machines, and create a school timetable with no clashes.
Allocation: pack boxes into as few trucks as possible, and choose the best set of projects within a budget.
Traveling salesman: plan the shortest tourist route through a city, then decide which sights are worth visiting when time is limited.
Vehicle routing: design delivery routes for several vans, then add truck capacities and customer time windows.
Every project follows the same simple rhythm: understand the problem in plain words, write the mathematical model, translate it into Python, solve it, and look at the results. Each one comes with its own Excel data file, so you'll also learn how to load real data into your models, not just hard-coded numbers.
Along the way, you'll find exercises that ask you to change something and see what happens. What if a machine breaks down? What if the team can only handle four projects? These "what if" moments are where the real understanding clicks.
Who this course is for
This course is for engineers, analysts, students, researchers and curious programmers who want to solve decision problems with code. You don't need a background in optimization or advanced math. If you can write basic Python (loops, lists and dictionaries), you're ready. I'll explain every formula in plain language before we write it.
What you'll walk away with
By the end, you'll be able to take a real problem, describe it as a model, and solve it with CP-SAT confidently. More importantly, you'll start seeing optimization opportunities everywhere, in your work, your research and your own projects.
I'd love to have you in the course. Let's get started!