Professional Documents
Culture Documents
contraintes
Table des matières
Généralités.
Modélisation avec la programmation par contraintes.
Stratégie de recherche dans la programmation par
contraintes.
Programmation par contraintes en utilisant COMET
Généralités
Deux contributions importantes de la programmation
par contraintes.
Une approche appliquée aux problèmes combinatoires
Complémentaire aux autres techniques classiques
de la recherche opérationnelle.
Faisabilité vs optimalité.
- Un langage d'optimisation combinatoire.
Langage riche (raisonnement par block).
Langage de stratégie de recherche par procédure.
La Programmation Par contraintes (PPC) combine un
raisonnement par contraintes et une stratégie de
recherche.
Généralités
PPC = Réduction de domaines + Recherche
Questions fondamentales.
● Comment assurer la réduction des domaines ?
Réduction de domaines.
● On représente explicitement le domaine de recherche
de chaque variable.
● On utilise les contraintes pour la réduction de
domaines.
Contrainte
Domain store
Généralités
0 1 2 3 4
? ? ? ? ?
Exemple : Série Magique
0 1 2 3 4
2 1 2 0 0
Exemple : Série Magique
int n = 5;
range D = 0..n-1;
var<CP>{int} s[D](cp,D);
solve<cp> {
Reification
forall(k in D)
cp.post(s[k] == sum(i in D) (s[i]==k));
}
Permettre l'utilisation d'une contrainte dans une autre
contrainte.
Remplacer une contrainte par une variable 0/1représentant si
la contrainte est vraie ou non.
Exemple : Mariage Stable
Exemple : Mariage Stable
Éléments de contraintes :
Possibilité d'indexer un vecteur ou une matrice par une va-
riable de décision
Contraintes logiques :
Possibilité d'utiliser une combinaison de contraintes lo-
giques.
Deux types de contraintes :
Si John est marié à Jane, alors Jane doit être mariée à
John.
Le mari de la femme de George est George.
Exemple : Mariage Stable
explore<cp> {
forall(i in Men)
cp.post(husband[wife[i]] == i);
forall(i in Women)
cp.post(wife[husband[i]] == i);
•
Y : variable 0 1 2 3 4 5
[0] [1] [2] [3] [4] [5]
•
C : vecteur 3 4 5 5 4 3
•
Contrainte: X = C[Y]
•
X≠3
•
Y≠1&Y≠4
Élément de Contrainte
Facility
location: Le client c est affecté à un entrepôt i seule-
ment si l'entrepôt i est ouvert. (open[i]=1 if warehouse i is
open)
X<Y Y<4
X ∈ {1,3,4,5} Y ∈ {3,4}
Application du Fixpoint
Constraints
Constraint Store
X<Y Y<4
X ∈ {1,3,4,5} Y ∈ {3,4}
Application du Fixpoint
Constraints
Constraint Store
X<Y Y<4
X ∈ {1,3} Y ∈ {3,4}
Application du Fixpoint
Constraints
Constraint Store
X<Y Y<4
X ∈ {1,3} Y ∈ {3}
Application du Fixpoint
Constraints
Constraint Store
X<Y Y<4
X ∈ {1,3} Y ∈ {3}
Application du Fixpoint
Constraints Constraint Store
X<Y Y<4
X ∈ {1} Y ∈ {3}
Propagation de contraintes
Domain-consistency
couleurs différentes.
● Le but est de minimiser le nombre utilisé de
couleurs.
Coloriage de carte
Objective
enum Countries = Function
{Belgium,Denmark,France,Germany,Netherlands,Luxembourg};
var<CP> color[Countries](cp,1..4);
minimize<cp>
max(c in Countries) color[c]
subject to {
cp.post(color[France] != color[Belgium]);
cp.post(color[France] != color[Luxembourg]);
cp.post(color[France] != color[Germany]);
cp.post(color[Luxembourg] != color[Germany]);
cp.post(color[Luxembourg] != color[Belgium]);
…
}
Exemple : Coloriage
B
el Germ
L
u any
x
Franc
e
Exemple : coloriage
Denmark
Nederla
nd
Belgiu
m Germa
Lu
x
ny
France
Exemple : Coloriage
Denmark
Nederland
Belgium
Germany
Lux
France
Exemple : coloriage
Denmark
Nederland
Belgium
Germany
Lux
France
Exemple : coloriage
Denmark
Nederland
Belgium
Germany
Lux
France
Exemple : coloriage
Denmark
Nederland
Belgium
Germany
Lux
France
Exemple : coloriage
Denmark
Nederland
Belgium
Germany
Lux
France
Modélisation avec la PPC
Contraintes globales
Contraintes redondantes
Symétrie
Sudoku
Sudoko
combinatorial array
range R = 1..9; constraint comprehension
var<CP>{int} S[R,R](cp,R);
explore<cp> {
// constraints for fixed positions
forall(i in R)
cp.post(alldifferent(all(j in R) S[i,j]),onDomains);
forall(j in R)
cp.post(alldifferent(all(i in R) S[i,j]),onDomains);
forall(i in 0..2,j in 0..2)
cp.post(alldifferent(all(r in i*3+1..i*3+3,
c in j*3+1..j*3+3)S[r,c]),
onDomains);
}
Contraintes globales
alldifferent(x,y,z)
states that x, y, and z take on different values.
So x=2, y=1, z=3 would be ok, but not x=1, y=3, z=1.
•
Very useful in many resource allocation, time tabling,
sport scheduling problems
Alldifferent Contrainte Globale
Alldifferent: Feasibility
Faisabilité ? Étant donné les domaines des
variables
Création domaine/variable (graphe bipartie)
Trouver un couplage maximum
x1
1
x2 2
x3 3
x4 4
x5 5
Alldifferent : Pruning
x4 4
5
x5
Contraintes Globales
Applications :
Utiliser le cheval pour visiter tous les cases d'un échiquier
exactement une seule.
Utiliser aussi dans la modélisation du TSP (problème du
voyageur de commerce).
Utiliser aussi dans la modélisation de toutes les variantes
du VRP.
Euler Knight
range Chessboard = 1..64;
var<CP>{int} jump[i in Chessboard](cp,Knightmoves(i));
solve<cp>
cp.post(circuit(jump));
Solver<CP> cp();
var<CP>{int} x[Items](cp,Bins);
var<CP>{int} load[Bins](cp,0..capa);
solve<cp>{
forall(b in Bins){
cp.post(load[b] == sum(i in Items) (x[i]==b)*weight[i]);
}
}using{
label(x);
}
cout << cp.getNFail() << endl;
2210 Failures
Bin Packing Problem
Constaints Domain
Domain store
store
Redundant Constraint.
Improves
communication
Contrainte Redondante
Rôle 1
Exprimer une propriété de la solution.
Booster la propagation des autres contraintes.
Rôle 2
Offrir une vue globale sur le problème.
Combiner les contraintes existantes.
Améliorer la communication.
Rôle 3
Représenter une conséquence des contraintes existantes.
Bin Packing : Contrainte Globale
Domain store
Global
Constraint
Symétrie
int n = 14;
range Periods = 1..n/2;
range Teams = 1..n;
range Weeks = 1..n-1;
range EWeeks = 1..n;
enum Location = {home,away};
range Games = 1..(n/2)*n-1;
tuple triple {int a1; int a2; int a3; }
set{triple} Triples();
forall(i in 1..n,j in 1..n: i < j)
Triples.insert(triple(i,j,(i-1)*n + j));
Table<CP> t( all(e in Triples) e.a1,
all(e in Triples) e.a2,
all(e in Triples) e.a3);
Sport Scheduling Problem
var<CP>{int} team[Periods,EWeeks,Location](m,Teams);
var<CP>{int} game[Periods,Weeks](cp,1..n^2);
explore<cp>{
forall(w in EWeeks)
cp.post(alldifferent(all(p in Periods,l in Location) team[p,w,l]));
forall(p in Periods)
cp.post(exactly(all(i in Teams)2,all(w in EWeeks,l in Location) team[p,w,l]));
cp.post(alldifferent(all(p in Periods,w in Weeks) game[p,w]));
forall(p in Periods,w in Weeks)
cp.post(table(team[p,w,home],team[p,w,away],game[p,w],t));
forall(p in Periods, w in Weeks)
cp.post(team[p,w,home] < team[p,w,away]);
}
La stratégie de recherche en PPC
Branching on V
Real search space
Heuristiques de recherche
A
Branchement
Variable/Value (labeling)
Deux étapes :
Choix de la variable dont on doit assigner une valeur.
Choix de la valeur à assigner.
Ordre dynamique
Choix de la prochaine variable dynamiquement.
Choix de la prochaine valeur dynamiquement.
Variable/Value (labeling)
Choix de la variable
Often the most constrained variable (first fail : smallest
domain for queen problem, largest item in Bin Packing)
Choix de la valeur
Often a value that decreases the solution space the least
forall(c in C: !row[c].bound())
by (row[c].getSize())
tryall<cp>(r in R: row[c].memberOf(r))
by (col[r].getSize())
cp.post(row[c] == r);
}
Domain Splitting
Motivation
Dans certains cas la stratégie du labeling peut engendrer
des erreurs et par conséquence des temps de calcul
très élevés.
Domain Splitting
Consiste à partitionner le domaine d'une variable.
The Magic Square Problem
Types de données
Nombres entiers
Nombres rationnels et réels
Booléens
Chaîne de caractère (Strings)
….
Introduction à Comet
Contrôle
Instructions de contrôle
If else
Switch
For (int i = 2 ; i < 5 ; i++)
Do while
While
forall
Introduction à Comet
Sélection
Select : selects a random element.
SelectMin : selects minimum element according to an eva-
luation function.
SelecMax : selects maximum element according to an eva-
luation function.
SelectPr : selects an element according to a probability
distribution.
SelectFirst : selects the lexicographically smallest element.
SelectCircular : selector with a state ; successive execu-
tions consider elements in a circular way.
Introduction à Comet
Function
Function int factorial(int n)
{ if(n == 1) return 1 ;
Else
Returne factorial(n-1)*n ; }
Introduction à Comet
Structure
Tuple pair{int a ; int b ;}
Pair p(2,3) ;
Cout << p << endl ;
Cout << p.b << endl ;
pair<a=2,b=3>
3
Introduction à Comet
Solveurs de Comet
Un solveur CP (programmation par contraintes)
Un solveur LP (programmation linéaire)
Un solveur MIP (programmation linéaire en
nombres entiers)
Un solveur LS (recherche locale basée sur les
contraintes)
Introduction à Comet