GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I...

42
GAMS Stefan Vigerske [email protected] October 2nd, 2015, CO@Work, Berlin

Transcript of GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I...

Page 1: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

GAMS

Stefan [email protected]

October 2nd, 2015, CO@Work, Berlin

Page 2: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Prologue

Material to this lecture: http://co-at-work.zib.de/files/gams/

I CO@Work virtual machines: GAMS is installed (run gams)I Download GAMS system: http://www.gams.com/downloadI Evaluation license (valid until 7.1.2016):

http://co-at-work.zib.de/files/gams/gamslice.txt(already installed on 32bit VM)

Next 85 minutes:I A short overview on GAMS.I GAMS syntax and programming. (gamsbasics.pdf)I Equitable coach problem in GAMS.I Exercise: Run and modify equitable coach problem.I “Hiding GAMS”: Calling GAMS from Python.I Exercise: Cutting Stock by Column Generation in Python.

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 2 / 33

Page 3: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Prologue

Material to this lecture: http://co-at-work.zib.de/files/gams/

I CO@Work virtual machines: GAMS is installed (run gams)I Download GAMS system: http://www.gams.com/downloadI Evaluation license (valid until 7.1.2016):

http://co-at-work.zib.de/files/gams/gamslice.txt(already installed on 32bit VM)

Next 85 minutes:I A short overview on GAMS.I GAMS syntax and programming. (gamsbasics.pdf)I Equitable coach problem in GAMS.I Exercise: Run and modify equitable coach problem.I “Hiding GAMS”: Calling GAMS from Python.I Exercise: Cutting Stock by Column Generation in Python.

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 2 / 33

Page 4: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Company

I Roots: World Bank, 1976I Went commercial in 1987

I LocationsI GAMS Development Corporation (Washington, D.C.)I GAMS Software GmbH (Germany)

Washington Braunschweig Frechen (Cologne)

ZIB, Berlin

I ≈ 16 employees (7 development, 5 sales, . . .)

I Product: The General Algebraic Modeling System

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 3 / 33

Page 5: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

The Vision: World Bank Slide, 1976

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 4 / 33

Page 6: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Timeline: 1976 . . . now1976 GAMS idea is presented at ISMP, Budapest1978 Phase I: GAMS supports linear programming. Supports Mainframes and Unix Workstations1979 Phase II: GAMS supports nonlinear programming1987 GAMS becomes a commercial product1988 First PC System (16 bit)1988 Alex Meeraus, the initiator of GAMS and founder of GAMS Development Corporation, is awarded

INFORMS Computing Society Prize1990 32 bit Dos Extender1990 GAMS moves to Georgetown, Washington, D.C.1991 Mixed Integer Non-Linear Programs capability (DICOPT)1994 GAMS supports mixed complementarity problems1995 MPSGE language is added for CGE modeling1996 European branch opens in Germany1998 32 bit native Windows1998 Stochastic programming capability (OSL/SE, DECIS)1999 Introduction of the GAMS Integrated development environment (IDE)2000 GAMS World initiative started2001 GAMS Data Exchange (GDX) is introduced2002 GAMS is listed in OR/MS 50th Anniversary list of milestones2003 Conic programming is added2003 Global optimization in GAMS2004 Support for Quadratic Constrained programs2005 Support for 64 bit PC Operating systems2006 GAMS supports parallel grid computing2007 GAMS supports open-source solvers from COIN-OR2008 Support for 32 and 64 bit Mac OS X2009 GAMS supports extended mathematical programs (EMP)2010 GAMS is awarded the company award of the German Society of Operations Research (GOR)2012 The Winners of the 2012 INFORMS Impact Prize included Alexander Meeraus. The prize was

awarded to the originators of the five most important algebraic modeling languages.Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 5 / 33

Page 7: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

User Community

I more than 11500 licensesI 50% academic users, 50% commercial

Application Areas:

I Agricultural EconomicsI Chemical EngineeringI EconometricsI Environmental EconomicsI FinanceI International TradeI Macro EconomicsI Management Science/ORI . . .

I Applied General EquilibriumI Economic DevelopmentI EnergyI EngineeringI ForestryI LogisticsI MilitaryI Mathematics

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 6 / 33

Page 8: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Language

Declarative Language:I Similar to mathematical notationI Few basic language elements: sets, parameters, variables, equations,

modelsI Model is executable (algebraic) description of the problem

Imperative Elements:I Control flow statements: loops, for, if, . . .I build algorithms within GAMSI exchange data with other systems

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 7 / 33

Page 9: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Transport Example (GAMS Model Library: TRNSPORT)

350

600

275

300 325

Seattle

San Diego

Topeka

Chicago New York

1.7

1.8

2.5

1.8

1.4

2.5

Canning Plants (supply) Markets (demand)shipments

(number of cases)

Minimize Transportation costsubject to Demand satisfaction at markets

Supply constraintsStefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 8 / 33

Page 10: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Transport Example (GAMS Model Library: TRNSPORT)

i canning plantsj marketsai capacity of plantsbj demand at marketsci,j transportation cost per casexi,j cases to ship from i to j

min∑

i,j

ci,jxi,j

s.t.∑

j

xi,j ≤ ai ∀i∑

i

xi,j ≥ bj ∀j

xi,j ≥ 0 ∀i , j

Setsi "canning plants",j "markets";

Parametersa(i) "capacity of plant i in cases",b(j) "demand at market j in cases",c(i,j) "transport cost in 1000$ per case";

Variablesx(i,j) "shipment quantities in cases",z "total transportation costs in 1000$";

Positive Variable x;

Equationscost "define objective function",supply(i) "observe supply limit at plant i",demand(j) "satisfy demand at market j";

cost .. z =e= sum((i,j), c(i,j)*x(i,j));supply(i) .. sum(j, x(i,j)) =l= a(i);demand(j) .. sum(i, x(i,j)) =g= b(j);

Model transport / all / ;

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 9 / 33

Page 11: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Independence of Model and Solver

GAMS is not a Solver!

GAMS: Model building and interaction with solvers and environment.Solver: Solve an instance (instantiation of a model with data) using mathematical

optimization.

I Major commercial and academic solvers integrated:31 solvers, half of them actively developed/updated

I Average number of commercial solvers per license:I Academic clients: 2.9I Commercial clients: 2.2

I Switch between solvers with one statement:option solver = scip; (since GAMS 24.5)

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 10 / 33

Page 12: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Solvers ↔ Problemtypes (GAMS 24.5)LP MIP NLP MCP MPEC CNS DNLP MINLP QCP MIQCP Stoch. Global

ALPHAECP x xANTIGONE 1.1 x x x x x x xBARON 15.8 x x x x x x x x xBDMLP x xBONMIN 1.8 x xCBC 2.9 x xCONOPT 3 x x x x xCOUENNE 0.5 x x x x x x xCPLEX 12.6 x x x xDECIS x xDICOPT x xGUROBI 6.0 x x x xIPOPT 3.12 x x x x xKNITRO 9.1 x x x x x x xLGO x x x x (x)LINDO 9.0 x x x x x x x x xLOCALSOLVER 5.5 x x x x x x x xMILES xMINOS x x x x xMOSEK 7 x x x x x xMSNLP x x x (x)NLPEC x xOQNLP x x x x x (x)PATH x xSBB x xSCIP 3.2 x x x x x x x xSNOPT x x x x xSOPLEX 2.2 xSULUM 4.3 x xXA x xXPRESS 28.01 x x x xStefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 11 / 33

Page 13: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Independence of Model and Platform

Supported Platforms:

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 12 / 33

Page 14: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Documentation and Help

Online: http://www.gams.com/help (with search)

Offline: <GAMS system directory>/docs/index.html (no search, use grep!)

I GAMS – A User’s Guide: Tutorial, Basics, Advanced TopicsI McCarl (Expanded) GAMS User GuideI Solver ManualsI Tools ManualsI APIs: Tutorials and Reference ManualsI Release Notes

Tutorial Videos: http://www.youtube.com/user/GAMSLessons

Support wiki: http://support.gams.com/doku.php

Discussion group: http://www.gamsworld.org/

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 13 / 33

Page 15: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Model LibrariesOnline: http://www.gams.com/modlibsOffline: gamslib, apilib, datalib, emplib, testlib tools

I GAMS Model LibraryI representing interesting and sometimes classic problemsI illustrating GAMS modeling capabilities

I GAMS API LibraryI scripts to compile and execute GAMS API examples

I GAMS Data Utilities LibraryI demonstrate utilities to interface GAMS with other applications

I GAMS EMP LibraryI illustrate and test capabilities of extended mathematical programming facility

I Contributed Libraries:I FINLIB – financial optimization models (by Consiglio, Nielsen and Zenios)I NOALIB – nonlinear optimization applications models (by Neculai Andrei)

I GAMS Testlib LibraryI testing and quality control

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 14 / 33

Page 16: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Model LibrariesOnline: http://www.gams.com/modlibsOffline: gamslib, apilib, datalib, emplib, testlib tools

I GAMS Model LibraryI representing interesting and sometimes classic problemsI illustrating GAMS modeling capabilities

I GAMS API LibraryI scripts to compile and execute GAMS API examples

I GAMS Data Utilities LibraryI demonstrate utilities to interface GAMS with other applications

I GAMS EMP LibraryI illustrate and test capabilities of extended mathematical programming facility

I Contributed Libraries:I FINLIB – financial optimization models (by Consiglio, Nielsen and Zenios)I NOALIB – nonlinear optimization applications models (by Neculai Andrei)

I GAMS Testlib LibraryI testing and quality control

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 14 / 33

Page 17: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Recall Tuesday morning . . .

The Equitable Coach Problem

What is important?

� How many players are in the team? How many on the field?

� What is the formation that the team will play? 2-3-1, 2-2-2 or 3-1-2?

� How many interchanges do you want to perform?

� What are the positional preferences of the players?

� Are you equitable for only a single game or across the two matches?

� Complicated: Does winning the first match impact your decision aboutbeing equitable for the second?

Burt, Maher, Witzig – Modelling 11 / 102

The Equitable Coach Problem: disaggregated model

We now consider the version of the ECP where we allocate players topositions at particular times.

xr,i,t [binary] is 1 if player r plays in position i in play period t;

�+r [continuous] is the number of periods player r plays above the average;

�−r [continuous] is the number of periods player r plays below the average.

R is the set of players;

N is the set of positions;

T is the total number of game minutes.

Burt, Maher, Witzig – Modelling 88 / 102

Problem definition

OBJECTIVE: For a single game identify the equitable number of playingminutes for each player given that there is only one goal keeper and playershave positional preferences.

� 7 players on the field, 12 players in a team

� 20 minutes games ⇒ 140 player minutes

� 4 different position types, Goal Keeper, Defence, Midfield and Striker.

Solution is not as easy. Need a more sophisticated mathematical model.

Burt, Maher, Witzig – Modelling 12 / 102

The Equitable Coach Problem: disaggregated model

ECPd : min�

r

��+r + �−r

r

xr,i,t = 1 ∀ t ∈ T, i ∈ N, (8)

i

xr,i,t ≤ 1 ∀ t ∈ T, r ∈ R, (9)

t,i

xr,i,t −�

t,i,r�

xr�,i,t

|R| = �+r − �−r ∀ r ∈ R, (10)

xr,i,t ∈ [0, 1] ∀r ∈ R, i ∈ N, t ∈ T,

�+r , �−r ∈ R≥0.

Burt, Maher, Witzig – Modelling 89 / 102

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 15 / 33

Page 18: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Recall: Equitable Coach Problem (ECP)I 7 players on the field, 12 players in a teamI 20 minutes games ⇒ 140 player minutesI 4 different position types, Goal Keeper, Defence, Midfield and Striker.

OBJECTIVE: For a single game identify the positions of players at particulartimes given that players have positional preferences and such that playing timesare as equitable as possible.

xr,i,t is 1 if player r plays in position i inplay period t;

ε+/−r is the number of periods player r plays

above/below the average;

R is the set of players;

Ri is the set of players than can play onposition i ;

P is the set of positions;

T is the total number of game minutes.

min∑r

(ε+r + ε−r

)∑r∈Ri

xr,i,t = 1 ∀ t ∈ T , i ∈ P,

∑i :r∈Ri

xr,i,t ≤ 1 ∀ t ∈ T , r ∈ R,

∑t,i :r∈Ri

xr,i,t −∑

t,i,r′∈Ri

xr′,i,t|R| = ε+r − ε−r ∀r ∈ R,

xr,i,t ∈ [0, 1] ∀r ∈ R, i ∈ N, t ∈ T ,

ε+r , ε−r ∈ R≥0.

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 16 / 33

Page 19: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Recall: Equitable Coach Problem (ECP)I 7 players on the field, 12 players in a teamI 20 minutes games ⇒ 140 player minutesI 4 different position types, Goal Keeper, Defence, Midfield and Striker.

OBJECTIVE: For a single game identify the positions of players at particulartimes given that players have positional preferences and such that playing timesare as equitable as possible.

xr,i,t is 1 if player r plays in position i inplay period t;

ε+/−r is the number of periods player r plays

above/below the average;

R is the set of players;

Ri is the set of players than can play onposition i ;

P is the set of positions;

T is the total number of game minutes.

min∑r

(ε+r + ε−r

)∑r∈Ri

xr,i,t = 1 ∀ t ∈ T , i ∈ P,

∑i :r∈Ri

xr,i,t ≤ 1 ∀ t ∈ T , r ∈ R,

∑t,i :r∈Ri

xr,i,t −∑

t,i,r′∈Ri

xr′,i,t|R| = ε+r − ε−r ∀r ∈ R,

xr,i,t ∈ [0, 1] ∀r ∈ R, i ∈ N, t ∈ T ,

ε+r , ε−r ∈ R≥0.

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 16 / 33

Page 20: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

ECPd.zpl (simplified version)param gametime := 20;param nsubperiods := 6;

# formationparam nkeeper := 1;param ndefender := 2;param nmidfielder := 3;param nstriker := 1;

# set number of positionsparam npositions := nkeeper + ndefender + nmidfielder + nstriker;

# sets of players , positions , and minutes per gameset Player := {

"Josh", "Simon", "Jordy", "Chris", "Andy", "Richie","Tritto", "Guy", "Neil", "Justin", "Steve", "Cory"

};set Classes := { "K", "D", "M", "S" };set Positions := { 1 .. npositions };set Periods := { 1 .. nsubperiods };

...

var x[Player * Positions * Periods] binary;var eplus[Player] real;var eminus[Player] real;

# the model:# 1.) objective function# 2.) ensure that each position is used at each point in time# 3.) ensure that each player plays on at most one position per time# 4.) calculate the minutes over/below the averageminimize cost: sum <r> in Player : gametime/nsubperiods * (eplus[r] + eminus[r]);

subto c1: forall <i> in Positions :forall <t> in Periods : sum <r> in Player with pos[r,GetClass(i)] == 1: x[r,i,t] == 1;

subto c2: forall <r> in Player :forall <t> in Periods : sum <i> in Positions with pos[r,GetClass(i)] == 1: x[r,i,t] <= 1;

...Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 17 / 33

Page 21: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

ECPd.gms

$title Equitable Coach Problem

∗ timing parameters∗ param gametime := 20;scalar gametime / 20 /;∗ param nsubperiods := 6;$set nsubperiods 6

∗ formation 1−2−3−1∗ param nkeeper := 1;∗ param ndefender := 2;∗ param nmidfielder := 3;∗ param nstriker := 1;scalar nkeeper / 1 /;scalar ndefender / 2 /;scalar nmidfielder / 3 /;scalar nstriker / 1 /;

∗ set number of positions∗ param npositions := nkeeper + ndefender + nmidfielder + nstriker;$set npositions 7

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 18 / 33

Page 22: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

ECPd.gms

∗ sets of players, positions, and minutes per game∗set Player := {∗ "Josh", "Simon", "Jordy", "Chris", "Andy", "Richie",∗ "Tritto", "Guy", "Neil", "Justin", "Steve", "Cory"∗};set r "Player" /

"Josh", "Simon", "Jordy", "Chris", "Andy", "Richie","Tritto", "Guy", "Neil", "Justin", "Steve", "Cory"

/;

∗set Classes := { "K", "D", "M", "S" };∗set Positions := { 1 .. npositions };∗set Periods := { 1 .. nsubperiods };set c "Position classes" / K, D, M, S /;set p "Positions" / 1 ∗ %npositions% /;set t "Periods" / 1 ∗ %nsubperiods% /;

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 19 / 33

Page 23: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

ECPd.gms∗ entry (i,j) is 1 <=> player i can play on position j$ontextparam pos[Player ∗ Classes] := | "K", "D", "M", "S" |

|"Josh" | 0, 0, 1, 1 ||"Simon" | 0, 0, 0, 1 |

...

|"Steve" | 0, 1, 0, 0 ||"Cory" | 1, 0, 0, 0 |;

$offtextTable pos(r,c)

K D M SJosh 1 1Simon 1Jordy 1Chris 1 1Andy 1Richie 1 1Tritto 1Guy 1Neil 1Justin 1Steve 1Cory 1

;

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 20 / 33

Page 24: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

ECPd.gms$ontextdefstrg GetClass(i) := if i <= nkeeper then "K"

elseif i <= nkeeper + ndefender then "D"else

if i <= nkeeper + ndefender + nmidfielder then "M"else

"S"end

endend;

$offtextset pos2class(p,c);loop(p,

if( p.val <= nkeeper,pos2class(p,’K’) = yes;

else if( p.val <= nkeeper + ndefender,pos2class(p,’D’) = yes;

else if( p.val <= nkeeper + ndefender + nmidfielder,pos2class(p,’M’) = yes;

elsepos2class(p,’S’) = yes;

))));

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 21 / 33

Page 25: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

ECPd.gms

set canplay(r,p) "whether player r can play in position p";∗ r can play at p if pos(r,c) is 1 for the position class that p belongs to (pos2class)canplay(r,p) = (sum(c$pos2class(p,c), pos(r,c)) = 1);

display pos, pos2class, canplay;

∗ variable definition∗var x[Player ∗ Positions ∗ Periods] binary;∗var eplus[Player] real;∗var eminus[Player] real;Binary Variable x(r,p,t);Positive Variables eplus(r), eminus(r);

∗ the model:∗ 1.) objective function∗ 2.) ensure that each position is used at each point in time∗ 3.) ensure that each player plays on at most one position per time∗ 4.) calculate the minutes over/below the averageVariable inquity;Equations obj, c1, c2, c3;

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 22 / 33

Page 26: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

ECPd.gms∗ minimize cost: sum <r> in Player : gametime/nsubperiods ∗ (eplus[r]+eminus[r]);obj.. inquity =E= sum(r, gametime/%nsubperiods% ∗ (eplus(r) + eminus(r)));

∗subto c1: forall <i> in Positions : forall <t> in Periods :∗ sum <r> in Player with pos[r,GetClass(i)] == 1: x[r,i,t] == 1;c1(p,t).. sum(r$canplay(r,p), x(r,p,t)) =E= 1;

∗subto c2: forall <r> in Player : forall <t> in Periods :∗ sum <i> in Positions with pos[r,GetClass(i)] == 1: x[r,i,t] <= 1;c2(r,t).. sum(canplay(r,p), x(r,p,t)) =L= 1;

∗subto c3: forall <r> in Player :∗ sum <i,t> in Positions ∗ Periods with pos[r,GetClass(i)] == 1 : x[r,i,t]∗ −sum <q,i,t> in Player ∗ Positions ∗ Periods with∗ pos[q,GetClass(i)] == 1 : (x[q,i,t]/card(Player)) == eplus[r]−eminus[r];alias(r,q);c3(r).. sum((p,t)$canplay(r,p), x(r,p,t))

− sum((q,p,t)$canplay(q,p), x(q,p,t)/card(r)) =E= eplus(r)−eminus(r);

∗ declare model ECPd, consisting of all equations seen so farModel ECPd / all /;

∗ solve ECPd (minimizing inquity) as MIP to optimality (gap tolerance 0)option optcr = 0;Solve ECPd minimizing inquity using MIP;Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 23 / 33

Page 27: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Exercise

TASK: Extend the equitable coach problem model by restricting the number ofposition changes for all players to 3.

I Get http://co-at-work.zib.de/files/gams/ECP_exercise.gmsI Open the file in your favorite text editor and read the more detailed

instructions at the beginning.I Run the model: gams ECP_exercise.gms errmsg=1I In case of compilation or execution errors, e.g.,

--- Starting compilation--- ECP_exercise.gms(111) 3 Mb 3 Errors*** Status: Compilation error(s)--- Job ECP_exercise.gms Stop 10/01/15 20:15:47 elapsed 0:00:00.007

see listing file (ECP_exercise.lst):90 c2(r,t).. sum(canplay(r,p), x(r,p,t)) <= 1;

**** $37**** 37 ’=l=’ or ’=e=’ or ’=g=’ operator expected

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 24 / 33

Page 28: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Independence of Model and Data

GDX – GAMS Data Exchange:I Binary data format for fast exchange of data with GAMSI Stores sets, parameters, values of variables/equations with domain

information, but no symbolic information (e.g., equation algebra)I Consistency: no duplicates or contradictionsI Platform independentI Can be compressed

I Read and write in GAMS:I on command line: parameter gdx=<filename>I during compilation: $gdxin, $gdxout, $load, $unload, . . .I during execution: execute_load, execute_unload, . . .

I Tools and APIs to read and write from other environments:I gdxdump, csv2gdx, gdxviewer (win only), . . .I Matlab, MS Access, MS Excel, ODBC/SQL, R, SQLiteI low-level APIs for C, C++, C#, Delphi, Fortran, Java, Python, VBA, VB.NETI high-level APIs for C#, Java, and Python

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 25 / 33

Page 29: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Independence of Model and Data

GDX – GAMS Data Exchange:I Binary data format for fast exchange of data with GAMSI Stores sets, parameters, values of variables/equations with domain

information, but no symbolic information (e.g., equation algebra)I Consistency: no duplicates or contradictionsI Platform independentI Can be compressedI Read and write in GAMS:

I on command line: parameter gdx=<filename>I during compilation: $gdxin, $gdxout, $load, $unload, . . .I during execution: execute_load, execute_unload, . . .

I Tools and APIs to read and write from other environments:I gdxdump, csv2gdx, gdxviewer (win only), . . .I Matlab, MS Access, MS Excel, ODBC/SQL, R, SQLiteI low-level APIs for C, C++, C#, Delphi, Fortran, Java, Python, VBA, VB.NETI high-level APIs for C#, Java, and Python

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 25 / 33

Page 30: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

High-level APIs (C#, Java, Python) to embed GAMSCreating input:

I Read GAMS model from file or string (APIs have no modeling capability!)I Create input data (wrapper around GDX API)

Callout to GAMS:I Set GAMS options (e.g., solver to use, gap limit, . . . )I Start GAMS run and store results in GDX file

Processing results:I Read and process result data (wrapper around GDX API)

Main Classes:I GAMSWorkspace: EnvironmentI GAMSJob: Storing and running a modelI GAMSDatabase: In-memory representation of data (“GDX in-memory”)I GAMSOptions: Manipulation of GAMS optionsI GAMSModelInstance: Hot-start solve for a sequence of closely related

problems (changes in numerical values of matrix, objective, sides, bounds only)

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 26 / 33

Page 31: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

High-level APIs (C#, Java, Python) to embed GAMSCreating input:

I Read GAMS model from file or string (APIs have no modeling capability!)I Create input data (wrapper around GDX API)

Callout to GAMS:I Set GAMS options (e.g., solver to use, gap limit, . . . )I Start GAMS run and store results in GDX file

Processing results:I Read and process result data (wrapper around GDX API)

Main Classes:I GAMSWorkspace: EnvironmentI GAMSJob: Storing and running a modelI GAMSDatabase: In-memory representation of data (“GDX in-memory”)I GAMSOptions: Manipulation of GAMS optionsI GAMSModelInstance: Hot-start solve for a sequence of closely related

problems (changes in numerical values of matrix, objective, sides, bounds only)

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 26 / 33

Page 32: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Example (Python)<GAMS system directory>/apifiles/Python/transport1.py:from gams import ∗import sys

if len(sys.argv) > 1:ws = GamsWorkspace(system_directory = sys.argv[1])

else:ws = GamsWorkspace()

ws.gamslib("trnsport") # get ’trnsport’ from GAMS model libraryt1 = ws.add_job_from_file("trnsport.gms")

opt = ws.add_options()opt.all_model_types = "soplex"t1.run(opt)

for rec in t1.out_db["x"]:print("x(" + rec.key(0) + "," + rec.key(1) + "): " + \

"level=" + str(rec.level) + " marginal=" + str(rec.marginal))

x(seattle,new-york): level=50.0 marginal=0.0x(seattle,chicago): level=300.0 marginal=0.0x(seattle,topeka): level=0.0 marginal=0.036000000000000004x(san-diego,new-york): level=275.0 marginal=0.0x(san-diego,chicago): level=0.0 marginal=0.009000000000000008x(san-diego,topeka): level=275.0 marginal=0.0Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 27 / 33

Page 33: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Cutting Stock by Column Generation

Problem: [Gilmore, Gomory, 1961, 1963]I Cut out some paper products of different sizes from large raw paper rolls, in

order to meet a customer’s order.I The objective is to minimize the required number of the paper rolls.

Example:I Raw paper roll width: 5600mmI Customer’s Order:

Width Rolls1380 221520 251560 121710 141820 181880 181930 20

Width Rolls2000 102050 122100 142140 162150 182200 20

Source: Wikipedia

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 28 / 33

Page 34: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Cutting Stock Problem FormulationWidth of raw paper roll: WCustomer order:

I Set I of widths.I Widths wi ∈ N, i ∈ I .I Required number of rolls for each width di ∈ N, i ∈ I .

Set of valid patterns P:I ap,i ∈ N, p ∈ P, i ∈ I : Number of width i in pattern p

I e.g., ap,· = (1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0)⇔ 1× 1380, 2× 2100

I pattern only valid if∑

i∈I ap,iwi ≤W .

Model:I Variable xp ∈ N: how often to use pattern p

I Satisfy demand:∑

p∈P ap,i xp ≥ di for all i ∈ I

I Minimize number of patterns: min∑

p∈P xp

Observation: Only a few of all valid patterns will be used in asolution ⇒ generate set of active patterns dynamically

W = 5600

Order:Width Rolls

I wi di1 1380 222 1520 253 1560 124 1710 145 1820 186 1880 187 1930 208 2000 109 2050 12

10 2100 1411 2140 1612 2150 1813 2200 20

Solution:

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 29 / 33

Page 35: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Cutting Stock Problem FormulationWidth of raw paper roll: WCustomer order:

I Set I of widths.I Widths wi ∈ N, i ∈ I .I Required number of rolls for each width di ∈ N, i ∈ I .

Set of valid patterns P:I ap,i ∈ N, p ∈ P, i ∈ I : Number of width i in pattern p

I e.g., ap,· = (1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0)⇔ 1× 1380, 2× 2100

I pattern only valid if∑

i∈I ap,iwi ≤W .

Model:I Variable xp ∈ N: how often to use pattern p

I Satisfy demand:∑

p∈P ap,i xp ≥ di for all i ∈ I

I Minimize number of patterns: min∑

p∈P xp

Observation: Only a few of all valid patterns will be used in asolution ⇒ generate set of active patterns dynamically

W = 5600

Order:Width Rolls

I wi di1 1380 222 1520 253 1560 124 1710 145 1820 186 1880 187 1930 208 2000 109 2050 12

10 2100 1411 2140 1612 2150 1813 2200 20

Solution:

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 29 / 33

Page 36: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Cutting Stock Problem FormulationWidth of raw paper roll: WCustomer order:

I Set I of widths.I Widths wi ∈ N, i ∈ I .I Required number of rolls for each width di ∈ N, i ∈ I .

Set of valid patterns P:I ap,i ∈ N, p ∈ P, i ∈ I : Number of width i in pattern p

I e.g., ap,· = (1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0)⇔ 1× 1380, 2× 2100

I pattern only valid if∑

i∈I ap,iwi ≤W .

Model:I Variable xp ∈ N: how often to use pattern p

I Satisfy demand:∑

p∈P ap,i xp ≥ di for all i ∈ I

I Minimize number of patterns: min∑

p∈P xp

Observation: Only a few of all valid patterns will be used in asolution ⇒ generate set of active patterns dynamically

W = 5600

Order:Width Rolls

I wi di1 1380 222 1520 253 1560 124 1710 145 1820 186 1880 187 1930 208 2000 109 2050 12

10 2100 1411 2140 1612 2150 1813 2200 20

Solution:

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 29 / 33

Page 37: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Cutting Stock Problem FormulationWidth of raw paper roll: WCustomer order:

I Set I of widths.I Widths wi ∈ N, i ∈ I .I Required number of rolls for each width di ∈ N, i ∈ I .

Set of valid patterns P:I ap,i ∈ N, p ∈ P, i ∈ I : Number of width i in pattern p

I e.g., ap,· = (1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0)⇔ 1× 1380, 2× 2100

I pattern only valid if∑

i∈I ap,iwi ≤W .

Model:I Variable xp ∈ N: how often to use pattern p

I Satisfy demand:∑

p∈P ap,i xp ≥ di for all i ∈ I

I Minimize number of patterns: min∑

p∈P xp

Observation: Only a few of all valid patterns will be used in asolution ⇒ generate set of active patterns dynamically

W = 5600

Order:Width Rolls

I wi di1 1380 222 1520 253 1560 124 1710 145 1820 186 1880 187 1930 208 2000 109 2050 12

10 2100 1411 2140 1612 2150 1813 2200 20

Solution:

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 29 / 33

Page 38: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Restricted Master and Pricing ProblemsLet P ⊆ P be the set of generated pattern.Restricted Master Problem:

min∑

p∈Pxp

s.t.∑

p∈Pap,i xp ≥ di ∀i ∈ I (RMP)

xp ∈ R+ ∀p ∈ PLet µ be the dual variable associated with the inequality constraint in (RMP).

Pricing Problem:

min 1−∑

i∈Iµiyi

s.t.∑

i∈Iwiyi ≤W (PP)

yi ∈ N+ ∀i ∈ I

If (PP) has a solution y∗ with value < 0, an improving pattern has been found ⇒add to P with ap,i = y∗i .Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 30 / 33

Page 39: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Restricted Master Problem in GAMS

min

p∈Pxp :

p∈Pap,i xp ≥ di ∀i ∈ I , xp ∈ R+

(RMP)

Set i "widths" / w1*w4 /;Parameter r "raw width" / 100 /

w(i) "width" / w1 45, w2 36, w3 31, w4 14 /d(i) "demand" / w1 97, w2 610, w3 395, w4 211 /;

Set p "possible patterns" / 1*1000 /pp(p) "dynamic subset of p";

Parameterapi(p,i) "number of width i in pattern p";

Variable xp(p) "how often to use pattern p"z "objective variable"

Integer Variable xp;xp.up(p) = sum(i, d(i));

Equations numpat "number of patterns used"demand(i) "meet demand";

numpat .. z =e= sum(pp, xp(pp));demand(i).. sum(pp, api(pp,i)*xp(pp)) =g= d(i);

Model master / numpat , demand /;Solve master min z using RMIP;

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 31 / 33

Page 40: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Pricing Problem in GAMS

min

{1−

i∈Iµiyi :

i∈Iwiyi ≤W , yi ∈ N+

}(PP)

Set i "widths" / w1*w4 /;Parameter r "raw width" / 100 /

w(i) "width" / w1 45, w2 36, w3 31, w4 14 /;

Variable z "objective variable"y(i) "new pattern";

Integer variable y;y.up(i) = ceil(r/w(i));

Equations defobj "pricing problem objective"knapsack "knapsack constraint";

defobj .. z =e= 1 - sum(i, demdual(i)*y(i));knapsack .. sum(i, w(i)*y(i)) =l= r;

Model pricing / defobj , knapsack /;Solve pricing min z using MIP;

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 32 / 33

Page 41: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Exercise

Task: Get the Python file cutstock.py and add the Column-Generation loop:1. Solve (RMP) as LP.2. Update µ in (PP) and solve (PP).3. If optimal value of (PP) is > 0, then construct new pattern for (RMP) from

solution of (PP) and go to 1.

Final master objective value should be 452.25.

Solution File: cutstock_sol.py

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 33 / 33

Page 42: GAMS - Combinatorial Optimization at Workco-at-work.zib.de/files/gams/lecture.pdf · I GAMS–AUser’sGuide: Tutorial,Basics,AdvancedTopics I McCarl(Expanded)GAMSUserGuide I SolverManuals

Exercise

Task: Get the Python file cutstock.py and add the Column-Generation loop:1. Solve (RMP) as LP.2. Update µ in (PP) and solve (PP).3. If optimal value of (PP) is > 0, then construct new pattern for (RMP) from

solution of (PP) and go to 1.

Final master objective value should be 452.25.

Solution File: cutstock_sol.py

Stefan Vigerske – GAMS – Material: http://co-at-work.zib.de/files/gams/ 33 / 33