Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Examples
Map coloring Problem Cryptarithmetic N-queens problem Magic sequence Magic square Zebra puzzle Uzbekian puzzle A tiny transportation problem Knapsack problem graceful labeling problem
Given a map and given a set of colors, the problem is how to color the map so that the regions sharing a boundary line dont have the same color.
a
d d c
Modeling MCP
MCP can be Modeled as a CSP: A set of variables representing the color of each region The domain of the variables is the set of the colors used. A set of constraints expressing the fact that countries that share the same boundary are not colored with the same color.
Modeling (MCP)
Example - Australia Variables = {a,b,c,d,e,f,g} Domain = {red, blue, green} Constraints: a!=b, a!=c b!=c, b!=d c!=e, c!=f e!=d, e!=f
b d a c e f
Coding MCP
Coding MCP
Coding MCP
ECLiPSe
:- lib(fd). coloured(Countries) :Countries=[A,B,C,D,E,F,G], Countries :: [red,green,blue], ne(A,B), ne(A,C), ne(B,C), ne(B,D), ne(C,E), ne(C,F), ne(E,D), ne(E,F), labeling(Countries).
ne(X,Y) :- X##Y.
Coding MCP
Cryptarithmetic
The problem is to find the digits corresponding to the letters involved in the following puzzle: SEND + MORE MONEY
Cryptarithmetic Modeling
A CSP model for Cryptarithmetic problem: Variables={S,E,N,D,M,O,R,Y} Domain={0,1,2,3,4,5,6,7,8,9} Constraints The variables are all different S!=0, M!=0 3 2 S*10 +E*10 +N*10+D {SEND} + M*103+O*102+R*10+E {MORE}= M*104+O*103+N*102+E*10+Y {MONEY}
Coding Cryptarithmetic
enum letter = {S,E,N,D,M,O,R,Y}; range Digit 0..9; var Digit value[letter]; solve{ alldifferent(value); value[S]<>0; value[M] <>0; value[S]*1000+value[E]*100+value[N]*10+value[D]+ value[M]*1000+value[O]*100+value[R]*10+value[E] = value[M]*10000+value[O]*1000+value[N]*100+value[E]*10+value[Y] };
Coding Cryptarithmetic
Coding Cryptarithmetic
ECLiPSe
:- lib(fd). sendmore(Digits) :Digits = [S,E,N,D,M,O,R,Y], Digits :: [0..9], alldifferent(Digits), S #\= 0, M #\= 0, 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E #= 10000*M + 1000*O + 100*N + 10*E + Y,
labeling(Digits).
Coding Cryptarithmetic
N-Queens Problem
The problem is to put N queens on a board of NxN such that no queen attacks any other queen.
The queens problem can be modeling via the following CSP Variables={Q1,Q2,Q3,Q4,...,QN}. Domain={1,2,3,,N} represents the column in which the variables can be. Constraints
Queens not on the same row: already taken care off by the good modeling of the variables. Queens not on the same column: Qi != Qj Queens not on the same diagonal: |Qi-Qj| != |i-j|
ECLiPSe
:-lib(fd).
nqueens(N, Q):length(Q,N), Q::1..N, alldifferent(Q), ( fromto(Q, [Q1|Cols], Cols, []) do ( foreach(Q2, Cols), param(Q1), count(Dist,1,_) do Q2 - Q1 #\= Dist, Q1 - Q2 #\= Dist ) ), search(Q). search([]). search(Q):deleteff(Var,Q,R), indomain(Var), search(R).
Given a finite integer n, the problem consists of finding a sequence S = (s0,s1,,sn), such that si represents the number of occurrences of i in S. Example: (2, 0, 2, 0) (1,2,1,0)
Modeling MSP
MSP can be modeled by the following CSP variables: s0,s1, ,sn-1 Domain:{0,1,,n} constraints:
Coding MSP
Coding MSP
ECLiPSe
:- lib(fd). :- lib(fd_global). :- lib(fd_search). solve(N, Sequence) :length(Sequence, N), Sequence :: 0..N-1, ( for(I,0,N-1), foreach(Xi, Sequence), foreach(I, Range), param(Sequence) do occurrences(I, Sequence, Xi) ), N #= sum(Sequence), % two redundant constraints N #= Sequence*Range, search(Sequence, 0, first_fail, indomain, complete, []).%search procedure
A magic square of size N is an NxN square filled with the numbers from 1 to N2 such that the sums of each row, each column and the two main diagonals are equal.
Example:
Magic square problem can be viewed as a CSP with the following properties:
Variables: the elements of the matrix representing the square Domain: 1..N*N Constraints:
magic sum = sum of the columns = sum of the rows = sum of the down diagonal = sum of the up diagonal Remove symmetries Redundant constraint:
OPL Studio:
int N << "The length of the square:"; range Dimension 1..N; range Values 1..N*N; int msum = N*(N*N+1)/2; //magic sum var Values square[1..N,1..N];
solve { //The elements of the square are all different alldifferent(square); //the sum of the diagonal is magic sum msum = sum(i in Dimension)square[i,i]; //the sum of the up diagonal is magic sum
ECLiPSe:
:- lib(fd).
magic(N) :Max is N*N, Magicsum is N*(Max+1)//2, dim(Square, [N,N]), Square[1..N,1..N] :: 1..Max, Rows is Square[1..N,1..N], flatten(Rows, Vars), alldifferent(Vars),
Unfortunately, ECLiPSe ran for a long time without providing any answer. ECLiPSe had also the same behavior for a similar code from ECLiPSe website.
How much should be shipped from several sources to several destinations Source
Demand Qty Shipped Destination Qty
Supply cpty
a1 a2
:
am
x1n x12
x11
1 2 : n
b1 b2 : bn
3 plants with known capacities, 4 clients with known demands and transport costs per unit between them.
500
300 400
1
2 3 Find qty shipped?
1 2
3 4
200
400
300
100
Modeling TTP
Coding TTP
Coding TTP
Coding TTP
ECLiPSe
:- lib(eplex_cplex). main1(Cost, Vars) :Vars = [A1, A2, A3, B1, B2, B3, C1, C2, C3, D1, D2, D3], Vars :: 0.0..inf,
A1 + A2 + A3 $= 200, B1 + B2 + B3 $= 400,
C1 + C2 + C3 $= 300, D1 + D2 + D3 $= 100, A1 + B1 + C1 + D1 $=< 500, A2 + B2 + C2 + D2 $=< 300, A3 + B3 + C3 + D3 $=< 400, optimize(min(10*A1 + 7*A2 + 11*A3 + 8*B1 + 5*B2 + 10*B3 + 5*C1 + 5*C2 + 8*C3 +9*D1 + 3*D2 + 7*D3), Cost).
Zebra puzzle
Zebra is a well known puzzle in which five men with different nationalities live in the first five house of a street. They practice five distinct professions, and each of them has a favorite animal and a favorite drink, all of them different. The five houses are painted in different colors. The puzzle is to find who owns Zebra. Zebra puzzle has different statements. Well handle two. This kind of Puzzle is usually treated as an instance of the class of tabular constraint satisfaction problems in which we express the problem using tables. In this presentation we show how to solve it using CSP languages: OPL studio and ECLiPSe.
In crucial step in Modeling Zebra puzzle is finding the decision variables. In our case, we consider: variables: persons, colors, pets, drinks, tobaccos. Domain: 1..5 (representing the five houses) Constraints: Expressed directly from the statement of the puzzle.
enum people {Englishman, Spaniard, Ukrainian, Norwegian, Japanese}; enum drinks {coffee, tea, milk, orange, water}; enum pets {dog, fox, zebra, horse, snails}; enum tabacco {winston, kools, chesterfields, luckyStrike, parliaments }; enum colors {ivory, yellow, red , green, blue}; range houses 1..5;//{first, second, third, fourth, fifth}; var var var var var houses houses houses houses houses hseClr[colors]; hsePple[people]; hsePts[pets]; hseDrks[drinks]; hseTaba[tabacco];
zebra :% we use 5 lists of 5 variables each Nat = [English, Spaniard, Japanese, Italian, Norwegian], Color = [Red, Green, White, Yellow, Blue], Profession = [Painter, Sculptor, Diplomat, Violinist, Doctor], Pet = [Dog, Snails, Fox, Horse, Zebra], Drink = [Tea, Coffee, Milk, Juice, Water], % domains: all the variables range over house numbers 1 to 5 Nat :: 1..5, Color :: 1..5, Profession :: 1..5, Pet :: 1..5, Drink :: 1..5,
% and here follow the actual constraints English = Red, Spaniard = Dog, Japanese = Painter, Italian = Tea, Norwegian = 1, Green = Coffee, Green #= White + 1, Sculptor = Snails, Diplomat = Yellow, Milk = 3, Dist1 #= Norwegian - Blue, Dist1 :: [-1, 1], Violinist = Juice, Dist2 #= Fox - Doctor, Dist2 :: [-1, 1], Dist3 #= Horse - Diplomat, Dist3 :: [-1, 1],
Answer from ECLiPSe after 0.02s The japanese owns the zebra The norwegian drinks water
Uzbekian Puzzle
An uzbekian sales man met five traders who live in five different cities. The five traders are: {Abdulhamid, Kurban,Ruza, Sharaf, Usman} The five cities are : {Bukhara, Fergana, Kokand, Samarkand, Tashkent}
Find the order in which he visited the cities given the following information:
He met Ruza before Sharaf after visiting Samarkand, He reached Fergana after visiting Samarkand followed by other two cities, The third trader he met was Tashkent, Immediately after his visit to Bukhara, he met Abdulhamid He reached Kokand after visiting the city of Kurban followed by other two cities;
The uzbekian puzzle can formulated within the CSP framework as follows: Variables: order in which he visited each city and met each trader Domain:1..5 constraints:
He met Ruza before Sharaf after visiting Samarkand, He reached Fergana after visiting Samarkand followed by other two cities, The third trader he met was Tashkent, Immediately after his visit to Bukhara, he met Abdulhamid He reached Kokand after visiting the city of Kurban followed by other two cities;
Knapsack problem
We have a knapsack with a fixed capacity and a number of items. Each item has a weight and a value. The problem consists of filling the knapsack without exceeding its capacity, while maximizing the overall value of its contents. Knapsack problem is an example of Mixed integer programming.
Variables: For each item, we associate a variable that gives the quantity of such an item we can put in the knapsack. Domain: 0..Capacity of the knapsack Constraints: sum of the weights in the knapsack is less than the capacity Objective function to maximize: sum of the values in the knapsack
maximize sum(i in items) value[i] * take[i] subject to sum(i in items) weight[i] * take[i] <= MaxCapacity;
Given a tree T with m+1 nodes, and m edges, find the possible labels in {0,1,,m} for the nodes such that the absolute value of the difference of the labels of the nodes related to each edge are all different. Formally, let f be a function from V ----> {0,,m}, where V is the set of the vertices of T. f is said to be a graceful labeling of T iff |f(vi)-f(vj)| are all different for any edge vivj in T.
Graceful labeling problem can be formulated into the following CSP: variables: labels to put on each node of T Domain: 0..m constraints: the absolute value of the difference between the labels of any edge are all different
Data file
nbreOfNodes = 7; adjacencyMatrix = [ 1, 0, 1, 0, [ 0, 1, 0, 1, [ 0, 0, 1, 0, [ 0, 0, 0, 1, [ 0, 0, 1, 0, [ 0, 0, 0, 0, [[ 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0], 0 ], 0 ], 0 ], 0 ], 5 1 ], 0 ]]; label[1] = 0 label[2] = 6 label[3] = 1 label[4] = 3 label[5] = 4 label[6] = 5 label[7] = 2
Solution Example: