gams an introduction
play

GAMS An Introduction Hands-on Tutorial on Optimization Frederik - PowerPoint PPT Presentation

GAMS An Introduction Hands-on Tutorial on Optimization Frederik Proske & Lutz Westermann GAMS Software GmbH GAMS Development Corp. GAMS Software GmbH www.gams.com Agenda GAMS at a Glance GAMS Hands On Examples Outlook A


  1. MIP Model: Minimum Shipment of 100 cases β€’ Shipment volume: x (continuous variable) β€’ Discrete decision: ship (binary variable) Cost ($) ship = 0 ship = 1 Not possible x (Number of cases) 0 100 add constraints: (if ship=1, then ship at least 100) 𝑦 𝑗,π‘˜ β‰₯ 100 βˆ™ π‘‘β„Žπ‘—π‘ž 𝑗,π‘˜ βˆ€ 𝑗, π‘˜ (if ship=0, then do not ship at all) 𝑦 𝑗,π‘˜ ≀ 𝑐𝑗𝑕𝑁 βˆ™ π‘‘β„Žπ‘—π‘ž 𝑗,π‘˜ βˆ€ 𝑗, π‘˜ Hands-On π‘‘β„Žπ‘—π‘ž 𝑗,π‘˜ ∈ 0,1 45

  2. MIP Model: GAMS Syntax 46

  3. MIP Model: Results 47

  4. MIP Model: Solution shipments Markets (demand) Canning Plants (supply) (Number of cases) 300 275 325 Freight: $90 case / thousand miles Total cost: $153,675 48

  5. LP MIP MINLP Scenarios β€’ Determine β€’ Discrete β€’ Non-linearity β€’ SolveLink minimum decisions β€’ E.g.: Decrease β€’ Grid Facility transportation cost β€’ E.g.: Ship at in unit cost β€’ GUSS β€’ Result: city to city least 100 with growing shipment volumes cases volumes 49

  6. MINLP: Cost Savings Cost ($) The cost per case decreases with an increasing shipment volume ship = 0 ship = 1 Not possible x (Number of cases) 0 100 Replace: (Minimize total transportation cost) 𝑗 π‘˜ 𝑑 π‘—π‘˜ βˆ™ 𝑦 π‘—π‘˜ min With 𝑗 π‘˜ 𝑑 π‘—π‘˜ βˆ™ 𝑦 π‘—π‘˜ 𝒄𝒇𝒖𝒃 (Minimize total transportation cost) min Hands-On 50

  7. MINLP Model: GAMS Syntax 51

  8. MINLP Model: Results 52

  9. MINLP Model: Solution shipments Markets (demand) Canning Plants (supply) (Number of cases) 300 275 325 Freight: $90 case / thousand miles Total cost: $115,438 53

  10. LP MIP MINLP Scenarios β€’ Determine β€’ Discrete β€’ Non-linearity β€’ SolveLink minimum decisions β€’ E.g.: Decrease β€’ Grid Facility transportation cost β€’ E.g.: Ship at in unit cost β€’ GUSS β€’ Result: city to city least 100 with growing shipment volumes cases volumes 54

  11. Motivation β€’ Solving challenging real-world problems often involves the solution of many optimization problems β€’ Decomposition Methods β€’ Scenario Analysis β€’ Heuristics β€’ … β€’ Such approaches are often chosen, if solving the problem at hand does not work with a single monolithic model, e.g. β€’ Due to it’s size and the required resources (e.g. memory) β€’ Due to time restrictions (Problem should be solved in minutes but it takes days) β€’ … β†’ GAMS Grid Facility β†’ Gather-Update-Solve-Scatter 55

  12. SolveLink Option Controls GAMS function when linking to solver. Model transport /all/ ; transport.solveLink={ 0 %Solvelink.ChainScript%, 1 %Solvelink.CallScript%, 2 %Solvelink.CallModule%, 3 %Solvelink.AsyncGrid%, 4 %Solvelink.AsyncSimulate%, 5 %Solvelink.LoadLibrary%, 6 %solveLink.aSyncThreads%, %solveLink.threadsSimulate% }; 7 solve transport using lp minimizing z; 56

  13. SolveLink Option – Sequential Solves β€’ ChainScript [0]: Solver process, GAMS vacates memory + Maximum memory available to solver + protection against solver failure ( hostile link) - swap to disk β€’ Call{Script [1]/Module [2]}: Solver process, GAMS stays live + protection against solver failure ( hostile link) + no swap of GAMS database - file based model communication β€’ LoadLibrary [5]: Solver DLL in GAMS process + fast memory based model communication + update of model object inside the solver (hot start) - not supported by all solvers 57

  14. SolveLink Option Sequential – Exercise β€’ Generate 100 distance scenarios with random data β€’ Solve these scenarios with the solveLink values… β€’ ChainScript [0] β€’ CallModule [2] β€’ LoadLibrary [5] β€’ Compare the execution time of solving all scenarios with different solveLink settings β€’ Hint: Look at the GAMS function jNow Hands-On 58

  15. SolveLink Option – Sequential Solves ---- 88 PARAMETER time time for 100 scenarios ChainScript 6.710, CallModule 2.694, LoadLibrary 0.578 59

  16. SolveLink Option – Asynchronous Solves β€’ aSyncGrid [3]: GAMS starts the solution and continues in a Grid computing environment β€’ aSyncThreads [6]: The problem is passed to the solver in core without use of temporary files, GAMS does not wait for the solver to come back 60

  17. SolveLink Option Asynchronous – Exercise β€’ Generate 100 distance scenarios with random data β€’ Solve these scenarios with the solveLink values… β€’ aSyncGrid [3] β€’ aSyncThreads [6] β€’ Compare the execution time of solving all scenarios with different solveLink settings β€’ Hint: Check the log for output about solveLink β€’ β†’ Use solver CplexD instead of Cplex β€’ Hint: Look at the following GAMS functions: β€’ readyCollect β€’ handleCollect β€’ handleDelete Hands-On 61

  18. SolveLink Option – Asynchronous Solves ChainScript 6.710 CallModule 2.694 LoadLibrary 0.578 aSyncGrid 4.259 aSyncThreads 0.496 ---- 96 PARAMETER time time for 100 scenarios aSyncGrid 4.259, aSyncThreads 0.496 62

  19. SolveLink Option – Asynchronous Solves β€’ Helpful, if large ratio of solver time / GAMS time ChainScript 29.807 CallModule 30.004 LoadLibrary 28.864 aSyncGrid 9.112 aSyncThreads 7.901 63

  20. GUSS – Gather-Update-Solve-Scatter aka Scenario Solver Simple Serial Scenario Solver/GUSS Solve Loop Generation Loop Generation Solution Solution Update Update Generates model once and updates the algebraic model keeping the model β€œhot” inside the solver. 64

  21. GUSS – Gather-Update-Solve-Scatter aka Scenario Solver ChainScript 6.710 CallModule 2.694 LoadLibrary 0.578 aSyncGrid 4.259 aSyncThreads 0.496 GUSS 0.273 65

  22. Grid & GUSS – Examples from the model library β€’ trnsgrid: https://www.gams.com/latest/gamslib_ml/libhtml/gamslib_trnsgrid.html β€’ Simple asynchronous solves in a loop, separate collection loop β€’ tgridmix : https://www.gams.com/latest/gamslib_ml/libhtml/gamslib_tgridmix.html β€’ Asynchronous solves in combined submission & collection loop. Keep number of submitted models <= #threads β€’ gussgrid : https://www.gams.com/latest/gamslib_ml/libhtml/gamslib_gussgrid.html β€’ Asynchronous GUSS-solves in combined submission & collection loop. Keep number of submitted models <= #threads 66

  23. A Model is a Model is a Model* * Freely adapted from the poetry of Gertrude Stein, 1874-1946, American writer

  24. What is a Model? 68

  25. What is a Model? β€’ A mathematical model is a description of a system using mathematical language (from: Wikipedia) βž” Mathematical Programming (MP) Model: List of Equations 69

  26. What is a Model in GAMS? β€’ A mathematical model is a description of a system using mathematical language (from: Wikipedia) βž” Mathematical Programming (MP) Model: List of Equations β€’ Collection of several intertwined MP Models β€’ Data Preparation β€’ Data Calibration β€’ β€œSolution” Module (e.g. sequential, parallel loop) β€’ Report Module 70

  27. Model as Communication Vehicle β€’ Defining scope of a (part of a) project/model β€’ IT, analysts, managers, model builders have different views β€’ Misunderstandings common with verbal descriptions β€’ Use a model to define the scope β€’ Requirements for such a model β€’ Rapid prototyping (max. 1-2 man days) β€’ Standard IO interface (Excel) β€’ Remote execution (e.g. through Web UI) 71

  28. Model as Analytic Framework β€’ Optimization models do not allow for any type of vagueness β€’ Input data requirements β€’ Objectives and constraints β€’ Results β€’ Misunderstandings result in failure of the model β€’ Compilation/execution errors β€’ Infeasible/unbounded MP models β€’ Model as a contract 72

  29. Model as Contract β€’ Good models do not rely on contract (input data) β€’ Input Module (handles bad data) β€’ Simple error checks β€’ Analyzing and reporting complex data problems β€’ Good models (modeling systems) provide access to results via independent result analyzers for non model experts β€’ Analytic framework help define result metric β€’ e.g. violations of soft constraints 73

  30. Model as Cost Saver β€’ Most convincing and obvious reason for using an optimization model β€’ Science of better (INFORMS) β€’ Often exaggerated/difficult to estimate Model Roles over Time Analytic Communication Contract Cost Saver Vehicle Framework 74

  31. Outlook: Parallelization on HPC Systems From simple sequential to highly parallel solve statements

  32. Available Computing Resources Multi-core shared memory standard core core core core memory Distributed (shared) memory Off-the-shelf Optimization software core core core core Node 0 (speedup via Problem memory parallelization) core core core core Node 1 HPC memory . . . How does GAMS support problem Node n-1 core core core core specific solution approaches that are well suited/customized for HPC? memory 76

  33. Solve sequence of problems Parallelize (e.g. in Benders Decomp.) … 1 … … Decompose time … time … … … … … GAMS supports different levels of parallelization Optimization Problem GAMS provides annotation facilities and HPC solver links Non-zero Plot of matrix User Annotation to define block structure 2 Use structure exploiting solver 77

  34. Sequential Solve Statements in Loops β€’ loop body code in sequence, often with an expensive solve statement: Model generation Solution process Model generation Solution process time Model generation Solution process … 78

  35. Parallel Solves – GAMS Grid Facility SolveLink option specifies the solver linking β€’ conventions Split loop in submission & collection loop: β€’ MG MG S MG S time S Model generation and loop body code in sequence β€’ Either file based IO or limited to shared memory β€’ 79

  36. Excursus 1: Message Passing Interface (MPI) gams myfile gams myfile mpiexec – n 5 gams myfile gams myfile gams myfile gams myfile 80

  37. Excursus 1: Message Passing Interface (MPI) gams myfile <-- %sysenv.PMI_RANK%=0 gams myfile <-- %sysenv.PMI_RANK%=1 mpiexec – n 5 gams myfile gams myfile <-- %sysenv.PMI_RANK%=2 gams myfile <-- %sysenv.PMI_RANK%=3 gams myfile <-- %sysenv.PMI_RANK%=4 myfile.gms – pseudo code broadcast(db,0) 0 1 2 3 4 gather(db,0) 0 1 2 3 4 81

  38. Excursus 1: Message Passing Interface (MPI) gams myfile <-- %sysenv.PMI_RANK%=0 gams myfile <-- %sysenv.PMI_RANK%=1 mpiexec – n 5 gams myfile gams myfile <-- %sysenv.PMI_RANK%=2 gams myfile <-- %sysenv.PMI_RANK%=3 gams myfile <-- %sysenv.PMI_RANK%=4 myfile.gms – pseudo code MG MG MG … S S S time 82

  39. Excursus 1: Message Passing Interface (MPI) gams myfile <-- %sysenv.PMI_RANK%=0 gams myfile <-- %sysenv.PMI_RANK%=1 mpiexec – n 5 gams myfile gams myfile <-- %sysenv.PMI_RANK%=2 gams myfile <-- %sysenv.PMI_RANK%=3 gams myfile <-- %sysenv.PMI_RANK%=4 myfile.gms – pseudo code β€’ Requires reorganization of the code but allows parallel solve β€’ Distribute/merging data easy (part of MPI) β€’ Network based communication β€’ Need to make GAMS aware of MPI β†’ Embedded Code 83

  40. Excursus 2: Embedded Code Facility 84

  41. Example: Sequential Benders Decomposition set scen 'scenario set' / scen1*scen100 / s(scen) 'dynamic secenario subset' k 'benders iterations' / k1*k1000 /; ... // preparatory work loop (k$( NOT done ), ... // setup model for master-problem solve master min obj_master use lp; ... // fix first stage variables loop (scen, ... // setup model for sub-problem option clear=s; s(scen) = yes ; solve sub min obj_sub use lp; ... // process results ); ... // compute cuts for next master ... // free fixed first stage variables ... // set done=1 if convergence criterion is met ); ... // reporting 85

  42. Example: Parallel Benders with mpi4py PMI_RANK=0 PMI_RANK>=1 set scen 'scenario set' / scen1*scen100 / set scen 'scenario set' / scen1*scen100 / s(scen) 'dynamic secenario subset' s(scen) 'dynamic secenario subset' k 'benders iterations' / k1*k1000 /; k 'benders iterations' / k1*k1000 /; ... ... embeddedCode Python: embeddedCode Python: from mpi4py import * from mpi4py import * comm = MPI.COMM_WORLD comm = MPI.COMM_WORLD ... ... pauseEmbeddedCode pauseEmbeddedCode ... // preparatory work ... // preparatory work $ifthen.MPI 0==%sysenv.PMI_RANK% $else.MPI loop (k$( NOT done ), s(scen) = ord (scen)=%sysenv.PMI_RANK%; ... // setup model for master-problem while (1, solve master min obj_master use lp; continueEmbeddedCode : ... // fix first stage variables primal_solution = comm.bcast(None, root=0) // broadcasted data β†’ GAMS data struct. continueEmbeddedCode: comm.bcast([[done]] + <data for sub>, root=0) pauseEmbeddedCode <GAMS data struct.> cut = comm.gather(None, root=0)[1:] abort .noerror$done 'terminating subprocess'; ... // gathered data β†’ GAMS data struct. solve sub min obj_sub use lp; pauseEmbeddedCode <load GAMS data struct.> ... // process results ... // compute cuts continueEmbeddedCode : ... // free fixed first stage variables comm.gather(<subproblem results>), root=0 ) ... // set done=1 if convergence criterion is met pauseEmbeddedCode ); ); continueEmbeddedCode: $endif.MPI comm.bcast([[done],<empty>], root=0) endEmbeddedCode ... // reporting $else.MPI 86

  43. Computational Result(s) β€’ Two-stage stochastic problem emerged from energy system model β€’ 100 scenarios β€’ Deterministic Equivalent: 21,029,101 rows, 23,217,077 columns, 85,721,477 non-zeroes β€’ Benders: Master: up to 553 rows, 177 columns, 24,911 non-zeroes β€’ Sub: 210,282 rows 232,161 columns 696,461 non-zeroes β€’ 19 lines of Python Code + some refactorization of GAMS code for MPI version β€’ TIME [sec] sub- master- Method problems problem total 4059.00 Deterministic Equivalent 1 2394.92 0.18 2395.10 Seq. Benders 2 28.35 0.16 28.51 MPI Benders 3 All runs were made with GAMS 25.1.2 on JURECA@JSC with 24 cores per node, 2.5 GHz, (Intel Xeon E5-2680 v3 Haswell), 128 GB RAM 1: single node, 16 cores, CPLEX barrier, no crossover 2: single node, 4 cores per solve statement, CPLEX barrier, advind 0 3: 17 nodes, 404 cores in total, 4 cores per solve statement, CPLEX barrier, advind 0 87

  44. Deployment of GAMS models

  45. Calling GAMS from your Application GAMS SOLVER Application GDX Creating Input for GAMS Model β†’ Data handling using GDX API Callout to GAMS β†’ GAMS option settings using Option API β†’ Starting GAMS using GAMS API Reading Solution from GAMS Model β†’ Data handling using GDX API 89

  46. Low level APIs β†’ Object Oriented API β€’ Low level APIs β€’ GDX, OPT, GAMSX, GMO, … β€’ High performance and flexibility β€’ Automatically generated imperative APIs for several languages (C, C++, C#, Delphi, Java, Python, VBA, …) β€’ Object Oriented GAMS API β€’ Additional layer on top of the low level APIs β€’ Object Oriented β€’ Written by hand to meet the specific requirements of different Object Oriented languages 90

  47. GAMS comes with several OO APIs (Python, Java, C++, C#, …) to β€’ develop applications β†’ Programming required to build your applications 91

  48. From GAMS Model to Visual Web User Interface 92

  49. From GAMS Model to Visual Web User Interface Basic setup: 1 βœ“ Annotating GAMS model (defining the input and output data to be displayed in the WebUI) 93

  50. Basic Setup – GAMS Model Annotations Fully functional interface by only βœ“ specifying input and output data Tabular view of input (editable) βœ“ and output data Graphical visualization via pivot βœ“ charts 94

  51. Basic Setup – GAMS Model Annotations 95

  52. Basic Setup – GAMS Model Annotations 96

  53. Basic Setup – GAMS Model Annotations 97

  54. From GAMS Model to Visual Web User Interface Basic setup: 1 βœ“ Annotating GAMS model (defining the input and output data to be displayed in the WebUI) Advanced setup: βœ“ Configuration of standard graphics and UI 2 βœ“ Sophisticated (custom) graphics (R API) βœ“ Scenario management with internal database 98

  55. Advanced Setup – Configuration Configuration via JSON file βœ“ Access to a number of pre- βœ“ implemented tools for graphical representation Focus on configuration, not βœ“ programming 99

  56. Advanced Setup – Sophisticated graphics Sophisticated graphics created in βœ“ R can be included as modules Access to the entire R ecosystem βœ“ Easily extendable with the wide βœ“ spectrum of the R programming language 100

  57. From GAMS Model to Visual Web User Interface Initialization: 1 βœ“ Annotating GAMS model (defining the input and output data to be displayed in the WebUI) Advanced setup: βœ“ Configuration of standard graphics and UI 2 βœ“ Sophisticated (custom) graphics (R API) βœ“ Scenario management with internal database Enterprise setup: 3 βœ“ User- and Application management 101

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend