modeling for cp
play

Modeling for CP Marco Chiarandini Department of Mathematics & - PowerPoint PPT Presentation

DM841 D ISCRETE O PTIMIZATION Modeling for CP Marco Chiarandini Department of Mathematics & Computer Science University of Southern Denmark Constraint Satisfaction Problem Modeling Examples Outline Example: Sudoku 1. Constraint


  1. DM841 D ISCRETE O PTIMIZATION Modeling for CP Marco Chiarandini Department of Mathematics & Computer Science University of Southern Denmark

  2. Constraint Satisfaction Problem Modeling Examples Outline Example: Sudoku 1. Constraint Satisfaction Problem 2. Modeling Examples n-Queens, Grocery, Magic Squares 3. Example: Sudoku 2

  3. Constraint Satisfaction Problem Modeling Examples Resume Example: Sudoku ◮ CP modeling examples ◮ Graph labeling with consecutive numbers ◮ Send More Money ◮ Constraint programming: representation (modeling language) + reasoning (propagation + search) ◮ model ◮ propagate, filtering, pruning ◮ search = backtracking + branching ◮ Gecode: model in Script class implementation ◮ Variables: declare as members initialize in constructor update in copy constructor ◮ Posting constraints (in constructor) ◮ Create branching (in constructor) ◮ Provide copy constructor (recomputation) and copy function (cloning) 3

  4. Constraint Satisfaction Problem Modeling Examples List of Contents Example: Sudoku ◮ Introduction to CP and Gecode ◮ Modeling with Finite Domain Integer Variables ◮ Overview on global constraints ◮ Notions of local consistency ◮ Constraint propagation algorithms ◮ Filtering algorithms for global constraints ◮ Search ◮ Set variables ◮ Symmetries 4

  5. Constraint Satisfaction Problem Modeling Examples Outline Example: Sudoku 1. Constraint Satisfaction Problem 2. Modeling Examples n-Queens, Grocery, Magic Squares 3. Example: Sudoku 5

  6. Constraint Satisfaction Problem Modeling Examples Constraint Programming Example: Sudoku The domain of a variable x , denoted D ( x ) , is a finite set of elements that can be assigned to x . A constraint C on X is a subset of the Cartesian product of the domains of the variables in X, i.e., C ⊆ D ( x 1 ) × · · · × D ( x k ) . A tuple ( d 1 , . . . , d k ) ∈ C is called a solution to C . Equivalently, we say that a solution ( d 1 , ..., d k ) ∈ C is an assignment of the value d i to the variable x i for all 1 ≤ i ≤ k , and that this assignment satisfies C . If C = ∅ , we say that it is inconsistent. Extensional: specifies the good (or bad) tuples (values) Intensional: specifies the characteristic function 6

  7. Constraint Satisfaction Problem Modeling Examples Constraint Programming Example: Sudoku Constraint Satisfaction Problem (CSP) A CSP is a finite set of variables X with domain extension D = D ( x 1 ) × · · · × D ( x n ) , together with a finite set of constraints C , each on a subset of X . A solution to a CSP is an assignment of a value d ∈ D ( x ) to each x ∈ X , such that all constraints are satisfied simultaneously. Constraint Optimization Problem (COP) A COP is a CSP P defined on the variables x 1 , . . . , x n , together with an objective function f : D ( x 1 ) × · · · × D ( x n ) → Q that assigns a value to each assignment of values to the variables. An optimal solution to a minimization (maximization) COP is a solution d to P that minimizes (maximizes) the value of f ( d ) . 7

  8. Constraint Satisfaction Problem Modeling Examples Example: Sudoku Task: ◮ determine whether the CSP/COP is consistent (has a solution): ◮ find one solution ◮ find all solutions ◮ find one optimal solution ◮ find all optimal solutions 8

  9. Constraint Satisfaction Problem Modeling Examples Solving CSPs Example: Sudoku ◮ Systematic search: ◮ choose a variable x i that is not yet assigned ◮ create a choice point, i.e. a set of mutually exclusive & exhaustive choices, e.g. x i = v vs x i � = v ◮ try the first & backtrack to try the other if this fails ◮ Constraint propagation: ◮ add x i = v or x � = v to the set of constraints ◮ re-establish local consistency on each constraint � remove values from the domains of future variables that can no longer be used because of this choice ◮ fail if any future variable has no values left 9

  10. Constraint Satisfaction Problem Modeling Examples Representing a Problem Example: Sudoku ◮ a CSP P = < X , D , C > represents a problem P, if every solution of P corresponds to a solution of P and every solution of P can be derived from at least one solution of P ◮ More than one solution of P can represent the same solution of P or viceversa, if symmetries are present ◮ The variables and values of P represent entities in P ◮ The constraints of P ensure the correspondence between solutions ◮ we must make sure that any solution to P yields exactly one solution to P, and that any solution to P corresponds to a solution to P or is symmetrically equivalent to such a solution, and that if P has no solutions, this is because P itself has no solutions. ◮ The aim is to find a model P that can be solved as quickly as possible (Note that shortest run-time might not mean least search!) 10

  11. Constraint Satisfaction Problem Modeling Examples Interactions with Search Strategy Example: Sudoku Whether a model is better than another can depend on the search algorithm and search heuristics ◮ Let’s assume that the search algorithm is fixed although different level of consistency can also play a role ◮ Let’s also assume that choice points are always x i = v vs x i � = v ◮ Variable (and value) order still interact with the model a lot ◮ Is variable & value ordering part of modelling? In practice it is. but it depends on the modeling language used 11

  12. Constraint Satisfaction Problem Modeling Examples Global Constraint: alldifferent Example: Sudoku Global constraint: set of more elementary constraints that exhibit a special structure when considered together. alldifferent constraint Let x 1 , x 2 , . . . , x n be variables. Then: alldifferent ( x 1 , ..., x n ) = { ( d 1 , ..., d n ) | ∀ i , d i ∈ D ( x i ) , ∀ i � = j , d i � = d j } . Constraint arity: number of variables involved in the constraint Note: different notation and names used in the literature 12

  13. Constraint Satisfaction Problem Modeling Examples Global Constraint Catalog Example: Sudoku http://www.emn.fr/z-info/sdemasse/gccat/sec5.html 13

  14. Constraint Satisfaction Problem Modeling Examples Outline Example: Sudoku 1. Constraint Satisfaction Problem 2. Modeling Examples n-Queens, Grocery, Magic Squares 3. Example: Sudoku 14

  15. Constraint Satisfaction Problem Modeling Examples Outline Example: Sudoku 1. Constraint Satisfaction Problem 2. Modeling Examples n-Queens, Grocery, Magic Squares 3. Example: Sudoku 15

  16. 8-Queens

  17. Problem Statement � � � � � � � � � Place 8 queens on a chess board such that the queens do not attack each other � Straightforward generalizations place an arbitrary number: n Queens � place as closely together as possible � 2010-03-25 63 ID2204-L02, Christian Schulte, ICT, KTH

  18. What Are the Variables? � Representation of position on board � First idea: two variables per queen � one for row � one for column � 2 � n variables � Insight: on each column there will be a queen! 2010-03-25 64 ID2204-L02, Christian Schulte, ICT, KTH

  19. ���������������� � Have a variable for each column � value describes row for queen � n variables � Variables: x 0 ����� x 7 x i � ��������� where 2010-03-25 65 ID2204-L02, Christian Schulte, ICT, KTH

  20. Other Possibilities � For each field: number of queen � ����������������������������������� � n 2 variables � For each field on board: is there a queen on the field? � 8 � 8 variables � variable has value 0: no queen � variable has value 1: queen � n 2 variables 2010-03-25 66 ID2204-L02, Christian Schulte, ICT, KTH

  21. Constraints: No Attack � not in same column � by choice of variables � not in same row � x i �� x j for i �� j � not in same diagonal � x i � i �� x j - j for i �� j � x i � j �� x j - i for i �� j � 3 � n � ( n � 1) constraints 2010-03-25 67 ID2204-L02, Christian Schulte, ICT, KTH

  22. ������������������ � Sufficient by symmetry i < j instead of i �� j � Constraints � x i �� x j for i < j � x i � i �� x j - j for i < j � x i � j �� x j - i for i < j � 3/2 � n � ( n � 1) constraints 2010-03-25 68 ID2204-L02, Christian Schulte, ICT, KTH

  23. Even Fewer Constraints � Not same row constraint x i �� x j for i < j means: values for variables pairwise distinct � Constraints � distinct( x 0 ����� x 7 ) � x i � i �� x j - j for i < j � x i � j �� x j - i for i < j 2010-03-25 69 ID2204-L02, Christian Schulte, ICT, KTH

  24. 12+3(/4%(0%!2$03"$, ! S%+R&#"+,&04#5,*#"&$,*+1/#4*1+&$#*&.%& $#91)/%0&.:&04+14*$1&$,*+1/#4*1+ ! +%%&#++45*;%*1 distinct(x0, x1, ..., x7) distinct(x0-0, x1-1, ..., x7-7) distinct(x0+0, x1+1, ..., x7+7) ())*")+"(, -.(()/"0)(123456'768&29:4$;7%12-3<12=<> I)

  25. Script: Variables Queens(void)):)q(*this,8,0,7)){ � } 2010-03-25 71 ID2204-L02, Christian Schulte, ICT, KTH

  26. Script: Constraints Queens(void)):)q(*this,8,0,7)){) distinct(*this,)q); for)(int i=0;)i<8;)i++) for)(int j=i+1;)j<8;)j++)){ post(*this,)x[i]fi !=)x[j]fj); rel post(*this,)x[i]fj)!=)x[j]fi); } � } 2010-03-25 72 ID2204-L02, Christian Schulte, ICT, KTH

Recommend


More recommend