Problem Solving — CS & DS

Lecture 30 — Optimization Formulations

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

The Poster Budget: turn a wish into a formulation

cs-117

Posters cost 3 each and reach 40 students; flyers cost 1 and reach 10. Budget 12. Maximise reach. Write the objective, the constraint, and the decision variables - then solve.

best = 0
best_pf = (0, 0)
for p in range(13):
    for f in range(13):
        if 3 * p + f <= 12:
            reach = 40 * p + 10 * f
            if reach > best:
                best, best_pf = reach, (p, f)
print(best, best_pf)

Expected: maximum reach 160 with 4 posters, 0 flyers

Cost: O(budget^2) enumeration here; the formulation scales to solvers when the space explodes.

Worked example

The Shift Schedule: constraints as first-class citizens

cs-118

Three shifts, two staff (ada, bo), each shift needs exactly one person, nobody works two adjacent shifts, ada cannot take shift 1. Find any valid roster.

def solve():
    for s1 in ("bo",):
        for s2 in ("ada", "bo"):
            for s3 in ("ada", "bo"):
                roster = (s1, s2, s3)
                if len(set(roster)) == 3 or roster[0] == roster[1] or roster[1] == roster[2]:
                    continue
                if "ada" == roster[0]:
                    continue
                return roster
    return None

print(solve())

Expected: valid roster: shift 1 bo, shift 2 ada, shift 3 bo

Cost: Exponential worst case, but pruning makes toy instances instant - the general shape behind schedulers and solvers.

Worked example

The Auction Bluff: optimise what you control

cs-124

You bid for one of two identical items against one rival. Your value 100, budget cap 80. Sealed bid. What should you bid - and which part of that question is actually optimisable?

best_bid, best_val = 0, -1
for b in range(81):
    val = (100 - b) * b / 100
    if val > best_val:
        best_val, best_bid = val, b
print(best_bid, best_val)

Expected: bid 50 (expected surplus 25 under the uniform-rival assumption)

Cost: O(budget) enumeration; closed-form parabola vertex exists (enrichment: calculus).

Case attempt · lecture 30
Case cs-117 · Beginner

The Poster Budget

Maximise posters bought under a budget with two price options and a wall-length constraint. Write objective and constraints formally, then solve by hand.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-117 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-117 — not embedded in this deck.

Case attempt · lecture 30
Case cs-118 · Foundational

The Shift Schedule

Cover a 7-day rota with staff availability rules. Formulate; then solve a small instance by reasoning and a bigger one by search.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-118 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-118 — not embedded in this deck.

Case attempt · lecture 30
Case cs-119 · Intermediate

The Knapsack Twins

Two university-trip packing variants (must-fill weight vs must-stay-under) change the algorithm. Same story, different formulation; recognise which is which.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-119 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-119 — not embedded in this deck.

Case attempt · lecture 30
Case cs-120 · Expert

The Exam Seating

Seat students so no two friends sit adjacent in a grid, or report impossible. Constraint search with pruning; when to admit failure honestly.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-120 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-120 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. Name the three parts of a formulation.
  2. Why is 'minimise cost' often incomplete?
  3. How do you verify a claimed optimum?
Expected answers (instructor): decisions, objective, constraints unstated service/quality constraints make it meaningless — cs-118 bound argument or exhaustive check on a reduced instance
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF