Professional Documents
Culture Documents
♦ What is AI?
♦ A brief history
Artificial Intelligence ♦ The state of the art
Chapter 1
Chapter 1 1 Chapter 1 2
HUMAN
INTERROGATOR ?
AI SYSTEM
Chapter 1 5 Chapter 1 6
Aristotle (Nicomachean Ethics): For any given class of environments and tasks, we seek the
Every art and every inquiry, and similarly every agent (or class of agents) with the best performance
action and pursuit, is thought to aim at some good Caveat: computational limitations make
perfect rationality unachievable
→ design best program for given machine resources
Chapter 1 7 Chapter 1 8
AI prehistory Potted history of AI
Philosophy logic, methods of reasoning 1943 McCulloch & Pitts: Boolean circuit model of brain
mind as physical system 1950 Turing’s “Computing Machinery and Intelligence”
foundations of learning, language, rationality 1952–69 Look, Ma, no hands!
Mathematics formal representation and proof 1950s Early AI programs, including Samuel’s checkers program,
algorithms, computation, (un)decidability, (in)tractability Newell & Simon’s Logic Theorist, Gelernter’s Geometry Engine
probability 1956 Dartmouth meeting: “Artificial Intelligence” adopted
Psychology adaptation 1965 Robinson’s complete algorithm for logical reasoning
phenomena of perception and motor control 1966–74 AI discovers computational complexity
experimental techniques (psychophysics, etc.) Neural network research almost disappears
1969–79 Early development of knowledge-based systems
Economics formal theory of rational decisions
1980–88 Expert systems industry booms
Linguistics knowledge representation
1988–93 Expert systems industry busts: “AI Winter”
grammar
1985–95 Neural networks return to popularity
Neuroscience plastic physical substrate for mental activity
1988– Resurgence of probability; general increase in technical depth
Control theory homeostatic systems, stability
“Nouvelle AI”: ALife, GAs, soft computing
simple optimal agent designs
1995– Agents, agents, everywhere . . .
2003– Human-level AI back on the agenda
Chapter 1 9 Chapter 1 10
Chapter 1 11 Chapter 1 12
State of the art State of the art
Which of the following can be done at present? Which of the following can be done at present?
♦ Play a decent game of table tennis ♦ Play a decent game of table tennis
♦ Drive safely along a curving mountain road ♦ Drive safely along a curving mountain road
♦ Drive safely along Telegraph Avenue ♦ Drive safely along Telegraph Avenue
♦ Buy a week’s worth of groceries on the web
Chapter 1 13 Chapter 1 14
Chapter 1 15 Chapter 1 16
State of the art State of the art
Which of the following can be done at present? Which of the following can be done at present?
♦ Play a decent game of table tennis ♦ Play a decent game of table tennis
♦ Drive safely along a curving mountain road ♦ Drive safely along a curving mountain road
♦ Drive safely along Telegraph Avenue ♦ Drive safely along Telegraph Avenue
♦ Buy a week’s worth of groceries on the web ♦ Buy a week’s worth of groceries on the web
♦ Buy a week’s worth of groceries at Berkeley Bowl ♦ Buy a week’s worth of groceries at Berkeley Bowl
♦ Play a decent game of bridge ♦ Play a decent game of bridge
♦ Discover and prove a new mathematical theorem ♦ Discover and prove a new mathematical theorem
♦ Design and execute a research program in molecular biology
Chapter 1 17 Chapter 1 18
Chapter 1 19 Chapter 1 20
State of the art State of the art
Which of the following can be done at present? Which of the following can be done at present?
♦ Play a decent game of table tennis ♦ Play a decent game of table tennis
♦ Drive safely along a curving mountain road ♦ Drive safely along a curving mountain road
♦ Drive safely along Telegraph Avenue ♦ Drive safely along Telegraph Avenue
♦ Buy a week’s worth of groceries on the web ♦ Buy a week’s worth of groceries on the web
♦ Buy a week’s worth of groceries at Berkeley Bowl ♦ Buy a week’s worth of groceries at Berkeley Bowl
♦ Play a decent game of bridge ♦ Play a decent game of bridge
♦ Discover and prove a new mathematical theorem ♦ Discover and prove a new mathematical theorem
♦ Design and execute a research program in molecular biology ♦ Design and execute a research program in molecular biology
♦ Write an intentionally funny story ♦ Write an intentionally funny story
♦ Give competent legal advice in a specialized area of law ♦ Give competent legal advice in a specialized area of law
♦ Translate spoken English into spoken Swedish in real time ♦ Translate spoken English into spoken Swedish in real time
♦ Converse successfully with another person for an hour
Chapter 1 21 Chapter 1 22
Chapter 1 23 Chapter 1 24
State of the art Unintentionally funny stories
Which of the following can be done at present? One day Joe Bear was hungry. He asked his friend Irving Bird where some
honey was. Irving told him there was a beehive in the oak tree. Joe threat-
♦ Play a decent game of table tennis
ened to hit Irving if he didn’t tell him where some honey was. The End.
♦ Drive safely along a curving mountain road
♦ Drive safely along Telegraph Avenue Henry Squirrel was thirsty. He walked over to the river bank where his good
♦ Buy a week’s worth of groceries on the web friend Bill Bird was sitting. Henry slipped and fell in the river. Gravity
♦ Buy a week’s worth of groceries at Berkeley Bowl drowned. The End.
♦ Play a decent game of bridge
♦ Discover and prove a new mathematical theorem Once upon a time there was a dishonest fox and a vain crow. One day the
♦ Design and execute a research program in molecular biology crow was sitting in his tree, holding a piece of cheese in his mouth. He
♦ Write an intentionally funny story noticed that he was holding the piece of cheese. He became hungry, and
♦ Give competent legal advice in a specialized area of law swallowed the cheese. The fox walked over to the crow. The End.
♦ Translate spoken English into spoken Swedish in real time
♦ Converse successfully with another person for an hour
♦ Perform a complex surgical operation
♦ Unload any dishwasher and put everything away
Chapter 1 25 Chapter 1 26
Chapter 2 1
Chapter 1 27
Reminders Outline
Assignment 0 (lisp refresher) due 1/28 ♦ Agents and environments
Lisp/emacs/AIMA tutorial: 11-1 today and Monday, 271 Soda ♦ Rationality
♦ PEAS (Performance measure, Environment, Actuators, Sensors)
♦ Environment types
♦ Agent types
Chapter 2 2 Chapter 2 3
percepts A B
?
environment
agent
actions
actuators
Chapter 2 4 Chapter 2 5
A vacuum-cleaner agent Rationality
Percept sequence Action Fixed performance measure evaluates the environment sequence
[A, Clean] Right – one point per square cleaned up in time T ?
[A, Dirty] Suck – one point per clean square per time step, minus one per move?
[B, Clean] Lef t – penalize for > k dirty squares?
[B, Dirty] Suck
A rational agent chooses whichever action maximizes the expected value of
[A, Clean], [A, Clean] Right
the performance measure given the percept sequence to date
[A, Clean], [A, Dirty] Suck
.. .. Rational 6= omniscient
– percepts may not supply all relevant information
function Reflex-Vacuum-Agent( [location,status]) returns an action Rational 6= clairvoyant
if status = Dirty then return Suck
– action outcomes may not be as expected
else if location = A then return Right Hence, rational 6= successful
else if location = B then return Left
Rational ⇒ exploration, learning, autonomy
PEAS PEAS
To design a rational agent, we must specify the task environment To design a rational agent, we must specify the task environment
Consider, e.g., the task of designing an automated taxi: Consider, e.g., the task of designing an automated taxi:
Performance measure?? Performance measure?? safety, destination, profits, legality, comfort, . . .
Environment?? Environment?? US streets/freeways, traffic, pedestrians, weather, . . .
Actuators?? Actuators?? steering, accelerator, brake, horn, speaker/display, . . .
Sensors?? Sensors?? video, accelerometers, gauges, engine sensors, keyboard, GPS, . . .
Chapter 2 8 Chapter 2 9
Internet shopping agent Internet shopping agent
Performance measure?? Performance measure?? price, quality, appropriateness, efficiency
Environment?? Environment?? current and future WWW sites, vendors, shippers
Actuators?? Actuators?? display to user, follow URL, fill in form
Sensors?? Sensors?? HTML pages (text, graphics, scripts)
Chapter 2 10 Chapter 2 11
Solitaire Backgammon Internet shopping Taxi Solitaire Backgammon Internet shopping Taxi
Observable?? Observable?? Yes Yes No No
Deterministic?? Deterministic??
Episodic?? Episodic??
Static?? Static??
Discrete?? Discrete??
Single-agent?? Single-agent??
Chapter 2 12 Chapter 2 13
Environment types Environment types
Solitaire Backgammon Internet shopping Taxi Solitaire Backgammon Internet shopping Taxi
Observable?? Yes Yes No No Observable?? Yes Yes No No
Deterministic?? Yes No Partly No Deterministic?? Yes No Partly No
Episodic?? Episodic?? No No No No
Static?? Static??
Discrete?? Discrete??
Single-agent?? Single-agent??
Chapter 2 14 Chapter 2 15
Solitaire Backgammon Internet shopping Taxi Solitaire Backgammon Internet shopping Taxi
Observable?? Yes Yes No No Observable?? Yes Yes No No
Deterministic?? Yes No Partly No Deterministic?? Yes No Partly No
Episodic?? No No No No Episodic?? No No No No
Static?? Yes Semi Semi No Static?? Yes Semi Semi No
Discrete?? Discrete?? Yes Yes Yes No
Single-agent?? Single-agent??
Chapter 2 16 Chapter 2 17
Environment types Agent types
Solitaire Backgammon Internet shopping Taxi Four basic types in order of increasing generality:
Observable?? Yes Yes No No – simple reflex agents
Deterministic?? Yes No Partly No – reflex agents with state
Episodic?? No No No No – goal-based agents
Static?? Yes Semi Semi No – utility-based agents
Discrete?? Yes Yes Yes No
All these can be turned into learning agents
Single-agent?? Yes No Yes (except auctions) No
Chapter 2 18 Chapter 2 19
Chapter 2 20 Chapter 2 21
Reflex agents with state Example
Environment
What my actions do (defun make-reflex-vacuum-agent-with-state-program ()
(let ((last-A infinity) (last-B infinity))
#’(lambda (percept)
(let ((location (first percept)) (status (second percept)))
Condition!action rules
What action I (incf last-A) (incf last-B)
should do now (cond
((eq status ’dirty)
Agent Actuators (if (eq location ’A) (setq last-A 0) (setq last-B 0))
’Suck)
((eq location ’A) (if (> last-B 3) ’Right ’NoOp))
((eq location ’B) (if (> last-A 3) ’Left ’NoOp)))))))
Chapter 2 22 Chapter 2 23
Sensors Sensors
State State
How the world evolves What the world How the world evolves What the world
is like now is like now
Environment
Environment
What my actions do What it will be like What my actions do What it will be like
if I do action A if I do action A
Chapter 2 24 Chapter 2 25
Learning agents Summary
Performance standard
Agents interact with environments through actuators and sensors
The agent function describes what the agent does in all circumstances
Critic Sensors
The performance measure evaluates the environment sequence
feedback A perfectly rational agent maximizes expected performance
Environment
changes Agent programs implement (some) agent functions
Learning Performance
element element PEAS descriptions define task environments
knowledge
learning Environments are categorized along several dimensions:
!!goals
observable? deterministic? episodic? static? discrete? single-agent?
Problem Several basic agent architectures exist:
generator
reflex, reflex with state, goal-based, utility-based
Agent Actuators
Chapter 2 26 Chapter 2 27
Reminders
Assignment 0 due 5pm today
Assignment 1 posted, due 2/9
Problem solving and search Section 105 will move to 9-10am starting next week
Chapter 3
Chapter 3 1 Chapter 3 2
Outline Problem-solving agents
♦ Problem-solving agents Restricted form of general agent:
♦ Problem types function Simple-Problem-Solving-Agent( percept) returns an action
static: seq, an action sequence, initially empty
♦ Problem formulation state, some description of the current world state
♦ Example problems goal, a goal, initially null
problem, a problem formulation
♦ Basic search algorithms state ← Update-State(state, percept)
if seq is empty then
goal ← Formulate-Goal(state)
problem ← Formulate-Problem(state, goal)
seq ← Search( problem)
action ← Recommendation(seq, state)
seq ← Remainder(seq, state)
return action
Chapter 3 5 Chapter 3 6
Problem types Example: vacuum world
Deterministic, fully observable =⇒ single-state problem Single-state, start in #5. Solution??
Agent knows exactly which state it will be in; solution is a sequence 1 2
Chapter 3 7 Chapter 3 8
Chapter 3 9 Chapter 3 10
Example: vacuum world Single-state problem formulation
Chapter 3 11 Chapter 3 12
L
⇒ state space must be abstracted for problem solving S S
L L
(Abstract) action = complex combination of real actions S S
S S
e.g., “Arad → Zerind” represents a complex set R
of possible routes, detours, rest stops, etc. L R
Chapter 3 13 Chapter 3 14
Example: vacuum world state space graph Example: vacuum world state space graph
R R
L R L R
L L
S S S S
R R R R
L R L R L R L R
L L L L
S S S S
S S S S
R R
L R L R
L L
S S S S
states??: integer dirt and robot locations (ignore dirt amounts etc.) states??: integer dirt and robot locations (ignore dirt amounts etc.)
actions?? actions??: Lef t, Right, Suck, N oOp
goal test?? goal test??
path cost?? path cost??
Chapter 3 15 Chapter 3 16
Example: vacuum world state space graph Example: vacuum world state space graph
R R
L R L R
L L
S S S S
R R R R
L R L R L R L R
L L L L
S S S S
S S S S
R R
L R L R
L L
S S S S
states??: integer dirt and robot locations (ignore dirt amounts etc.) states??: integer dirt and robot locations (ignore dirt amounts etc.)
actions??: Lef t, Right, Suck, N oOp actions??: Lef t, Right, Suck, N oOp
goal test??: no dirt goal test??: no dirt
path cost?? path cost??: 1 per action (0 for N oOp)
Chapter 3 17 Chapter 3 18
Example: The 8-puzzle Example: The 8-puzzle
7 2 4 5
1 2 3 7 2 4 5
1 2 3
5 6 4 5 6 5 6 4 5 6
8 3 1 7 8 8 3 1 7 8
Chapter 3 19 Chapter 3 20
5 6 4 5 6 5 6 4 5 6
8 3 1 7 8 8 3 1 7 8
states??: integer locations of tiles (ignore intermediate positions) states??: integer locations of tiles (ignore intermediate positions)
actions??: move blank left, right, up, down (ignore unjamming etc.) actions??: move blank left, right, up, down (ignore unjamming etc.)
goal test?? goal test??: = goal state (given)
path cost?? path cost??
Chapter 3 21 Chapter 3 22
Example: The 8-puzzle Example: robotic assembly
P
R R
7 2 4 5
1 2 3
R R
5 6 4 5 6
R
8 3 1 7 8
Chapter 3 23 Chapter 3 24
function Tree-Search( problem, strategy) returns a solution, or failure Arad Fagaras Oradea Rimnicu Vilcea Arad Lugoj Arad Oradea
Chapter 3 25 Chapter 3 26
Tree search example Tree search example
Arad Arad
Arad Fagaras Oradea Rimnicu Vilcea Arad Lugoj Arad Oradea Arad Fagaras Oradea Rimnicu Vilcea Arad Lugoj Arad Oradea
Chapter 3 27 Chapter 3 28
Chapter 3 29 Chapter 3 30
Search strategies Uninformed search strategies
A strategy is defined by picking the order of node expansion Uninformed strategies use only the information available
in the problem definition
Strategies are evaluated along the following dimensions:
completeness—does it always find a solution if one exists? Breadth-first search
time complexity—number of nodes generated/expanded
space complexity—maximum number of nodes in memory Uniform-cost search
optimality—does it always find a least-cost solution? Depth-first search
Time and space complexity are measured in terms of Depth-limited search
b—maximum branching factor of the search tree
d—depth of the least-cost solution Iterative deepening search
m—maximum depth of the state space (may be ∞)
Chapter 3 31 Chapter 3 32
B C B C
D E F G D E F G
Chapter 3 33 Chapter 3 34
Breadth-first search Breadth-first search
Expand shallowest unexpanded node Expand shallowest unexpanded node
Implementation: Implementation:
fringe is a FIFO queue, i.e., new successors go at end fringe is a FIFO queue, i.e., new successors go at end
A A
B C B C
D E F G
D E F G
Chapter 3 35 Chapter 3 36
Chapter 3 37 Chapter 3 38
Properties of breadth-first search Properties of breadth-first search
Complete?? Yes (if b is finite) Complete?? Yes (if b is finite)
Time?? 1 + b + b2 + b3 + . . . + bd + b(bd − 1) = O(bd+1), i.e., exp. in d Time?? 1 + b + b2 + b3 + . . . + bd + b(bd − 1) = O(bd+1), i.e., exp. in d
Space?? Space?? O(bd+1) (keeps every node in memory)
Optimal??
Chapter 3 39 Chapter 3 40
Chapter 3 41 Chapter 3 42
Depth-first search Depth-first search
Expand deepest unexpanded node Expand deepest unexpanded node
Implementation: Implementation:
fringe = LIFO queue, i.e., put successors at front fringe = LIFO queue, i.e., put successors at front
A A
B C B C
D E F G D E F G
H I J K L M N O H I J K L M N O
Chapter 3 43 Chapter 3 44
B C B C
D E F G D E F G
H I J K L M N O H I J K L M N O
Chapter 3 45 Chapter 3 46
Depth-first search Depth-first search
Expand deepest unexpanded node Expand deepest unexpanded node
Implementation: Implementation:
fringe = LIFO queue, i.e., put successors at front fringe = LIFO queue, i.e., put successors at front
A A
B C B C
D E F G D E F G
H I J K L M N O H I J K L M N O
Chapter 3 47 Chapter 3 48
B C B C
D E F G D E F G
H I J K L M N O H I J K L M N O
Chapter 3 49 Chapter 3 50
Depth-first search Depth-first search
Expand deepest unexpanded node Expand deepest unexpanded node
Implementation: Implementation:
fringe = LIFO queue, i.e., put successors at front fringe = LIFO queue, i.e., put successors at front
A A
B C B C
D E F G D E F G
H I J K L M N O H I J K L M N O
Chapter 3 51 Chapter 3 52
B C B C
D E F G D E F G
H I J K L M N O H I J K L M N O
Chapter 3 53 Chapter 3 54
Properties of depth-first search Properties of depth-first search
Complete?? Complete?? No: fails in infinite-depth spaces, spaces with loops
Modify to avoid repeated states along path
⇒ complete in finite spaces
Time??
Chapter 3 55 Chapter 3 56
Chapter 3 57 Chapter 3 58
Properties of depth-first search Depth-limited search
Complete?? No: fails in infinite-depth spaces, spaces with loops = depth-first search with depth limit l,
Modify to avoid repeated states along path i.e., nodes at depth l have no successors
⇒ complete in finite spaces
Recursive implementation:
Time?? O(bm): terrible if m is much larger than d
but if solutions are dense, may be much faster than breadth-first function Depth-Limited-Search( problem, limit) returns soln/fail/cutoff
Recursive-DLS(Make-Node(Initial-State[problem]), problem, limit)
Space?? O(bm), i.e., linear space! function Recursive-DLS(node, problem, limit) returns soln/fail/cutoff
cutoff-occurred? ← false
Optimal?? No if Goal-Test(problem, State[node]) then return node
else if Depth[node] = limit then return cutoff
else for each successor in Expand(node, problem) do
result ← Recursive-DLS(successor, problem, limit)
if result = cutoff then cutoff-occurred? ← true
else if result 6= failure then return result
if cutoff-occurred? then return cutoff else return failure
Chapter 3 59 Chapter 3 60
Chapter 3 61 Chapter 3 62
Iterative deepening search l = 1 Iterative deepening search l = 2
Limit = 1 A A A A Limit = 2 A A A A
B C B C B C B C B C B C B C B C
D E F G D E F G D E F G D E F G
A A A A
B C B C B C B C
D E F G D E F G D E F G D E F G
Chapter 3 63 Chapter 3 64
B C B C B C B C
D E F G D E F G D E F G D E F G
H I J K L M N O H I J K L M N O H I J K L M N O H I J K L M N O
A A A A
B C B C B C B C
D E F G D E F G D E F G D E F G
H I J K L M N O H I J K L M N O H I J K L M N O H I J K L M N O
A A A A
B C B C B C B C
D E F G D E F G D E F G D E F G
H I J K L M N O H I J K L M N O H I J K L M N O H I J K L M N O
Chapter 3 65 Chapter 3 66
Properties of iterative deepening search Properties of iterative deepening search
Complete?? Yes Complete?? Yes
Time?? Time?? (d + 1)b0 + db1 + (d − 1)b2 + . . . + bd = O(bd)
Space??
Chapter 3 67 Chapter 3 68
IDS does better because other nodes at depth d are not expanded
BFS can be modified to apply goal test when a node is generated
Chapter 3 69 Chapter 3 70
Summary of algorithms Repeated states
Criterion Breadth- Uniform- Depth- Depth- Iterative Failure to detect repeated states can turn a linear problem into an exponential
First Cost First Limited Deepening one!
Complete? Yes∗ Yes∗ No Yes, if l ≥ d Yes A A
Chapter 3 71 Chapter 3 72
Chapter 3 73 Chapter 3 74
Best-first search Greedy search example Greedy search example
Idea: use an evaluation function for each node
Arad Arad
– estimate of “desirability” 366
Chapter 4, Sections 1–2 1 Chapter 4, Sections 1–2 4 Chapter 4, Sections 1–2 7 Chapter 4, Sections 1–2 10
Outline Romania with step costs in km Greedy search example Properties of greedy search
♦ Best-first search Straight−line distance Complete??
Oradea to Bucharest Arad
71
Neamt Arad
♦ A∗ search Bucharest
366
0
Zerind
87
75 151 Craiova 160
♦ Heuristics Arad
Iasi Dobreta
Eforie
242 Sibiu Timisoara Zerind
140 161 253 329 374
92 Fagaras 178
Sibiu 99 Fagaras Giurgiu 77
118 Hirsova
Vaslui 151
80
Iasi 226
Rimnicu Vilcea Lugoj
Timisoara 244
142 Mehadia 241
111 211 Neamt 234
Lugoj 97 Pitesti
Oradea 380
70 98 Pitesti 98
146 85 Hirsova
Mehadia 101 Urziceni Rimnicu Vilcea 193
75 138 86 Sibiu 253
Bucharest Timisoara 329
120
Dobreta
90 Urziceni 80
Craiova Eforie Vaslui 199
Giurgiu Zerind 374
Chapter 4, Sections 1–2 2 Chapter 4, Sections 1–2 5 Chapter 4, Sections 1–2 8 Chapter 4, Sections 1–2 11
Review: Tree search Greedy search Greedy search example Properties of greedy search
function Tree-Search( problem, fringe) returns a solution, or failure Evaluation function h(n) (heuristic) Complete?? No–can get stuck in loops, e.g., with Oradea as goal,
Arad
fringe ← Insert(Make-Node(Initial-State[problem]), fringe) = estimate of cost from n to the closest goal Iasi → Neamt → Iasi → Neamt →
loop do Complete in finite space with repeated-state checking
if fringe is empty then return failure E.g., hSLD(n) = straight-line distance from n to Bucharest
Sibiu Timisoara Zerind
node ← Remove-Front(fringe) 329 374
Time??
Greedy search expands the node that appears to be closest to goal
if Goal-Test[problem] applied to State(node) succeeds return node
fringe ← InsertAll(Expand(node, problem), fringe) Arad Fagaras Oradea Rimnicu Vilcea
366 176 380 193
Chapter 4, Sections 1–2 3 Chapter 4, Sections 1–2 6 Chapter 4, Sections 1–2 9 Chapter 4, Sections 1–2 12
E.g., hSLD(n) never overestimates the actual road distance 418=418+0 615=455+160 607=414+193
Chapter 4, Sections 1–2 13 Chapter 4, Sections 1–2 16 Chapter 4, Sections 1–2 19 Chapter 4, Sections 1–2 22
Properties of greedy search A∗ search example A∗ search example Optimality of A∗ (standard proof )
Complete?? No–can get stuck in loops, e.g., Suppose some suboptimal goal G2 has been generated and is in the queue.
Arad Arad
Iasi → Neamt → Iasi → Neamt → 366=0+366
Let n be an unexpanded node on a shortest path to an optimal goal G1.
Complete in finite space with repeated-state checking Start
Sibiu Timisoara Zerind
Time?? O(bm), but a good heuristic can give dramatic improvement 447=118+329 449=75+374
n
Space?? O(bm)—keeps all nodes in memory Arad Fagaras Oradea Rimnicu Vilcea
646=280+366 415=239+176 671=291+380
Chapter 4, Sections 1–2 14 Chapter 4, Sections 1–2 17 Chapter 4, Sections 1–2 20 Chapter 4, Sections 1–2 23
Properties of greedy search A∗ search example A∗ search example Optimality of A∗ (more useful)
Complete?? No–can get stuck in loops, e.g., Lemma: A∗ expands nodes in order of increasing f value∗
Arad Arad
Iasi → Neamt → Iasi → Neamt →
Complete in finite space with repeated-state checking Gradually adds “f -contours” of nodes (cf. breadth-first adds layers)
Sibiu Timisoara Zerind Sibiu Timisoara Zerind Contour i has all nodes with f = fi, where fi < fi+1
Time?? O(bm), but a good heuristic can give dramatic improvement 393=140+253 447=118+329 449=75+374 447=118+329 449=75+374
O
Space?? O(bm)—keeps all nodes in memory Arad Fagaras Oradea Rimnicu Vilcea
N
646=280+366 671=291+380 Z
L P
H
M U
B
420
D
E
C
G
Chapter 4, Sections 1–2 15 Chapter 4, Sections 1–2 18 Chapter 4, Sections 1–2 21 Chapter 4, Sections 1–2 24
Properties of A∗ Properties of A∗ Admissible heuristics Relaxed problems
Complete?? Complete?? Yes, unless there are infinitely many nodes with f ≤ f (G) E.g., for the 8-puzzle: Admissible heuristics can be derived from the exact
solution cost of a relaxed version of the problem
Time?? Exponential in [relative error in h × length of soln.] h1(n) = number of misplaced tiles
h2(n) = total Manhattan distance If the rules of the 8-puzzle are relaxed so that a tile can move anywhere,
Space?? Keeps all nodes in memory (i.e., no. of squares from desired location of each tile) then h1(n) gives the shortest solution
Optimal?? 7 2 4 5
1 2 3
If the rules are relaxed so that a tile can move to any adjacent square,
5 6 4 5 6
then h2(n) gives the shortest solution
Key point: the optimal solution cost of a relaxed problem
8 3 1 7 8
is no greater than the optimal solution cost of the real problem
Start State Goal State
h1(S) =??
h2(S) =??
Chapter 4, Sections 1–2 25 Chapter 4, Sections 1–2 28 Chapter 4, Sections 1–2 31 Chapter 4, Sections 1–2 34
Chapter 4, Sections 1–2 26 Chapter 4, Sections 1–2 29 Chapter 4, Sections 1–2 32 Chapter 4, Sections 1–2 35
Chapter 4, Sections 1–2 27 Chapter 4, Sections 1–2 30 Chapter 4, Sections 1–2 33 Chapter 4, Sections 1–2 36
Outline
♦ Hill-climbing
♦ Simulated annealing
Local search algorithms ♦ Genetic algorithms (briefly)
♦ Local search in continuous spaces (very briefly)
T decreased slowly enough =⇒ always reach best state x∗ Problem: quite often, all k states end up on same local hill
E(x∗ ) E(x) E(x∗ )−E(x)
because e kT /e kT = e kT ≫ 1 for small T Idea: choose k successors randomly, biased towards good ones
Is this necessarily an interesting guarantee?? Observe the close analogy to natural selection!
Devised by Metropolis et al., 1953, for physical process modelling
Widely used in VLSI layout, airline scheduling, etc.
24748552 24 31% 32752411 32748552 32748152 Crossover helps iff substrings are meaningful components
32752411 23 29% 24748552 24752411 24752411
24415124 20 26% 32752411 32752124 32252124
24415411 24415417 + =
32543213 11 14% 24415124
∇f = , , , , ,
∂x1 ∂y1 ∂x2 ∂y2 ∂x3 ∂y3
Chapter 5 1
Outline Constraint satisfaction problems (CSPs)
♦ CSP examples Standard search problem:
state is a “black box”—any old data structure
♦ Backtracking search for CSPs
that supports goal test, eval, successor
♦ Problem structure and problem decomposition
CSP:
♦ Local search for CSPs state is defined by variables Xi with values from domain Di
Chapter 5 2 Chapter 5 3
Northern
Territory Northern
Territory
Western Queensland
Australia Western Queensland
Australia
South
Australia South
Australia
New South Wales
New South Wales
Victoria
Victoria
Tasmania
Tasmania
Variables W A, N T , Q, N SW , V , SA, T
Domains Di = {red, green, blue}
Constraints: adjacent regions must have different colors Solutions are assignments satisfying all constraints, e.g.,
e.g., W A 6= N T (if the language allows this), or {W A = red, N T = green, Q = red, N SW = green, V = red, SA = blue, T = green}
(W A, N T ) ∈ {(red, green), (red, blue), (green, red), (green, blue), . . .}
Chapter 5 4 Chapter 5 5
Constraint graph Varieties of CSPs
Binary CSP: each constraint relates at most two variables Discrete variables
finite domains; size d ⇒ O(dn) complete assignments
Constraint graph: nodes are variables, arcs show constraints ♦ e.g., Boolean CSPs, incl. Boolean satisfiability (NP-complete)
infinite domains (integers, strings, etc.)
NT ♦ e.g., job scheduling, variables are start/end days for each job
Q ♦ need a constraint language, e.g., StartJob1 + 5 ≤ StartJob3
WA ♦ linear constraints solvable, nonlinear undecidable
Chapter 5 8 Chapter 5 9
Real-world CSPs Standard search formulation (incremental)
Assignment problems Let’s start with the straightforward, dumb approach, then fix it
e.g., who teaches what class
States are defined by the values assigned so far
Timetabling problems
e.g., which class is offered when and where? ♦ Initial state: the empty assignment, { }
Chapter 5 10 Chapter 5 11
Chapter 5 12 Chapter 5 13
Backtracking example Backtracking example
Chapter 5 14 Chapter 5 15
Chapter 5 16 Chapter 5 17
Improving backtracking efficiency Minimum remaining values
General-purpose methods can give huge gains in speed: Minimum remaining values (MRV):
choose the variable with the fewest legal values
1. Which variable should be assigned next?
2. In what order should its values be tried?
3. Can we detect inevitable failure early?
4. Can we take advantage of problem structure?
Chapter 5 18 Chapter 5 19
Chapter 5 20 Chapter 5 21
Forward checking Forward checking
Idea: Keep track of remaining legal values for unassigned variables Idea: Keep track of remaining legal values for unassigned variables
Terminate search when any variable has no legal values Terminate search when any variable has no legal values
WA NT Q NSW V SA T WA NT Q NSW V SA T
Chapter 5 22 Chapter 5 23
WA NT Q NSW V SA T WA NT Q NSW V SA T
Chapter 5 24 Chapter 5 25
Constraint propagation Arc consistency
Forward checking propagates information from assigned to unassigned vari- Simplest form of propagation makes each arc consistent
ables, but doesn’t provide early detection for all failures:
X → Y is consistent iff
for every value x of X there is some allowed y
WA NT Q NSW V SA T
WA NT Q NSW V SA T
Chapter 5 26 Chapter 5 27
WA NT Q NSW V SA T WA NT Q NSW V SA T
Chapter 5 28 Chapter 5 29
Arc consistency Arc consistency algorithm
Simplest form of propagation makes each arc consistent
function AC-3( csp) returns the CSP, possibly with reduced domains
X → Y is consistent iff inputs: csp, a binary CSP with variables {X1, X2, . . . , Xn}
local variables: queue, a queue of arcs, initially all the arcs in csp
for every value x of X there is some allowed y
while queue is not empty do
(Xi, Xj ) ← Remove-First(queue)
if Remove-Inconsistent-Values(Xi , Xj ) then
for each Xk in Neighbors[Xi] do
WA NT Q NSW V SA T add (Xk , Xi) to queue
Chapter 5 30 Chapter 5 31
V
Victoria
Chapter 5 32 Chapter 5 33
Tree-structured CSPs Algorithm for tree-structured CSPs
1. Choose a variable as root, order variables from root to leaves
A E such that every node’s parent precedes it in the ordering
B D A E
B D A B C D E F
C F
C F
Theorem: if the constraint graph has no loops, the CSP can be solved in
2. For j from n down to 2, apply RemoveInconsistent(P arent(Xj ), Xj )
O(n d2) time
3. For j from 1 to n, assign Xj consistently with P arent(Xj )
Compare to general CSPs, where worst-case time is O(dn)
This property also applies to logical and probabilistic reasoning:
an important example of the relation between syntactic restrictions
and the complexity of reasoning.
Chapter 5 34 Chapter 5 35
Chapter 5 36 Chapter 5 37
Example: 4-Queens Performance of min-conflicts
States: 4 queens in 4 columns (44 = 256 states) Given random initial state, can solve n-queens in almost constant time for
arbitrary n with high probability (e.g., n = 10,000,000)
Operators: move queen in column
The same appears to be true for any randomly-generated CSP
Goal test: no attacks except in a narrow range of the ratio
Evaluation: h(n) = number of attacks number of constraints
R=
number of variables
CPU
time
R
critical
ratio
Chapter 5 38 Chapter 5 39
Summary
CSPs are a special kind of problem:
states defined by values of a fixed set of variables
goal test defined by constraints on variable values
Constraint Satisfaction Problems
Backtracking = depth-first search with one variable assigned per node
Variable ordering and value selection heuristics help significantly
Forward checking prevents assignments that guarantee later failure
Chapter 5
Constraint propagation (e.g., arc consistency) does additional work
to constrain values and detect inconsistencies
The CSP representation allows analysis of problem structure
Tree-structured CSPs can be solved in linear time
Iterative min-conflicts is usually effective in practice
Chapter 5 40 Chapter 5 1
Outline Constraint satisfaction problems (CSPs)
♦ CSP examples Standard search problem:
state is a “black box”—any old data structure
♦ Backtracking search for CSPs
that supports goal test, eval, successor
♦ Problem structure and problem decomposition
CSP:
♦ Local search for CSPs state is defined by variables Xi with values from domain Di
Chapter 5 2 Chapter 5 3
Northern
Territory Northern
Territory
Western Queensland
Australia Western Queensland
Australia
South
Australia South
Australia
New South Wales
New South Wales
Victoria
Victoria
Tasmania
Tasmania
Variables W A, N T , Q, N SW , V , SA, T
Domains Di = {red, green, blue}
Constraints: adjacent regions must have different colors Solutions are assignments satisfying all constraints, e.g.,
e.g., W A 6= N T (if the language allows this), or {W A = red, N T = green, Q = red, N SW = green, V = red, SA = blue, T = green}
(W A, N T ) ∈ {(red, green), (red, blue), (green, red), (green, blue), . . .}
Chapter 5 4 Chapter 5 5
Constraint graph Varieties of CSPs
Binary CSP: each constraint relates at most two variables Discrete variables
finite domains; size d ⇒ O(dn) complete assignments
Constraint graph: nodes are variables, arcs show constraints ♦ e.g., Boolean CSPs, incl. Boolean satisfiability (NP-complete)
infinite domains (integers, strings, etc.)
NT ♦ e.g., job scheduling, variables are start/end days for each job
Q ♦ need a constraint language, e.g., StartJob1 + 5 ≤ StartJob3
WA ♦ linear constraints solvable, nonlinear undecidable
Chapter 5 8 Chapter 5 9
Real-world CSPs Standard search formulation (incremental)
Assignment problems Let’s start with the straightforward, dumb approach, then fix it
e.g., who teaches what class
States are defined by the values assigned so far
Timetabling problems
e.g., which class is offered when and where? ♦ Initial state: the empty assignment, { }
Chapter 5 10 Chapter 5 11
Chapter 5 12 Chapter 5 13
Backtracking example Backtracking example
Chapter 5 14 Chapter 5 15
Chapter 5 16 Chapter 5 17
Improving backtracking efficiency Minimum remaining values
General-purpose methods can give huge gains in speed: Minimum remaining values (MRV):
choose the variable with the fewest legal values
1. Which variable should be assigned next?
2. In what order should its values be tried?
3. Can we detect inevitable failure early?
4. Can we take advantage of problem structure?
Chapter 5 18 Chapter 5 19
Chapter 5 20 Chapter 5 21
Forward checking Forward checking
Idea: Keep track of remaining legal values for unassigned variables Idea: Keep track of remaining legal values for unassigned variables
Terminate search when any variable has no legal values Terminate search when any variable has no legal values
WA NT Q NSW V SA T WA NT Q NSW V SA T
Chapter 5 22 Chapter 5 23
WA NT Q NSW V SA T WA NT Q NSW V SA T
Chapter 5 24 Chapter 5 25
Constraint propagation Arc consistency
Forward checking propagates information from assigned to unassigned vari- Simplest form of propagation makes each arc consistent
ables, but doesn’t provide early detection for all failures:
X → Y is consistent iff
for every value x of X there is some allowed y
WA NT Q NSW V SA T
WA NT Q NSW V SA T
Chapter 5 26 Chapter 5 27
WA NT Q NSW V SA T WA NT Q NSW V SA T
Chapter 5 28 Chapter 5 29
Arc consistency Arc consistency algorithm
Simplest form of propagation makes each arc consistent
function AC-3( csp) returns the CSP, possibly with reduced domains
X → Y is consistent iff inputs: csp, a binary CSP with variables {X1, X2, . . . , Xn}
local variables: queue, a queue of arcs, initially all the arcs in csp
for every value x of X there is some allowed y
while queue is not empty do
(Xi, Xj ) ← Remove-First(queue)
if Remove-Inconsistent-Values(Xi , Xj ) then
for each Xk in Neighbors[Xi] do
WA NT Q NSW V SA T add (Xk , Xi) to queue
Chapter 5 30 Chapter 5 31
V
Victoria
Chapter 5 32 Chapter 5 33
Tree-structured CSPs Algorithm for tree-structured CSPs
1. Choose a variable as root, order variables from root to leaves
A E such that every node’s parent precedes it in the ordering
B D A E
B D A B C D E F
C F
C F
Theorem: if the constraint graph has no loops, the CSP can be solved in
2. For j from n down to 2, apply RemoveInconsistent(P arent(Xj ), Xj )
O(n d2) time
3. For j from 1 to n, assign Xj consistently with P arent(Xj )
Compare to general CSPs, where worst-case time is O(dn)
This property also applies to logical and probabilistic reasoning:
an important example of the relation between syntactic restrictions
and the complexity of reasoning.
Chapter 5 34 Chapter 5 35
Chapter 5 36 Chapter 5 37
Example: 4-Queens Performance of min-conflicts
States: 4 queens in 4 columns (44 = 256 states) Given random initial state, can solve n-queens in almost constant time for
arbitrary n with high probability (e.g., n = 10,000,000)
Operators: move queen in column
The same appears to be true for any randomly-generated CSP
Goal test: no attacks except in a narrow range of the ratio
Evaluation: h(n) = number of attacks number of constraints
R=
number of variables
CPU
time
R
critical
ratio
Chapter 5 38 Chapter 5 39
Summary
CSPs are a special kind of problem:
states defined by values of a fixed set of variables
goal test defined by constraints on variable values Game playing
Backtracking = depth-first search with one variable assigned per node
Variable ordering and value selection heuristics help significantly
Forward checking prevents assignments that guarantee later failure Chapter 6
Chapter 6 1
Chapter 5 40
Outline Games vs. search problems
♦ Games “Unpredictable” opponent ⇒ solution is a strategy
specifying a move for every possible opponent reply
♦ Perfect play
– minimax decisions Time limits ⇒ unlikely to find goal, must approximate
– α–β pruning
Plan of attack:
♦ Resource limits and approximate evaluation
• Computer considers possible lines of play (Babbage, 1846)
♦ Games of chance
• Algorithm for perfect play (Zermelo, 1912; Von Neumann, 1944)
♦ Games of imperfect information • Finite horizon, approximate evaluation (Zuse, 1945; Wiener, 1948;
Shannon, 1950)
• First chess program (Turing, 1951)
• Machine learning to improve evaluation accuracy (Samuel, 1952–57)
• Pruning to allow deeper search (McCarthy, 1956)
Chapter 6 2 Chapter 6 3
X O X X O X O ...
MIN (O) X X
X O X X O X X O X ...
TERMINAL O X O O X X
O X X O X O O
Utility !1 0 +1
Chapter 6 4 Chapter 6 5
Minimax Minimax algorithm
Perfect play for deterministic, perfect-information games
function Minimax-Decision(state) returns an action
Idea: choose move to position with highest minimax value inputs: state, current state in game
= best achievable payoff against best play return the a in Actions(state) maximizing Min-Value(Result(a, state))
Chapter 6 6 Chapter 6 7
Chapter 6 8 Chapter 6 9
Properties of minimax Properties of minimax
Complete?? Yes, if tree is finite (chess has specific rules for this) Complete?? Yes, if tree is finite (chess has specific rules for this)
Optimal?? Yes, against an optimal opponent. Otherwise?? Optimal?? Yes, against an optimal opponent. Otherwise??
Time complexity?? Time complexity?? O(bm)
Space complexity??
Chapter 6 10 Chapter 6 11
Chapter 6 12 Chapter 6 13
α–β pruning example α–β pruning example
MAX 3 MAX 3
MIN 3 2 MIN 3 2 14
X X X X
3 12 8 2 3 12 8 2 14
Chapter 6 14 Chapter 6 15
MAX 3 MAX 3 3
MIN 3 2 14 5 MIN 3 2 14 5 2
X X X X
3 12 8 2 14 5 3 12 8 2 14 5 2
Chapter 6 16 Chapter 6 17
Why is it called α–β ? The α–β algorithm
function Alpha-Beta-Decision(state) returns an action
return the a in Actions(state) maximizing Min-Value(Result(a, state))
MAX
If V is worse than α, max will avoid it ⇒ prune that branch function Min-Value(state, α, β) returns a utility value
same as Max-Value but with roles of α, β reversed
Define β similarly for min
Chapter 6 18 Chapter 6 19
Good move ordering improves effectiveness of pruning • Use Cutoff-Test instead of Terminal-Test
With “perfect ordering,” time complexity = O(b m/2
) e.g., depth limit (perhaps add quiescence search)
⇒ doubles solvable depth • Use Eval instead of Utility
i.e., evaluation function that estimates desirability of position
A simple example of the value of reasoning about which computations are
relevant (a form of metareasoning) Suppose we have 100 seconds, explore 104 nodes/second
Unfortunately, 3550 is still impossible! ⇒ 106 nodes per move ≈ 358/2
⇒ α–β reaches depth 8 ⇒ pretty good chess program
Chapter 6 20 Chapter 6 21
Evaluation functions Digression: Exact values don’t matter
MAX
MIN 1 2 1 20
1 2 2 4 1 20 20 400
Chapter 6 24 Chapter 6 25
Nondeterministic games in general Algorithm for nondeterministic games
In nondeterministic games, chance introduced by dice, card-shuffling Expectiminimax gives perfect play
Simplified example with coin-flipping: Just like Minimax, except we must also handle chance nodes:
MAX ...
if state is a Max node then
return the highest ExpectiMinimax-Value of Successors(state)
if state is a Min node then
CHANCE 3 "1 return the lowest ExpectiMinimax-Value of Successors(state)
if state is a chance node then
0.5 0.5 0.5 0.5 return average of ExpectiMinimax-Value of Successors(state)
...
MIN 2 4 0 "2
2 4 7 4 6 0 5 "2
Chapter 6 26 Chapter 6 27
Chapter 6 28 Chapter 6 29
Games of imperfect information Example
E.g., card games, where opponent’s initial cards are unknown Four-card bridge/whist/hearts hand, Max to play first
8 6
Typically we can calculate a probability for each possible deal 6 6 8 7 6 6 7 6 6 7 6 6 7 6 7
4 2 9 3 4 2 9 3 4 2 3 4 3 4 3
0
Seems just like having one big dice roll at the beginning of the game∗ 9 2
Chapter 6 30 Chapter 6 31
Example Example
Four-card bridge/whist/hearts hand, Max to play first Four-card bridge/whist/hearts hand, Max to play first
MAX 6 6 8 7 8 6 6 7 6 6 7 6 6 7 6 6 7 MAX 6 6 8 7 8 6 6 7 6 6 7 6 6 7 6 6 7
MIN 4 2 9 3 4 2 9 3 4 2 3 4 3 4 3
0 MIN 4 2 9 3 4 2 9 3 4 2 3 4 3 4 3
0
9 2 9 2
8 6 MAX 6 6 8 7 8 6 6 7 6 6 7 6 6 7 6 6 7
MAX 6 6 8 7 6 6 7 6 6 7 6 6 7 6 7
0 MIN 4 2 9 3 4 2 9 3 4 2 3 4 3 4 3
0
MIN 4 2 9 3 4 2 9 3 4 2 3 4 3 4 3 9 2
9 2
6 6 7
4 3
"0.5
MAX 6 6 8 7 8 6 6 7 6 6 7 6 6 7
MIN 4 2 9 3 4 2 9 3 9 4 2 3 2 4 3
6 7
6 4 3
"0.5
Chapter 6 32 Chapter 6 33
Commonsense example Commonsense example
Road A leads to a small heap of gold pieces Road A leads to a small heap of gold pieces
Road B leads to a fork: Road B leads to a fork:
take the left fork and you’ll find a mound of jewels; take the left fork and you’ll find a mound of jewels;
take the right fork and you’ll be run over by a bus. take the right fork and you’ll be run over by a bus.
Road A leads to a small heap of gold pieces
Road B leads to a fork:
take the left fork and you’ll be run over by a bus;
take the right fork and you’ll find a mound of jewels.
Chapter 6 34 Chapter 6 35
Chapter 6 36 Chapter 6 37
Summary
Games are fun to work on! (and dangerous)
They illustrate several important points about AI
♦ perfection is unattainable ⇒ must approximate Logical agents
♦ good idea to think about what to think about
♦ uncertainty constrains the assignment of values to states
Chapter 7
♦ optimal decisions depend on information state, not real state
Games are to AI as grand prix racing is to automobile design
Chapter 6 38 Chapter 7 1
Chapter 7 2 Chapter 7 3
A simple knowledge-based agent Wumpus World PEAS description
Performance measure
function KB-Agent( percept) returns an action
static: KB, a knowledge base gold +1000, death -1000
t, a counter, initially 0, indicating time -1 per step, -10 for using the arrow
Breeze
Gold
PIT
Represent states, actions, etc. Releasing drops the gold in same square 1 2 3 4
Chapter 7 4 Chapter 7 5
Chapter 7 6 Chapter 7 7
Wumpus world characterization Wumpus world characterization
Observable?? No—only local perception Observable?? No—only local perception
Deterministic?? Yes—outcomes exactly specified Deterministic?? Yes—outcomes exactly specified
Episodic?? Episodic?? No—sequential at the level of actions
Static??
Chapter 7 8 Chapter 7 9
Chapter 7 10 Chapter 7 11
Wumpus world characterization Exploring a wumpus world
Observable?? No—only local perception
Deterministic?? Yes—outcomes exactly specified
Episodic?? No—sequential at the level of actions
Static?? Yes—Wumpus and Pits do not move
Discrete?? Yes
Single-agent?? Yes—Wumpus is essentially a natural feature OK
OK OK
A
Chapter 7 12 Chapter 7 13
P?
B OK B OK P?
A A
OK OK OK OK
A A
Chapter 7 14 Chapter 7 15
Exploring a wumpus world Exploring a wumpus world
P? P?
P
B OK P? B OK P?
OK
A A
OK S OK OK S OK
A A A A
W
Chapter 7 16 Chapter 7 17
P? P? OK
P P
B OK P? B OK P? OK
OK OK
A A A A
OK S OK OK S OK
A A
W A A
W
Chapter 7 18 Chapter 7 19
Exploring a wumpus world Other tight spots
P?
P A
OK B
A
OK
P?
Assuming pits uniformly distributed,
(2,2) has pit w/ prob 0.86, vs. 0.31
B OK P? BGS OK
OK
A A A Smell in (1,1)
⇒ cannot move
OK S OK Can use a strategy of coercion:
shoot straight ahead
A A
W S
A
wumpus was there ⇒ dead ⇒ safe
wumpus wasn’t there ⇒ safe
Chapter 7 20 Chapter 7 21
E.g., the language of arithmetic E.g., the KB containing “the Giants won” and “the Reds won”
entails “Either the Giants won or the Reds won”
x + 2 ≥ y is a sentence; x2 + y > is not a sentence
E.g., x + y = 4 entails 4 = x + y
x + 2 ≥ y is true iff the number x + 2 is no less than the number y
Entailment is a relationship between sentences (i.e., syntax)
x + 2 ≥ y is true in a world where x = 7, y = 1 that is based on semantics
x + 2 ≥ y is false in a world where x = 0, y = 6
Note: brains process syntax (of some sort)
Chapter 7 22 Chapter 7 23
Models Entailment in the wumpus world
Logicians typically think in terms of models, which are formally
structured worlds with respect to which truth can be evaluated
We say m is a model of a sentence α if α is true in m
M (α) is the set of all models of α Situation after detecting nothing in [1,1],
moving right, breeze in [2,1]
Then KB |= α if and only if M (KB) ⊆ M (α)
x x x
x ? ?
E.g. KB = Giants won and Reds won x
x
x x
x
B
α = Giants won M( )
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
Consider possible models for ?s
assuming only pits
A A
?
x x x x
xx x xx
x x x
x
x x 3 Boolean choices ⇒ 8 possible models
M(KB) x
x x x
x x
x
Chapter 7 24 Chapter 7 25
2 PIT 2 PIT
2 2
Breeze Breeze
1 1
Breeze Breeze
1 PIT 1 PIT
1 2 3 1 2 3
1 2 3 1 2 3
KB
2 PIT 2 PIT
2 PIT 2 PIT
2 2
Breeze Breeze
Breeze
1 PIT Breeze
1 PIT
1 1
Breeze Breeze
1 1
1 2 3 1 2 3
1 2 3 1 2 3
1 2 3 1 2 3
Breeze Breeze
1 1
Breeze Breeze
1 PIT 1 PIT
2 PIT PIT 2 PIT PIT
1 2 3 1 2 3
1 2 3 1 2 3
Breeze Breeze
1 PIT 1 PIT
1 2 3 1 2 3
Chapter 7 26 Chapter 7 27
Wumpus models Wumpus models
2 PIT 2 PIT
2 2
Breeze Breeze
1 1
Breeze Breeze
1 PIT 1 PIT
1 2 3 1 2 3
1 2 3 1 2 3
KB KB
1
2 PIT 2 PIT
2 PIT 2 PIT
2 2
Breeze Breeze
Breeze
1 PIT Breeze
1 PIT
1 1
Breeze Breeze
1 1
1 2 3 1 2 3
1 2 3 1 2 3
1 2 3 1 2 3
Breeze Breeze
1 1
Breeze Breeze
1 PIT 1 PIT
2 PIT PIT 2 PIT PIT
1 2 3 1 2 3
1 2 3 1 2 3
Breeze Breeze
1 PIT 1 PIT
1 2 3 1 2 3
Breeze
Consequences of KB are a haystack; α is a needle.
1
1
Breeze
PIT
1 2 3
2 Entailment = needle in haystack; inference = finding it
1 2 3
KB
Soundness: i is sound if
2 PIT
2
2 PIT
whenever KB ⊢i α, it is also true that KB |= α
Breeze
Breeze
1 PIT
1
Completeness: i is complete if
Breeze
1
1 2 3
1 2 3
1 2 3
Breeze
1
Breeze Preview: we will define a logic (first-order logic) which is expressive enough
1 PIT
to say almost anything of interest, and for which there exists a sound and
2 PIT PIT
1 2 3
1 2 3
Breeze
1
1 2
PIT
3
complete inference procedure.
That is, the procedure will answer any question whose answer follows from
KB = wumpus-world rules + observations what is known by the KB.
α2 = “[2,2] is safe”, KB 6|= α2
Chapter 7 30 Chapter 7 31
Propositional logic: Syntax Propositional logic: Semantics
Propositional logic is the simplest logic—illustrates basic ideas Each model specifies true/false for each proposition symbol
The proposition symbols P1, P2 etc are sentences E.g. P1,2 P2,2 P3,1
true true f alse
If S is a sentence, ¬S is a sentence (negation)
(With these symbols, 8 possible models, can be enumerated automatically.)
If S1 and S2 are sentences, S1 ∧ S2 is a sentence (conjunction)
Rules for evaluating truth with respect to a model m:
If S1 and S2 are sentences, S1 ∨ S2 is a sentence (disjunction)
¬S is true iff S is false
If S1 and S2 are sentences, S1 ⇒ S2 is a sentence (implication) S1 ∧ S 2 is true iff S1 is true and S2 is true
If S1 and S2 are sentences, S1 ⇔ S2 is a sentence (biconditional) S1 ∨ S 2 is true iff S1 is true or S2 is true
S1 ⇒ S 2 is true iff S1 is false or S2 is true
i.e., is false iff S1 is true and S2 is false
S1 ⇔ S 2 is true iff S1 ⇒ S2 is true and S2 ⇒ S1 is true
Simple recursive process evaluates an arbitrary sentence, e.g.,
¬P1,2 ∧ (P2,2 ∨ P3,1) = true ∧ (f alse ∨ true) = true ∧ true = true
Chapter 7 32 Chapter 7 33
Chapter 7 34 Chapter 7 35
Wumpus world sentences Truth tables for inference
Let Pi,j be true if there is a pit in [i, j]. B1,1 B2,1 P1,1 P1,2 P2,1 P2,2 P3,1 R1 R2 R3 R4 R5 KB
Let Bi,j be true if there is a breeze in [i, j]. false false false false false false false true true true true false false
false false false false false false true true true false true false false
¬P1,1 ... ... ... ... ... ... ... ... ... ... ... ... ...
¬B1,1 false true false false false false false true true false true true false
false true false false false false true true true true true true true
B2,1
false true false false false true false true true true true true true
“Pits cause breezes in adjacent squares” false true false false false true true true true true true true true
false true false false true false false true false false true true false
... ... ... ... ... ... ... ... ... ... ... ... ...
B1,1 ⇔ (P1,2 ∨ P2,1)
true true true true true true true false true true false true false
B2,1 ⇔ (P1,1 ∨ P2,2 ∨ P3,1)
“A square is breezy if and only if there is an adjacent pit” Enumerate rows (different assignments to symbols),
if KB is true in row, check that α is too
Chapter 7 36 Chapter 7 37
Chapter 7 40 Chapter 7 41
Chapter 7 42 Chapter 7 43
Forward chaining algorithm Forward chaining example
Chapter 7 44 Chapter 7 45
Q Q
1 1
P P
2 2
M M
2 1
L L
1 1 1 0
A B A B
Chapter 7 46 Chapter 7 47
Forward chaining example Forward chaining example
Q Q
1 1
P P
1 0
M M
0 0
L L
1 0 1 0
A B A B
Chapter 7 48 Chapter 7 49
Q Q
0 0
P P
0 0
M M
0 0
L L
0 0 0 0
A B A B
Chapter 7 50 Chapter 7 51
Forward chaining example Proof of completeness
Chapter 7 52 Chapter 7 53
A B
Chapter 7 54 Chapter 7 55
Backward chaining example Backward chaining example
Q Q
P P
M M
L L
A B A B
Chapter 7 56 Chapter 7 57
Q Q
P P
M M
L L
A B A B
Chapter 7 58 Chapter 7 59
Backward chaining example Backward chaining example
Q Q
P P
M M
L L
A B A B
Chapter 7 60 Chapter 7 61
Q Q
P P
M M
L L
A B A B
Chapter 7 62 Chapter 7 63
Backward chaining example Backward chaining example
Q Q
P P
M M
L L
A B A B
Chapter 7 64 Chapter 7 65
P1,3 A A
OK
OK S OK
Chapter 7 66 Chapter 7 67
Conversion to CNF Resolution algorithm
B1,1 ⇔ (P1,2 ∨ P2,1) Proof by contradiction, i.e., show KB ∧ ¬α unsatisfiable
1. Eliminate ⇔, replacing α ⇔ β with (α ⇒ β) ∧ (β ⇒ α). function PL-Resolution(KB, α) returns true or false
inputs: KB, the knowledge base, a sentence in propositional logic
(B1,1 ⇒ (P1,2 ∨ P2,1)) ∧ ((P1,2 ∨ P2,1) ⇒ B1,1) α, the query, a sentence in propositional logic
2. Eliminate ⇒, replacing α ⇒ β with ¬α ∨ β. clauses ← the set of clauses in the CNF representation of KB ∧ ¬α
new ← { }
(¬B1,1 ∨ P1,2 ∨ P2,1) ∧ (¬(P1,2 ∨ P2,1) ∨ B1,1) loop do
for each Ci, Cj in clauses do
3. Move ¬ inwards using de Morgan’s rules and double-negation: resolvents ← PL-Resolve(Ci, Cj )
if resolvents contains the empty clause then return true
(¬B1,1 ∨ P1,2 ∨ P2,1) ∧ ((¬P1,2 ∧ ¬P2,1) ∨ B1,1) new ← new ∪ resolvents
if new ⊆ clauses then return false
4. Apply distributivity law (∨ over ∧) and flatten:
clauses ← clauses ∪ new
(¬B1,1 ∨ P1,2 ∨ P2,1) ∧ (¬P1,2 ∨ B1,1) ∧ (¬P2,1 ∨ B1,1)
Chapter 7 68 Chapter 7 69
Chapter 7 70 Chapter 7 71
Outline
♦ Why FOL?
♦ Syntax and semantics of FOL
First-order logic ♦ Fun with sentences
♦ Wumpus world in FOL
Chapter 8
Chapter 8 1 Chapter 8 2
Chapter 8 3 Chapter 8 4
Logics in general Syntax of FOL: Basic elements
Chapter 8 5 Chapter 8 6
Chapter 8 7 Chapter 8 8
Truth in first-order logic Models for FOL: Example
Sentences are true with respect to a model and an interpretation
crown
Model contains ≥ 1 objects (domain elements) and relations among them
Interpretation specifies referents for on head
constant symbols → objects person brother
predicate symbols → relations person
function symbols → functional relations brother king
Chapter 8 9 Chapter 8 10
Chapter 8 11 Chapter 8 12
Universal quantification A common mistake to avoid
∀ hvariablesi hsentencei Typically, ⇒ is the main connective with ∀
Everyone at Berkeley is smart: Common mistake: using ∧ as the main connective with ∀:
∀ x At(x, Berkeley) ⇒ Smart(x)
∀ x At(x, Berkeley) ∧ Smart(x)
∀ x P is true in a model m iff P is true with x being
means “Everyone is at Berkeley and everyone is smart”
each possible object in the model
Roughly speaking, equivalent to the conjunction of instantiations of P
(At(KingJohn, Berkeley) ⇒ Smart(KingJohn))
∧ (At(Richard, Berkeley) ⇒ Smart(Richard))
∧ (At(Berkeley, Berkeley) ⇒ Smart(Berkeley))
∧ ...
Chapter 8 13 Chapter 8 14
Someone at Stanford is smart: Common mistake: using ⇒ as the main connective with ∃:
∃ x At(x, Stanf ord) ∧ Smart(x)
∃ x At(x, Stanf ord) ⇒ Smart(x)
∃ x P is true in a model m iff P is true with x being
is true if there is anyone who is not at Stanford!
some possible object in the model
Roughly speaking, equivalent to the disjunction of instantiations of P
(At(KingJohn, Stanf ord) ∧ Smart(KingJohn))
∨ (At(Richard, Stanf ord) ∧ Smart(Richard))
∨ (At(Stanf ord, Stanf ord) ∧ Smart(Stanf ord))
∨ ...
Chapter 8 15 Chapter 8 16
Properties of quantifiers Fun with sentences
∀ x ∀ y is the same as ∀ y ∀ x (why??) Brothers are siblings
Chapter 8 17 Chapter 8 18
Chapter 8 19 Chapter 8 20
Fun with sentences Fun with sentences
Brothers are siblings Brothers are siblings
∀ x, y Brother(x, y) ⇒ Sibling(x, y). ∀ x, y Brother(x, y) ⇒ Sibling(x, y).
“Sibling” is symmetric “Sibling” is symmetric
∀ x, y Sibling(x, y) ⇔ Sibling(y, x). ∀ x, y Sibling(x, y) ⇔ Sibling(y, x).
One’s mother is one’s female parent One’s mother is one’s female parent
∀ x, y M other(x, y) ⇔ (F emale(x) ∧ P arent(x, y)). ∀ x, y M other(x, y) ⇔ (F emale(x) ∧ P arent(x, y)).
A first cousin is a child of a parent’s sibling A first cousin is a child of a parent’s sibling
∀ x, y F irstCousin(x, y) ⇔ ∃ p, ps P arent(p, x) ∧ Sibling(ps, p) ∧
P arent(ps, y)
Chapter 8 21 Chapter 8 22
Chapter 8 23 Chapter 8 24
Knowledge base for the wumpus world Deducing hidden properties
“Perception” Properties of locations:
∀ b, g, t P ercept([Smell, b, g], t) ⇒ Smelt(t) ∀ x, t At(Agent, x, t) ∧ Smelt(t) ⇒ Smelly(x)
∀ s, b, t P ercept([s, b, Glitter], t) ⇒ AtGold(t) ∀ x, t At(Agent, x, t) ∧ Breeze(t) ⇒ Breezy(x)
Reflex: ∀ t AtGold(t) ⇒ Action(Grab, t) Squares are breezy near a pit:
Reflex with internal state: do we have the gold already? Diagnostic rule—infer cause from effect
∀ t AtGold(t) ∧ ¬Holding(Gold, t) ⇒ Action(Grab, t) ∀ y Breezy(y) ⇒ ∃ x P it(x) ∧ Adjacent(x, y)
Holding(Gold, t) cannot be observed Causal rule—infer effect from cause
⇒ keeping track of change is essential ∀ x, y P it(x) ∧ Adjacent(x, y) ⇒ Breezy(y)
Neither of these is complete—e.g., the causal rule doesn’t say whether
squares far away from pits can be breezy
Definition for the Breezy predicate:
∀ y Breezy(y) ⇔ [∃ x P it(x) ∧ Adjacent(x, y)]
Chapter 8 25 Chapter 8 26
PIT
Qualification problem: true descriptions of real actions require endless caveats—
Gold PIT
what if gold is slippery or nailed down or . . .
PIT
PIT
Ramification problem: real actions have many secondary consequences—
Gold PIT
what about the dust on the gold, wear and tear on gloves, . . .
S1
PIT
Forward
S0
Chapter 8 27 Chapter 8 28
Describing actions II Making plans
Successor-state axioms solve the representational frame problem Initial condition in KB:
At(Agent, [1, 1], S0)
Each axiom is “about” a predicate (not an action per se): At(Gold, [1, 2], S0)
P true afterwards ⇔ [an action made P true Query: Ask(KB, ∃ s Holding(Gold, s))
∨ P true already and no action made P false] i.e., in what situation will I be holding the gold?
Answer: {s/Result(Grab, Result(F orward, S0))}
For holding the gold: i.e., go forward and then grab the gold
∀ a, s Holding(Gold, Result(a, s)) ⇔ This assumes that the agent is interested in plans starting at S0 and that S0
[(a = Grab ∧ AtGold(s)) is the only situation described in the KB
∨ (Holding(Gold, s) ∧ a 6= Release)]
Chapter 8 29 Chapter 8 30
Chapter 8 31 Chapter 8 32
Outline
♦ Reducing first-order inference to propositional inference
♦ Unification
Inference in first-order logic ♦ Generalized Modus Ponens
♦ Forward and backward chaining
♦ Logic programming
Chapter 9
♦ Resolution
Chapter 9 1 Chapter 9 2
Chapter 9 3 Chapter 9 4
Existential instantiation (EI) Existential instantiation contd.
For any sentence α, variable v, and constant symbol k UI can be applied several times to add new sentences;
that does not appear elsewhere in the knowledge base: the new KB is logically equivalent to the old
∃v α EI can be applied once to replace the existential sentence;
Subst({v/k}, α) the new KB is not equivalent to the old,
but is satisfiable iff the old KB was satisfiable
E.g., ∃ x Crown(x) ∧ OnHead(x, John) yields
Crown(C1) ∧ OnHead(C1, John)
provided C1 is a new constant symbol, called a Skolem constant
Another example: from ∃ x d(xy )/dy = xy we obtain
d(ey )/dy = ey
provided e is a new constant symbol
Chapter 9 5 Chapter 9 6
King(John) ∧ Greedy(John) ⇒ Evil(John) Theorem: Herbrand (1930). If a sentence α is entailed by an FOL KB,
King(Richard) ∧ Greedy(Richard) ⇒ Evil(Richard) it is entailed by a finite subset of the propositional KB
King(John)
Idea: For n = 0 to ∞ do
Greedy(John)
create a propositional KB by instantiating with depth-n terms
Brother(Richard, John)
see if α is entailed by this KB
The new KB is propositionalized: proposition symbols are
Problem: works if α is entailed, loops if α is not entailed
King(John), Greedy(John), Evil(John), King(Richard) etc.
Theorem: Turing (1936), Church (1936), entailment in FOL is semidecidable
Chapter 9 7 Chapter 9 8
Problems with propositionalization Unification
Propositionalization seems to generate lots of irrelevant sentences. We can get the inference immediately if we can find a substitution θ
E.g., from such that King(x) and Greedy(x) match King(John) and Greedy(y)
∀ x King(x) ∧ Greedy(x) ⇒ Evil(x) θ = {x/John, y/John} works
King(John)
∀ y Greedy(y) Unify(α, β) = θ if αθ = βθ
Brother(Richard, John)
it seems obvious that Evil(John), but propositionalization produces lots of p q θ
facts such as Greedy(Richard) that are irrelevant Knows(John, x) Knows(John, Jane)
Knows(John, x) Knows(y, OJ)
With p k-ary predicates and n constants, there are p · nk instantiations
Knows(John, x) Knows(y, M other(y))
With function symbols, it gets nuch much worse! Knows(John, x) Knows(x, OJ)
Chapter 9 9 Chapter 9 10
Unification Unification
We can get the inference immediately if we can find a substitution θ We can get the inference immediately if we can find a substitution θ
such that King(x) and Greedy(x) match King(John) and Greedy(y) such that King(x) and Greedy(x) match King(John) and Greedy(y)
θ = {x/John, y/John} works θ = {x/John, y/John} works
Unify(α, β) = θ if αθ = βθ Unify(α, β) = θ if αθ = βθ
p q θ p q θ
Knows(John, x) Knows(John, Jane) {x/Jane} Knows(John, x) Knows(John, Jane) {x/Jane}
Knows(John, x) Knows(y, OJ) Knows(John, x) Knows(y, OJ) {x/OJ, y/John}
Knows(John, x) Knows(y, M other(y)) Knows(John, x) Knows(y, M other(y))
Knows(John, x) Knows(x, OJ) Knows(John, x) Knows(x, OJ)
Chapter 9 11 Chapter 9 12
Unification Unification
We can get the inference immediately if we can find a substitution θ We can get the inference immediately if we can find a substitution θ
such that King(x) and Greedy(x) match King(John) and Greedy(y) such that King(x) and Greedy(x) match King(John) and Greedy(y)
θ = {x/John, y/John} works θ = {x/John, y/John} works
Unify(α, β) = θ if αθ = βθ Unify(α, β) = θ if αθ = βθ
p q θ p q θ
Knows(John, x) Knows(John, Jane) {x/Jane} Knows(John, x) Knows(John, Jane) {x/Jane}
Knows(John, x) Knows(y, OJ) {x/OJ, y/John} Knows(John, x) Knows(y, OJ) {x/OJ, y/John}
Knows(John, x) Knows(y, M other(y)) {y/John, x/M other(John)} Knows(John, x) Knows(y, M other(y)) {y/John, x/M other(John)}
Knows(John, x) Knows(x, OJ) Knows(John, x) Knows(x, OJ) f ail
Standardizing apart eliminates overlap of variables, e.g., Knows(z17, OJ)
Chapter 9 13 Chapter 9 14
Chapter 9 15 Chapter 9 16
Example knowledge base Example knowledge base contd.
The law says that it is a crime for an American to sell weapons to hostile . . . it is a crime for an American to sell weapons to hostile nations:
nations. The country Nono, an enemy of America, has some missiles, and
all of its missiles were sold to it by Colonel West, who is American.
Prove that Col. West is a criminal
Chapter 9 17 Chapter 9 18
Chapter 9 19 Chapter 9 20
Example knowledge base contd. Example knowledge base contd.
. . . it is a crime for an American to sell weapons to hostile nations: . . . it is a crime for an American to sell weapons to hostile nations:
American(x)∧W eapon(y)∧Sells(x, y, z)∧Hostile(z) ⇒ Criminal(x) American(x)∧W eapon(y)∧Sells(x, y, z)∧Hostile(z) ⇒ Criminal(x)
Nono . . . has some missiles, i.e., ∃ x Owns(N ono, x) ∧ M issile(x): Nono . . . has some missiles, i.e., ∃ x Owns(N ono, x) ∧ M issile(x):
Owns(N ono, M1) and M issile(M1) Owns(N ono, M1) and M issile(M1)
. . . all of its missiles were sold to it by Colonel West . . . all of its missiles were sold to it by Colonel West
∀ x M issile(x) ∧ Owns(N ono, x) ⇒ Sells(W est, x, N ono) ∀ x M issile(x) ∧ Owns(N ono, x) ⇒ Sells(W est, x, N ono)
Missiles are weapons: Missiles are weapons:
M issile(x) ⇒ W eapon(x)
An enemy of America counts as “hostile”:
Chapter 9 21 Chapter 9 22
Chapter 9 23 Chapter 9 24
Forward chaining proof Forward chaining proof
Chapter 9 25 Chapter 9 26
Chapter 9 27 Chapter 9 28
Efficiency of forward chaining Hard matching example
Simple observation: no need to match a rule on iteration k Diff(wa, nt) ∧ Diff(wa, sa) ∧
if a premise wasn’t added on iteration k − 1 Diff(nt, q)Diff(nt, sa) ∧
⇒ match each rule whose premise contains a newly added literal NT
Diff(q, nsw) ∧ Diff(q, sa) ∧
Q
Chapter 9 29 Chapter 9 30
Chapter 9 31 Chapter 9 32
Backward chaining example Backward chaining example
Chapter 9 33 Chapter 9 34
Missile(y) Missile(y)
{ y/M1 }
Chapter 9 35 Chapter 9 36
Backward chaining example Backward chaining example
Chapter 9 37 Chapter 9 38
Chapter 9 39 Chapter 9 40
Prolog systems Prolog examples
Basis: backward chaining with Horn clauses + bells & whistles Depth-first search from a start state X:
Widely used in Europe, Japan (basis of 5th Generation project)
Compilation techniques ⇒ approaching a billion LIPS dfs(X) :- goal(X).
dfs(X) :- successor(X,S),dfs(S).
Program = set of clauses = head :- literal1, . . . literaln.
No need to loop over S: successor succeeds for each
criminal(X) :- american(X), weapon(Y), sells(X,Y,Z), hostile(Z).
Appending two lists to produce a third:
Efficient unification by open coding
Efficient retrieval of matching clauses by direct linking append([],Y,Y).
Depth-first, left-to-right backward chaining append([X|L],Y,[X|Z]) :- append(L,Y,Z).
Built-in predicates for arithmetic etc., e.g., X is Y*Z+3
Closed-world assumption (“negation as failure”) query: append(A,B,[1,2]) ?
e.g., given alive(X) :- not dead(X). answers: A=[] B=[1,2]
alive(joe) succeeds if dead(joe) fails A=[1] B=[2]
A=[1,2] B=[]
Chapter 9 41 Chapter 9 42
Chapter 9 43 Chapter 9 44
Conversion to CNF contd. Resolution proof: definite clauses
3. Standardize variables: each quantifier should use a different one American(x) Weapon(y) Sells(x,y,z) Hostile(z) Criminal(x) Criminal(West)
>
>
>
>
L
L
∀ x [∃ y Animal(y) ∧ ¬Loves(x, y)] ∨ [∃ z Loves(z, x)] American(West) American(West) Weapon(y) Sells(West,y,z) Hostile(z)
>
>
>
L
L
4. Skolemize: a more general form of existential instantiation. Missile(x) Weapon(x) Weapon(y) Sells(West,y,z) Hostile(z)
>
>
>
L
L
Each existential variable is replaced by a Skolem function
of the enclosing universally quantified variables: Missile(M1) Missile(y) Sells(West,y,z) Hostile(z)
>
>
L
L
∀ x [Animal(F (x)) ∧ ¬Loves(x, F (x))] ∨ Loves(G(x), x) Missile(x) Owns(Nono,x) Sells(West,x,Nono) Sells(West,M1,z) Hostile(z)
>
>
>
L
L
5. Drop universal quantifiers: Missile(M1) Missile(M1) Owns(Nono,M1) Hostile(Nono)
>
>
L
L
[Animal(F (x)) ∧ ¬Loves(x, F (x))] ∨ Loves(G(x), x) Owns(Nono,M1) Owns(Nono,M1) Hostile(Nono)
>
L
L
6. Distribute ∧ over ∨: Enemy(x,America) Hostile(x) Hostile(Nono)
>
L
L
[Animal(F (x)) ∨ Loves(G(x), x)] ∧ [¬Loves(x, F (x)) ∨ Loves(G(x), x)] Enemy(Nono,America) Enemy(Nono,America)
Chapter 9 45 Chapter 9 46
Outline
♦ Uncertainty
♦ Probability
Uncertainty ♦ Syntax and Semantics
♦ Inference
♦ Independence and Bayes’ Rule
Chapter 13
Chapter 13 1 Chapter 13 2
Uncertainty Methods for handling uncertainty
Let action At = leave for airport t minutes before flight Default or nonmonotonic logic:
Will At get me there on time? Assume my car does not have a flat tire
Assume A25 works unless contradicted by evidence
Problems: Issues: What assumptions are reasonable? How to handle contradiction?
1) partial observability (road state, other drivers’ plans, etc.)
2) noisy sensors (KCBS traffic reports) Rules with fudge factors:
3) uncertainty in action outcomes (flat tire, etc.) A25 7→0.3 AtAirportOnT ime
4) immense complexity of modelling and predicting traffic Sprinkler 7→0.99 W etGrass
W etGrass 7→0.7 Rain
Hence a purely logical approach either Issues: Problems with combination, e.g., Sprinkler causes Rain??
1) risks falsehood: “A25 will get me there on time”
or 2) leads to conclusions that are too weak for decision making: Probability
“A25 will get me there on time if there’s no accident on the bridge Given the available evidence,
and it doesn’t rain and my tires remain intact etc etc.” A25 will get me there on time with probability 0.04
Mahaviracarya (9th C.), Cardamo (1565) theory of gambling
(A1440 might reasonably be said to get me there on time
but I’d have to stay overnight in the airport . . .) (Fuzzy logic handles degree of truth NOT uncertainty e.g.,
W etGrass is true to degree 0.2)
Chapter 13 3 Chapter 13 4
Chapter 13 5 Chapter 13 6
Probability basics Random variables
Begin with a set Ω—the sample space A random variable is a function from sample points to some range, e.g., the
e.g., 6 possible rolls of a die. reals or Booleans
ω ∈ Ω is a sample point/possible world/atomic event e.g., Odd(1) = true.
A probability space or probability model is a sample space P induces a probability distribution for any r.v. X:
with an assignment P (ω) for every ω ∈ Ω s.t.
P (X = xi) = Σ{ω:X(ω) = xi}P (ω)
0 ≤ P (ω) ≤ 1
Σω P (ω) = 1 e.g., P (Odd = true) = P (1) + P (3) + P (5) = 1/6 + 1/6 + 1/6 = 1/2
e.g., P (1) = P (2) = P (3) = P (4) = P (5) = P (6) = 1/6.
An event A is any subset of Ω
P (A) = Σ{ω∈A}P (ω)
E.g., P (die roll < 4) = P (1) + P (2) + P (3) = 1/6 + 1/6 + 1/6 = 1/2
Chapter 13 7 Chapter 13 8
>
event a ∧ b = points where A(ω) = true and B(ω) = true
Often in AI applications, the sample points are defined
by the values of a set of random variables, i.e., the
sample space is the Cartesian product of the ranges of the variables
With Boolean variables, sample point = propositional logic model
e.g., A = true, B = f alse, or a ∧ ¬b.
de Finetti (1931): an agent who bets according to probabilities that violate
Proposition = disjunction of atomic events in which it is true
these axioms can be forced to bet so as to lose money regardless of outcome.
e.g., (a ∨ b) ≡ (¬a ∧ b) ∨ (a ∧ ¬b) ∨ (a ∧ b)
⇒ P (a ∨ b) = P (¬a ∧ b) + P (a ∧ ¬b) + P (a ∧ b)
Chapter 13 9 Chapter 13 10
Syntax for propositions Prior probability
Propositional or Boolean random variables Prior or unconditional probabilities of propositions
e.g., Cavity (do I have a cavity?) e.g., P (Cavity = true) = 0.1 and P (W eather = sunny) = 0.72
Cavity = true is a proposition, also written cavity correspond to belief prior to arrival of any (new) evidence
Discrete random variables (finite or infinite) Probability distribution gives values for all possible assignments:
e.g., W eather is one of hsunny, rain, cloudy, snowi P(W eather) = h0.72, 0.1, 0.08, 0.1i (normalized, i.e., sums to 1)
W eather = rain is a proposition
Values must be exhaustive and mutually exclusive Joint probability distribution for a set of r.v.s gives the
probability of every atomic event on those r.v.s (i.e., every sample point)
Continuous random variables (bounded or unbounded) P(W eather, Cavity) = a 4 × 2 matrix of values:
e.g., T emp = 21.6; also allow, e.g., T emp < 22.0.
Arbitrary Boolean combinations of basic propositions W eather = sunny rain cloudy snow
Cavity = true 0.144 0.02 0.016 0.02
Cavity = f alse 0.576 0.08 0.064 0.08
Every question about a domain can be answered by the joint
distribution because every event is a sum of sample points
Chapter 13 11 Chapter 13 12
0.125
0
18 dx 26
Chapter 13 13 Chapter 13 14
Conditional probability Conditional probability
Conditional or posterior probabilities Definition of conditional probability:
e.g., P (cavity|toothache) = 0.8
P (a ∧ b)
i.e., given that toothache is all I know P (a|b) = if P (b) 6= 0
P (b)
NOT “if toothache then 80% chance of cavity”
Product rule gives an alternative formulation:
(Notation for conditional distributions: P (a ∧ b) = P (a|b)P (b) = P (b|a)P (a)
P(Cavity|T oothache) = 2-element vector of 2-element vectors)
A general version holds for whole distributions, e.g.,
If we know more, e.g., cavity is also given, then we have P(W eather, Cavity) = P(W eather|Cavity)P(Cavity)
P (cavity|toothache, cavity) = 1 (View as a 4 × 2 set of equations, not matrix mult.)
Note: the less specific belief remains valid after more evidence arrives,
but is not always useful Chain rule is derived by successive application of product rule:
P(X1, . . . , Xn) = P(X1, . . . , Xn−1) P(Xn|X1, . . . , Xn−1)
New evidence may be irrelevant, allowing simplification, e.g., = P(X1, . . . , Xn−2) P(Xn1 |X1, . . . , Xn−2) P(Xn|X1, . . . , Xn−1)
P (cavity|toothache, 49ersW in) = P (cavity|toothache) = 0.8 = ...
This kind of inference, sanctioned by domain knowledge, is crucial n
= Πi = 1P(Xi|X1, . . . , Xi−1)
Chapter 13 15 Chapter 13 16
L
catch catch catch catch catch catch catch catch
L
L
cavity .108 .012 .072 .008 cavity .108 .012 .072 .008
cavity .016 .064 .144 .576 cavity .016 .064 .144 .576
L
L
For any proposition φ, sum the atomic events where it is true: For any proposition φ, sum the atomic events where it is true:
P (φ) = Σω:ω|=φP (ω) P (φ) = Σω:ω|=φP (ω)
P (toothache) = 0.108 + 0.012 + 0.016 + 0.064 = 0.2
Chapter 13 17 Chapter 13 18
Inference by enumeration Inference by enumeration
Start with the joint distribution: Start with the joint distribution:
toothache toothache toothache toothache
L
catch catch catch catch catch catch catch catch
L
cavity .108 .012 .072 .008 cavity .108 .012 .072 .008
cavity .016 .064 .144 .576 cavity .016 .064 .144 .576
L
L
For any proposition φ, sum the atomic events where it is true: Can also compute conditional probabilities:
P (φ) = Σω:ω|=φP (ω)
P (¬cavity ∧ toothache)
P (¬cavity|toothache) =
P (cavity∨toothache) = 0.108+0.012+0.072+0.008+0.016+0.064 = 0.28 P (toothache)
0.016 + 0.064
= = 0.4
0.108 + 0.012 + 0.016 + 0.064
Chapter 13 19 Chapter 13 20
Then the required summation of joint entries is done by summing out the
hidden variables:
P(Y|E = e) = αP(Y, E = e) = αΣhP(Y, E = e, H = h)
Denominator can be viewed as a normalization constant α
The terms in the summation are joint entries because Y, E, and H together
P(Cavity|toothache) = α P(Cavity, toothache)
exhaust the set of random variables
= α [P(Cavity, toothache, catch) + P(Cavity, toothache, ¬catch)]
= α [h0.108, 0.016i + h0.012, 0.064i] Obvious problems:
= α h0.12, 0.08i = h0.6, 0.4i 1) Worst-case time complexity O(dn) where d is the largest arity
2) Space complexity O(dn) to store the joint distribution
General idea: compute distribution on query variable 3) How to find the numbers for O(dn) entries???
by fixing evidence variables and summing over hidden variables
Chapter 13 21 Chapter 13 22
Independence Conditional independence
A and B are independent iff P(T oothache, Cavity, Catch) has 23 − 1 = 7 independent entries
P(A|B) = P(A) or P(B|A) = P(B) or P(A, B) = P(A)P(B)
If I have a cavity, the probability that the probe catches in it doesn’t depend
Cavity
Cavity decomposes into Toothache Catch on whether I have a toothache:
Toothache Catch (1) P (catch|toothache, cavity) = P (catch|cavity)
Weather
Weather The same independence holds if I haven’t got a cavity:
(2) P (catch|toothache, ¬cavity) = P (catch|¬cavity)
P(T oothache, Catch, Cavity, W eather)
Catch is conditionally independent of T oothache given Cavity:
= P(T oothache, Catch, Cavity)P(W eather)
P(Catch|T oothache, Cavity) = P(Catch|Cavity)
32 entries reduced to 12; for n independent biased coins, 2n → n
Equivalent statements:
Absolute independence powerful but rare P(T oothache|Catch, Cavity) = P(T oothache|Cavity)
P(T oothache, Catch|Cavity) = P(T oothache|Cavity)P(Catch|Cavity)
Dentistry is a large field with hundreds of variables,
none of which are independent. What to do?
Chapter 13 23 Chapter 13 24
= α P(toothache ∧ catch|Cavity)P(Cavity)
1,2 2,2 3,2 4,2
= α P(toothache|Cavity)P(catch|Cavity)P(Cavity) B
OK
This is an example of a naive Bayes model: 1,1 2,1 3,1 4,1
B
P(Cause, Ef f ect1 , . . . , Ef f ectn ) = P(Cause)ΠiP(Ef f ecti|Cause) OK OK
Chapter 13 27 Chapter 13 28
Chapter 13 29 Chapter 13 30
Using conditional independence Using conditional independence contd.
Basic insight: observations are conditionally independent of other hidden
squares given neighbouring hidden squares X
P(P1,3|known, b) = α P(P1,3, unknown, known, b)
1,4 2,4 3,4 4,4 unknown
X
= α P(b|P1,3, known, unknown)P(P1,3, known, unknown)
unknown
X X
1,3 2,3 3,3 4,3 = α P(b|known, P1,3, f ringe, other)P(P1,3, known, f ringe, other)
QUERY
OTHER f ringe other
X X
= α P(b|known, P1,3, f ringe)P(P1,3, known, f ringe, other)
1,2 2,2 3,2 4,2 f ringe other
X X
= α P(b|known, P1,3, f ringe) P(P1,3, known, f ringe, other)
f ringe other
1,1 2,1 FRINGE
3,1 4,1 X X
KNOWN = α P(b|known, P1,3, f ringe) P(P1,3)P (known)P (f ringe)P (other)
f ringe other
X X
= α P (known)P(P1,3) P(b|known, P1,3, f ringe)P (f ringe) P (other)
f ringe other
X
Define U nknown = F ringe ∪ Other = α′ P(P1,3) P(b|known, P1,3, f ringe)P (f ringe)
f ringe
P(b|P1,3, Known, U nknown) = P(b|P1,3, Known, F ringe)
Manipulate query into a form where we can use this!
Chapter 13 31 Chapter 13 32
OK
B
OK OK
B
OK OK
B
OK OK
B
OK OK
B
OK
Queries can be answered by summing over atomic events
0.2 x 0.2 = 0.04 0.2 x 0.8 = 0.16 0.8 x 0.2 = 0.16 0.2 x 0.2 = 0.04 0.2 x 0.8 = 0.16
For nontrivial domains, we must find a way to reduce the joint size
Independence and conditional independence provide the tools
′
P(P1,3|known, b) = α h0.2(0.04 + 0.16 + 0.16), 0.8(0.04 + 0.16)i
≈ h0.31, 0.69i
Chapter 13 33 Chapter 13 34
Outline
♦ Learning agents
♦ Inductive learning
Learning from Observations ♦ Decision tree learning
♦ Measuring learning performance
Environment
changes
Learning Performance
element element
knowledge
learning
goals
experiments
Problem
generator
Agent Effectors
Utility−based agent Transition model Dynamic Bayes net Outcome (This is a highly simplified model of real learning:
Simple reflex agent Percept−action fn Neural net Correct action
– Ignores prior knowledge
– Assumes a deterministic, observable “environment”
Supervised learning: correct answers for each instance – Assumes examples are given
Reinforcement learning: occasional rewards – Assumes that the agent wants to learn f —why?)
x x
x x
x x
Substantially simpler than “true” tree—a more complex hypothesis isn’t jus- 0.4
0 10 20 30 40 50 60 70 80 90 100
tified by small amount of data
Training set size
Chapter 18, Sections 1–3 27 Chapter 18, Sections 1–3 28
Performance measurement contd. Summary
Learning curve depends on Learning needed for unknown environments, lazy designers
– realizable (can express target function) vs. non-realizable
non-realizability can be due to missing attributes Learning agent = performance element + learning element
or restricted hypothesis class (e.g., thresholded linear function) Learning method depends on type of performance element, available
– redundant expressiveness (e.g., loads of irrelevant attributes) feedback, type of component to be improved, and its representation
% correct For supervised learning, the aim is to find a simple hypothesis
1 realizable that is approximately consistent with training examples
redundant
Decision tree learning using information gain
nonrealizable Learning performance = prediction accuracy measured on test set
# of examples
Abstract
After studying the modules on Search and Logical Inference in an Artificial Intelligence
course, we usually enter the uncertain waters of probabilistic models and planning-learning. This
note is supposed to be a bridge from the certain world of hard computing (and haphazard heuristic
soft computing when the hard computing becomes NP-Hard) to embracing uncertainty with rigour.
We demonstrate how the language of probability theory is a convenient shorthand to model myriad
possibilities of NP-Hard computation of boolean logic, without sacrificing a lot of guarantees coming
from rigour. We use the Monty Hall Problem to demonstrate this. This is a game-show-related
paradox which defied the arguments of no less than a genius like Paul Erdös.
1 2
express in FOL the probabilities thus: 3 Maximizing the Winning Probability
The Car Door P[car(A)] = a; P[car(B)] = b; P[car(C)] = c; a + b + c = 1. Note that ax + by + cz is the probability of the Contestant choosing the car door in the first chance,
and with the fixed strategy of always switching, the winning probability is 1 − (ax + by + cz), regardless
The Chosen Door P[chosen(A)] = x; P[chosen(B)] = y; P[chosen(C)] = z; x + y + z = 1.
of the Host’s choice when indeed the Contestant chooses the car door the first time. That means, to
The Shown Door P[shown(B)|car(A) ∧ chosen(A)] = ta ; P[shown(C)|car(A) ∧ chosen(A)] = 1 − ta . maximize the chance of winning finally, the Contestant must minimize the chance of choosing the car
P[shown(A)|car(B) ∧ chosen(B)] = tb ; P[shown(C)|car(B) ∧ chosen(B)] = 1 − tb . door in the first attempt! For a, b, c all distinct, when is this minimization happening? Intuitively, when
P[shown(A)|car(C) ∧ chosen(C)] = tc ; P[shown(B)|car(C) ∧ chosen(C)] = 1 − tc . the Contestant’s guesses about a, b, c are all “wrong”. Formally, ax+by +cz is minimized when a < b < c
and x > y > z, and maximized when a < b < c and x < y < z – by the Rearrangement Inequality(Hardy
Switching P[switched|chosen(A) ∧ shown(B)] = sa ; P[switched|chosen(A) ∧ shown(C) = 1 − sa . et al., 1952, Section 10.2, Theorem 368). Thus, if the Contestant can guess the car door priors better,
P[switched|chosen(B) ∧ shown(A)] = sb ; P[switched|chosen(B) ∧ shown(C) = 1 − sb . then actually the first choice must be made against the guess so that the Host reveals more information
P[switched|chosen(C) ∧ shown(A)] = sc ; P[switched|chosen(C) ∧ shown(B)] = 1 − sc . to confirm the correct guess about the car door.
Now we can state the main result:
3.1 Generalization
Claim 1. Given the probabilities in Table 1 as a = b = c; x = y = z; ta = tb = tc = 21 ; s∗∗ = 1, i.e. with
fairness and uniformity assumptions, when the Contestant chooses the strategy of always switching, the If there are n > 3 doors (with ai probabilities of being the reward door), 1 reward, and the Host reveals
probability of winning is 23 ; otherwise, if in the same situation the Contestant never chooses to switch k < n − 1 non-reward doors, then the probability that the reward is behind one of the n − k − 1
(s∗∗ = 0), then the probability of winning is 31 . doors not revealed to and not chosen by the Contestant is the complement of the probability that the
Contestant chose the reward door in the first choice. Thus taking the reward door priors to be hai i and
Proof. First note that by a full joint distribution, the Contestant guesses of them to be hxi i, and the switching probabilities hyilj i, the winning probability
X after (always) switching is
P[win] = P[car(X) ∧ chosen(X) ∧ ¬switched] + P[car(X) ∧ ¬chosen(X) ∧ switched]
X∈{A,B,C}
!
X X
P[win|switching] = 1 − a i xi aα xα yilα
X
= P[car(X) ∧ chosen(X) ∧ shown(Y )¬switched]
i l∈[1..n]−{i},α∈([1..n]−{i,l} )
X,Y,Z∈{A,B,C};X6=Y ;Y 6=Z;X6=Z k
3 4
the given position, if we move any single queen anywhere else in its row, except any of the corner
queens moving to the other corner in its corner, the number of conflicts grow only. That means,
the four-down-four-up solution is not found by a strategy here, unless any sequence of moves of
any length assessed in hindsight is considered a strategy.
When Many Queens Fight Over Territory
How to make M IN -C ONFLICTS work 1.1 Formal Definition
We know that two queens cannot be on the same row or column without attacking each other.
Ramprasad S Joshi Therefore, to reduce the search space, we fix that each queen is on a different column, such that
then we need to choose different rows for them that place them on different diagonals, to satisfy
April 2, 2017 all the constraints.
Definition 1.1 (N-Q UEENS). Given n, find a permutation x1 , x2 , . . . , xn of [1..n] such that
Abstract
∀i 6= j ∈ [1..n], |xi − xj |6= |i − j|.
This short note is about the assertion in AIMA(Russell and Norvig, 2010, p.221) that “on
the n-queens problem, if you don’t count the initial placement of queens, the run time of min- In this formal definition, it is implicit that the ith queen remains on the ith column. We choose
conflicts is roughly independent of problem size”. We learnt the hard way, that to verify this state- only a permutation on the rows, thus again making sure that each queen is on a unique row. Then
ment, we need to carefully implement the heuristic with full rigour.
the constraints are only on the diagonals.
1 2
2.1 Local Search 3.1 Description and Algorithm
The local search strategy is very simple to define in its full generality: In Line 5 in Algorithm The same page(Russell and Norvig, 2010, p.221) describes the M IN -C ONFLICTS as follows (see
figure 2):
Algorithm 1 Local Search for NQueens function M IN -C ONFLICTS(csp, max steps) returns a solution or failure
inputs: csp, a constraint satisfaction problem
Require: n, the number of queens. max steps , the number of steps allowed before giving up
1: Q ← RandomPermutation([1..n]) current ← an initial complete assignment for csp
2: State ← InitializeState(Q) for i = 1 to max steps do
3: while ¬(Safe(Q) ∨ Failed(State)) do if current is a solution for csp then return current
var ← a randomly chosen conflicted variable from csp.VARIABLES
4: q ← ChooseRandom([1..n]) value ← the value v for var that minimizes C ONFLICTS(var,v, current,csp)
5: Q ← UpdateState(State, Q) set var = value in current
6: end while return f ailure
7: return (Q, Safe(Q))
1, the updation can depend on both the current (algorithmic) state and the curent assignment.
The state can include random number generator state, or deterministic state, number of steps
passed or to go, etc. This allows such a wide variety of heuristics that it can represent plain hill-
climbing, hill-climbing with different tiebreaker criteria to escape various local traps in search,
etc. S IMULATED A NNEALING and singleton population mutation-only G ENETIC A LGORITHM can
similarly be retrofitted. Even the B ACK T RACKING S EARCH can be retrofitted to this, by removal
of randomization.
3 M IN -C ONFLICTS in Action
Figure 2: The Text-Book Description
AIMA says(Russell and Norvig, 2010, p.221):
Amazingly, on the n-queens problem, if you don’t count the initial placement of queens, the run time of The line “value ← the value v for var that minimizes C ONFLICTS(var,v, current,csp)” in the text-book’s
min-conflicts is roughly independent of problem size. It solves even the million-queens problem in an average pseudo-code version gives an impression that there is (always) a unique value that minimizes etc. It
of 50 steps (after the initial assignment). This remarkable observation was the stimulus leading to a great
deal of research in the 1990s on local searc h and the distinction between easy and hard problems, which does not answer (or, for that matter, even raise) the question of how to break the tie when there are
we take up in Chapter 7. Roughly speaking, n-queens is easy for local search because solutions are densely more than one values that minimize.
distributed throughout the state space. Min-conflicts also works well for hard problems. We found that indeed, this unfortunate gloss over this important question is the root of all
trouble. Let us see how.
However, when we implemented M IN -C ONFLICTS initially we did not get such a nice perfor-
mance. We got something between linear and quadratic time in the number of queens n, making
us wonder, what went wrong and why? 3.2 The Resolution
To answer this question, we had to go back to the basics: looking at the algorithm’s description First, there is this Python implementation from the text-book’s web-site (see figure 3). The assign-
and the various implementations given in the accompanying material on the text-book’s website ment to value is made by a call to min conflicts value. And in that function, the comment,
(aima.cs.berkeley.edu), we reviewed our implementation, and finally got to the root of the prob- ”Return the value that will give var the least number of conflicts. If there is a tie, choose at
lem, which is the subject matter of this article. random”, or its implementation “argmin random tie(csp.domains[var], lambda val:
Next, first we describe the description-specification of the algorithm, and then go to the im- csp.nconflicts(var, val, current))” is a clear giveaway: the question of the missing
plementation issues. tiebreaker is both raised and answered here. Now we know what was missing in the text-book
itself.
3 4
3.2.1 The Solution and its Validation The performance with random choice is below, in Table 2.
Therefore, we implemented Algorithm 2, in which, line 4 is where the difference between the text- n Step Size Iterations Restarts Unresolved Conflicts
book description version and the Python version given above is made explicit by the variable 25 1 100 0 0
RandomChoice. We implemented this in C (see Appendix A), just a surrendipitous choice, though 26 1 249 0 0
again doing Python of this might have been equally useful or rewarding. We tested the same 52 1 79 0 0
with the following conditions: 85 1 117 0 0
1. Compile the program with the flag “-D BADCHOICE”, and test run for various n repeatedly. 86 1 111 0 0
68 1 140 0 0
2. Do the same without that flag during compilation. 60 1 155 0 0
69 1 96 0 0
Do the experiment and you will know yourselves.
80 1 85 0 0
66 1 124 0 0
Validation See the performance in Table 1 with the bad choice without randomization.
5 6
A Appendix: C Implementation of Algorithm 2 if(qj>0) qj = rand()%(qj+1);
s[qi] = u[qj];
#include<stdio.h> #endif
#include<stdlib.h> val = min;
#include<time.h> c = &(tempc[u[qj]][0]);
int Eval(int n, int *s, int *c) { }
int i, j, totc = 0; }
for(i = 0; i < n; i++) c[i] = 0; return 0;
for(i = 0; i < n-1; i++) { }
for(j = i+1; j < n; j++) { int main(int argc, char*argv[]) {
if(s[i] == s[j]) { int n = 10, maxit = 1000, seed, step = 1;
c[i]++; if(argc > 1) n = atoi(argv[1]);
c[j]++; if(argc > 2) seed = atoi(argv[2]); else seed=time(0);
totc++; if(argc > 3) maxit = atoi(argv[3]);
} if(argc > 4) step = atoi(argv[4]);
else if(abs(s[i]-s[j])==abs(i-j)) { srand(seed);
c[i]++; int s[n];
c[j]++; int i;
totc++; for(i = 0; i < n; i++) s[i] = rand()%n;
} int restarts;
} int tempit=maxit-HillClimbNQueens(n,s,maxit,&restarts,step);
} if(tempit<maxit) {
return totc; printf("%d\t%d\t%d\t%d\t0\n", n, step, tempit, restarts);
} }
int HillClimbNQueens(int n, int *s, int MaxIterations, int *restarts, int step) { else {
int val, i, j, qi, qj, min, temp; int c[n];
int *c, u[n], tempc[n][n]; printf("%d\t%d\t%d\t%d\t%d\n", n, step, maxit-tempit, restarts, Eval(n,s,c));
*restarts=0; }
if(step > n) step -= n; return 0;
for(i=0; i<n; i++) s[i] = rand()%n; }
val = Eval(n,s,c=&(tempc[0][0]));
while(MaxIterations-- > 0) {
if(val==0) return MaxIterations;
do qi = rand()%n; while(c[qi]==0);
min = val;
qj = -1;
for(j = 0; j < n; j++) {
s[qi] = j;
temp = Eval(n,s,&(tempc[j][0]));
if(temp < min) {
min = temp;
qj = 0;
u[qj] = j;
}
else if(temp == min) {
qj++;
u[qj] = j;
}
}
if(qj>=0) {
#ifdef BADCHOICE
s[qi] = u[0];
#else
7 8
References
Stuart Russell and Peter Norvig. Artificial Intelligence: A Modern Approach. Prentice Hall, 3 edition,
2010.