Project 2 posted - dots.rkt on csserver in /home/hwang/cs430...

36
Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 1 Lecture 13 Project 2 posted - dots.rkt on csserver in /home/hwang/cs430/project2 Questions?

Transcript of Project 2 posted - dots.rkt on csserver in /home/hwang/cs430...

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 1

    Lecture 13

    Project 2 posted - dots.rkt on csserver in /home/hwang/cs430/project2

    Questions?

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 2

    Outline

    Project 2 - Dots Game Chapter 6 - Constraint Satisfaction Problems

    Defining CSPs Constraint Propagation: Inference in CSPs

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 3

    Project 2 - Dots Game

    2 player, turn-based game Data structures:

    dotbox – struct containing name (symbol), top, bottom, left, right (edge flags)

    boxarray – 2D array of dotboxes, note that neighboring boxes share edges

    arrays are defined in the SRFI 25 library, operations include

    (array-ref array row col) (array-end array dim) (array-set! array row col elem)

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 4

    Project 2 - Dots Game

    Playing field functions (init-dots n) – create n x n grid (print-dots boxarray) – prints out grid, (0, 0) is upper

    left (draw-edge boxarray player row col edge) – "draw"

    edge of box at (row,col), wrapper function (check-complete player box) returns true if all edges

    of box are drawn (count-boxes boxarray label) – returns number of

    completed boxes with label as name

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 5

    Project 2 - Dots Game

    Game controller (playgame n depth p1-fun player1 p2-fun player2

    p1-init p2-init) – plays game given the search functions and names

    (next-player current free-turn players) – if free-turn is true returns current else returns the other player

    (human-play player boxarray depth) – interactive "search" function

    (random-play plater boxarray depth) – randomly picks available edge

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 6

    Project 2 - Dots Game

    Utility functions (copy-dots-array array) – returns a copy of a

    boxarray (copy-box) – returns a copy of a single box

    Game play examples (playgame 3 0 human-play 'a human-play 'b #f #f) (playgame 3 0 random-play 'a random-play 'b #f #f) (playgame 3 0 human-play 'a random-play 'b #f #f) (playgame 3 0 random-play 'a human-play 'b #f #f)

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 7

    Project 2 - Dots Game

    Project is to write computer player that uses minimax search with alpha-beta pruning and cutoff to determine next move. Will need: move generator – generates legal moves for a state evaluation function – ideally this function gives

    higher values to moves more likely to lead to wins Player function basically starts the alpha-beta

    search as described in textbook. Tricky part is that there may be more than one move per turn.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 8

    Project 2 - Dots Game

    For a chance at extra credit points, can enter the Dots tournament. Code due Friday before due date so instructor can set up the tournament.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 9

    Constraint Satisfaction Problems

    So far, we have looked at problem-solving as searching a space of states that are evaluated by domain-specific heuristics and tested to see if they are goal states. From POV of the search algorithm, each state is atomic, a black box without internal structure.

    Wide variety of problems can be solved more efficiently using a factored representation for each state: a set of variables, each of which has a value.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 10

    Constraint Satisfaction Problems

    A problem is solved when each variable has a value that satisfies all the constraints on the variable. Called a constraint satisfaction problems (CSP).

    CSP search algorithms use the structure of states to produce general purpose heuristics rather than problem specific heuristics. These eliminate large portions of search space by identifying variable/value combinations that violate the constraints.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 11

    Definition of CSPs

    CSP has three components: X, a set of variables, { X

    1, … , X

    n }

    D, a set of domains, { D1, … , D

    n }, one for each

    variable C, a set of constraints that specify allowable

    combinations of values

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 12

    Definition of CSPs

    Each domain Di consists of a set of allowable value, { v

    1, … , v

    k } for variable X

    i

    Each constraint Ci consists of a pair

    < scope, rel >, where scope is a tuple of variables that participate in the constraint and rel is a relation that defines the values those variables can take on.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 13

    Definition of CSPs

    To solve a CSP, a state is defined by an assignment of values to some or all of the variables, { X

    i = v

    i,

    Xj = v

    j, … }

    An assignment that does not violate any constraints is a consistent or legal assignment.

    A partial assignment assigns values to only some of the variables. A complete assignment is one where every variable is assigned.

    A solution to a CSP is a consistent, complete assignment.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 14

    Example: Map Coloring

    Consider map of Australia. Want to color each region either red, green, or blue so that no neighboring regions have the same color. Formulate problem as a CSP.

    X = { WA, NT, Q, NSW, V, SA, T } D

    i = { red, green, blue } for all X

    i

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 15

    Example: Map Coloring

    The constraints require neighboring regions to have distinct colors. There are nine border regions, so there are nine constraints:

    C = { SA WA, SA NT, SA Q, SA NSW, SA V, WA NT, NT Q, Q NSW, NSW V }

    Note: SA WA, etc. is a shortcut for < (SA, WA), SA WA >, where SA WA can be enumerated as { (red, green), (red, blue), (green, red), (green, blue), (blue, red), (blue, green) }

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 16

    Example: Map Coloring

    A CSP can be visualized as a constraint graph. Nodes are the variables, and edges connect any two variables that participate in a constraint.

    There are many possible solutions such as

    { WA = red, NT = green, Q = red, NSW = green, V = red, SA = blue, T = red }

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 17

    Why Use a CSP Formulation?

    Why might a CSP formulation be better than regular state-space search? CSP is a natural representation for a wide variety of

    problems. If you already have a CSP-solving system, it is faster to use than a custom solution.

    CSP solvers can be faster than state-space searchers by quickly eliminating large swatches of search space. E.g., if choose { SA = blue }, can conclude that none of the five neighbors will have value of blue. Reduces possible assignments of neighbors from 35 = 243 to 25 = 32.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 18

    Why Use a CSP Formulation?

    Also, can determine when partial assignments will not become a solution, and why the assignment is not a solution, so can concentrate on the variables that matter.

    Results in many problems that are intractable in regular state-space search can be solved quickly as CSPs.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 19

    Example: 8-Queens

    Consider 8-Queens problem. Formulate as a CSP. X = ??? D = ??? C = ???

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 20

    Variations on CSP Formalism

    Simplest CSPs have discrete, finite domains. E.g. map coloring, 8-queens. Discrete domains can be infinite, e.g. set of integers or strings. With these cannot enumerate all possible combinations of values, so need to use a constraint language.

    E.g., for a job scheduling problem, might have T

    1 + d

    1

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 21

    Variations on CSP Formalism

    CSPs with continuous domains are common in the real world and are studied in operations research. E.g., scheduling experiments on Hubble Telescope requires meeting astronomical, precedence, and power constraints.

    Best-known category of continuous-domain CSPs is linear programming problems, where all constraints are linear equalities or inequalities.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 22

    Variations on CSP Formalism

    Can also look at the types of constraints. Unary constraints restrict value of a single variable. Binary constraints relate two variables.

    Higher-order constraints are possible. E.g. Between (X, Y, Z) that specifies that Y is between X and Z.

    Constraint with arbitrary number of variables is a global constraint. One of the most common is AllDiff.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 23

    Variations on CSP Formalism

    In addition to absolute constraints, where violations rule out a potential solution, many real-world CSPs also have preference constraints indicating which solutions are preferred.

    E.g., in university class-scheduling problem, absolute constraint that no professor can teach two classes at the same time. But might allow preference constraints like Dr. Blandford prefers early morning classes, Dr. Hwang prefers afternoon classes, etc.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 24

    Variations on CSP Formalism

    Preference constraints can be encoded as costs on individual variable assignments that are part of the overall objective function. The problem becomes a constraint optimization problem (COP) that can be solved using optimization search methods, either path-based or local.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 25

    Constraint Propagation

    In solving CSPs, at each step there is a choice: algorithm can search (choose a new variable assignment from several possibilities) or do a specific type of inference called constraint propagation.

    Constraints are used to reduce the number of legal values for a variable, which in turn can reduce the legal values for another variable, etc. Can be done as a preprocessing step, and sometimes can solve the whole problem.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 26

    Node Consistency

    Key idea is local consistency. Treat each variable a node in a graph and each binary constraint as an arc. The process of enforcing local consistency in each part of the graph causes inconsistent values to be eliminated throughout the graph.

    A single variable is node-consistent if all the values in the variable's domain satisfy the variable's unary constraints. Make it node consistent by eliminating values from domain.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 27

    Arc Consistency

    A variable is arc-consistent if every value in its domain satisfies the variable's binary constraints. Formally, X

    i is arc-consistent with

    Xj if for every value in current domain D

    i there is

    some value in domain Dj that satisfies the

    binary constraint on the arc (Xi, X

    j).

    E.g., Y = X2 where domain of both X and Y is the set of digits { 0, … , 9 }. Constraint is

    < (X, Y), { (0,0), (1,1), (2,4), (3,9) } >

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 28

    Arc Consistency

    To make X arc-consistent with respect to Y, reduce X's domain to { 0, 1, 2, 3 }. If also make Y arc-consistent with respect to X, reduce Y's domain to { 0, 1, 4, 9 }. An entire CSP is arc-consistent if all of its variables are arc-consistent.

    Most popular algorithm is called AC-3.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 29

    AC-3 Algorithm

    Receives: cspReturns: false if there is an inconsistency; true otherwiseLocals: queue – "queue" of arcs ((X

    i, X

    j) pairs) to be

    checked, initially all arcs in the CSP

    1. While queue is not empty do 1.1 (X

    i, X

    j) = Remove-First(queue)

    1.2 If Revise (csp, Xi, X

    j) then

    1.2.1 If size of Di = 0 then return false

    1.2.2 For each Xk in X

    i.Neighbors - { X

    j } do

    1.2.2.1 Add (Xk, X

    i) to queue // changes in D

    i can affect D

    k

    2. Return true

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 30

    Revise Algorithm

    Receives: csp, Xi, X

    j

    Returns: true iff the domain of Xi is revised

    1. revised = false2. For each x in Di do 2.1 If no value y in Dj allows (x, y) to satisfy the constraint between Xi and Xj then 2.1.1 Delete x from Di 2.1.2 revised = true3. Return revised

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 31

    Complexity of AC-3

    Result is CSP with same solution as original, but with smaller domains, so generally faster to solve.

    Assume CSP with n variables, each with domain size d, and with c binary constraints (arcs). Each arc can be inserted in the queue maximum of d times (once for each possible value).

    Checking consistency of an arc is O(d2), so get a total of O(cd3).

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 32

    Path Consistency

    Path consistency looks as triples of variables. A two-variable set { X

    i, X

    j } is path-consistent

    with respect to a third variable Xm if for every

    assignment { Xi=a, X

    j=b } consistent with

    constraints on { Xi, X

    j }, there exists an

    assignment to Xm that satisfies constraints on

    { Xi, X

    m } and { X

    m, X

    j }

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 33

    K-consistency

    Generalize notion of 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. Node consistency is 1-consistency Arc consistency is 2-consistency Path consistency is 3-consistency, for binary

    constraints Unfortunately, exponential in time and space.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 34

    Global Constraints

    Some global constraints are common enough to be handled by special-purpose algorithms.

    E.g. For AllDiff, if m variables are involved in the constraint, and if they have n possible distinct values altogether, and m > n, then the constraint cannot be satisfied.

    Algorithm: Remove any variable in constraint with a singleton domain and delete that variable's value from the domains of the remaining variables. Repeat as needed.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 35

    Global Constraints

    Consider assignment { WA = red, NSW = red }. Note SA, NT, Q are effectively connected with AllDiff

    After running AC-3, domain of each variable reduced to { green, blue }, but need three colors for SA, NT, Q, so assignment is inconsistent.

  • Tuesday, February 21 CS 430 Artificial Intelligence - Lecture 13 36

    Global Constraints

    Other common global constraints include Resource constraint: Atmost (N, X

    1, … X

    k),

    inconsistency if sum of the minimum domain values is greater than N.

    Bounds constraint: Di = [ lb

    i, ub

    i ], D

    j = [ lb

    j, ub

    j ],

    handled by bounds propagation.

    Lecture 13OutlineProject 2 - Dots Game 1Project 2 - Dots Game 2Project 2 - Dots Game 3Project 2 - Dots Game 4Project 2 - Dots Game 5Project 2 - Dots Game 6Constraint Satisfaction Problems 1Constraint Satisfaction Problems 2Definition of CSPs 1Definition of CSPs 2Definition of CSPs 3Example: Map Coloring 1Example: Map Coloring 2Example: Map Coloring 3Why Use a CSP Formulation? 1Why Use a CSP Formulation? 2Example: 8-QueensVariations on CSP Formalism 1Variations on CSP Formalism 2Variations on CSP Formalism 3Variations on CSP Formalism 4Variations on CSP Formalism 5Constraint PropagationNode ConsistencyArc Consistency 1Arc Consistency 2AC-3 AlgorithmRevise AlgorithmComplexity of AC-3Path ConsistencyK-consistencyGlobal Constraints 1Global Constraints 2Global Constraints 3