Top Banner

of 58

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

Examples for Discrete Constraint ProgrammingBelaid 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

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 dont have the same color.

Map Coloring Problem (MCP)

MCP can be viewed as a graph coloring problem:a b c b

ad 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

g

Coding MCP

OPL studio - Ilog Solver: 54 solutions found

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

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

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

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.

A queen moves vertically, horizontally and diagonally

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|

Coding N-Queens Problem

OPL Studio - ILog Solverint n