MILP Factory › Example

Michael BrenndoerferAugust 29, 20259 min read
Machine Learning from ScratchAll chapters
Part 9 of 9Optimization
Chapter 62MILP Factory › Example
Reading section 4 of 8 · Example · 9 minSection 4 of 8 · 9 min

Book contents

Section 4 of 8 · 9 min read

MILP Factory › ExampleOne section · Sections in this chapter4 / 8 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's work through a concrete factory optimization example step by step. Consider a factory that produces two products using two machines with the following specifications:

Problem Setup:

  • Product 1: requires 2 hours on Machine A, 4 hours on Machine B, profit of $3 per unit
  • Product 2: requires 3 hours on Machine A, 2 hours on Machine B, profit of $2 per unit
  • Machine A: 12 hours available
  • Machine B: 16 hours available
  • Additional constraint: We can only produce products in whole units (integer constraint)

Step 1: Define Variables

Let x1x_1 = number of units of Product 1 to produce Let x2x_2 = number of units of Product 2 to produce

Step 2: Formulate the Problem

Our objective is to maximize profit: max⁡3x1+2x2\max 3x_1 + 2x_2

Subject to constraints:

  • Machine A capacity: 2x1+3x2≤122x_1 + 3x_2 \leq 12
  • Machine B capacity: 4x1+2x2≤164x_1 + 2x_2 \leq 16
  • Non-negativity: x1,x2≥0x_1, x_2 \geq 0
  • Integer constraint: x1,x2∈Zx_1, x_2 \in \mathbb{Z}

Step 3: Solve the LP Relaxation First

If we ignore the integer constraint, we can solve this as a linear program. We need to find where the constraint boundaries intersect, which gives us the optimal solution.

From Machine A constraint: 2x1+3x2≤122x_1 + 3x_2 \leq 12, we can express x2x_2 as:

x2≤12−2x13x_2 \leq \frac{12 - 2x_1}{3}

From Machine B constraint: 4x1+2x2≤164x_1 + 2x_2 \leq 16, we can express x2x_2 as:

x2≤16−4x12=8−2x1x_2 \leq \frac{16 - 4x_1}{2} = 8 - 2x_1

To find the intersection point where both constraints are binding (satisfied with equality), we set the two expressions equal:

12−2x13=8−2x1\frac{12 - 2x_1}{3} = 8 - 2x_1

Multiplying both sides by 3 to eliminate the denominator:

12−2x1=3(8−2x1)12 - 2x_1 = 3(8 - 2x_1)

Expanding the right side:

12−2x1=24−6x112 - 2x_1 = 24 - 6x_1

Rearranging terms to collect x1x_1 on one side:

−2x1+6x1=24−12-2x_1 + 6x_1 = 24 - 12

Simplifying:

4x1=124x_1 = 12

Solving for x1x_1:

x1=124=3x_1 = \frac{12}{4} = 3

Substituting x1=3x_1 = 3 back into the Machine A constraint (with equality) to find x2x_2:

2(3)+3x2=122(3) + 3x_2 = 12

Simplifying:

6+3x2=126 + 3x_2 = 12

Solving for x2x_2:

3x2=12−6=63x_2 = 12 - 6 = 6 x2=63=2x_2 = \frac{6}{3} = 2

Verification: Let's verify this solution satisfies both constraints:

  • Machine A: 2(3)+3(2)=6+6=12≤122(3) + 3(2) = 6 + 6 = 12 \leq 12 ✓
  • Machine B: 4(3)+2(2)=12+4=16≤164(3) + 2(2) = 12 + 4 = 16 \leq 16 ✓

So the LP relaxation gives us x1=3,x2=2x_1 = 3, x_2 = 2 with profit:

Profit=3x1+2x2=3(3)+2(2)=9+4=13\text{Profit} = 3x_1 + 2x_2 = 3(3) + 2(2) = 9 + 4 = 13

The optimal profit is \13$.

Step 4: Apply Branch-and-Bound

Since we need integer solutions, we use branch-and-bound:

Branch 1: x1≤3x_1 \leq 3

  • New constraint: x1≤3x_1 \leq 3
  • With x1=3x_1 = 3 fixed, from Machine A: 2(3)+3x2≤122(3) + 3x_2 \leq 12, so 3x2≤12−6=63x_2 \leq 12 - 6 = 6, giving x2≤2x_2 \leq 2
  • From Machine B: 4(3)+2x2≤164(3) + 2x_2 \leq 16, so 2x2≤16−12=42x_2 \leq 16 - 12 = 4, giving x2≤2x_2 \leq 2
  • The binding constraint is x2≤2x_2 \leq 2, so the solution is x1=3,x2=2x_1 = 3, x_2 = 2 (this is already an integer solution)
  • Profit = 3(3)+2(2)=9+4=133(3) + 2(2) = 9 + 4 = 13

Branch 2: x1≥4x_1 \geq 4

  • New constraint: x1≥4x_1 \geq 4
  • From Machine B: 4x1+2x2≤164x_1 + 2x_2 \leq 16, with x1=4x_1 = 4: 4(4)+2x2≤164(4) + 2x_2 \leq 16
  • Simplifying: 16+2x2≤1616 + 2x_2 \leq 16, so 2x2≤02x_2 \leq 0, giving x2≤0x_2 \leq 0
  • Since x2≥0x_2 \geq 0 (non-negativity), we have x2=0x_2 = 0
  • Solution: x1=4,x2=0x_1 = 4, x_2 = 0 (integer solution)
  • Profit = 3(4)+2(0)=12+0=123(4) + 2(0) = 12 + 0 = 12

Branch 1.1: x1≤3x_1 \leq 3 and x2≤1x_2 \leq 1

  • With x1=3x_1 = 3 and x2=1x_2 = 1:
  • Verification: Machine A: 2(3)+3(1)=6+3=9≤122(3) + 3(1) = 6 + 3 = 9 \leq 12 ✓
  • Verification: Machine B: 4(3)+2(1)=12+2=14≤164(3) + 2(1) = 12 + 2 = 14 \leq 16 ✓
  • Solution: x1=3,x2=1x_1 = 3, x_2 = 1 (integer solution)
  • Profit = 3(3)+2(1)=9+2=113(3) + 2(1) = 9 + 2 = 11

Branch 1.2: x1≤3x_1 \leq 3 and x2≥2x_2 \geq 2

  • With x1=3x_1 = 3 and x2=2x_2 = 2:
  • Verification: Machine A: 2(3)+3(2)=6+6=12≤122(3) + 3(2) = 6 + 6 = 12 \leq 12 ✓
  • Verification: Machine B: 4(3)+2(2)=12+4=16≤164(3) + 2(2) = 12 + 4 = 16 \leq 16 ✓
  • Solution: x1=3,x2=2x_1 = 3, x_2 = 2 (integer solution)
  • Profit = 3(3)+2(2)=9+4=133(3) + 2(2) = 9 + 4 = 13

Step 5: Compare Integer Solutions

We have found three feasible integer solutions:

  • Solution 1: x1=3,x2=1x_1 = 3, x_2 = 1, Profit = 3(3)+2(1)=9+2=113(3) + 2(1) = 9 + 2 = 11
  • Solution 2: x1=4,x2=0x_1 = 4, x_2 = 0, Profit = 3(4)+2(0)=12+0=123(4) + 2(0) = 12 + 0 = 12
  • Solution 3: x1=3,x2=2x_1 = 3, x_2 = 2, Profit = 3(3)+2(2)=9+4=133(3) + 2(2) = 9 + 4 = 13

Since we're maximizing profit, the optimal integer solution is Solution 3: x1=3,x2=2x_1 = 3, x_2 = 2 with a profit of \13$.

Note: In this example, the LP relaxation solution (3,2)(3, 2) happens to be an integer solution, so the integer constraint doesn't reduce the optimal value. This is not always the case. Often, integer constraints force us to accept a lower objective value than the LP relaxation.

Let's visualize this example to see the LP relaxation solution compared to the integer solutions:

Out[5]:
Visualization
LP relaxation plot showing continuous feasible region with optimal point marked in green at coordinates (3,2).
LP relaxation solution showing the continuous feasible region and optimal point. The green marker indicates the optimal solution at (3, 2) with profit of $13. In this case, the LP relaxation solution happens to be an integer solution.
Integer solutions scatter plot with color-coded profit levels, optimal solution highlighted.
Integer solutions within the feasible region with their associated profits. Each point represents a feasible integer combination of Product 1 and Product 2. The optimal integer solution at (3, 2) with profit $13 is highlighted in green. Points with profit ≥ $10 are shown in magenta, while lower-profit points are shown in gray.
Out[6]:
Console
LP Relaxation Solution: x₁ = 3, x₂ = 2, Profit = \$13
Optimal Integer Solution: x₁ = 3, x₂ = 2, Profit = \$13

Note: In this case, the LP relaxation happens to give an integer solution. However, the integer constraint ensures we only consider feasible integer points