An Introduction to Constraint Programming in JChoco
Constraint satisfaction Constraint satisfaction problem (CSP) is a triple where: –V is set of variables –Each X in V has set of values, D_X Usually assume finite domain {true,false}, {red,blue,green}, [0,10], … –C is set of constraints Goal: find assignment of values to variables to satisfy all the constraints
Di = {1,2,3,4,5} V1 V3 V2 V4 V4 V3 V1 V4 1 V4 + V2 = 5 V1 V2 V2 V3 6 AR33 figure 18, page 35 What can you infer?
Di = {1,2,3,4,5} V1 V3 V2 V4 V4 V3 V1 V4 1 V4 + V2 = 5 V1 V2 V2 V3 6 D1 = {1,2} D2 = {2,3} D3 = {4,5} D4 = {2,3} Do you agree? Was that easy?
A program that models csp6
With print statements and search
Packages used
Can throw an exception
Create a problem
Create variables
Post constraints
Establish AC
Magic code to get all solutions
Code to count solutions
output/trace Problem with no V or C
variables
Variables and constraints
Made AC
All solutions
2 colour K 3 The difference allDifferent makes Just how weak/powerful is AC processing?
Prop0.java Run it Make it AC
allDifferent Prop1.java Run it
X + Y + Z = 20 X,Y,Z {1..10} See Test.java How many solutions? AF2 assessed exercise One of 5 questions
Test.java Count number of solutions
As a decision problem: is there a glomb ruler with n ticks with length no more than m units?
How many n digit numbers are there where the number must contain at least 0ne number 3 and at least one number 5? n = … See ThreesAndFives.java AF2 assessed exercise
Magic square An example of how to represent a problem An idea from Chris Beck
put a number in each square each number is different a number is in the range 1 to 16 the sum of a column is the same as a sum of a row the same as the sum of a main diagonal
put a number in each square each number is different a number is in the range 1 to 16 the sum of a column is the same as a sum of a row the same as the sum of a main diagonal 1st stab Use sum(x) = sum(y) where x and y are - different rows - different columns - different diagonals Every element of the array is different - represent as a clique of not equals How does it go? For propagation and search?
put a number in each square each number is different a number is in the range 1 to 16 the sum of a column is the same as a sum of a row the same as the sum of a main diagonal 2nd stab But what is k? How does model perform?
Magic square on the web
Thanks to Chris Beck
Thanks to Bouygues