Midterm preparation for Informed Search, Local Search & Genetic Algorithms.
Part A: Concept Check (True / False)¶
State whether each claim is true or false and justify it in one sentence.
A* with an admissible heuristic always returns an optimal solution when it terminates by selecting the goal for expansion.
Greedy best-first search is optimal whenever is admissible.
With for all , A* behaves exactly like Uniform-Cost Search.
Every consistent heuristic is admissible, and every admissible heuristic is consistent.
A* may stop as soon as the goal is generated (added to the frontier) and still be optimal.
If and are both admissible, then is admissible.
If and are admissible and for all , A* with never expands more nodes than with (ignoring ties).
Hill climbing is complete: it always finds the global optimum if one exists.
Simulated annealing with a very high temperature behaves like a random walk; with it behaves like hill climbing.
Tabu search can accept a move that makes the current solution worse.
Single-point crossover is a valid operator for permutation chromosomes.
Larger tournament size means higher selection pressure.
Mutation is the GA’s main exploitation operator.
Roulette-wheel selection can be applied directly to fitness values that may be negative.
Solution
True. Admissibility guarantees that no cheaper path remains on the frontier when the goal is selected. (For graph search without re-opening nodes, consistency is needed.)
False. Greedy ignores , so it can follow a cheap-looking but expensive path.
True. .
False. Consistent admissible, but not the converse.
False. A cheaper path to the goal may still be on the frontier. Stop only when the goal is selected for expansion.
False. The sum can overestimate. Example: if and , then . is admissible.
True. dominates and is therefore at least as informed.
False. It gets stuck on local optima, plateaus and ridges.
True. At high , so almost every move is accepted. At , worse moves are never accepted.
True. It moves to the best non-tabu neighbour even if it is worse than the current state.
False. Swapping tails produces duplicated or missing symbols, which is an invalid permutation. Use OX1, PMX or CX.
True. The best individual wins more of the tournaments.
False. Crossover exploits existing building blocks. Mutation is the exploration operator.
False. Probabilities would be negative or meaningless. Shift or scale the fitness, or use rank-based selection.
Part B: Tracing Greedy, UCS and A*¶
Exercise B1¶
Consider the directed graph below. Start node is S, goal node is G. The edge labels are step costs and the heuristic value is written in each node.
Compute the true cost-to-goal for every node. Is admissible? Is it consistent?
Run Greedy Best-First Search. Give the order of expansion, the path found and its cost.
Run Uniform-Cost Search. Give the order of expansion, the path found and its cost.
Run A*. Give the order of expansion (with the value of each expanded node), the path found and its cost.
Which algorithm expanded the fewest nodes? Which returned a non-optimal path? Explain why.
Solution
1. True costs. .
| Node | S | A | B | C | D | G |
|---|---|---|---|---|---|---|
| 7 | 6 | 4 | 7 | 3 | 0 | |
| 8 | 8 | 4 | 8 | 3 | 0 |
everywhere, so is admissible. Consistency, : S→A: ; S→B: ; A→C: ; A→D: ; B→D: ; B→G: ; C→G: ; D→G: . All hold, so is consistent.
2. Greedy (order by ). Expand S (A:6, B:4) → B (D:3, G:0) → G (h=0). Order: S, B, G. Path S→B→G, cost (not optimal).
3. UCS (order by ). S(0) → A(1) → C(3) → B(4) → D(5, via B) → G(8, via D). Order: S, A, C, B, D, G (6 nodes). Path S→B→D→G, cost 8.
4. A* ().
| Step | Expanded | Frontier after expansion | ||
|---|---|---|---|---|
| 1 | S | 0 | 7 | A(1+6=7), B(4+4=8) |
| 2 | A | 1 | 7 | B(8), D(6+3=9), C(3+7=10) |
| 3 | B | 4 | 8 | D(5+3=8, replaces 9), C(10), G(13) |
| 4 | D | 5 | 8 | G(8+0=8, replaces 13), C(10) |
| 5 | G | 8 | 8 | goal selected, stop |
Order: S, A, B, D, G (5 nodes). Path S→B→D→G, cost 8 (optimal).
5. Greedy expanded the fewest nodes (3) but returned cost 13 because it ignores the cost already paid (). UCS and A* are optimal, and A* used 5 expansions versus 6 for UCS because pruned node C.
Exercise B2¶
Use the same graph as in B1 but with this different heuristic: .
Is the new heuristic admissible? Is it consistent?
Run A*. What path does it return and what is its cost? Explain what changed compared to B1.
Solution
and the others are unchanged, so it is admissible. It is not consistent: S→B requires , which fails.
: S=7. Expand S: A(1+6=7), B(4+1=5). Expand B(5): D(5+3=8), G(13). Expand A(7): C(10), D(6+3=9, not better than 8). Expand D(8): G(8). Expand G. Path S→B→D→G, cost 8, still optimal because the heuristic is admissible. The difference: B is now expanded before A, and the values along the path are no longer non-decreasing (S: 7, B: 5, D: 8), which is the signature of an inconsistent heuristic.
Part C: Heuristics¶
Exercise C1: Admissible or consistent?¶
Directed graph with start S and goal G:
For each heuristic below, decide whether it is admissible, consistent, both, or neither. Justify with the relevant inequality.
| Node | S | A | B | C | G |
|---|---|---|---|---|---|
| 5 | 4 | 3 | 2 | 0 | |
| 6 | 5 | 1 | 3 | 0 | |
| 6 | 7 | 4 | 3 | 0 | |
| 0 | 0 | 0 | 0 | 0 | |
| 6 | 5 | 4 | 3 | 1 |
Solution
True costs: .
: admissible (). Consistent: S→A , S→B , A→C , B→C , B→G , C→G . Both.
: admissible (). S→B: fails. Admissible, not consistent.
: . Not admissible (hence not consistent either).
: always and . Both (this is UCS).
: . Not admissible. A heuristic must be 0 at the goal.
Exercise C2: Combining heuristics¶
and are admissible. For each of the following, state whether it is always admissible (give a proof) or not necessarily (give a counter-example).
Solution
Admissible. because both are . It is also at least as informed as each of them.
Admissible. .
Admissible. The average of two values is .
Not necessarily. If and , then .
Not necessarily. Same counter-example: .
Exercise C3: 8-Puzzle heuristics¶
Initial state (left) and goal (right); _ is the blank.
Initial: Goal:
2 8 3 1 2 3
1 6 4 8 _ 4
7 _ 5 7 6 5Compute (number of misplaced tiles, blank excluded) and (sum of Manhattan distances) for the initial state.
The blank can move up, left or right. For each of the three successors compute , and . Which one does A* expand next?
Which relaxation of the puzzle rules yields ? Which yields ?
Does dominate ? What does it imply for the number of nodes expanded?
Solution
1. Misplaced: tiles 2, 8, 1, 6, so . Manhattan: tile 2 → 1, tile 8 → 2, tile 1 → 1, tile 6 → 1, so .
2.
| Move blank | Resulting state (rows) | |||
|---|---|---|---|---|
| up | 2 8 3 / 1 _ 4 / 7 6 5 | 1 | 4 (2:1, 8:2, 1:1) | 5 |
| left | 2 8 3 / 1 6 4 / _ 7 5 | 1 | 6 | 7 |
| right | 2 8 3 / 1 6 4 / 7 5 _ | 1 | 6 | 7 |
A* expands the blank-up successor (lowest ).
3. Rule: “a tile can move from A to B if A is adjacent to B and B is blank.”
Drop B is blank (tiles may move to any adjacent square): (Manhattan).
Drop both conditions (tiles jump anywhere): (misplaced tiles).
4. Yes: every tile contributes at least 1 to whenever it contributes 1 to , so and both are admissible. A* with never expands more nodes than with (ignoring ties).
Exercise C4: Grid path planning¶
A robot moves in four directions (up/down/left/right), each move costs 1. # is a wall. Start S = (r0,c0), goal G = (r3,c4).
c0 c1 c2 c3 c4
r0 : S . . # .
r1 : . # . . .
r2 : . # # . #
r3 : . . . . GCompute using the Manhattan distance to G.
Is Manhattan distance admissible and consistent on this grid? Why?
What is the true shortest path length? How many shortest paths exist?
Using = true shortest distance from S, which free cells have ? What does that tell you about A*?
Solution
.
Both. Each move changes the Manhattan distance by exactly 1 and costs 1, so . Walls can only make the real cost larger.
The shortest length is 7. There are 2 shortest paths: down the left column then right along row 3 (S, r1c0, r2c0, r3c0, r3c1, r3c2, r3c3, G), or right along row 0 to c2, down to (r1,c2), right to (r1,c3), down through (r2,c3) and (r3,c3), then right to G.
Only (r0,c4) has : (via (r1,c4) with ), , so . Every other free cell has . A* never needs to expand cells with , so (r0,c4) is never expanded. Because the heuristic is perfect along the shortest paths, the cells expanded all have .
Exercise C5: Effective branching factor¶
A* solves a problem at depth and generates nodes. The effective branching factor is the branching factor of a uniform tree of depth that contains nodes:
Estimate to two decimals. What does a close to 1 say about the heuristic?
Solution
Solve . Trying values: , , . So .
A close to 1 means the heuristic is very accurate (the search is nearly a straight line to the goal). A equal to the real branching factor means the heuristic is useless.
Part D: Local Search¶
Exercise D1: Hill climbing on 4-Queens¶
A state is a list of rows, one queen per column (rows and columns numbered 1 to 4). The loss is the number of attacking pairs. A move relocates one queen inside its column. Use steepest descent: move to the best neighbour and stop if no neighbour has a strictly lower loss.
Initial state: .
Compute of the initial state.
Compute for all 12 neighbours. Which neighbour is chosen?
Continue until the algorithm stops. What is the final state and its loss? Is it a solution?
What simple modifications would let the search finish? Apply one and show the solution.
Solution
1. Attacking pairs of : (col1, col2) diagonal, (col1, col3) same row, (col2, col3) diagonal. .
2. Neighbour losses (new row for the moved queen):
| Moved column | New row | Resulting state | |
|---|---|---|---|
| 1 | 1 | (1,3,2,4) | 2 |
| 1 | 3 | (3,3,2,4) | 2 |
| 1 | 4 | (4,3,2,4) | 4 |
| 2 | 1 | (2,1,2,4) | 3 |
| 2 | 2 | (2,2,2,4) | 4 |
| 2 | 4 | (2,4,2,4) | 2 |
| 3 | 1 | (2,3,1,4) | 1 |
| 3 | 3 | (2,3,3,4) | 3 |
| 3 | 4 | (2,3,4,4) | 4 |
| 4 | 1 | (2,3,2,1) | 5 |
| 4 | 2 | (2,3,2,2) | 5 |
| 4 | 3 | (2,3,2,3) | 5 |
The best neighbour is with .
3. From the only attacking pair is (col1, col2) (diagonal). None of its neighbours has . The two solutions and differ from it in 2 and 4 columns, so none is a single move away. Neighbours with exist, for example , but they are not strictly better, so the algorithm stops at , : a local minimum, not a solution.
4. Options: sideways moves, random restart, simulated annealing. With a sideways move (still ), moving queen 4 to row 3 gives with , which is a solution.
Exercise D2: Hill climbing on a small TSP¶
Distances (symmetric) between five cities:
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | 0 | 2 | 9 | 10 | 7 |
| B | 2 | 0 | 6 | 4 | 8 |
| C | 9 | 6 | 0 | 8 | 5 |
| D | 10 | 4 | 8 | 0 | 3 |
| E | 7 | 8 | 5 | 3 | 0 |
A tour starts and ends at A. Neighbourhood: swap two of the four non-A cities in the tour. Starting from A→C→B→D→E→A:
Compute the tour length.
Evaluate the six neighbours and run steepest-descent hill climbing until it stops.
Is the final tour a local or global optimum? How do you know?
Solution
1. .
2. Neighbours of A C B D E:
| Swap | Tour | Length |
|---|---|---|
| C↔B | A B C D E | 26 |
| C↔D | A D B C E | 32 |
| C↔E | A E B D C | 36 |
| B↔D | A C D B E | 36 |
| B↔E | A C E D B | 23 |
| D↔E | A C B E D | 36 |
Move to A C E D B (23). Its neighbours: A E C D B (26), A D E C B (26), A B E D C (30), A C D E B (30), A C B D E (29), A C E B D (36). All are , so the algorithm stops.
3. The final tour has length 23. There are only distinct tours here, so brute force confirms 23 is also the global optimum (A C E D B is the reverse of A B D E C, with the same length). In general, hill climbing gives only a local optimum.
Exercise D3: Simulated annealing¶
Compute for with , and .
With , decide for each move whether SA accepts it. Draw given in the table.
| Move | Accepted? | |||
|---|---|---|---|---|
| 1 | +3 | 0.40 | ||
| 2 | +10 | 0.20 | ||
| 3 | −2 | 0.95 | ||
| 4 | +5 | 0.35 |
The schedule is exponential, with and . What is after 10 iterations? After how many iterations does first drop below 1?
Explain why SA must remember the best state ever seen.
Solution
1. ; ; .
2.
| Move | Accepted? | |||
|---|---|---|---|---|
| 1 | +3 | 0.40 | Yes () | |
| 2 | +10 | 0.20 | No () | |
| 3 | −2 | 0.95 | 1 | Yes (improvement) |
| 4 | +5 | 0.35 | Yes () |
3. . We need , so , i.e. .
4. Because worse moves are accepted, the current state at the end of the run may be worse than a state visited earlier.
Exercise D4: Tabu search¶
States lie on a line. A move goes to state . The loss is:
| State | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| 6 | 4 | 3 | 5 | 7 | 4 | 1 | 2 |
Start in state 1.
Run hill climbing. Where does it stop?
Run tabu search where the tabu list holds the last visited states (including the current one). At each step move to the best non-tabu neighbour, even if it is worse. Give the sequence of states and tabu list until the global minimum is found.
What happens with ?
Solution
1. 1 (L=4) → 2 (L=3). Neighbours of 2 are 1 (4) and 3 (5), both worse. Stops at state 2, , a local minimum. The global minimum is state 6 ().
2. :
| Iteration | Current | Tabu list | Candidates | Move to |
|---|---|---|---|---|
| 0 | 1 | [1] | 0 (6), 2 (3) | 2 |
| 1 | 2 | [1, 2] | 3 (5) (1 is tabu) | 3 |
| 2 | 3 | [2, 3] | 4 (7) (2 is tabu) | 4 |
| 3 | 4 | [3, 4] | 5 (4) (3 is tabu) | 5 |
| 4 | 5 | [4, 5] | 6 (1) (4 is tabu) | 6 |
Tabu search climbs over the barrier at states 3–4 and reaches the global minimum 6 () at iteration 5.
3. With only the current state is tabu. From 2 the search goes back to 1 (L=4), then to 2, and so on forever. It cycles between 1 and 2 and never escapes. The tabu list must be long enough to prevent short cycles.
Part E: Genetic Algorithms¶
Exercise E1: Roulette-wheel selection¶
Maximise over 5-bit integers. The population is:
| Chromosome | A: 00110 | B: 10100 | C: 01011 | D: 11001 |
|---|
Decode each chromosome and compute its fitness.
Compute the selection probability, cumulative probability and expected count ().
Spins of the wheel give . Which chromosomes enter the mating pool?
Why is rank-based selection preferable if one chromosome had fitness 10000 and the others about 10?
Solution
1–2.
| Cumulative | |||||
|---|---|---|---|---|---|
| A | 6 | 36 | 0.030 | 0.030 | 0.12 |
| B | 20 | 400 | 0.338 | 0.369 | 1.35 |
| C | 11 | 121 | 0.102 | 0.471 | 0.41 |
| D | 25 | 625 | 0.529 | 1.000 | 2.12 |
| Sum | 1182 | 1 | 4 |
3. first cumulative value is 0.471, so C. A. D. 0.369, so B. Mating pool: {C, A, D, B}.
4. The dominant chromosome would occupy almost the whole wheel and the population would converge prematurely. Rank-based selection depends only on the order, not on the magnitude of the fitness values.
Exercise E2: Crossover on bit strings¶
Parents: P1 = 11010011 and P2 = 00101100. Give both children for:
Single-point crossover after bit 3.
Two-point crossover with cuts after bit 2 and after bit 5.
Uniform crossover with mask
10110010(1 = take from , 0 = take from for child 1; child 2 takes the opposite parent).
Solution
110|10011and001|01100→ child 1 =11001100, child 2 =00110011.11|010|011and00|101|100→ child 1 =11101011, child 2 =00010100.Child 1 takes P1 at positions 1, 3, 4, 7 and P2 at 2, 5, 6, 8:
10011110. Child 2 is the complement choice:01100001.
Exercise E3: Permutation operators¶
Show with and that single-point crossover after position 2 gives an invalid child.
Apply order crossover (OX1) with and . Copy the slice at positions 3 to 5 of , then fill the remaining empty positions from left to right with the missing cities in the order they appear in .
Apply to : (a) swap mutation of positions 2 and 5, (b) inversion mutation of positions 2 to 4, (c) insertion mutation moving the gene at position 5 to position 2.
Why is inversion mutation a good choice for TSP?
Solution
Child = : cities 1 and 2 appear twice, and cities 3 and 5 are missing. Invalid.
The slice is
3 4 5, so the child is_ _ 3 4 5 _ _ _. Remaining cities in order (skipping 3, 4, 5):6 8 1 7 2. Child =[6,8,3,4,5,1,7,2].(a) . (b) reverse to get . (c) .
Inversion reverses a segment of the tour (a 2-opt move). It changes only 2 edges of the tour, so it is a gentle perturbation, whereas a swap can change up to 4 edges.
Exercise E4: One generation by hand¶
Maximise the number of ones (OneMax) over 6 bits. Population:
A: 110100 | B: 111011 | C: 000100 | D: 101010 |
|---|
Compute the fitness of each chromosome and the population average.
Selection gave the mating pool {B, A, B, D}. Pair them as (B, A) and (B, D), and apply single-point crossover after bit 3 to each pair. Write all four children.
Mutation (bit flip): flip bit 4 of the last child. Write the new population, with fitness and average.
Did the average improve? Which operator contributed to the improvement of the best fitness?
Solution
1. Fitness: A=3, B=5, C=1, D=3. Average = .
2. (B, A): 111|011 × 110|100 → 111100, 110011. (B, D): 111|011 × 101|010 → 111010, 101011.
3. Flip bit 4 of 101011 → 101111. New population: 111100 (4), 110011 (4), 111010 (4), 101111 (5). Average = .
4. The average rose from 3 to 4.25. Selection removed the weak chromosome C and duplicated B. Crossover recombined good segments but none of its children (fitness 4, 4, 4, 4) matched B (5). Mutation produced the one child with fitness 5 that is different from B (101111), which is how new genetic material enters the population.
Exercise E5: Design questions¶
For each problem, choose the encoding, the crossover and the mutation operator, and write the fitness function.
0/1 knapsack with 12 items and a weight limit.
Tuning 5 real-valued parameters of a controller.
Exam timetable where each of 20 exams gets a time slot (slots may repeat).
Delivery route through 15 cities.
Solution
| Problem | Encoding | Crossover | Mutation | Fitness |
|---|---|---|---|---|
| Knapsack | Binary, length 12 | Single-/two-point or uniform | Bit flip | Total value, with a penalty (or 0) if the weight exceeds the limit |
| Controller | Real vector of length 5 | Arithmetic | Gaussian | Negative tracking error |
| Timetable | Integer vector, gene = slot of exam | Uniform or one-point | Replace a gene by a random slot | (number of conflicts) |
| Route | Permutation of 15 cities | OX1 / PMX | Inversion or swap | (tour length) |