Presentation is loading. Please wait.

Presentation is loading. Please wait.

G53CLP Constraint Logic Programming Modeling CSPs – Case Study II Dr Rong Qu.

Similar presentations


Presentation on theme: "G53CLP Constraint Logic Programming Modeling CSPs – Case Study II Dr Rong Qu."— Presentation transcript:

1 G53CLP Constraint Logic Programming Modeling CSPs – Case Study II Dr Rong Qu

2 2 Solving Sudoku Variables pos i,j : number placed at position (i,j) Domain pos i,j = {1, …, 9} 652 39 761 634 471 598 416 38 245 G53CLP – Constraint Logic ProgrammingDr R. Qu

3 3 Solving Sudoku Constraints {1, …, 9} on each row {1, …, 9} on each column {1, …, 9} in each 3X3 square 652 39 761 634 471 598 416 38 245 G53CLP – Constraint Logic ProgrammingDr R. Qu

4 4 Solving Sudoku Constraints 1. All different each row 2. All different each column 3. All different each 3X3 square How to model constraint 3? 652 39 761 634 471 598 416 38 245 G53CLP – Constraint Logic ProgrammingDr R. Qu

5 5 Solving Sudoku Constraints All different pos[i,j] = {1, …, 9} i = {1, 2, 3}; {4, 5, 6} or {7, 8, 9} j = {1, 2, 3}; {4, 5, 6} or {7, 8, 9} 652 39 761 634 471 598 416 38 245 G53CLP – Constraint Logic ProgrammingDr R. Qu

6 Solving Sudoku – Demo

7 7 Data File & Project in OPL Input data in OPL Project in OPL Add/insert model file.mod Add/insert data file.dat Data type declared in model file.mod will look up the data file.dat G53CLP – Constraint Logic ProgrammingDr R. Qu

8 8 Solving Sudoku in OPL //.dat file input= [[0 6 0 0 5 0 0 2 0] [0 0 0 3 0 0 0 9 0] [7 0 0 6 0 0 0 1 0] [0 0 6 0 3 0 4 0 0] [0 0 4 0 7 0 1 0 0] [0 0 5 0 9 0 8 0 0] [0 4 0 0 0 1 0 0 6] [0 3 0 0 0 8 0 0 0] [0 2 0 0 4 0 0 5 0]]; 652 39 761 634 471 598 416 38 245 G53CLP – Constraint Logic ProgrammingDr R. Qu

9 9 Solving Sudoku in OPL //.mod file range dim1 1..9; range dim2 1..9; range bindex 0..2; int input[dim1,dim2] =...;//read in the.dat {int} setnum[f in bindex] = {x | x in dim1: x 3*f }; var int pos[dim1,dim2] in 1..9; 652 39 761 634 471 598 416 38 245 G53CLP – Constraint Logic ProgrammingDr R. Qu

10 10 Solving Sudoku in OPL solve{ forall(i in 1..9) forall(j in dim2 : input[i,j] <> 0) pos[i,j] = input[i,j] ; … }; G53CLP – Constraint Logic ProgrammingDr R. Qu

11 11 Solving Sudoku in OPL solve{ … forall(j in dim2) alldifferent(all(i in dim1) pos[i,j]); forall(i in dim1) alldifferent(all(j in dim2) pos[i,j]); … }; G53CLP – Constraint Logic ProgrammingDr R. Qu

12 12 Solving Sudoku in OPL solve{ … … forall(b1 in bindex){ forall(b2 in bindex){ alldifferent(all(i in setnum[b1], j in setnum[b2]) pos[i,j]); } } }; G53CLP – Constraint Logic ProgrammingDr R. Qu

13 13 Exercise – Coins in Bank You have a bag of coins to save in your bank Weights of coins If the bag of coins weighs 131g, how much in total? How about 4g? How many possible solutions? How can you solve this problem? Lab session next week £150p20p10p5p2p1p Weight(g)10865321 G53CLP – Constraint Logic ProgrammingDr R. Qu


Download ppt "G53CLP Constraint Logic Programming Modeling CSPs – Case Study II Dr Rong Qu."

Similar presentations


Ads by Google