Review & Finish CSPs Begin Logical Agents Constraint - - PowerPoint PPT Presentation
Review & Finish CSPs Begin Logical Agents Constraint - - PowerPoint PPT Presentation
Review & Finish CSPs Begin Logical Agents Constraint Satisfaction Problems Constraint Satisfaction Problems What is a CSP? Finite set of variables X 1 , X 2 , , X n Nonempty domain of possible values for each variable D 1 ,
Constraint Satisfaction Problems Constraint Satisfaction Problems
- What is a CSP?
– Finite set of variables X1, X2, …, Xn – Nonempty domain of possible values for each variable D1, D2, …, Dn – Finite set of constraints C1, C2, …, Cm Finite set of constraints C1, C2, …, Cm
- Each constraint Ci limits the values that variables can take,
- e.g., X1 ≠ X2
– Each constraint Ci is a pair <scope, relation>
- Scope = Tuple of variables that participate in the constraint.
- Relation = List of allowed combinations of variable values.
May be an explicit list of allowed combinations. M b b t t l ti ll i b hi t ti d May be an abstract relation allowing membership testing and listing.
- CSP benefits
– Standard representation pattern Standard representation pattern – Generic goal and successor functions – Generic heuristics (no domain specific expertise).
CSPs --- what is a solution? CSPs what is a solution?
A t t i i t f l t ll i bl
- A state is an assignment of values to some or all variables.
– An assignment is complete when every variable has a value. – An assignment is partial when some variables have no values.
- Consistent assignment
– assignment does not violate the constraints
- A solution to a CSP is a complete and consistent assignment.
- Some CSPs require a solution that maximizes an objective function.
CSP as a standard search problem
- A CSP can easily be expressed as a standard search problem.
- Incremental formulation
– Initial State: the empty assignment {} – Actions (3rd ed.), Successor function (2nd ed.): Assign a value to an unassigned variable provided that it does not violate a constraint – Goal test: the current assignment is complete (by construction it is consistent) – Path cost: constant cost for every step (not really relevant) C f
- Can also use complete-state formulation
– Local search techniques (Chapter 4) tend to work well
Improving CSP efficiency Improving CSP efficiency
- Previous improvements on uninformed search
introduce heuristics
- For CSPS, general-purpose methods can give large gains in speed,
e.g., – Which variable should be assigned next? – In what order should its values be tried? – Can we detect inevitable failure early? – Can we take advantage of problem structure? Note: CSPs are somewhat generic in their formulation, and so the heuristics are more general compared to methods in Chapter 4
Backtracking search
function BACKTRACKING-SEARCH(csp) return a solution or failure return RECURSIVE-BACKTRACKING({} , csp)
Backtracking search
function RECURSIVE-BACKTRACKING(assignment, csp) return a solution or failure if assignment is complete then return assignment var SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp],assignment,csp) for each value in ORDER-DOMAIN-VALUES(var, assignment, csp) do if value is consistent with assignment according to CONSTRAINTS[csp] then add {var=value} to assignment result RRECURSIVE-BACTRACKING(assignment, csp) ( g , p) if result failure then return result remove {var=value} from assignment return failure
Minimum remaining values (MRV) (MRV)
var SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp] assignment csp) var SELECT UNASSIGNED VARIABLE(VARIABLES[csp],assignment,csp)
- A.k.a. most constrained variable heuristic
- Heuristic Rule: choose variable with the fewest legal moves
– e.g., will immediately detect failure if X has no legal values
Degree heuristic for the initial variable variable
- Heuristic Rule: select variable that is involved in the largest number of constraints
- n other unassigned variables.
- Degree heuristic can be useful as a tie breaker.
- In what order should a variable’s values be tried?
Least constraining value for value ordering value-ordering
- Least constraining value heuristic
- Least constraining value heuristic
- Heuristic Rule: given a variable choose the least constraining value
– leaves the maximum flexibility for subsequent variable assignments
Forward checking
- Assign {Q=green}
Assign {Q green}
- Effects on other variables connected by constraints with WA
– NT can no longer be green – NSW can no longer be green NSW can no longer be green – SA can no longer be green
- MRV heuristic would automatically select NT or SA next
Forward checking
- If V is assigned blue
If V is assigned blue
- Effects on other variables connected by constraints with WA
– NSW can no longer be blue SA is empty – SA is empty
- FC has detected that partial assignment is inconsistent with the constraints and backtracking
can occur.
Arc consistency
- An Arc X Y is consistent if
for every value x of X there is some value y consistent with x for every value x of X there is some value y consistent with x (note that this is a directed property)
- Consider state of search after WA and Q are assigned:
SA NSW is consistent if SA bl d NSW d SA=blue and NSW=red
Arc consistency
- X Y is consistent if
for every value x of X there is some value y consistent with x for every value x of X there is some value y consistent with x
- NSW SA is consistent if
NSW=red and SA=blue NSW=blue and SA=???
Arc consistency
- Can enforce arc-consistency:
Can enforce arc consistency: Arc can be made consistent by removing blue from NSW
- Continue to propagate constraints….
– Check V NSW – Not consistent for V = red f – Remove red from V
Arc consistency
- Continue to propagate constraints
Continue to propagate constraints….
- SA NT is not consistent
and cannot be made consistent – and cannot be made consistent
- Arc consistency detects failure earlier than FC
Arc consistency algorithm (AC-3) Arc consistency algorithm (AC 3)
function AC-3(csp) return the CSP, possibly with reduced domains inputs: csp, a binary csp with variables {X1, X2, …, Xn} local variables: queue a queue of arcs initially the arcs in csp local variables: queue, a queue of arcs initially the arcs in csp while queue is not empty do (Xi, Xj) REMOVE-FIRST(queue) if REMOVE-INCONSISTENT-VALUES(Xi, Xj) then
j
for each Xk in NEIGHBORS[Xi ] do add (Xi, Xj) to queue function REMOVE-INCONSISTENT-VALUES(Xi, Xj) return true iff we remove a value removed false removed false for each x in DOMAIN[Xi] do if no value y in DOMAIN[Xj] allows (x,y) to satisfy the constraints between Xi and Xj then delete x from DOMAIN[Xi]; removed true return removed
(from Mackworth, 1977)
K-consistency K consistency
- Arc consistency does not detect all inconsistencies:
P ti l i t {WA d NSW d} i i i t t – Partial assignment {WA=red, NSW=red} is inconsistent.
- Stronger forms of propagation can be defined using the notion of k-consistency.
- A CSP is k-consistent if for any set of k-1 variables and for any consistent
assignment to those variables, a consistent value can always be assigned to any kth variable.
– E.g. 1-consistency = node-consistency E g 2 consistency arc consistency – E.g. 2-consistency = arc-consistency – E.g. 3-consistency = path-consistency
- Strongly k-consistent:
k consistent for all values {k k 1 2 1} – k-consistent for all values {k, k-1, …2, 1}
Local search for CSP Local search for CSP
function MIN-CONFLICTS(csp, max_steps) return solution or failure i t t i t ti f ti bl inputs: csp, a constraint satisfaction problem max_steps, the number of steps allowed before giving up current an initial complete assignment for csp for i = 1 to max_steps do if current is a solution for csp then return current var a randomly chosen, conflicted variable from VARIABLES[csp] value the value v for var that minimize CONFLICTS(var,v,current,csp) set var = value in current return failure
Graph structure and problem complexity
- Solving disconnected subproblems
– Suppose each subproblem has c variables out of a total of n. – Worst case solution cost is O(n/c dc), i.e. linear in n
- Instead of O(d n), exponential in n
- E.g. n= 80, c= 20, d=2
E.g. n 80, c 20, d 2
– 280 = 4 billion years at 1 million nodes/sec. – 4 * 220= .4 second at 1 million nodes/sec
Tree-structured CSPs
- Theorem:
- Theorem:
– if a constraint graph has no loops then the CSP can be solved in O(nd 2) time – linear in the number of variables!
- Compare difference with general CSP, where worst case is O(d n)
Algorithm for Solving Tree- structured CSPs structured CSPs
– Choose some variable as root, order variables from root to leaves such that every node’s parent precedes it in the ordering.
- Label variables from X1 to Xn)
1 n)
- Every variable now has 1 parent
– Backward Pass
- For j from n down to 2, apply arc consistency to arc [Parent(Xj), Xj) ]
- Remove values from Parent(Xj) if needed
– Forward Pass
- For j from 1 to n assign Xj consistently with Parent(Xj )
Tree CSP Example Tree CSP Example
G B
Tree CSP Example Tree CSP Example
B R G B G B R G R G B
Backw ard Pass ( constraint propagation)
Tree CSP Example Tree CSP Example
B R G B G B R G R G B
Backw ard Pass ( constraint propagation)
B G R G B R
Forw ard Pass ( assignm ent)
Tree CSP complexity Tree CSP complexity
- Backward pass
– n arc checks – Each has complexity d2 at worst
- Forward pass
– n variable assignments, O(nd) Overall complexity is O(nd 2) Algorithm works because if the backward pass succeeds, then every variable by definition has a legal assignment in the forward pass y g g p
What about non-tree CSPs? What about non tree CSPs?
- General idea is to convert the graph to a tree
g p 2 general approaches
- 1. Assign values to specific variables (Cycle Cutset
method) method) 2 Construct a tree-decomposition of the graph
- 2. Construct a tree decomposition of the graph
- Connected subproblems (subgraphs) form a tree
structure
Cycle-cutset conditioning Cycle cutset conditioning
- Choose a subset S of variables from the graph so that
g p graph without S is a tree – S = “cycle cutset”
- For each possible consistent assignment for S
Remove any inconsistent values from remaining – Remove any inconsistent values from remaining variables that are inconsistent with S – Use tree-structured CSP to solve the remaining tree- g structure
- If it has a solution, return it along with S
- If not, continue to try other assignments for S
Finding the optimal cutset Finding the optimal cutset
- If c is small this technique works very well
If c is small, this technique works very well H fi di ll t l t t i
- However, finding smallest cycle cutset is
NP-hard
– But there are good approximation algorithms
Tree Decompositions Tree Decompositions
Rules for a Tree Decomposition Rules for a Tree Decomposition
- Every variable appears in at least one of the
Every variable appears in at least one of the subproblems
- If two variables are connected in the original
problem, they must appear together (with the p , y pp g ( constraint) in at least one subproblem
- If a variable appears in two subproblems, it must
appear in each node on the path connecting those subproblems.
Summary Summary
- CSPs
i l ki d f bl t t d fi d b l f fi d t f i bl l t t d fi d – special kind of problem: states defined by values of a fixed set of variables, goal test defined by constraints on variable values
- Backtracking=depth-first search with one variable assigned per node
- Heuristics
– Variable ordering and value selection heuristics help significantly
- Constraint propagation does additional work to constrain values and detect
inconsistencies
– Works effectively when combined with heuristics
- Iterative min-conflicts is often effective in practice.
- Graph structure of CSPs determines problem complexity
– e.g., tree structured CSPs can be solved in linear time. g ,
Logical Agents (Part I) Logical Agents (Part I)
Chapter 7 Propositional Logic Propositional Logic
Why Do We Need Logic? Why Do We Need Logic?
- Problem-solving agents were very inflexible: hard code
g g y every possible state.
- Search is almost always exponential in the number of
states.
- Problem solving agents cannot infer unobserved
information.
- We want an algorithm that reasons in a way that
bl i i h resembles reasoning in humans.
Knowledge-Based Agents g g
- KB = knowledge base
– A set of sentences or facts – e.g., a set of statements in a logic language
- Inference
– Deriving new sentences from old – e.g., using a set of logical statements to infer new ones
- A simple model for reasoning
– Agent is told or perceives new evidence
- E.g., A is true
g – Agent then infers new facts to add to the KB
- E.g., KB = { A -> (B OR C) }, then given A and not C we can infer
that B is true
- B is now added to the KB even though it was not explicitly asserted,
i.e., the agent inferred B
Types of Logics Types of Logics
- Propositional logic deals with specific objects and concrete statements
that are either true or false – E.g., John is married to Sue.
- Predicate logic (first order logic) allows statements to contain variables,
f ti d tifi functions, and quantifiers – For all X, Y: If X is married to Y then Y is married to X.
- Fuzzy logic deals with statements that are somewhat vague, such as this
paint is grey or the sky is cloudy paint is grey, or the sky is cloudy.
- Probability deals with statements that are possibly true, such as whether I
will win the lottery next week.
- Temporal logic deals with statements about time such as John was a
Temporal logic deals with statements about time, such as John was a student at UC Irvine for four years.
- Modal logic deals with statements about belief or knowledge, such as Mary
believes that John is married to Sue, or Sue knows that search is NP- complete.
Wumpus World PEAS description
- Performance measure
Would DFS work well? A*? – gold: +1000, death: -1000 – -1 per step, -10 for using the arrow
- Environment
– Squares adjacent to wumpus are smelly – Squares adjacent to pit are breezy – Glitter iff gold is in the same square – Shooting kills wumpus if you are facing it – Shooting uses up the only arrow – Grabbing picks up gold if in same square – Releasing drops the gold in same square
- Sensors: Stench, Breeze, Glitter, Bump, Scream
- Actuators: Left turn, Right turn, Forward, Grab, Release, Shoot
Exploring a wumpus world Exploring a wumpus world
Exploring a wumpus world Exploring a wumpus world
Exploring a wumpus world Exploring a wumpus world
Exploring a wumpus world Exploring a wumpus world
Exploring a Wumpus world Exploring a Wumpus world
If the Wumpus were here, stench should be h Th f it i
- here. Therefore it is
here. Since, there is no breeze here, the pit must be there and it m st be OK there, and it must be OK here
We need rather sophisticated reasoning here! p g
Exploring a wumpus world Exploring a wumpus world
Exploring a wumpus world Exploring a wumpus world
Exploring a wumpus world Exploring a wumpus world
Logic g
- We used logical reasoning to find the gold.
f f f
- Logics are formal languages for representing information such
that conclusions can be drawn
- Syntax defines the sentences in the language
y g g
- Semantics define the "meaning” or interpretation of sentences;
– connects symbols to real events in the world, i e define truth of a sentence in a world – i.e., define truth of a sentence in a world
- E.g., the language of arithmetic
2 i t 2 {} i t t t – x+2 ≥ y is a sentence; x2+y > {} is not a sentence syntax – – x+2 ≥ y is true in a world where x = 7, y = 1
sem
– x+2 ≥ y is false in a world where x = 0, y = 6
antics
Entailment Entailment
- Entailment means that one thing follows from
g another: KB ╞ α
- Knowledge base KB entails sentence α if and
- nly if α is true in all worlds where KB is true
y
– E.g., the KB containing “the Giants won and the Reds won” entails “The Giants won”. – E.g., x+y = 4 entails 4 = x+y – E.g., “Mary is Sue’s sister and Amy is Sue’s daughter” entails “Mary is Amy’s aunt.” y y
Models Models
- Logicians typically think in terms of models, which are formally
structured worlds with respect to which truth can be evaluated 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 α
- Then KB ╞ α iff M(KB) M(α)
╞ ( ) ( )
– E.g. KB = Giants won and Reds won α = Giants won
- Think of KB and α as collections of
- Think of KB and α as collections of
constraints and of models m as possible states. M(KB) are the solutions to KB and M(α) the solutions to α. to KB and M(α) the solutions to α. Then, KB ╞ α when all solutions to KB are also solutions to α.
Wumpus models Wumpus models
All possible models in this reduced Wumpus world.
Wumpus models Wumpus models
- KB = all possible wumpus-worlds
consistent with the observations and the consistent with the observations and the “physics” of the Wumpus world.
Wumpus models Wumpus models
α1 = "[1,2] is safe", KB ╞ α1, proved by model checking
Wumpus models Wumpus models
α2 = "[2,2] is safe", KB ╞ α2
Inference Procedures Inference Procedures
├
- KB ├i α = sentence α can be derived from KB by
procedure i
- Soundness: i is sound if whenever KB ├i α, it is also true
that KB╞ α (no wrong inferences, but maybe not all inferences) inferences)
- Completeness: i is complete if whenever KB╞ α, it is also
├ true that KB ├i α (all inferences can be made, but maybe some wrong extra ones as well)
Recap propositional logic: Syntax
P iti l l i i th i l t l i ill t t
- Propositional logic is the simplest logic – illustrates
basic ideas
- The proposition symbols P1, P2 etc are sentences
If S is a sentence S is a sentence (negation) – If S is a sentence, S is a sentence (negation) – If S1 and S2 are sentences, S1 S2 is a sentence (conjunction) – If S1 and S2 are sentences, S1 S2 is a sentence (disjunction) If S and S are sentences S S is a sentence (implication) – If S1 and S2 are sentences, S1 S2 is a sentence (implication) – If S1 and S2 are sentences, S1 S2 is a sentence (biconditional)
Recap propositional logic: Semantics
Each model/world specifies true or false for each proposition symbol Each model/world specifies true or false for each proposition symbol
E.g. P1,2 P2,2 P3,1 false true false With these symbols, 8 possible models, can be enumerated automatically.
Rules for evaluating truth with respect to a model m:
- S
is true iff S is false S1 S2 is true iff S1 is true and S2 is true S S i t iff S i t S i t S1 S2 is true iff S1is true or S2 is true S1 S2 is true iff S1 is false or S2 is true i.e., is false iff S1 is true and S2 is false S1 S2 is true iff S1S2 is true andS2S1 is true
1 2 1 2 2 1
Simple recursive process evaluates an arbitrary sentence, e.g., P (P P ) = true (true false) = true true = true
- P1,2 (P2,2 P3,1) = true (true false) = true true = true
Recap truth tables for connectives
OR: P or Q is true or both are true. XOR: P or Q is true but not both Implication is always true when the premises are False! XOR: P or Q is true but not both. when the premises are False!
Inference by enumeration Inference by enumeration
- Enumeration of all models is sound and complete.
- For n symbols, time complexity is O(2n)...
- We need a smarter way to do inference!
- In particular, we are going to infer new logical sentences
from the data-base and see if they match a query.
Logical equivalence Logical equivalence
- To manipulate logical sentences we need some rewrite
rules.
- Two sentences are logically equivalent iff they are true in
same models: α ≡ ß iff α╞ β and β╞ α
You need to know these !
Validity and satisfiability Validity and satisfiability
A sentence is valid if it is true in all models,
T A A A A (A (A B)) B e.g., True, A A, A A, (A (A B)) B
Validity is connected to inference via the Deduction Theorem:
KB ╞ α if and only if (KB α) is valid ╞ y ( )
A sentence is satisfiable if it is true in some model
e.g., A B, C
A sentence is unsatisfiable if it is false in all models
e.g., AA
Satisfiability is connected to inference via the following:
KB ╞ α if and only if (KB α) is unsatisfiable (there is no model for which KB=true and is false)
Summary (Part I) Summary (Part I)
- Logical agents apply inference to a knowledge base to derive new
information and make decisions information and make decisions
- Basic concepts of logic:
– syntax: formal structure of sentences syntax: formal structure of sentences – semantics: truth of sentences wrt models – entailment: necessary truth of one sentence given another – inference: deriving sentences from other sentences d d i ti d l t il d t – soundness: derivations produce only entailed sentences – completeness: derivations can produce all entailed sentences – valid: sentence is true in every model (a tautology)
- Logical equivalences allow syntactic manipulations
- Propositional logic lacks expressive power
p g p p
– Can only state specific facts about the world. – Cannot express general rules about the world (use First Order Predicate Logic)