Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15...

16
20/10/2013 1 RCP104 Optimisation en Informatique 2013 – 2014 Dr. Nazih OUWAYED [email protected] http://nouwayed.yolasite.com Chapitre 3: Introduction GLPK Installation GLPK (1/2) Octobre 2013 RCP104 – Optimisation en Informatique 2 Téléchargement http://www.gnu.org/software/glpk/#downloading 1 2 3

Transcript of Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15...

Page 1: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

1

RCP104

Optimisation en Informatique

2013 – 2014

Dr. Nazih [email protected]

http://nouwayed.yolasite.com

Chapitre 3: Introduction GLPK

Installation GLPK (1/2)

Octobre 2013RCP104 – Optimisation en Informatique2

� Téléchargement http://www.gnu.org/software/glpk/#downloading

1

2

3

Page 2: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

2

Installation GLPK (2/2)

Octobre 2013RCP104 – Optimisation en Informatique3

� Configuration

� Déplacer le dossier décompresser glpk-4.52 dans C:\

� Configurez-le en suivant les instructions

� \glpk-4.52\w64\readme.txt

GLPK nécessite un compilateur C/C++

Turbo c++ http://turbo-c.soft32.com/

Instructions GLPK (1/4)

Octobre 2013RCP104 – Optimisation en Informatique4

� Définition

� Le « GNU Linear Programming Kit » (GLPK) est un outilperformant pour résoudre des problèmes d'optimisationlinéaire de variables continues ou mixtes (entières etcontinues).

� Ce kit est composé d'un langage de modélisation GNUMathProg et d'une librairie de fonctions C (GLPK) utilisant lesolveur Glpsol.

� GNU MathProg qui est un sous-ensemble du langage demodélisation AMPL

� L'extrême avantage de ce kit est d’être en libre accès etrelativement facile à installer et à utiliser

Page 3: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

3

Instructions GLPK (2/4)

Octobre 2013RCP104 – Optimisation en Informatique5

� La modélisation d'un problème d'optimisation se divise en deux parties :� La section Modèle contient toutes les déclarations, les paramètres

calculables et les défnitions des contraintes et de l'objectif.

� La section Données contient toutes les données fixes (valeurs desparamètres, du contenu des ensembles).

� Il est recommender de séparer (mais on peut mettre modèleet données dans le même fichier .mod) les deux parties endeux fichiers :� Modèle devra être sauvegardée avec l'extension .mod et les

données sont placées entre les instructions data; et end;� Le fichier .mod joue le rôle d'un programme principal en programmation.

� Données devra être sauvegardée avec l'extension avec l'extension.dat

Instructions GLPK (3/4)

Octobre 2013RCP104 – Optimisation en Informatique6

� Compilation : se placer dans

� glpsol --options --model modele.mod --data donnees.dat

� glpsol --help

Page 4: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

4

Instructions GLPK (4/4)

Octobre 2013RCP104 – Optimisation en Informatique7

� Plus de détails

� Doc GLPK – AMPL

� http://gnuwin32.sourceforge.net/packages/glpk.htm

� Cours GLPK – MathProg

� http://catalogue.polytechnique.fr/site.php?id=122&fileid=1811

Déclaration des variables

Octobre 2013RCP104 – Optimisation en Informatique8

� Déclaration des variables � param nom_du_varaiable:=valeur;

� Affichage � display nom_du_variable ;

� Commentaire� display nom_du_variable ; # Commentaire

#Exemple 1: param a := 4;param b := 5;param c:=a*b;# a fois b display c;

#Exemple 2: param a := 4;param b := 5;param c:=sqrt(a*b);# racine carée de a*b display c;

ex1.mod ex2.mod

Page 5: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

5

Fonctions mathématiques (1/3)

Octobre 2013RCP104 – Optimisation en Informatique9

Fonctions mathématiques (2/3)

Octobre 2013RCP104 – Optimisation en Informatique10

� Exemple 3� Calculer le maximum entre deux variables a=sin(10)et b=sin(82)

#Exemple 3: param a := sin(10);param b := sin(82);param c:=max(a,b);# maximum entre a et b display c;

ex3.mod

Page 6: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

6

Fonctions mathématiques (3/3)

Octobre 2013RCP104 – Optimisation en Informatique11

#Exemple 4

param c:=sum{i in 1..5}2*i ;display c;

ex4.mod

Vecteur (1/4)

Octobre 2013RCP104 – Optimisation en Informatique12

� Déclaration� N.B. A ajouter dans le fichier .mod

set I := {1..4};

param givens{I};

� Affectation d’un vecteur� N.B. A ajouter à la fin de fichier .mod ou dans un fichier .dat

data;

param givens:= 1 36, 2 34, 3 38, 4 27;

end;

Page 7: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

7

Vecteur (2/4)

Octobre 2013RCP104 – Optimisation en Informatique13

� Utilisation� N.B. A ajouter dans le fichier .mod

param res:=givens[3];

display res;

Vecteur (3/4)

Octobre 2013RCP104 – Optimisation en Informatique14

� Exemple 5

� Calculer le maximum de givens{I}

#Exemple 5

set I := {1..4};param givens{I};param res:=max{i in I}givens[i];display res;

ex5.mod#Exemple 5

data;

param givens:= 1 36, 2 34, 3 38, 4 27;end;

ex5.dat

Page 8: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

8

Vecteur (4/4)

Octobre 2013RCP104 – Optimisation en Informatique15

� Exemple 6

� Calculer

#Exemple 6

set I := {1..4};param givens{I};param res:=Prod{i in I}givens[i]*2;display res;

ex6.mod#Exemple 6

data;

param givens:= 1 36, 2 34, 3 38, 4 27;end;

ex6.dat

∏=

4

1

2*)(i

igivens

Matrice (1/3)

Octobre 2013RCP104 – Optimisation en Informatique16

� Déclaration� N.B. A ajouter dans le fichier .mod

set I := {1..4}; # nombre de lignes

set J := {1..3}; # nombre de colonnes

param givens{I, J};

� Affectation d’une matrice� N.B. A ajouter à la fin de fichier .mod ou dans un fichier .dat

data;

param givens : 1 2 3 :=

1 12 13 14

2 16 77 88

3 44 55 66

4 13 67 98;

end;

Page 9: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

9

Matrice (2/3)

Octobre 2013RCP104 – Optimisation en Informatique17

� Utilisation� N.B. A ajouter dans le fichier .mod

param res:=givens[3,2];

display res;

Matrice (3/3)

Octobre 2013RCP104 – Optimisation en Informatique18

� Exemple 7

� Calculer ∑∑= =

4

1

3

1

),(i j

jigivens

#Exemple 7

set I := {1..4};set J := {1..3};param givens{I, J};param res:=sum{i in I, j in J}givens[i,j];display res;

ex7.mod #Exemple 7data;param givens : 1 2 3:=

1 12 13 14 2 16 77 883 44 55 664 13 67 98;

end;

ex7.dat

Page 10: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

10

Conditionnelle (1/2)

Octobre 2013RCP104 – Optimisation en Informatique19

� Syntaxe :

� Param x:= If condition then ValeurA else ValeurB;

#Exemple 8

param x:=1;

param y:= if x>10 then 10 else 15;

display y;

ex8.mod

Conditionnelle (2/2)

Octobre 2013RCP104 – Optimisation en Informatique20

� Exemple 9

� Ecrire l’algorithme qui retourne 1 si la vitesse moyenne d’une voiture qui roule à 60, 100 ou 200 km/h est inférieure à la vitesse maximale et 0 dans le cas inverse.

#Exemple 9

set I:= {1..3};param vitesses{I};param maxi:=max{i in I}vitesses[i];param moy:=(sum{i in I}vitesses[i])/3;param res:= if moy<maxi then 1 else 0;display res;

ex9.mod #Exemple 9

data;

param vitesses:= 1 60, 2 100, 3 200;end;

ex9.dat

Page 11: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

11

Opérateurs arithmétiques (1/2)

Octobre 2013RCP104 – Optimisation en Informatique21

Opérateurs arithmétiques (2/2)

Octobre 2013RCP104 – Optimisation en Informatique22

� Exemple 10

� Ecrire l’algorithme pour 2

5

1

2/))1(*( iiii

++∑=

#Exemple 10

param c:=sum{i in 1..5}(i*(i+1))/2+i^2 ;display c;

ex10.mod

Page 12: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

12

Prédicat (1/2)

Octobre 2013RCP104 – Optimisation en Informatique23

� Exemple 11

� Ecrire l’algorithme qui retourne la somme de givens{i,j} lorsque i=j

#Exemple 11

set I := {1..4};set J := {1..3};param givens{I, J};param res:=sum{i in I, j in J : i=j}givens[i,j];display res;

ex11.mod #Exemple 11data;param givens : 1 2 3:=

1 12 13 14 2 16 77 883 44 55 664 13 67 98;

end;

ex11.dat

Prédicat (2/2)

Octobre 2013RCP104 – Optimisation en Informatique24

� Exemple 12

� Ecrire l’algorithme qui retourne la somme de givens{i,j} lorsque i=j et i différent de 2

#Exemple 12

set I := {1..4};set J := {1..3};param givens{I, J};param res:=sum{i in I, j in J : i=j && i!=2}givens[i,j];display res;

ex12.mod #Exemple 12data;param givens : 1 2 3:=

1 12 13 14 2 16 77 883 44 55 664 13 67 98;

end;

ex12dat

Page 13: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

13

Expressions relationnelles (1/2)

Octobre 2013RCP104 – Optimisation en Informatique25

Expressions relationnelles (1/2)

Octobre 2013RCP104 – Optimisation en Informatique26

� Exemple 13

� Soit une matrice A=[12 15 16; 19 18 16]

� Calculer la valeur maximale

#Exemple 13

set I := {1..2};set J := {1..3};param A{I, J};param maxi:=max{i in I, j in J}A[i,j];display maxi;

ex13.mod

#Exemple 13

data;param A: 1 2 3:=

1 12 15 162 19 18 16;

end;

ex13.dat

Page 14: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

14

Boucle (1/2)

Octobre 2013RCP104 – Optimisation en Informatique27

� Syntaxe

� For(i in I){ }

#Exemple 14

Set I:={1..2};for{i in I}

{ printf "*\n";

}

ex14.mod

Boucle (2/2)

Octobre 2013RCP104 – Optimisation en Informatique28

� Exemple 15

� Ecrire l’algorithme qui affiche le résultat suivant

#Exemple 15set I := {1..2};set J := {1..3};param A{I, J};for {i in I}

{ for{j in J}{ printf "A[%s,%s] is %g \n", i, j, A[i,j];

}}

ex15.mod #Exemple 15

data;param A: 1 2 3:=

1 12 15 162 19 18 16;

end;

ex15.dat

Page 15: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

15

Optimisation (1/3)

Octobre 2013RCP104 – Optimisation en Informatique29

A

B

C

ex16.lp

� Modèle

Ou Minimize

Optimisation (2/3)

Octobre 2013RCP104 – Optimisation en Informatique30

� Résolution

glpsol --cpxlp ex16.lp --output ex16.sol

Page 16: Chapitre 3: Introduction GLPKnouwayed.yolasite.com/resources/03- IntroGLPK.pdf · 20/10/2013 15 Optimisation (1/3) 29 RCP104 –Optimisation en Informatique Octobre 2013 A B C ex16.lp

20/10/2013

16

Optimisation (3/3)

Octobre 2013RCP104 – Optimisation en Informatique31

A

B

C

ex16.sol

� Solution

Références

Octobre 2013RCP104 – Optimisation en Informatique32

� Introduction au langage de modélisation GNU MathProg (GLPK) - Romain Apparigliato - GDF-Suez, Direction de la Recherche et de l'Innovation