Artificial Intelligence Applications in Engineering (MUH-920), week 2 of 14

Problem Solving by Search: Routes, Plans and Paths

Prof. Dr. Utku Kose, Süleyman Demirel University

Problems as state spaces

A search problem has five parts [1]. The initial state describes where the agent starts. The actions available in a state say what the agent can do. The transition model gives the state that results from an action. The goal test decides whether a state solves the problem, and the path cost adds up the cost of the actions along a sequence. A solution is a sequence of actions from the initial state to a goal state, and an optimal solution has the lowest path cost among all solutions.

The formulation is an engineering decision in its own right. For a haul truck in an open-pit mine, a state can be a node of the road network, and the path cost can be travel time, fuel or tyre wear. For a mobile robot in a factory, the state can be a cell of an occupancy grid, which divides the floor into squares that are free or blocked [2]. For a crane that stacks containers, the state is the arrangement of all containers, and the number of states grows combinatorially. A good formulation keeps only the details that matter for the decision: A route planner for a truck does not need the colour of the truck, but it may need the slope of every road segment.

Check your understanding. A planner chooses the order in which a laser cutter visits 60 contours on a steel sheet. What is a natural state in the formulation?

Formulas: A search problem

A search problem is written with five parts [1]. The initial state is s0s_0. The set of actions available in state ss is A(s)A(s). The transition model T(s,a)T(s, a) returns the state reached by doing action aa in state ss. The goal test decides whether a state is a goal. The step cost c(s,a,s′)c(s, a, s') is the cost of moving from ss to s′s' with action aa. A solution is a sequence of actions a1,a2,…,ama_1, a_2, \ldots, a_m that leads through the states s0,s1,…,sms_0, s_1, \ldots, s_m to a goal state, and its path cost is the sum of the step costs:

g=∑i=1mc(si−1,ai,si)g = \sum_{i=1}^{m} c(s_{i-1}, a_i, s_i)

An optimal solution has the smallest path cost C⋆C^{\star} among all solutions. The size of the state space decides which methods are feasible. An occupancy grid with RR rows and KK columns has R×KR \times K cells. A robot that also has one of HH headings has R×K×HR \times K \times H states. Orders of nn jobs are counted by the factorial

n!=n×(n−1)×(n−2)×⋯×2×1n! = n \times (n - 1) \times (n - 2) \times \cdots \times 2 \times 1

because the first job can be chosen in nn ways, the second in n−1n - 1 ways, and so on. The factorial also follows step by step from n!=n×(n−1)!n! = n \times (n - 1)! with 1!=11! = 1.

Worked example: How large are three state spaces?

Step 1. A factory floor. A grid with 40 rows and 25 columns has 40×25=100040 \times 25 = 1000 cells, so a search for a mobile robot has at most 1000 states.

Step 2. The same floor with headings. A vehicle that cannot turn on the spot must also remember one of 8 headings: 1000×8=80001000 \times 8 = 8000 states.

Step 3. Orders of jobs. The factorial grows by one multiplication per job:

1!=1,    2!=2×1=2,    3!=3×2=6,    4!=4×6=24,    5!=5×24=1201! = 1, \;\; 2! = 2 \times 1 = 2, \;\; 3! = 3 \times 2 = 6, \;\; 4! = 4 \times 6 = 24, \;\; 5! = 5 \times 24 = 120
6!=6×120=720,    7!=7×720=5040,    8!=8×5040=403206! = 6 \times 120 = 720, \;\; 7! = 7 \times 720 = 5040, \;\; 8! = 8 \times 5040 = 40320
9!=9×40320=362880,    10!=10×362880=36288009! = 9 \times 40320 = 362880, \;\; 10! = 10 \times 362880 = 3628800

Continuing in the same way gives 15!=130767436800015! = 1307674368000 and 20!=243290200817664000020! = 2432902008176640000, about 2.43×10182.43 \times 10^{18}, the "more than two quintillion" orders of the lecture.

Step 4. Time to try them all. A computer that evaluates 10910^{9} orders per second needs

2.43×1018109=2.43×109 s,    2.43×1093.156×107=77 years\frac{2.43 \times 10^{18}}{10^{9}} = 2.43 \times 10^{9}\ \text{s}, \;\; \frac{2.43 \times 10^{9}}{3.156 \times 10^{7}} = 77\ \text{years}

since a year has about 3.156×1073.156 \times 10^{7} seconds. Enumeration is hopeless already for twenty jobs, while the grid of Step 1 is searched in a fraction of a second.

Uninformed search: Breadth first, depth first and uniform cost

Uninformed strategies know nothing about the goal beyond the goal test. They differ only in the order in which they expand nodes of the search tree. Breadth-first search expands the shallowest node first by keeping the frontier in a first-in first-out queue. It finds the solution with the fewest actions, which is optimal when every action has the same cost. Depth-first search expands the deepest node first with a last-in first-out stack, needs little memory, but may wander along an infinite branch and does not guarantee the shortest solution. Iterative deepening repeats depth-limited search with growing limits and combines the memory use of depth-first search with the completeness of breadth-first search [3].

When actions have different costs, the fewest actions is not the cheapest path. Uniform-cost search expands the node with the lowest path cost so far, which is Dijkstra's algorithm on an explicit graph [4]. It is optimal for non-negative costs, because a node is expanded only when no cheaper route to it can exist.

The price of uninformed search is growth. With branching factor b and solution depth d, breadth-first search may generate on the order of b to the power d nodes. On a grid with four moves and a goal forty steps away, blind search explores almost every reachable cell within that radius. The number becomes unmanageable for combinatorial problems such as sequencing: Twenty jobs can be ordered in more than two quintillion ways.

Growth of the number of nodes in a complete search tree with branching factor b. Uninformed search must be guided or limited when b or d is large .
Figure 2.1. Growth of the number of nodes in a complete search tree with branching factor b. Uninformed search must be guided or limited when b or d is large [1].

Check your understanding. Road segments of a network have different travel times. Which strategy returns the fastest route without extra knowledge about the goal?

Formulas: Search trees, frontiers and uniform cost

Search builds a tree whose root is the initial state. If every state has bb successors, the branching factor, then depth kk of the tree holds bkb^k nodes, and a complete tree of depth dd holds

N(b,d)=1+b+b2+⋯+bd=∑k=0dbk=bd+1−1b−1N(b, d) = 1 + b + b^2 + \cdots + b^d = \sum_{k=0}^{d} b^k = \frac{b^{d+1} - 1}{b - 1}

nodes, by the formula for a geometric series. Breadth-first search must keep the whole deepest level in memory, about bdb^d nodes, while depth-first search keeps one path with its unexplored siblings, about b×db \times d nodes. Iterative deepening runs depth-limited searches with limits 0,1,…,d0, 1, \ldots, d, so a node at depth kk is generated d+1−kd + 1 - k times:

NIDS(b,d)=∑k=0d(d+1−k) bkN_{\text{IDS}}(b, d) = \sum_{k=0}^{d} (d + 1 - k)\, b^k

Uniform-cost search stores for every reached state the cost g(n)g(n) of the cheapest path found so far. When node nn is expanded, each neighbour n′n' receives the candidate cost g(n)+c(n,n′)g(n) + c(n, n'), and the stored value is replaced only if the candidate is smaller:

g(n′)←min⁡(g(n′),  g(n)+c(n,n′))g(n') \leftarrow \min\left(g(n'),\; g(n) + c(n, n')\right)

The arrow means "is replaced by". The frontier node with the smallest gg is expanded next, which is Dijkstra's algorithm [4]. Because costs are not negative, a node's gg can no longer improve once it is expanded, and the cost of the goal at expansion is optimal.

Worked example: How fast does the tree grow?

A grid robot has b=4b = 4 moves, and the goal lies d=5d = 5 moves away.

Step 1. Nodes per level. 40=14^0 = 1, 41=44^1 = 4, 42=164^2 = 16, 43=644^3 = 64, 44=2564^4 = 256 and 45=10244^5 = 1024.

Step 2. The whole tree, by adding the levels.

N(4,5)=1+4+16+64+256+1024=1365N(4, 5) = 1 + 4 + 16 + 64 + 256 + 1024 = 1365

Step 3. The same number from the closed formula.

N(4,5)=46−14−1=4096−13=40953=1365N(4, 5) = \frac{4^{6} - 1}{4 - 1} = \frac{4096 - 1}{3} = \frac{4095}{3} = 1365

Step 4. Iterative deepening. The root is generated six times, the nodes of depth 1 five times, and so on:

NIDS(4,5)=6×1+5×4+4×16+3×64+2×256+1×1024=6+20+64+192+512+1024=1818N_{\text{IDS}}(4, 5) = 6 \times 1 + 5 \times 4 + 4 \times 16 + 3 \times 64 + 2 \times 256 + 1 \times 1024 = 6 + 20 + 64 + 192 + 512 + 1024 = 1818

Iterative deepening generates 1818/1365=1.3321818 / 1365 = 1.332 times as many nodes as one breadth-first pass, only about a third more, but it needs memory for one path only: b×d=4×5=20b \times d = 4 \times 5 = 20 nodes instead of the 45=10244^5 = 1024 nodes of the deepest level. Part A of the interactive lab repeats the count for any branching factor and depth.

Worked example: Breadth-first and uniform-cost search on a road network

The examples of this week use a small road network with seven junctions. Each junction has map coordinates in kilometres, and each road has a travel time in minutes. Every travel time is at least as large as the straight-line distance between its two junctions, as on real roads. Part B of the interactive lab draws the network and runs every algorithm on it step by step.

Junction Coordinates (x,y)(x, y) in km
S (0,0)(0, 0)
A (2,3)(2, 3)
B (3,0)(3, 0)
C (5,3)(5, 3)
D (6,0)(6, 0)
E (4,−2)(4, -2)
G (8,1)(8, 1)
Road Travel time cc (min)
S to A 4
S to B 3
S to E 6
A to B 4
A to C 3
B to C 5
B to D 4
C to G 4
D to G 3
E to G 6

Breadth-first search. The queue starts with S. Neighbours are added in alphabetical order, and a junction that has been reached before is not added again.

Expansion 1. Expand S; the new neighbours A, B, E join the back of the queue. Queue: A, B, E.

Expansion 2. Expand A; the new neighbours C join the back of the queue. Queue: B, E, C.

Expansion 3. Expand B; the new neighbours D join the back of the queue. Queue: E, C, D.

Expansion 4. Expand E; the new neighbours G join the back of the queue. Queue: C, D, G.

Expansion 5. Expand C; all neighbours have been reached before. Queue: D, G.

Expansion 6. Expand D; all neighbours have been reached before. Queue: G.

Expansion 7. G is taken from the front of the queue and passes the goal test.

Breadth-first search returns the route S to E to G with 2 roads, the fewest possible, but its travel time is 6+6=126 + 6 = 12 minutes.

Uniform-cost search. The frontier starts with g(S)=0g(\text{S}) = 0. In each expansion the junction with the smallest gg leaves the frontier; ties are broken alphabetically. The frontier lists junctions in the order in which they would be expanded.

Expansion 1. Expand S with g(S)=0g(\text{S}) = 0: g(A)=0+4=4g(\text{A}) = 0 + 4 = 4 (new); g(B)=0+3=3g(\text{B}) = 0 + 3 = 3 (new); g(E)=0+6=6g(\text{E}) = 0 + 6 = 6 (new). Frontier: B, A, E.

Expansion 2. Expand B with g(B)=3g(\text{B}) = 3: A would get 3+4=73 + 4 = 7, which is not better than 4, so nothing changes; g(C)=3+5=8g(\text{C}) = 3 + 5 = 8 (new); g(D)=3+4=7g(\text{D}) = 3 + 4 = 7 (new); S is already expanded. Frontier: A, E, D, C.

Expansion 3. Expand A with g(A)=4g(\text{A}) = 4: B is already expanded; C gets 4+3=74 + 3 = 7, which is better than 8, so g(C)=7g(\text{C}) = 7; S is already expanded. Frontier: E, C, D.

Expansion 4. Expand E with g(E)=6g(\text{E}) = 6: g(G)=6+6=12g(\text{G}) = 6 + 6 = 12 (new); S is already expanded. Frontier: C, D, G.

Expansion 5. Expand C with g(C)=7g(\text{C}) = 7: A is already expanded; B is already expanded; G gets 7+4=117 + 4 = 11, which is better than 12, so g(G)=11g(\text{G}) = 11. Frontier: D, G.

Expansion 6. Expand D with g(D)=7g(\text{D}) = 7: B is already expanded; G gets 7+3=107 + 3 = 10, which is better than 11, so g(G)=10g(\text{G}) = 10. Frontier: G.

Expansion 7. G has the smallest cost on the frontier, g(G)=10g(\text{G}) = 10, and passes the goal test.

Following the stored predecessors back from G gives the route S to B to D to G with travel time 3+4+3=103 + 4 + 3 = 10 minutes, the optimum. Uniform-cost search expanded 7 junctions to prove it.

Informed search: Heuristics and A*

A heuristic function h(n) estimates the cost from node n to the nearest goal. Greedy best-first search expands the node with the smallest h and often reaches a goal quickly, but it can be misled into long detours. A* combines the cost already paid, g(n), with the estimate, and expands the node with the smallest f(n) = g(n) + h(n) [5].

Hart, Nilsson and Raphael showed that A* returns an optimal path if the heuristic is admissible, which means it never overestimates the true remaining cost [5]. The straight-line distance is admissible for road travel, because no road is shorter than a straight line. On a grid with four moves, the Manhattan distance, the sum of horizontal and vertical offsets, is admissible. A heuristic is consistent when its estimate never drops by more than the cost of one step. Consistency implies admissibility and guarantees that A* never needs to reopen a node [6].

Better heuristics save work. If one admissible heuristic is always at least as large as another, A* with the larger one expands no more nodes. The zero heuristic turns A* into uniform-cost search, and a perfect heuristic walks straight to the goal. Weighted A* multiplies the heuristic by a factor larger than one: It usually expands far fewer nodes and returns a path whose cost is at most that factor times the optimum, which is a common engineering compromise when planning must meet a deadline.

Animation: Breadth-first search and A* on the same map

Both searches start at the green cell and look for the red cell. Blue cells have been expanded. Compare how many cells each algorithm expands before it finds a path of the same length.

Breadth-first search
A* with Manhattan distance

Check your understanding. Which heuristic is admissible for a robot that moves on a grid with four moves of cost 1?

Formulas: Heuristics and A*

A heuristic h(n)h(n) estimates the cost from node nn to the nearest goal, and g(n)g(n) is the cost already paid to reach nn. Greedy best-first search expands the node with the smallest h(n)h(n). A* expands the node with the smallest evaluation [5]:

f(n)=g(n)+h(n)f(n) = g(n) + h(n)

Let h⋆(n)h^{\star}(n) be the true cost of the cheapest path from nn to a goal. A heuristic is admissible when it never overestimates:

h(n)≤h⋆(n)   for every node nh(n) \le h^{\star}(n) \;\text{ for every node } n

and consistent when the estimate never falls by more than the cost of one step from nn to a neighbour n′n':

h(n)≤c(n,n′)+h(n′)h(n) \le c(n, n') + h(n')

Two common heuristics measure the offsets Δx\Delta x and Δy\Delta y between a node and the goal. The Manhattan distance suits grids with four moves, and the straight-line, or Euclidean, distance suits roads and free movement [6]:

hM=∣Δx∣+∣Δy∣,    hE=Δx2+Δy2h_{\text{M}} = |\Delta x| + |\Delta y|, \;\; h_{\text{E}} = \sqrt{\Delta x^2 + \Delta y^2}

The zero heuristic h=0h = 0 turns A into uniform-cost search. Weighted A expands by fw(n)=g(n)+w h(n)f_w(n) = g(n) + w\, h(n) with a weight w>1w > 1; with an admissible hh, the cost CC of its solution satisfies C≤w C⋆C \le w\, C^{\star}.

Worked example: A* on the same road network

Step 1. Heuristic values. The goal G lies at (8,1)(8, 1). The straight-line distance from each junction, rounded to three decimal places, is

Junction Calculation h(n)h(n)
S (8−0)2+(1−0)2=64+1=65\sqrt{(8 - 0)^2 + (1 - 0)^2} = \sqrt{64 + 1} = \sqrt{65} 8.062
A (8−2)2+(1−3)2=36+4=40\sqrt{(8 - 2)^2 + (1 - 3)^2} = \sqrt{36 + 4} = \sqrt{40} 6.325
B (8−3)2+(1−0)2=25+1=26\sqrt{(8 - 3)^2 + (1 - 0)^2} = \sqrt{25 + 1} = \sqrt{26} 5.099
C (8−5)2+(1−3)2=9+4=13\sqrt{(8 - 5)^2 + (1 - 3)^2} = \sqrt{9 + 4} = \sqrt{13} 3.606
D (8−6)2+(1−0)2=4+1=5\sqrt{(8 - 6)^2 + (1 - 0)^2} = \sqrt{4 + 1} = \sqrt{5} 2.236
E (8−4)2+(1−(−2))2=16+9=25\sqrt{(8 - 4)^2 + (1 - (-2))^2} = \sqrt{16 + 9} = \sqrt{25} 5
G (8−8)2+(1−1)2=0+0=0\sqrt{(8 - 8)^2 + (1 - 1)^2} = \sqrt{0 + 0} = \sqrt{0} 0

Step 2. Admissibility. The cheapest travel times to G, found by uniform-cost search from every junction, are the values h⋆(n)h^{\star}(n). Every estimate is smaller, as it must be, because no road is shorter than the straight line:

Junction h(n)h(n) h⋆(n)h^{\star}(n) h(n)≤h⋆(n)h(n) \le h^{\star}(n)
S 8.062 10 yes
A 6.325 7 yes
B 5.099 7 yes
C 3.606 4 yes
D 2.236 3 yes
E 5 6 yes
G 0 0 yes

The heuristic is also consistent. For the road from S to B, for example, h(S)=8.062≤c(S,B)+h(B)=3+5.099=8.099h(\text{S}) = 8.062 \le c(\text{S}, \text{B}) + h(\text{B}) = 3 + 5.099 = 8.099.

Step 3. The search. The frontier starts with S, and f(S)=0+8.062=8.062f(\text{S}) = 0 + 8.062 = 8.062.

Expansion 1. Expand S with f(S)=0+8.062=8.062f(\text{S}) = 0 + 8.062 = 8.062: g(A)=0+4=4g(\text{A}) = 0 + 4 = 4 and f(A)=4+6.325=10.325f(\text{A}) = 4 + 6.325 = 10.325; g(B)=0+3=3g(\text{B}) = 0 + 3 = 3 and f(B)=3+5.099=8.099f(\text{B}) = 3 + 5.099 = 8.099; g(E)=0+6=6g(\text{E}) = 0 + 6 = 6 and f(E)=6+5=11f(\text{E}) = 6 + 5 = 11. Frontier in order of ff: B, A, E.

Expansion 2. Expand B with f(B)=3+5.099=8.099f(\text{B}) = 3 + 5.099 = 8.099: A would get 3+4=73 + 4 = 7, which is not better than 4, so nothing changes; g(C)=3+5=8g(\text{C}) = 3 + 5 = 8 and f(C)=8+3.606=11.606f(\text{C}) = 8 + 3.606 = 11.606; g(D)=3+4=7g(\text{D}) = 3 + 4 = 7 and f(D)=7+2.236=9.236f(\text{D}) = 7 + 2.236 = 9.236; S is already expanded. Frontier in order of ff: D, A, E, C.

Expansion 3. Expand D with f(D)=7+2.236=9.236f(\text{D}) = 7 + 2.236 = 9.236: B is already expanded; g(G)=7+3=10g(\text{G}) = 7 + 3 = 10 and f(G)=10+0=10f(\text{G}) = 10 + 0 = 10. Frontier in order of ff: G, A, E, C.

Expansion 4. G has the smallest ff on the frontier, f(G)=10+0=10f(\text{G}) = 10 + 0 = 10, and passes the goal test.

Step 4. Result. A* returns the same optimal route, S to B to D to G with 10 minutes, after 4 expansions instead of 7. Junctions A, C and E, which lie away from the goal or behind expensive roads, were never expanded, because their ff values stayed above 10. Part C of the interactive lab runs the uninformed and informed algorithms on a grid with walls and slow terrain and reports the path cost and the expanded cells of each.

Path planning for vehicles and machines

Path planning in engineering adds physical constraints to graph search. Occupancy grids and road graphs give the search space, while costs encode travel time, energy, risk or wear [2]. Vehicles cannot turn on the spot, so planners for cars and trucks search over positions and headings. Hybrid A* keeps continuous positions inside discrete cells and has been used to park and manoeuvre autonomous vehicles in unstructured environments such as parking lots [7]. Surveys of self-driving urban vehicles place such graph-search planners next to sampling-based and optimisation-based methods [8].

Terrain adds cost and feasibility. A haul road in a mine, a pipeline or a forest road must respect a maximum grade, and steep segments cost more fuel and time. A digital elevation model turns the terrain into a grid of heights, and the cost of moving between two cells can combine distance with a penalty on slope and a hard limit above which the move is forbidden. The notebook applies this idea to a real elevation grid near Isparta from the Copernicus GLO-90 model, served by the Open-Meteo Elevation API [9, 10].

Formulas: Cost of a move across terrain

A digital elevation model stores the height zz of every cell of a grid with spacing ℓ\ell (ell), 90 metres for the GLO-90 model [9]. A move to one of the eight neighbouring cells, Δr\Delta r rows and Δk\Delta k columns away, has the horizontal length

d=ℓΔr2+Δk2d = \ell \sqrt{\Delta r^2 + \Delta k^2}

which is ℓ\ell for a straight move and ℓ2\ell\sqrt{2} for a diagonal one. The grade of the move is the rise over the run, often quoted as a percentage:

grade=∣z′−z∣d\text{grade} = \frac{|z' - z|}{d}

The planner of the notebook forbids moves whose grade exceeds a limit Gmax⁡G_{\max} and charges the others with a slope penalty pp:

c=d (1+p×grade)=d+p ∣z′−z∣c = d\, (1 + p \times \text{grade}) = d + p\, |z' - z|

The second form follows by multiplying out, since d×p×∣z′−z∣/d=p ∣z′−z∣d \times p \times |z' - z| / d = p\, |z' - z|: Every metre of height difference costs pp metres of extra distance. The heuristic h=ℓΔr2+Δk2h = \ell\sqrt{\Delta r^2 + \Delta k^2} to the goal stays admissible, because no move costs less than its horizontal length.

Worked example: Three moves from one cell

A cell at z=1210z = 1210 m has three candidate neighbours. The planner uses ℓ=90\ell = 90 m, p=10p = 10 and Gmax⁡=0.25G_{\max} = 0.25.

Step 1. East, to z′=1225z' = 1225 m. The length is d=90×02+12=90d = 90 \times \sqrt{0^2 + 1^2} = 90 m, the rise is ∣1225−1210∣=15|1225 - 1210| = 15 m and the grade is 15/90=0.166715 / 90 = 0.1667, below the limit 0.25. The cost is

c=90×(1+10×0.1667)=90×2.667=240.03c = 90 \times (1 + 10 \times 0.1667) = 90 \times 2.667 = 240.03

or exactly 90+10×15=24090 + 10 \times 15 = 240 m with the second form; the small difference comes from rounding the grade.

Step 2. North-east, to z′=1240z' = 1240 m. The diagonal length is d=90×12+12=90×1.414214=127.279d = 90 \times \sqrt{1^2 + 1^2} = 90 \times 1.414214 = 127.279 m, the rise is 30 m and the grade is 30/127.279=0.235730 / 127.279 = 0.2357, still below 0.25. The cost is

c=127.279+10×30=127.279+300=427.279c = 127.279 + 10 \times 30 = 127.279 + 300 = 427.279

Step 3. North, to z′=1235z' = 1235 m. The grade is 25/90=0.277825 / 90 = 0.2778, which exceeds 0.25, so the move is forbidden and never enters the frontier.

The penalty makes the planner prefer the long way round a hill to a short climb over it: The diagonal move climbs 30 m and costs as much as a level route of 427 m. Part D of the interactive lab plans a route across a valley with such costs and forbids moves steeper than a grade limit.

Systematic search remembers the paths it explores. Local search keeps only a current solution and improves it by small changes, which suits problems where the path does not matter and only the final configuration counts, such as a layout or a schedule. Hill climbing accepts only improvements and stops at the first local optimum. Simulated annealing sometimes accepts worse solutions, with a probability that falls as a temperature parameter is lowered, and can therefore escape local optima [11]. Weeks 5 and 6 develop this line into evolutionary and swarm algorithms, which keep a population of candidate solutions instead of one.

Check your understanding. Why can simulated annealing escape a local optimum where hill climbing stops?

Formulas: Hill climbing and simulated annealing

Local search keeps one current solution xx with cost f(x)f(x) and proposes a neighbour x′x'. The change in cost is

Δ=f(x′)−f(x)\Delta = f(x') - f(x)

Hill climbing, written here for minimisation, accepts the neighbour only if Δ<0\Delta < 0. Simulated annealing always accepts improvements and accepts a worse neighbour, Δ>0\Delta > 0, with the probability [11]

P(accept)=e−Δ/TP(\text{accept}) = e^{-\Delta / T}

where the temperature T>0T > 0 controls how tolerant the search is. In practice a random number uu between 0 and 1 is drawn, and the worse neighbour is accepted if u<e−Δ/Tu < e^{-\Delta / T}. The temperature is lowered by a cooling schedule, most often geometric with a factor α\alpha (alpha) slightly below one:

Tk=T0 αkT_k = T_0\, \alpha^{k}

Solving T0αk=TendT_0 \alpha^{k} = T_{\text{end}} for kk with logarithms gives the number of steps until the temperature reaches TendT_{\text{end}}:

k=ln⁡(Tend/T0)ln⁡αk = \frac{\ln(T_{\text{end}} / T_0)}{\ln \alpha}

Worked example: Accepting a worse layout

A layout has cost f(x)=52f(x) = 52, and the proposed neighbour has f(x′)=55f(x') = 55.

Step 1. Change in cost. Δ=55−52=3\Delta = 55 - 52 = 3, a worsening. Hill climbing rejects the move.

Step 2. Acceptance probability at three temperatures.

T=10:    e−3/10=e−0.3=0.740818T = 10: \;\; e^{-3/10} = e^{-0.3} = 0.740818
T=2:    e−3/2=e−1.5=0.22313T = 2: \;\; e^{-3/2} = e^{-1.5} = 0.22313
T=0.5:    e−3/0.5=e−6=0.002479T = 0.5: \;\; e^{-3/0.5} = e^{-6} = 0.002479

Step 3. Decision with a random number. The random number generator returns u=0.41u = 0.41. At T=10T = 10, 0.41<0.7408180.41 < 0.740818, so the worse layout is accepted. At T=2T = 2, 0.41>0.223130.41 > 0.22313, so it is rejected. Early in the search, when the temperature is high, the search moves freely between valleys of the cost landscape; late in the search it behaves almost like hill climbing.

Step 4. Cooling schedule. With T0=10T_0 = 10 and α=0.95\alpha = 0.95, the temperature after 20 steps is

T20=10×0.9520=10×0.358486=3.58486T_{20} = 10 \times 0.95^{20} = 10 \times 0.358486 = 3.58486

and the number of steps until TT falls to 0.1 is

k=ln⁡(0.1/10)ln⁡0.95=−4.60517−0.051293=89.78k = \frac{\ln(0.1 / 10)}{\ln 0.95} = \frac{-4.60517}{-0.051293} = 89.78

so the temperature drops below 0.1 after 90 steps. Part E of the interactive lab runs simulated annealing on a cost profile with these formulas and shows each decision.

Python step 2: Lists, tuples and loops

The Python step of this week is part of the Colab notebook, where every explanation stands next to a cell that runs it and the step closes with a quick check and exercises with immediate feedback. The printable lecture notes contain the same step together with the outputs of its code. The Colab button below opens the notebook at the step. Run the setup cell just above the step first, since the exercises use its check function.

Open Python step 2 in Colab View the notebook on GitHub

Review cards

Select a card to turn it over.

State space
All states reachable from the initial state by sequences of actions, with the transitions between them.
Uniform-cost search
Expands the frontier node with the lowest path cost; Dijkstra's algorithm on an explicit graph [4].
Admissible heuristic
Never overestimates the true cost to the goal; guarantees optimal paths for A* [5].
Consistent heuristic
h(n) is at most the step cost to a neighbour n' plus h(n'); implies admissibility.
Weighted A*
Uses g + w h with w greater than one; faster search, path cost within a factor w of the optimum.
heapq
Python module for a binary heap: heappush adds an item, heappop removes the smallest.

Continue the week

The week continues with the simulation and the self-assessment of the interactive lab and with the Python step and the hands-on work of the Colab notebook. The week overview lists the discipline challenges, the weekly task and the research assignment.

Interactive lab Colab notebook Self-assessment Week overview and tasks

References

[1] Russell, S., & Norvig, P. (2021). Artificial Intelligence: A Modern Approach (4th ed.). Pearson.

[2] LaValle, S. M. (2006). Planning Algorithms. Cambridge University Press. https://doi.org/10.1017/CBO9780511546877

[3] Korf, R. E. (1985). Depth-first iterative-deepening: An optimal admissible tree search. Artificial Intelligence, 27(1), 97-109. https://doi.org/10.1016/0004-3702(85)90084-0

[4] Dijkstra, E. W. (1959). A note on two problems in connexion with graphs. Numerische Mathematik, 1, 269-271. https://doi.org/10.1007/BF01386390

[5] Hart, P. E., Nilsson, N. J., & Raphael, B. (1968). A formal basis for the heuristic determination of minimum cost paths. IEEE Transactions on Systems Science and Cybernetics, 4(2), 100-107. https://doi.org/10.1109/TSSC.1968.300136

[6] Pearl, J. (1984). Heuristics: Intelligent Search Strategies for Computer Problem Solving. Addison-Wesley.

[7] Dolgov, D., Thrun, S., Montemerlo, M., & Diebel, J. (2010). Path planning for autonomous vehicles in unknown semi-structured environments. The International Journal of Robotics Research, 29(5), 485-501. https://doi.org/10.1177/0278364909359210

[8] Paden, B., Čáp, M., Yong, S. Z., Yershov, D., & Frazzoli, E. (2016). A survey of motion planning and control techniques for self-driving urban vehicles. IEEE Transactions on Intelligent Vehicles, 1(1), 33-55. https://doi.org/10.1109/TIV.2016.2578706

[9] European Space Agency (2021). Copernicus Global Digital Elevation Model (GLO-90). https://doi.org/10.5270/ESA-c5d3d65

[10] Open-Meteo (2026). Open-Meteo free weather API: Historical Weather API and Elevation API (data licensed under CC BY 4.0). https://open-meteo.com

[11] Kirkpatrick, S., Gelatt, C. D., & Vecchi, M. P. (1983). Optimization by simulated annealing. Science, 220(4598), 671-680. https://doi.org/10.1126/science.220.4598.671