Beruflich Dokumente
Kultur Dokumente
Constraint Programming
Belaid MOA
UVIC, SHRINC Project
Examples
a
d
d
Modeling MCP
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
g
Coding MCP
enum Country={a,b,c,d,e,f,g};
enum Color = {red, green, blue};
var Color color[Country];
solve{
color[a]<>color[b];color[a]<>color[c];
color[b]<>color[c];color[b]<>color[d];
color[c]<>color[e];color[c]<>color[f];
color[e]<>color[d];color[e]<>color[f];
color[b]<>color[c];color[b]<>color[d];
};
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
Cryptarithmetic Modeling
S!=0, M!=0
S*103+E*102+N*10+D {SEND}
+ M*103+O*102+R*10+E {MORE}=
M*104+O*103+N*102+E*10+Y {MONEY}
Coding Cryptarithmetic
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
Modeling N-Queens
Problem
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).
Example:
(2, 0, 2, 0)
(1,2,1,0)
Modeling MSP
Domain:{0,1,,n}
constraints:
Redundant 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
Example:
Magic square problem can be viewed as a CSP with the following properties:
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),
A Tiny Transportation
problem
Supply
cpty
Source
a1
a2
2
:
m
:
am
Demand
Qty Shipped Destination Qty
x1n x12
x21
x22
x2n
x11
b1
2
:
n
b2
:
bn
A Tiny Transportation
problem
500
200
300
400
300
100
400
Modeling TTP
Demand constraints:
Capacity constraints:
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).
It didnt run!
calling an undefined procedure cplex_prob_init()
Zebra puzzle
houses
houses
houses
houses
houses
hseClr[colors];
hsePple[people];
hsePts[pets];
hseDrks[drinks];
hseTaba[tabacco];
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:
Knapsack problem
Modeling Knapsack
problem
Coding Knapsack Pb in
OPL
range items 1..10;
int MaxCapacity= 1000;
int value[items] = [29,30,25,27, 28,21,23,19,18,17];
int weight[items] =[30,32,33,31,34,40,45,44,39,35];
var int take[items] in 0..MaxCapacity;
maximize
sum(i in items) value[i] * take[i]
subject to
sum(i in items) weight[i] * take[i] <= MaxCapacity;
Data file
nbreOfNodes = 7;
adjacencyMatrix = [[ 0, 1, 0, 0, 0, 0, 0],
[ 1, 0, 1, 0, 0, 0, 0 ],
[ 0, 1, 0, 1, 0, 1, 0 ],
[ 0, 0, 1, 0, 1, 0, 0 ],
[ 0, 0, 0, 1, 0, 0, 0 ],
5
[ 0, 0, 1, 0, 0, 0, 1 ],
[ 0, 0, 0, 0, 0, 1, 0 ]];
label[1] = 0
Solution Example:
label[2] = 6
label[3] = 1
label[4] = 3
label[5] = 4
label[6] = 5
label[7] = 2