Download Constraint Satisfaction Problems in Artificial Intelligence and Programming and more Study notes Computer Science in PDF only on Docsity!
Artificial IntelligenceProgramming^ Constraint Satisfaction
Chris BrooksDepartment of Computer ScienceUniversity of San Francisco
Constraint Satisfaction So far, we’ve focused on using search to find the
best
solution. In many cases, you just need to find a solution thatsatisfies some criteria. These criteria are called constraints. A problem in which we want to find any solution thatsatisfies our constraints is a
constraint satisfaction problem Constraints provide us with additional knowledge aboutthe problem that we can exploit.^ We can also consider optimizing constrainedproblems.
Department of Computer Science — University of San Francisco – p.1/
??
Examples Toy Problems^ Map coloring, N-queens, cryptarithmetic Real life problems^ Scheduling, register allocation, resource allocation
Department of Computer Science — University of San Fra
Formalizing a CSP We can model a CSP as a set of variables
{x, x, ..., x^12
}n
Each variable as a domain of possible values D, D, ..., D^12
n We also have a set of constraints
C, C, ...^12
Unary constraints:
x <^10 , ymod
2 == 0, etc
Binary constraints:
x < y, x^ +
y <^50 ,^ no two adjacent
squares can be the same color, etc N-ary constraints:
x+^ x+^1
...^ +^ x= 75n^
, weight of
chassis plus engine plus body < 3000 lbs, etc. An assignment of values to variables that satisfies allconstraints is called a
consistent
solution.
We might might also have an objective function y^ =^ f^ (x, ..., x^1
)^ that lets us compare solutions.n
Department of Computer Science — University of San Francisco – p.3/
??
Approaches If the domain of all variables is continuous (i.e. realnumbers) and constraints are all linear functions, we canuse^ linear programming
to solve the problem. Express the problem as a system of equations In other cases, we can use
dynamic programming
Dynamic programming is a form of search. In the most general case, we can express a CSP as asearch problem.
Department of Computer Science — University of San Francisco – p.4/
??
Solving CSPs with search We’ll begin with an initial state: no values assigned to x, ..., x^1 n An action assigns values to variables. A goal is an assignment to each variable such that allconstraints are met. The successor function returns all possible singleassignments such that constraints are still met.^ Notice that our solution for this sort of problem is thegoal state, as opposed to a path through the statespace.
Department of Computer Science — University of San Fra
Constraint graph Often, the most difficult part of solving a CSP isformulating the problem. It is often useful to visualize the CSP as a
constraint
graph Nodes represent variables, edges represent binaryconstraints.^ For n-ary constraints, we must add nodes thatrepresent combinations of values.
Department of Computer Science — University of San Francisco – p.6/
??
Example - Coloring a Bay Area Map
Can we color this mapusing^ only
four^ colors (R,Y,B,G), so that no ad-jacent counties have thesame color?^ Department of Computer Science — University of San Francisco – p.7/
??
Example - Coloring a Bay Area Ma SFSanMateo^
SolanoContraCostaAlameda SantaClara NapaSonomaMarin
Can we color this mapusing^ only
four^ colors (R,Y,B,G), so that no ad-jacent counties have thesame color?^ Department of Computer Science — University of San Fra
Example - Coloring a Bay Area Map Initially, pick a color for SF. (Red) Our successor function will return all possible coloringsthat don’t violate the constraint.^ Marin = Blue, Solano = Red, Alameda = Yellow, Napa= Yellow, etc We can be more clever about how to approach thisproblem^ CSPs are commutative; it doesn’t matter which orderwe assign colors to counties.^ Therefore, we should consider one assignment at atime. For the moment, let’s start by always assigning a colorto the county with the smallest domain.
Department of Computer Science — University of San Francisco – p.9/
??
Example - Coloring a Bay Area Map SF = Red. SF = Red, Marin = Blue SF = Red, Marin = Blue, Alameda = Yellow SF = Red, Marin = Blue, Alameda = Yellow, CC = Green etc. In this case, we can find a consistent four-coloring, What about a 3-coloring?
Department of Computer Science — University of San Francisco – p.10/
??
Example - Australia NorthernTerritory WesternAustraliaSouthAustralia
QueenslandNewSouthWalesVictoriaTasmania
Three-coloring the mapof Australia (Red, Green,Blue) Let’s draw the searchtree Initially, we color Q=Red. What are possible suc-cessors?^ Department of Computer Science — University of San Fran
Backjumping What we’d like to do is identify those variables that arecausing a problem and change them directly. To do this, when we reach a variable for which wecannot find a consistent value, we look for all variablesthat it shares a constraint with. We call these variables the conflict set. We then ’unroll’ our search and undo the mostrecently-set variable in the conflict set.
Department of Computer Science — University of San Francisco – p.18/
??
Example - Australia NorthernTerritory WesternAustraliaSouthAustralia
QueenslandNewSouthWalesVictoriaTasmania
Let’s say our search algorithmdid the following coloring:^ Q: Red, NSW: Green, V:Blue, T: Red There is no consistent color forSA. Backtracking will try all othercolors for T, which cannotpossibly help. The conlict set for SA is{Q,NSW,V}. We^ backjump
and^ undo
V.
Once V: Red, we can color SA.
Department of Computer Science — University of San Francisco – p.19/
??
CSP summary Many interesting real-world problems can be formulatedas CSPs.^ Scheduling, resource allocation, design, etc. CSPs can be solved using DFS^ Successor chooses an unassigned variable andgives it a value Problem structure can be exploited to guide search^ Heurustics, intelligent backtracking.
Department of Computer Science — University of San Fran
Branch-and-Bound Search We can also use heuristics to prune portions of a searchtree. In many problems, we need to search for an optimalsolution in a depth-first manner.^ Laying out components on a chip, finding the fastestschedule, TSP How can we identify regions of the search space thatcan’t possibly lead to an optimal solution?
Department of Computer Science — University of San Francisco – p.21/
??
Applying heuristics If we have an admissible heuristic, we can identifybranches that cannot lead to a solution. Search down the left-most branch of the search tree andfind the first candidate solution. As we search down the remaining branches, we applyour heuristic at each step. If we reach a point where the f-cost of an incompletesolution is higher than the previous best solution, wecan discard the branch (assuming we want a minimalsolution)^ Since h underestimates, the true solution cost will beat least as high as f.^ If f is already worse than what we’ve seen, there’s nopoint in expanding this branch.
Department of Computer Science — University of San Francisco – p.22/
??
Branch-and-Bound Search This approach is used extensively in game-playingsearch to eliminate branches that can’t lead to a winningstrategy. Challenge: Branch-and-bound is most effective whenvery good solutions are found early in the search.^ Why is this? This leads to the development of heuristics for selectingthe order in which nodes are placed on the stack in DFS. Most promising node is at the top.
Department of Computer Science — University of San Fran
Summary Heuristics can be used not only to guide search, butalso to reduce the size of the search space. In backjumping, they can be used to ’re-order’ the tree. In branch-and-bound search, they can be used to prunepieces of the search space. The more knowledge you have about a problem, themore aggressive you can be about solving it.^ Caveats:^ Your knowledge must be correct.^ Your solution will be less applicable if the problemchanges, or when applied to another problem.
Department of Computer Science — University of San Francisco – p.24/
??