CSC242: Intro to AI Lecture 5 Tuesday, February 26, 13 CSUG Tutoring: bit.ly/csug-tutoring League of Legends LAN Party: Sat 2/2 @ 2PM in CSB 209 $2 to benefit Big Brothers Big Sisters bit.ly/URLoL Tuesday, February 26, 13 ULW Topics due to me by tomorrow First draft due Mar 1 Questions, more feedback, etc.? Get in touch early! Tuesday, February 26, 13 Project 1 Code posted on BB Everyone should have it working Tuesday, February 26, 13 Local Search Tuesday, February 26, 13 Tuesday, February 26, 13 States + Actions + Transition Model = State Space The set of all states reachable from the initial state by some sequence of actions Tuesday, February 26, 13 Arad Sibiu Rimnicu Vilcea Arad Faragas Tuesday, February 26, 13 Timosoara Oradea Arad Lugoj Zerind Arad Oradea 7 2 1 6 8 3 N 7 W 7 1 7 6 2 8 6 3 4 5 3 Tuesday, February 26, 13 W 7 2 1 7 2 4 8 6 8 5 3 4 8 6 3 4 5 3 4 E 1 2 2 5 S 6 S 4 E 7 1 8 6 2 8 5 3 4 5 5 7 3 2 1 6 8 4 5 S W 1 1 7 2 1 7 2 1 6 4 8 6 4 8 3 5 3 5 Solution graphSearch(Problem p) { Set<Node> frontier = new Set<Node>(p.getInitialState()); Set<Node> explored = new Set<Node>(); while (true) { if (frontier.isEmpty()) { return null; } Node node = frontier.selectOne(); if (p.isGoalState(node.getState())) { return node.getSolution(); } explored.add(node); for (Node n : node.expand()) { if (!explored.contains(n)) { frontier.add(n); } } } } Tuesday, February 26, 13 Search Strategies Uninformed No additional information about states Tuesday, February 26, 13 Informed (Heuristic) Can identify “promising” states Search Strategies Compl ete? Optima l? BFS DFS IDS Greedy A* ✓ ✗ ✓ ✗ ✓† ✓ ✗ ✓* ✗ ✓† Time d O(b ) O(b ) O(b ) O(b ) O(b ) Space O(bd ) O(bm) O(bd) O(bm ) O(bd ) d m m d If step costs are identical † With an admissible heuristic * Tuesday, February 26, 13 Systematic Search • Enumerates paths from initial state • Records what alternatives have been explored at each point in the path Good: Systematic → Exhaustive Bad: Exponential time and/or space Tuesday, February 26, 13 Local Search Tuesday, February 26, 13 Tuesday, February 26, 13 Initial State Tuesday, February 26, 13 Goal State Tuesday, February 26, 13 N-Queens as StateSpace Search Problem • State • Actions • Transition Model • Initial State • Goal State(s)/Test • Step costs Tuesday, February 26, 13 Tuesday, February 26, 13 Local Search Tuesday, February 26, 13 Local Search • Evaluates and modifies a small number of current states • Does not record history of search (paths, explored set, etc.) Good: Very little (constant) memory Bad: May not explore all alternatives => Incomplete Tuesday, February 26, 13 State localSearch(Problem p) { Solution graphSearch(Problem p) { Set<Node> frontier = new Set<Node>(p.getInitialState()); Node node explored = new Node(p.getInitialState()); Set<Node> = new Set<Node>(); while (true) { if (frontier.isEmpty()) { return null; } Node node = frontier.selectOne(); if (p.isGoalState(node.getState())) { return return node.getSolution(); node.getState(); } explored.add(node); for (Node n : node.expand()) { if (!explored.contains(n)) { frontier.add(n); } } } } Tuesday, February 26, 13 State localSearch(Problem p) { Solution graphSearch(Problem p) { Set<Node> frontier = new Set<Node>(p.getInitialState()); Node node explored = new Node(p.getInitialState()); Set<Node> = new Set<Node>(); while (true) { if (frontier.isEmpty()) { return null; } Node node = frontier.selectOne(); if (p.isGoalState(node.getState())) { return return node.getSolution(); node.getState(); } explored.add(node); for (Node n : node.expand()) { if (!explored.contains(n)) { ??? frontier.add(n); } } } } Tuesday, February 26, 13 h(n) n Tuesday, February 26, 13 State localSearch(Problem p) { Solution graphSearch(Problem p) { Set<Node> frontier = new Set<Node>(p.getInitialState()); Node node explored = new Node(p.getInitialState()); Set<Node> = new Set<Node>(); while (true) { if (frontier.isEmpty()) { return null; } Node node = frontier.selectOne(); if (p.isGoalState(node.getState())) { return return node.getSolution(); node.getState(); } explored.add(node); for (Node n : node.expand()) { if { if (!explored.contains(n)) (p.value(n) >= p.value(node)) { frontier.add(n); node = n; }} } } } Tuesday, February 26, 13 State localSearch(Problem p) { Node node = new Node(p.getInitialState()); while (true) { if (p.isGoalState(node.getState())) { return node.getState(); } } } for (Node n : node.expand()) { if (p.value(n) >= p.value(node)) { node = n; } } Tuesday, February 26, 13 State localSearch(Problem p) { Node node = new Node(p.getInitialState()); while (true) { if (p.isGoalState(node.getState())) { return node.getState(); } Node next = null; for (Node n : node.expand()) { if (p.value(n) >= p.value(node)) { next = n; } } } if (next == null) { } return node.getState(); } else { node = next; } } } Tuesday, February 26, 13 State localSearch(Problem p) { Node node = new Node(p.getInitialState()); while (true) { Node next = null; for (Node n : node.expand()) { if (p.value(n) >= p.value(node)) { next = n; } } if (next == null) { return node.getState(); } else { node = next; } } } Tuesday, February 26, 13 Hill-climbing Search • Move through state space in the direction of increasing value (“uphill”) State-space landscape Tuesday, February 26, 13 State: [r0 , . . . , r7 ] Action: < i, ri > h(n) = # of pairs of queens attacking each other Tuesday, February 26, 13 h(n) = 17 Tuesday, February 26, 13 18 12 14 13 13 12 14 14 16 13 15 12 14 12 14 12 18 13 15 12 14 15 14 14 13 16 13 14 17 15 14 16 17 16 18 15 15 18 14 15 15 14 14 14 13 17 12 14 12 h(n) = 17 Tuesday, February 26, 13 14 16 14 16 16 16 18 h(n) = 12 Tuesday, February 26, 13 State hillClimb(Problem p) { Node node = new Node(p.getInitialState()); while (true) { Node next = null; for (Node n : node.expand()) { if (p.value(n) >= p.value(node)) { next = n; } } if (next == null) { return node.getState(); } else { node = next; } } } Tuesday, February 26, 13 h(n) = 1 Tuesday, February 26, 13 objective function global maximum shoulder local maximum “flat” local maximum current state Tuesday, February 26, 13 state space Tuesday, February 26, 13 If at first you don’t succeed, try again. Tuesday, February 26, 13 State randomRestart(Problem p) { while (true) { p.setInitialState(new random State); State solution = hillClimb(p); if (p.isGoal(solution)) { return solution; } } } Does it work? Yes (but) How well does it work? Prob of success = p Expected # of tries = 1/p = 0.14 Tuesday, February 26, 13 ⇡7 State randomRestart(Problem p) { while (true) { p.setInitialState(new random State); State solution = hillClimb(p); if (p.isGoal(solution)) { return solution; } } } Randomness Stochastic hill climbing Tuesday, February 26, 13 Randomness in Search Pure random walk Greedy local search Complete, but horribly slow Incomplete, but fast Tuesday, February 26, 13 Tuesday, February 26, 13 Tuesday, February 26, 13 Simulated Annealing • Follow landscape down towards global minimum of state cost function • Occasionally allow an upward move (“shake”) to get out of local minima • Don’t shake so hard that you bounce out of global minimum Tuesday, February 26, 13 Cost State space Tuesday, February 26, 13 Annealing • A heat treatment that alters the microstructure of a material causing changes in properties such as strength and hardness and ductility • High temp => Atoms jumping around • Low temp => Atoms settle into position Tuesday, February 26, 13 State simulatedAnnealing(Problem p, Schedule schedule) { Node node = new Node(p.getInitialState()); for (t=1; true; t++) { Number T = schedule(t); if (T == 0) { return node; } Node next = randomly selected successor of node Number deltaE = p.cost(node) - p.cost(next); if (deltaE > 0 || Math.exp(-deltaE/T) > new Random(1)) { node = next; } } E } T e Tuesday, February 26, 13 1 0.75 y=e 0.5 E/10 0.25 y=e 0 Tuesday, February 26, 13 1 E 2 3 4 5 6 7 8 9 10 Simulated Annealing Complete? No. Optimal? No. “But if the schedule lowers T slowly enough, simulated annealing will find a global minimum with probability approaching one.” Tuesday, February 26, 13 Tuesday, February 26, 13 Local Search • Evaluates and modifies a small number of current states • Does not record history of search Good: Very little (constant) memory Bad: May not explore all alternatives => Incomplete Tuesday, February 26, 13 Local Beam Search • During hill-climbing: • Keep track of k states rather than just one • At each step, generate all successors of all k states (k*b of them) • Keep the most promising k of them Tuesday, February 26, 13 Local Search Tuesday, February 26, 13 Parallel Local Search Tuesday, February 26, 13 Parallel Local Search -8 -5 -7 -2 Tuesday, February 26, 13 4 6 9 1 -3 3 0 -9 Local Beam Search Tuesday, February 26, 13 Local Beam Search Tuesday, February 26, 13 Tuesday, February 26, 13 Local Beam Search • During hill-climbing: • Keep track of k states rather than just one • At each step, generate all successors of all k states (k*b of them) • Keep the most promising k of them Tuesday, February 26, 13 Tuesday, February 26, 13 Asexual Reproduction Wikipedia Tuesday, February 26, 13 Mitosis Two diploid cells DNA replication Mitosis Wikipedia Tuesday, February 26, 13 Tuesday, February 26, 13 Sexual Reproduction S N W F Wikipedia Tuesday, February 26, 13 Meiosis Daughter Nuclei II Daughter Nuclei Interphase Meiosis I Homologous Chromosomes Meiosis II Wikipedia Tuesday, February 26, 13 Genetic Algorithms • Start with k random states • Select pairs of states and have them “mate” to produce “offspring” • Most fit (highest-scoring) individuals reproduce more often Tuesday, February 26, 13 Genetic Algorithms • States encoded as “chromosomes” (linear sequences, a.k.a. strings) • During mating: • Crossover: swap chunks of code • Mutation: randomly change bits of code Tuesday, February 26, 13 24748552 24 31% 32752411 32748552 32748152 32752411 23 29% 24748552 24752411 24752411 24415124 20 26% 32752411 32752124 32252124 32543213 11 14% 24415124 24415411 24415417 (a) Initial Population (b) Fitness Function (c) Selection (d) Crossover (e) Mutation Tuesday, February 26, 13 Genetic Algorithms • States encoded as “chromosomes” (linear sequences, a.k.a. strings) • During mating: • Crossover: swap chunks of code • Mutation: randomly change bits of code Tuesday, February 26, 13 GA Summary • A version of stochastic local beam search with a special way to generate successor states (motivated by a naive biology analogy) • “Much work remains to be done to identify the conditions under which genetic algorithms perform well.” Tuesday, February 26, 13 • Evaluates and modifies a small number of current states Does not record history of search •Hill-climbing Good: Very little (constant) memory Simulated Annealing Bad: May not explore all alternatives 24748552 24 31% 32752411 32748552 32748152 32752411 23 29% 24748552 24752411 24752411 24415124 20 26% 32752411 32752124 32252124 32543213 11 14% 24415124 24415411 24415417 (a) Initial Population (b) Fitness Function (c) Selection (d) Crossover (e) Mutation => Incomplete Local Beam Search Tuesday, February 26, 13 Genetic Algorithms For next time: AIMA 5.0–5.2.2 Quiz? Project 1: Get Going! Tuesday, February 26, 13