Building AI Solutions

with Google OR-Tools


Barry S. Stahl

Principal Engineer - AZNerds.net

@bsstahl@cognitiveinheritance.com

https://CognitiveInheritance.com

Transparent Half Width Image 800x800.png

Favorite Physicists & Mathematicians

Favorite Physicists

  1. Harold "Hal" Stahl
  2. Carl Sagan
  3. Richard Feynman
  4. Marie Curie
  5. Nikola Tesla
  6. Albert Einstein
  7. Neil deGrasse Tyson
  8. Niels Bohr
  9. Galileo Galilei
  10. Michael Faraday

Other notables: Stephen Hawking, Edwin Hubble, Leonard Susskind, Christiaan Huygens

Favorite Mathematicians

  1. Ada Lovelace
  2. Alan Turing
  3. Tim Berners-Lee
  4. Isaac Newton
  5. Emmy Noether
  6. Johannes Kepler
  7. René Descartes
  8. George Boole
  9. Carl Friedrich Gauss
  10. Grace Hopper

Other notables: Blaise Pascal, Daphne Koller, Grady Booch, Evelyn Berezin, Pascal Van Hentenryck

Fediverse Supporter

Logos.png

Some OSS Projects I Run

  1. Liquid Victor : Media tracking and aggregation [used to assemble this presentation]
  2. Prehensile Pony-Tail : A static site generator built in c#
  3. TestHelperExtensions : A set of extension methods helpful when building unit tests
  4. Conference Scheduler : A conference schedule optimizer
  5. IntentBot : A microservices framework for creating conversational bots on top of Bot Framework
  6. LiquidNun : Library of abstractions and implementations for loosely-coupled applications
  7. Toastmasters Agenda : A c# library and website for generating agenda's for Toastmasters meetings
  8. ProtoBuf Data Mapper : A c# library for mapping and transforming ProtoBuf messages

http://GiveCamp.org

GiveCamp.png

Achievement Unlocked

bss-100-achievement-unlocked-1024x250.png

AI ≠ ML ≠ LLMs

  • LLMs are one kind of learning model
  • Learning models are one kind of AI model
  • Logic, search, optimization, and planning are often AI

Image: Yoshua Bengio by By Xuthoria

Yoshua_Bengio_663x800.jpg

Simulates A Rational Actor

Attempts to make the best possible decision​ based on the model and the data

  • Model - Our description of the problem
  • Data - Our understanding of the state of the domain
A Rational Actor 800x800.jpeg

Types of AI Models

  • Logic​al - Reducible to conditionals​
    • Object Oriented​
    • Rules Engine
  • Search/Optimization - Reduce and Search the Solution Space
    • Dynamic Programming​
    • Constraint Programming
  • Probabilistic/Learning - Predicts best solution from earlier data
    • Neural/Bayesian Networks
    • Genetic Algorithms

Optimization

Finding the best available solution to a problem by minimizing or eliminating undesirable factors and maximizing desirable ones.

training_animation.gif

Feasibility Problems

  • Find the best values for a set of decision variables that
    • Satisfies all constraints

Optimization Problems

  • Find the best values for a set of decision variables that​
    • Satisfies all constraints
    • Maximizes the features we want​
    • Minimizes the features we don’t want
 

Constraint Programming Models

ConstraintProgrammingTypesPyramid.png

Feasibility vs Optimization

  • Feasible Solutions
    • 7 Mixed Fruit
    • 1 Mixed Fruit , 2 Hot Wings, 1 Sampler Plate
  • Possible Optimizations
    • Minimize # of any single item
    • Maximize total # of distinct items
  • Possible Relaxation
    • Offer discount to relax constraint
    • Convert = to >= resulting in more feasible solutions
    • Maximize Profit

Constraint: A Required Condition

  • As an entity, constraints do 2 things

    • Verify feasibility (is there a solution that satisfies this constraint?)
    • Prune the search space (more on this coming up)
  • Infeasibility

    • Solution does not satisfy 1 or more constraints
      • Previous decisions that led to this point may need to be revisited
    • Problem has no feasible solutions
      • Relaxations may be available to find a “good-enough” solution

Types of Constraints

  • Inequality Constraints
    • \(X_1 <> 4.5\) or \(X_1 + 4X_2 < 24.0\)
  • Integer Constraints
    • Counter-intuitively, they can be harder to find solutions for
    • \(X_1 + 4X_2 < 24 \quad\text{with}\quad X_1, X_2 \in \{1,\dots,n\}\)
  • Global Constraints
    • Special “pre-defined” constraints that have a polynomial-time algorithm
      • i.e. AllDifferent Constraint
    • There are more than 400 Global Constraints known today
    • Defining these constraints using polynomial algorithms is an active research area

Objective: A Goal for the Solution

  • Objective Function

    • Equation describing how to improve a solution
    • Specify what to minimize or maximize to produce the best result
  • Specified as a goal relative to an equation

    • \(\max 3X_1 + 9X_2 \text { subject to } c_n\)
    • \(\min \sum_{i=1}^{n} A_i X_i\)
  • One objective per Model

    • Combine features by creating a "score"

Sudoku

  • A Combinatorial Puzzle
    • 9x9 grid
    • Each row, column and 3x3 region contains [1 to 9]
  • AllDifferent constraints
    • With values restricted to integers [1 to 9]
    • 27 Constraints in total (9 rows, 9 columns, 9 regions)
01 - Sudoku Constraints - 603x800.png

Sudoku Solution Space

  • Solution Space (aka Search Space or Feasibility Set)

    • All values that satisfy the constraint(s)
  • The solution space for a 9x9 grid is inconceivably large

    • Roughly \(2 \times 10^{77}\) possible solutions
    • At 1 Trillion per second it would take \(6 \times 10^{57}\) years
  • Even with the AllDifferent constraint, its \(6.7 \times 10^{21}\)

    • Naïve (brute-force) methods are not practical
02 - Sudoku Solution Space - 603x800.png

Pruning the Solution Space

  • Using constraints, we can reduce the possibilities
  • Each new learning adds new constraints

  • Question: How do we check for feasibility here?
03 - Pruning the Solution Space 01 - 603x800.png

Pruning the Solution Space

  • Possible solutions: \(5 \times 10^{73}\)

    • Search space reduced by 4 orders-of-magnitude
    • Roughly 10K Possibilities Eliminated
  • Let's jump ahead

    • All of the values we've been given are filled-in
04 - Pruning the Solution Space 02 - 603x800.png

Pruning the Solution Space

  • All starting info filled-in
    • Search space pruned appropriately
    • Possible solutions: \(4 \times 10^{21}\)
    • Eliminating 56 orders of magnitude
  • We're not nearly done yet
    • Look closely at the remaining options
05 - Pruning the Solution Space 03 - 603x800.png

Pruning the Solution Space

  • The reduction of search options
    • "Fixed" the value of 4 cells
06 - Pruning the Solution Space 04 - 603x800.png

Propogation of Constraints

  • Adding info means we learn even more
    • We can reduce the search space further
  • Possible solutions: \(2 \times 10^{20}\)
07 - Pruning the Solution Space 05 - 603x800.png

Propogation of Constraints

  • We now have even more "fixed points"
  • We continue to iteratively propagate constraints
    • Known as a "Fixed-Point Algorithm"
  • Let's jump ahead to completion of the propagation
08 - Pruning the Solution Space 06 - 603x800.png

Propogation of Constraints

  • Possible solutions: 1
  • “Guesses” Required: 0
    • Only solution was apparent after propagation
  • CSPs are not always this easy
    • Many require some form of search
09 - Pruning the Solution Space 07 - 603x800.png

Constraint Programming Pattern

  • Constraint Programming is about narrowing choices

    • Model the valid states of the problem
      • Variables, domains, and required conditions
    • Let constraints prune impossible choices
      • Every new constraint removes possible solutions
    • Search what remains until a feasible answer is found
  • LP and MIP build on the same idea

    • Constraints still define the feasible region
    • An objective chooses the best feasible answer
    • Linear structure gives us specialized algorithms

Optimization Use-Case

Determine the ideal production targets for Pete's Pottery Paradise

PetesPotteryParadise-800x800.png

Use Case: Production Targets

  • Products​
    • Small Vase​
      • 1 oz. clay, 1 oz. glaze​
      • Sells for $3.00 each​
    • Large Vase​
      • 4 oz. clay, 2 oz. glaze​
      • Sells for $9.00 each​
  • Inventory​
    • Clay - 24 oz.​
    • Glaze - 16 oz.​
  • Goal
    • Maximize Revenue

Solution Space

Solution Space Graph - With Feasible Solutions.png

Constraint Equations

  • Clay Constraint
    • X + 4Y <= 24 ​
      • X <= 24 and Y <= 6​
  • Glaze Constraint
    • X + 2Y <= 16​
      • X <=16 and Y <= 8

Feasible Region

Solution Space Graph - With Feasible Solutions and Constraint Lines.png

Linear Programming - Polytope

Solution Space Graph - With Polytope.png

Additional Constraints

Solution Space Graph - New Polytope - 1132x645.png

The Simplex Algorithm

  • Created by George Dantzig in 1947
  • Built to solve linear programming problems
    • Allocate scarce resources under constraints
  • Searches the vertices of the feasible region
    • Usually far fewer places than every possible solution
  • Still used inside modern optimization solvers
    • Planning, routing, scheduling, logistics, finance
george-bernard-dantzig.jpg

Walk the Vertices

Simplex turns optimization into a short guided walk

  • Start at any feasible vertex
    • Determine direction that improves revenue fastest
    • Move along an edge until a constraint stops you
  • Pivot to the new axis
    • Repeat from the new vertex
  • Stop when every neighboring vertex is worse
SimplexAlgorithm.png

One Pivot

  • Start: (0 small, 0 large) -> revenue $0
  • Enter: make large vases first
    • They add $9 each, so revenue rises quickly
  • Leave: clay becomes the limiting constraint
    • 24 oz clay / 4 oz per large = 6 large
  • New corner: (0 small, 6 large) -> revenue $54
  • Pivot: trade along the clay edge
    • Stop at (8 small, 4 large) when glaze also binds
  • No adjacent corner improves revenue -> optimum $60
Solution Space Graph - With Polytope-800x433.png

Scheduling Use-Case

Determine the best schedule for talks at a conference

ConferenceScheduling-Photo-800x800.png

Conference Schedule

  Room 1 Room 2 Room 3
Slot 1 Session 1 Session 2 Session 3
Slot 2 Session 4 Session 5 Session 6
Slot 3 Session 7 Session 8 Session 9
Slot 4 Session 10 Open Open

LP Model - Variables

  Room 1 Room 2 Room 3
Slot 1 Session 1 Session 2 Session 3
Slot 2 Session 4 Session 5 Session 6
Slot 3 Session 7 Session 8 Session 9
Slot 4 Session 10 Open Open

  • \(X_{t,r}\)
    • \(t\) is the id of the timeslot
    • \(r\) is the id of the room
    • \(X_{t,r} \in \mathbb{Z}\) is the session id assigned to timeslot \(t\) in room \(r\)
      • \(\mathbb{Z}\) is the set of all session ids

LP Model - Constraints

  Room 1 Room 2 Room 3
Slot 1 Session 1 Session 2 Session 3
Slot 2 Session 4 Session 5 Session 6
Slot 3 Session 7 Session 8 Session 9
Slot 4 Session 10 Open Open

  • Each room/timeslot combination can have no more than 1 session
  • Each session must be scheduled exactly once
  • What if session 7 & 9 are the same speaker?
    • Desired rule: \(slot(7) \ne slot(9)\)
    • But \(X_{t,r}\) stores a session id, not a timeslot for a session
    • This requires a lookup: "where is session 7 scheduled?"
    • That lookup is not naturally a linear expression

MIP Model - Variables

Session 1 Room 1 Room 2 Room 3
Slot 1 0 0 0
Slot 2 0 0 0
Slot 3 0 1 0
Slot 4 0 0 0

Session 2 Room 1 Room 2 Room 3
Slot 1 1 0 0
Slot 2 0 0 0
Slot 3 0 0 0
Slot 4 0 0 0

Session 3 Room 1 Room 2 Room 3
Slot 1 0 0 0

MIP Model - Variables

Session 1 Room 1 Room 2 Room 3
Slot 1 0 0 0
Slot 2 0 0 0
Slot 3 0 1 0
Slot 4 0 0 0

  • \(X_{t,r,s}\)
    • \(t\) is the id of the timeslot
    • \(r\) is the id of the room
    • \(s\) is the id of the session
    • \(X_{t,r,s}\) is \(1\) if \(s\) is the session id assigned to timeslot \(t\) in room \(r\)

MIP Model - Constraints

Session 1 Room 1 Room 2 Room 3
Slot 1 0 0 0
Slot 2 0 0 0
Slot 3 0 1 0
Slot 4 0 0 0

  • Each session must be scheduled exactly once
    • \(\sum_t \sum_r X_{t,r,s} = 1 \quad \forall s\)
  • Each room/timeslot combination can have no more than 1 session
    • \(\sum_s X_{t,r,s} \le 1 \quad \forall t,r\)
  • Sessions 7 and 9 cannot be scheduled in the same timeslot
    • \(\sum_r X_{t,r,7} + \sum_r X_{t,r,9} \le 1 \quad \forall t\)

MIP Model - Objective

Session 1 Room 1 Room 2 Room 3
Slot 1 0 0 0
Slot 2 0 0 0
Slot 3 0 1 0
Slot 4 0 0 0

Create a score that improves with the desirability of the solution

  • Factors might include
    • Reduce # of different rooms in a track
    • Reduce the # of timeslot conflicts between sessions in a track
    • Reduce total steps between sessions of a track
    • Reduce distance (time & room) between multi-part sessions
    • Reduce # of different days a speaker speaks

MIP Model - Objective Example

Session 1 Room 1 Room 2 Room 3
Slot 1 0 0 0
Slot 2 0 0 0
Slot 3 0 1 0
Slot 4 0 0 0

Maximize # of sessions in the same room with Sessions 2 & 4 (a track)

  • For each Room:
    • \(\max \sum_r \left(\sum_t X_{t,r,2}\right)\left(\sum_t X_{t,r,4}\right)\)
      • If they are in the same room, the total will be 1
      • If they are in different room, the total will be 0

Simplex for MIP

  • Relax the constraints:

    • Boolean vars → continuous vars \(\in [0,1]\)
    • Run Simplex to find a solution in the continuous space
    • This becomes a "smooth" estimate of good solution locations
  • Use this to guide a search through nearby boolean options

  • Repeat until the best integral solution is found

SimplexForMIP.png

Solvers

We don't need to implement Simplex ourselves

  • A solver takes the model:
    • Decision variables
    • Constraints
    • Objective function
  • Chooses the algorithms needed
  • Searches for the best solution
  • Examples:

Google OR-Tools

A Suite of Tools for Solving Combinatorial Optimization Problems

  • Constraint Solver
  • Unified LP & MIP Interface
    • GLOP – LP Solver
    • CBC – MIP Solver
    • Can be built with other solvers, including Gurobi
gdp-800x361.png

Decision Variables

  • Variable Definitions

    • \(x_S\) – Number of small vases to create
    • \(x_L\) – Number of large vases to create
  • Implementation

    • var xS = solver.MakeIntVar(0.0, maxSmall, "xS");
    • var xL = solver.MakeIntVar(0.0, maxLarge, "xL");

Constraints

  • Clay Constraint Definition

    • \(x_S + 4 x_L \ge 0\)
    • \(x_S + 4 x_L \le claySupply\)
  • Similar for Glaze Constraint

  • Implementation

    • var cClay = solver.MakeConstraint(0.0, claySupply);
    • cClay.SetCoefficient($x_S$, 1);
    • cClay.SetCoefficient($x_L$, 4);

Objective Function

\(\max \; 3 x_S + 9 x_L\)

  • var obj = solver.Objective();
  • obj.SetCoefficient($x_S$, 3);
  • obj.SetCoefficient($x_L$, 9);
  • obj.SetMaximization();

Execute the Model

  • int resultStatus = solver.Solve();
  • var xSmall = xS.SolutionValue();
  • var xLarge = xL.SolutionValue();

Linear and Mixed-Integer Programming

  • Define Constraints that the Solution Must Satisfy​
    • Use these constraints to limit the search space​
      • More constraints = smaller search space
        • Solutions that exist are found faster
        • May not find a feasible solution
  • Add an Objective Function to Improve Solutions​
    • All constraints must be satisfied first​
    • Objectives are a goal relative to an equation​
      • i.e. Maximize revenue where r = 3X + 9Y