Interactive companion · Chapter 1 · Introduction to search and optimization

Optimization Basics Lab

Ants find the shortest path to food, bees build honeycombs with the least wax, and delivery companies trim every mile. Optimization means choosing the values of decision variables that make an objective as good as possible while respecting constraints. Every optimization method is, at heart, a search.

Based on Chapter 1, “Introduction to search and optimization,” in Alaa Khamis, Optimization Algorithms: AI techniques for design, planning, and control problems, Manning Publications, 2024 (ISBN 978-1633438835).

A search agent mixing exploration (gold) and exploitation (blue) on a landscape with a wide local minimum and a narrow global one.

Part 1 · Sections 1.1 and 1.2

Why care about search and optimization?

Search is the systematic examination of states, starting from an initial state and ending (hopefully) at the goal state. Optimization techniques are search methods whose goal is an optimal or near-optimal state within the feasible search space: the part of the problem space where every constraint is satisfied.

Optimization is everywhere: nature (foraging ants and bees, bird migration), daily life (planning errands, shopping on a budget), and industry (parcel delivery, cell tower placement, ride-hailing, emergency dispatch, airline crew scheduling, hospital planning, Industry 4.0 and smart cities). Scale matters, though: a small pizzeria with ten deliveries a night gains little from sophisticated routing, while fleet dispatching and crew scheduling gain a lot.

Nature

Ants and bees optimize foraging; honeycombs use hexagons for space, wax and strength; migration routes save energy.

Daily life

Ordering errands to save travel time, stretching a grocery budget, fitting a workout into limited time.

Industry

Delivery routing, tower placement, ride-hailing dispatch, crew scheduling, supply chains, assembly lines.

Cities

Asset assignment, energy, water, pollution, waste management and bushfire containment.

The book’s methodology (Figure 1.1)

Source of inspiration→Pseudocode→Parameters and heuristics→Hand iterations on a toy problem→Python on a scaled-up problem→Refinement and tuning→Pros, cons and adaptation→Real-world problems
Part 2 · Section 1.2

From toy problems to the real world

Toy problems are small enough to solve by hand, so every step of an algorithm can be followed. Real-world problems fall into three groups that differ in how long we can wait and how good the answer must be (Figure 1.2).

GroupExamplesTime to solveExpected quality
Benchmark and classicTest functions (Ackley, Rastrigin, Rosenbrock…), n-puzzle, n-queens, bin packing, knapsack, MST, TSP, Chinese postman, assignment, job shop, graph coloringVariesKnown optimum, for testing
Design (strategic)Suspension and wheel design, VLSI, assembly line balancing, railway scheduling, PID tuning, timetabling, political districting, hospital planning, placing EV chargersMinutes to days, run rarelyHigh
Planning (tactical)Motion planning, ride-sharing, shift planning, task allocation, vehicle routing, appointment and patient scheduling, trip itinerariesSeconds to minutesMedium
Control (operational)Elevator dispatching, communication relaying, vehicle motion control, multirobot control, microrobot drug delivery, truck platooningMilliseconds to seconds, repeatedlyLower: speed matters more
Part 3 · Section 1.3

Basic ingredients of optimization problems

Find X that optimizes f(X) subject to equality constraints gi(X) = 0 and inequality constraints hj(X) ≤ 0 (equation 1.1), where

  • X = (x1, …, xn) are the decision variables, the inputs we control;
  • f(X) = (f1, …, fM) are the objective functions (also called criteria, cost, utility or merit functions);
  • g and h are the constraints, plus boundary constraints on each variable.
SolutionMeaning
FeasibleSatisfies all the constraints
OptimalFeasible, with the best objective value
Near-optimalFeasible and very good, but not necessarily the best
Global minimumf(x*) ≤ f(x) for every x in the domain
Strong local minimumf(x*) < f(x) for all other x in a neighborhood
Weak local minimumf(x*) ≤ f(x) in a neighborhood: flat spots and saddle points

Feasible solutions, global and local minima (Figure 1.3)

Minima inside the bounds

Click the curve to probe a point
global minimum (feasible)strong local minimumminimum outside the boundsinfeasible region

Decision variables and objectives: ticket pricing (Sections 1.3.1 and 1.3.2)

An event organizer pays fixed costs a (venue, security, speakers) and a variable cost b per attendee (badges, lunch). Demand falls with the ticket price x: Q = 5,000 − 20x tickets (equation 1.2), so 5,000 attend for free and nobody at $250. The profit is f(x) = Qx − (a + Qb) (equations 1.3 and 1.4). With a = $50,000 and b = $60, f(x) = −20x² + 6,200x − 350,000; setting df/dx = −40x + 6,200 = 0 gives x* = $155 and a profit of $130,500 (Figure 1.5).

Duality: maximizing f is the same as minimizing −f. Scaling f by a positive constant or shifting it by a constant changes the objective value but not the optimal decision (Figure 1.4).

Set the price

Listing 2.4 solves it with SciPy

Multiple objectives: electric vehicle design (Figure 1.6)

An EV designer wants to minimize acceleration time (0 to 60 mph) and maximize EPA range. A bigger battery adds range but also weight, so the objectives conflict. Decision variables might be wheel size, motor power and battery capacity.

A preference-based approach scalarizes the objectives with weights; the result depends on the weights you choose. A Pareto approach keeps every nondominated design (no other design is better in one objective without being worse in the other) and decides later. The 13 designs from the book’s notebook are all nondominated: each extra mile of range costs acceleration.

When there is no objective at all, only constraints, the problem is a constraint-satisfaction problem, such as placing 4 queens on a 4 × 4 board without attacks (two equally good solutions exist).

Weigh range against acceleration

Hard and soft constraints: the cargo bike (Section 1.3.3)

Hard constraints must be satisfied; soft constraints are desirable. In school timetabling, two lectures in one room at once is forbidden (hard), while back-to-back lectures in nearby rooms are merely preferred (soft). In a navigation app, avoiding ferries or toll roads can be hard, avoiding school zones at drop-off time soft.

Soft constraints enter the objective as a penalty or reward. A cargo bike can carry 100 kg; choose parcels to maximize profit. As a soft constraint, an overweight load loses a penalty of 50 (equations 1.5 and 1.6). Penalties can also be raised as the search goes on: lenient early to encourage exploration, strict at the end.

Load the bike (Figure 1.7)

Tick the parcels

Part 4 · Section 1.4

Well-structured vs. ill-structured problems

Herbert Simon described well-structured problems (WSPs): a clear test for solutions, a problem space that represents the states, legal state changes, and computation that stays tractable. A robot picking defective parts off a conveyor belt is one: the states (arm position, speed, gripper), operators (motion commands), goal, stopping and evaluation criteria (duration, success rate) are all clear, and the workspace is static and fully observable (Figure 1.8).

Ill-structured problems (ISPs) have some of the following: unclear or changing goals, multimodality, no exact model or proven algorithm, hard-to-predict consequences, uncertainty and partial observability, and computational intractability. Dispatching four elevators over ten floors is one: calls cannot be predicted and the state space is enormous (Figure 1.9).

Some problems are WSPs in principle but ISPs in practice: the traveling salesman problem is easy to state, but naive search over 13 cities faces 13! = 6,227,020,800 tours. Most algorithms in the book are stochastic, derivative-free black-box solvers aimed at such problems.

How big is the state space?

Part 5 · Section 1.5

Search algorithms and the search dilemma

Exploration (diversification) investigates new regions of the feasible search space; exploitation (intensification) focuses on regions where good solutions were already found (Figure 1.10). Move to a new city and you first try many restaurants, then settle on a few favorites. Honeybee foragers exploit known flowers while scouts (5 to 25% of the foragers) keep exploring.

Pure exploitation behaves like local search and gets trapped in the nearest valley; pure exploration behaves like random search and rarely pins down the exact optimum. Here a searcher has 150 evaluations. At each one it explores (samples anywhere) with probability ε, or exploits (samples near the best point so far). A run counts as a success if it gets within 5% of the global minimum.

ExploitativeExplorative
Local searchRandom search
Fast, precise near a good solutionCovers the whole space
Easily trapped in local optimaImpractically slow to refine

Good algorithms balance the two.

Explore or exploit?

Minimize: brighter is lower
explore sampleexploit samplebest so farglobal minimum

Part 6 · Code

Python in Google Colab

Open the book’s Chapter 1 notebooks directly in Colab, or the Optimization Basics Lab companion notebook with the from-scratch implementations below. They need only the Python standard library.

Notebooks

From-scratch implementations

# Feasible solutions, global and local minima (Section 1.3, Figure 1.3)
import math

f = lambda x: 3 * math.cos(x * x) - math.sin(3 * x) * math.exp(-x)    # the multimodal function of MathPlots.ipynb
LB, UB = -3, 3                                                       # boundary constraints on the decision variable


def local_minima(lo, hi, n=60000):
    """Scan a fine grid and keep the points lower than both neighbors."""
    xs = [lo + (hi - lo) * i / n for i in range(n + 1)]
    return [x for a, x, b in zip(xs, xs[1:], xs[2:]) if f(x) < f(a) and f(x) < f(b)]


minima = local_minima(-4, 4)
feasible = [x for x in minima if LB <= x <= UB]
best = min(feasible + [LB, UB], key=f)                               # the bounds can hold the optimum too
for x in minima:
    kind = "infeasible" if not LB <= x <= UB else "global minimum" if x == best else "strong local minimum"
    print(f"x = {x:6.3f}   f(x) = {f(x):8.3f}   {kind}")
print(f"Lowest point overall: x = {min(minima, key=f):.3f}, but it lies outside the bounds [{LB}, {UB}]")

To run: open the companion notebook in Colab and press Shift+Enter in each cell, or paste a tab’s code into any notebook.

Part 7 · Summary

What to remember

Three ingredients

Decision variables you control, objective functions to minimize or maximize, and hard or soft constraints the solution must respect.

Feasible and optimal

The best solution is the best feasible one; local minima can trap a search, and bounds can move the optimum.

Ill-structured problems

Most real problems are dynamic, uncertain and huge, so near-optimal answers found quickly often beat optimal answers found late.

Explore and exploit

Balancing diversification and intensification lets an algorithm escape local optima without wasting time.

References: H. A. Simon, “The structure of ill structured problems,” Artificial Intelligence, 1973. A. Khamis, Optimization Algorithms: AI techniques for design, planning, and control problems, Manning, 2024, Chapter 1.

Part 8 · Quiz

Check your understanding

Two quick questions on Chapter 1. Pick an answer to check it.

Q1. In the electric vehicle design example, what makes a design part of the Pareto set?

Q2. A searcher with a fixed budget of evaluations only exploits, always sampling near the best point found so far. What is the most likely outcome on a landscape with a wide local minimum and a narrow global one?

For more exercises with full solutions, see Appendix C, “Exercises and solutions,” in Optimization Algorithms.