Top Banner
Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project
58

Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Mar 26, 2015

Download

Documents

Aaron Hensley
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Examples for Discrete Constraint Programming

Belaid MOAUVIC, SHRINC Project

Page 2: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

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

Page 3: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Map Coloring Problem (MCP)

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 don’t have the same color.

Page 4: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Map Coloring Problem (MCP)

MCP can be viewed as a graph coloring problem:

a

bc

d

a

b

cd

Page 5: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

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.

Page 6: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

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

a

b

c

d

f

e

g

Page 7: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding MCP

OPL studio - Ilog Solver: 54 solutions foundenum 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];};

Page 8: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding MCP

Page 9: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

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.

Page 10: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding MCP

Page 11: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Cryptarithmetic

The problem is to find the digits corresponding to the letters involved in the following puzzle:

SEND +

MORE

MONEY

Page 12: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

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 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}

Page 13: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Cryptarithmetic

OPL Studio - ILog Solver

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]};

Page 14: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Cryptarithmetic

Page 15: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

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).

Page 16: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Cryptarithmetic

Page 17: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

N-Queens Problem

The problem is to put N queens on a board of NxN such that no queen attacks any other queen.

A queen moves vertically, horizontally and diagonally

Page 18: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Modeling N-Queens Problem

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|

Page 19: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding N-Queens Problem OPL Studio - ILog Solver

int n << "number of queens:"; range Domain 1..n;var Domain queens[Domain];solve { alldifferent(queens); forall(ordered i,j in Domain) { abs(queens[i]-queens[j])<> abs(i-j) ; };};

Page 20: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding N-Queens Problem

Page 21: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding 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).

Page 22: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding N-Queens Problem

Page 23: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Magic sequence problem

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)

Page 24: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Modeling MSP

MSP can be modeled by the following CSP variables: s0,s1, …,sn-1

Domain:{0,1,…,n} constraints:

number of occurences of i in (s0,s1, …,sn-1) is si. Redundant constraints:

the sum of s0,s1, …,sn-1 is n the sum of i*Si, i in [0..n-1] is n

Page 25: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding MSP OPL - ILog Solver

int n << "Number of Variables:";

range Range 0..n-1;range Domain 0..n;

int value[i in Range ] = i;

var Domain s[Range];solve { distribute(s,value,s); //global constraint s[i]=sum(j in Range)(s[j]=i) sum(i in Range) s[i] = n; //redundant constraint sum(i in Range) s[i]*i = n; //redundant constraint};

Page 26: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

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

Page 27: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Magic square problem

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:

Page 28: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Modeling Magic square pb 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:

magic sum = N(N2+1)/2

Page 29: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Magic square pb

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 sumvar 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

Page 30: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Magic square pb

msum = sum(i in Dimension)square[i,N-i+1]; //the sum of the rows and columns are all magic sum

forall(i in Dimension){ msum = sum(j in Dimension)square[i,j]; msum = sum(k in Dimension)square[k,i]; }; //remove symmetric behavior of the square square[N,1] < square[1,N]; square[1,1] < square[1,N]; square[1,1] < square[N,N]; square[1,1] < square[N,1];};

Page 31: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Magic square problem

Page 32: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Magic square pb

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),

Page 33: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Magic square pb%constraints on rows

( for(I,1,N), foreach(U,UpDiag), foreach(D,DownDiag), param(N,Square,Sum)do Magicsum #= sum(Square[I,1..N]), Magicsum #= sum(Square[1..N,I]), U is Square[I,I], D is Square[I,N+1-I]),%constraints on diagonalsMagicsum #= sum(UpDiag),Magicsum #= sum(DownDiag),

Page 34: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Magic square pb%remove symmetry

Square[1,1] #< Square[1,N],Square[1,1] #< Square[N,N],Square[1,1] #< Square[N,1],Square[1,N] #< Square[N,1],

%searchlabeling(Vars),print(Square).

Unfortunately, ECLiPSe ran for a long time without providing any answer. ECLiPSe had also the same behavior for a similar code from ECLiPSe website.

Page 35: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

A Tiny Transportation problem

How much should be shipped from several sources to several destinations

2222 2222

nnnn

aaaa 1111 bbbb1111 xxxx11111111 1111

aaaa 2222 bbbb 2222

:::: :::: :::: ::::aaaa mmmm bbbb nnnn

xxxx1n1n1n1n xxxx12121212

mmmm

11

Supply Supply cptycpty

Supply Supply cptycpty SourceSourceSourceSource Qty ShippedQty ShippedQty ShippedQty Shipped DestinationDestinationDestinationDestination

Demand Demand Qty Qty

Demand Demand Qty Qty

xxxx22222222xxxx2n2n2n2n

xxxx21212121

Page 36: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

A Tiny Transportation problem

3 plants with known capacities, 4 clients with known demands and transport costs per unit between them.

2222 2222

4444

500500500500 1111 200200200200

3333

11

300300300300

4004004004003333

400400400400

300300300300

100100100100

Find qty shipped?Find qty shipped?Find qty shipped?Find qty shipped?

Page 37: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Modeling TTP

TTP can be modeled by a CSP with optimization as follows: Variables: A1,A2,A3,B1,B2,B3,C1, C2, C3,D1,D2,D3 Domain: 0.0..Inf constraints:

Demand constraints:A1+A2+A3=200; B1+B2+B3=400; C1+C2+C3=300; D1+D2+D3=100; Capacity constraints:A1+B1+C1+D1500; A2+B2+C2+D2300; A3+B3+C3+D3400;

Minimization of the objective function:10*A1 + 7*A2 + 11*A3 + 8*B1 + 5*B2 + 10*B3 +

5*C1 + 5*C2 + 8*C3 + 9*D1 + 3*D2 + 7*D3

Page 38: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding TTP OPL Studio - Cplex Solver

var float+ productA[Range];var float+ productB[Range];var float+ productC[Range]; var float+ productD[Range];minimize 10*productA[1] + 7*productA[2] + 11*productA[3] + 8*productB[1] + 5*productB[2] + 10*productB[3] + 5*productC[1] + 5*productC[2] + 8*productC[3] + 9*productD[1] + 3*productD[2] + 7*productD[3]subject to { productA[1] + productA[2] + productA[3] = 200; productB[1] + productB[2] + productB[3] = 400; productC[1] + productC[2] + productC[3] = 300; productD[1] + productD[2] + productD[3] = 100; productA[1] + productB[1] + productC[1] + productD[1]<=500 productA[2] + productB[2] + productC[2]+productC[2] <= 300; productA[3] + productB[3] + productC[3] + productD[3] <= 400; };

Page 39: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding TTP OPL Studio - Cplex Solver: Solution found

Optimal Solution with Objective Value: 6200.0000productA[1] = 100.0000productA[2] = 0.0000productA[3] = 100.0000

productB[1] = 100.0000productB[2] = 300.0000productB[3] = 0.0000

productC[1] = 300.0000productC[2] = 0.0000productC[3] = 0.0000

productD[1] = 0.0000productD[2] = 100.0000productD[3] = 0.0000

Page 40: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

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 didn’t run! calling an undefined procedure cplex_prob_init(…)

Page 41: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

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. We’ll 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.

Page 42: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Modeling Zebra puzzle 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.

Page 43: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Zebra puzzle OPL Studio - ILog Solverenum 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 houses hseClr[colors];var houses hsePple[people];var houses hsePts[pets];var houses hseDrks[drinks];var houses hseTaba[tabacco];

Page 44: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Zebra puzzlesolve{ hsePple[Englishman] = hseClr[red]; hsePple[Spaniard]=hsePts[dog]; hseDrks[coffee] = hseClr[green]; hsePple[Ukrainian] = hseDrks[tea]; hseClr[green] = hseClr[ivory]+1; hseTaba[winston] = hsePts[snails]; hseTaba[kools]=hseClr[yellow]; hseDrks[milk]=3; hsePple[Norwegian] = 1; hseTaba[chesterfields]=hsePts[fox]+1 \/ hseTaba[chesterfields]=hsePts[fox]-

1; hseTaba[kools] = hsePts[horse]+1 \/ hseTaba[kools] = hsePts[horse]-1 ; hseTaba[ luckyStrike] = hseDrks[orange]; hsePple[ Japanese] = hseTaba[parliaments]; hsePple[Norwegian] = hseClr[blue]+1 \/ hsePple[Norwegian] = hseClr[blue]-1; //slient constraints-Global constraint alldifferent alldifferent(hsePple);//forall(ordered i,j in people) hsePple[i] <> hsePple[j]; alldifferent(hsePts);//forall(ordered i,j in pets) hsePts[i] <> hsePts[j]; alldifferent(hseClr);//forall(ordered i,j in colors) hseClr[i] <> hseClr[j]; alldifferent(hseDrks);//forall(ordered i,j in drinks) hseDrks[i] <> hseDrks[j]; alldifferent(hseTaba);//forall(ordered i,j in tabacco) hseTaba[i] <> hseTaba[j];

};

Page 45: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Zebra puzzle% The Englishman lives in a red house.% The Spaniard owns a dog.% The Japanese is a painter.% The Italian drinks tea.% The Norwegian lives in the first house on the left.% The owner of the green house drinks coffee.% The green house is on the right of the white one.% The sculptor breeds snails.% The diplomat lives in the yellow house.% Milk is drunk in the middle house.% The Norwegian's house is next to the blue one.% The violinist drinks fruit juice.% The fox is in a house next to that of the doctor.% The horse is in a house next to that of the diplomat.% % Who owns a Zebra, and who drinks water?%

Page 46: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Zebra puzzle:- lib(fd).

zebra :-

% we use 5 lists of 5 variables eachNat = [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 5Nat :: 1..5,Color :: 1..5,Profession :: 1..5,Pet :: 1..5,Drink :: 1..5,

Page 47: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Zebra puzzle% the values in each list are exclusive

alldifferent(Nat),

alldifferent(Color),

alldifferent(Profession),

alldifferent(Pet),

alldifferent(Drink),

% 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],

Page 48: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Zebra puzzle% put all the variables in a single list

flatten([Nat, Color, Profession, Pet, Drink], List),

% search: label all variables with valueslabeling(List),

% print the answers: we need to do some decodingNatNames = [English-english, Spaniard-spaniard, Japanese-japanese,

Italian-italian, Norwegian-norwegian],memberchk(Zebra-ZebraNat, NatNames),memberchk(Water-WaterNat, NatNames),printf("The %w owns the zebra%n", [ZebraNat]),printf("The %w drinks water%n", [WaterNat]).

Answer from ECLiPSe after 0.02sThe japanese owns the zebraThe norwegian drinks water

Page 49: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

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;

Page 50: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Modeling Uzbekian Puzzle

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;

Page 51: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Uzbekian Puzzle in OPL

enum cities {Bukhara, Fergana, Kokand, Samarkand, Tashkent};enum traders {Abdulhamid, Kurban, Ruza, Sharaf, Usman};range order 1..5;var order mtTrader[traders];var order vstCty[cities];solve{ mtTrader[Ruza] < mtTrader[Sharaf]; mtTrader[Ruza] > vstCty[Samarkand]; vstCty[Fergana] = vstCty[Samarkand] + 2; vstCty[Tashkent] = 3; mtTrader[Abdulhamid] = vstCty[Bukhara] + 1; vstCty[Kokand] = mtTrader[Kurban] + 2; alldifferent(mtTrader); alldifferent(vstCty);};

Solution [1]

mtTrader[Abdulhamid] = 2mtTrader[Kurban] = 3mtTrader[Ruza] = 4mtTrader[Sharaf] = 5mtTrader[Usman] = 1

vstCty[Bukhara] = 1vstCty[Fergana] = 4vstCty[Kokand] = 5vstCty[Samarkand] = 2vstCty[Tashkent] = 3

Page 52: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

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.

Page 53: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Modeling Knapsack problem

A CSP model for knapsack problem is given by: 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

Page 54: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

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;

integer programming (CPLEX MIP)displaying solution ...

take[1] = 28 take[2] = 5 take[i] = 0, i=3..10

Page 55: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Graceful labeling problem 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.

Page 56: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Modeling Graceful labeling Pb

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

Page 57: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Graceful labeling Pb

OPL program for graceful labeling problemint nbreOfNodes = ...;//set the range of the labels of the nodesrange nodes 0..nbreOfNodes-1; range indices 1..nbreOfNodes; int adjacencyMatrix[indices, indices] = ...;var nodes label[indices];solve { alldifferent(label); forall(ordered i, j in indices){ if(adjacencyMatrix[i,j] <> 0) then forall(ordered k, h in indices){ if(adjacencyMatrix[k,h] <> 0 & not(k=i & h=j)) then abs(label[i]-label[j])<>abs(label[h]-label[k]) endif; } endif; };};

Page 58: Examples for Discrete Constraint Programming Belaid MOA UVIC, SHRINC Project.

Coding Graceful labeling Pb

Data filenbreOfNodes = 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 ], [ 0, 0, 1, 0, 0, 0, 1 ], [ 0, 0, 0, 0, 0, 1, 0 ]];

Solution Example:

label[1] = 0label[2] = 6label[3] = 1label[4] = 3label[5] = 4label[6] = 5label[7] = 2

1

3

75

4 6

2