370 likes | 664 Views
Working with GAMS. Vikas Argod Research Computing and Cyberinfrastructure vikasargod@psu.edu. Research Computing and Cyberinfrastructure . Empowering scholars in their ability to compute and manage data by developing and maintaining several state-of-the-art computational clusters
E N D
Working with GAMS Vikas Argod Research Computing and Cyberinfrastructure vikasargod@psu.edu
Research Computing and Cyberinfrastructure • Empowering scholars in their ability to compute and manage data by developing and maintaining several state-of-the-art computational clusters • Staff members provide support and expertise for research using • programming languages • numerical libraries • statistical packages • finite element solvers and fluid mechanics • Optimization packages Visit us at http://rcc.its.psu.edu/hpc/
Compute Engines • A 48-port 10 GigE switch connects all compute engines and storage • 1280 core system coming online in Summer 2010 • All compute engines together to deliver 40 million core hours in 2010 • Emerging technologies in the hands of the user community –ScaleMP and GPU cluster
Connecting to Hammer, Lion-X systems • Remote Desktop Connection to hammer.aset.psu.edu OR • Download and install SSH client http://downloads.its.psu.edu OR • Download Putty : http://rcc.its.psu.edu/hpc/guides/connectivity/putty/ Host name : hammer.aset.psu.edu for hammer lionxo.aset.psu.edu for lion-xo lionxc.aset.psu.edu for lion-xc Username : Penn state access id Authentication method : Password
File transfer • In Windows, use WinSCP : http://rcc.its.psu.edu/hpc/guides/filetransfer/winscp/ In Linux/Mac: scplocal_fileuser@hostname:destination_directory • Copy individual files: [abc123@funkmachine ~]$ scpfoo.cfoo.habc123@lionxj.rcc.psu.edu:. • Copy directory: [abc123@funkmachine ~]$ scp -r srcdir abc123@hammer.aset.psu.edu:. • For more details:http://rcc.its.psu.edu/hpc/guides/connectivity/ssh/#filetransfer
Working with Linux • Basic Linux commands: for more commands!
GAMS in Hammer and LION-X • Load the module: module load gams • Command to run GAMS files in Hammer: gams <gams_input_filename> gams_input_filename is an ASCII file with GAMS commands and has an extension ‘.gms’ e.g. gams blend_model.gms • Command to run GAMS files in LION-X* • Submit to Queue using PBS script • Useful for jobs which take longer computation time
Sample PBS script #PBS -l nodes=1:ppn=1 #PBS -l walltime=02:30:00 #PBS -joe cd $PBS_O_WORKDIR module load gams gams blend_model.gms • Submit this file to the queue : qsub <script_name> • Good idea to write results to output file (Example given in the handout) • Visit http://rcc.its.psu.edu/hpc/guides/apps/pbs/ for more information on working with PBS
General Algebraic Modeling System (GAMS) • High-level modeling system for mathematical programming and optimization • Algebraic formulation : closeness to mathematical notation • Calling appropriate Algorithms • Efficient handling of mathematical optimization problems • Simple model building and solution process • Increase productivity and maintainable models
Example Problem Statement Several forms of gasoline are produced during the petroleum refining process, and a last step combines them to obtain market products with specified quality measures. Suppose the following four gasoline products are available: Determine the minimum cost blend which has quality index-1 between 85 and 90 and quality index-2 between 270 and 280.
equations • objective objective or cost function • min_spec_cons(j) minimum Quality index constraint • max_spec_cons(j) maximum Quality index constraint • tot_frac_cons total fraction=1 constraint ; • ***********Model Definition******************** • objective .. • tot_cost =e= sum(i,cost(i)*x(i)); • min_spec_cons(j) .. • sum(i,quality_val(i,j)*x(i)) =g= quality_spec_min(j); • max_spec_cons(j) .. • sum(i,quality_val(i,j)*x(i)) =l= quality_spec_max(j); • tot_frac_cons .. • sum(i,x(i)) =e= 1; • x.lo(i) = 0; x.up(i) = 1; • *********************************************** • model blend / • objective,min_spec_cons,max_spec_cons,tot_frac_cons/; • ***********Solve Statement********************* • solve blend using lp minimizing tot_cost; • *********************************************** • display x.l; sets i set of gasoline /G1*G4/ j set of quality indices /Q1,Q2/; parameters cost(i) cost of gasoline set i /G1 48 G2 43 G3 58 G4 46/ quality_spec_min(j) quality specifications -minimum /Q1 85 Q2 270/ quality_spec_max(j) quality specifications-maximum /Q1 90 Q2 280/; Table quality_val(i,j) quality indices Q1 Q2 G1 99 210 G2 70 335 G3 78 280 G4 91 265; variables tot_cost total cost in $ x(i) fraction of each Gasoline set; Page 1 Page 2
blend_model.gms: sets Sets are the basic building blocks of a GAMS model, corresponding exactly to the indices in the algebraic representations of models. Text is optional. It’s like a comment sets i set of gasoline /G1,G2,G3,G4/ j set of quality indices /Q1,Q2/; • Any name of your choice • No space in any name • G1*G4 is same as G1,G2,G3,G4 Mathematically: i = {G1, G2,G3,G4} j = {Q1,Q2}
blend_model.gms: parameters parameters are for inserting given data parameters cost(i) cost of gasoline set i /G1 48 G2 43 G3 58 G4 46/ quality_spec_min(j) quality specifications -minimum /Q1 85 Q2 270/ quality_spec_max(j) quality specifications-maximum /Q1 90 Q2 280/; Name of the parameter Optional text, useful for understanding Given data
blend_model.gms: Tables Table quality_val(i,j) quality indices Q1 Q2 G1 99 210 G2 70 335 G3 78 280 G4 91 265; • Very useful for larger datasets • GAMS will perform domain checking to make sure that the row and column names of the table are members of the appropriate sets.
General Comments on Data Entry • Direct Assignment e. g cost(‘Q1') = 48; • Zero is the default value for all the parameters. • A scalar is regarded as a parameter that has no domain. e.g..Scalar f cost in dollars per mile /90/ ; • A scalar is regarded as a parameter that has no domain • The same parameter can be assigned a value more than once. Each assignment statement takes effect immediately and overrides any previous values. • The same parameter may not be declared more than once • Element-value can be separated by commas or entered as separate lines • Entire list must be enclosed in slashes
Blend_model.gms: variables variables tot_cost total cost in $ x(i) fraction of each Gasoline set ; Every GAMS problem must contain at least one variable which is minimized or maximized (tot_cost in this example) e. g Positive variable x;
Blend_model.gms: equations equations objective objective or cost function min_spec_cons(j) minimum Quality index constraint max_spec_cons(j) maximum Quality index constraint tot_frac_cons total fraction=1 constraint; The power of algebraic modeling languages like GAMS is most apparent in the creation of the equations and inequalities that comprise the model under construction. This is because whenever a group of equations or inequalities has the same algebraic structure, all the members of the group are created simultaneously, not individually.
Equation Declaration • Equations must be declared and defined in separate statements. • The format of the declaration is the same as for other GAMS entities. objective .. tot_cost =e= sum(i,cost(i)*x(i)); • The name of the equation being defined • The symbol '..‘ • Left-hand-side expression • Relational operator: =l=, =e=, or =g= • Right-hand-side expression
Equation declaration :Summation Format : Sum(index of summation, summand) As a simple example, let us consider that the model has summation of x over i Sum(i,x(i)) =g=1 Sum(i, Sum(j, c(i,j)*x(i,j))) or Sum((i,j), c(i,j)*x(i,j))
Other types of indexed operations Product over controlling index prod(i,x(i)) =g=1 Maximum value over controlling index max_demand = smax((i,j), demand_array(i,j)) Minimum value over controlling index min_distance = smin((p,q), distance(p,q)) Full list of functions are given in the handout
blend_model.gms: equations min_spec_cons(j) .. sum(i,quality_val(i,j)*x(i)) =g= quality_spec_min(j); max_spec_cons(j) .. sum(i,quality_val(i,j)*x(i)) =l= quality_spec_max(j); tot_frac_cons .. sum(i,x(i)) =e= 1; • Variables can appear on the left or right-hand side of an equation or both. The same variable can appear in an equation more than once. • An equation definition can appear anywhere in the GAMS input, provided the equation and all variables and parameters to which it refers are previously declared.
blend_model.gms: Bounds GAMS is designed with a small database system in which records are maintained for the variables and equations. There are four fields in each record: .lo = lower bound .l = level or primal value .up = upper bound .m = marginal or dual value x.lo(i) = 0; x.up(i) = 1;
blend_model.gms: model model blend / objective,min_spec_cons,max_spec_cons,tot_frac_cons/; or model blend /all/ ; • Model is a collection of Equations. • Like other GAMS entities, it must be given a name in a declaration. • The format of the declaration is the keyword Model followed by the name of the model, followed by a list of equation names enclosed in slashes. • If all the defined equations are to be used /all/ can be used
blend_model.gms: Solve solve blend using lp minimizing tot_cost; • The format of the solve statement is as follows: • The keyword solve • The name of the model to be solved • The keyword using • An available solution procedure. Examples are • lp for linear programming • nlp for nonlinear programming • mip for mixed integer programming • rmip for relaxed mixed integer programming • minlp for mixed integer nonlinear programming • The keyword minimizing or maximizing • The name of the variable to be optimized
Display Statements • The solve statement will cause several things to happen when executed. • The specific instance of interest of the model will be generated • the appropriate data structures for inputting this problem to the solver will be created • the solver will be invoked • the output from the solver will be printed to a file. (*.lst) • To get the optimal values of the primal and/or dual variables, we can look at the solver output, or, if we wish, we can request a display of these result from GAMS as follows; display x.l; for final values of x
GAMS output • Echo print • Error messages • Reference maps • Model statistics • Status reports • Solution reports
Echo Print and Error messages • Echo Print: • Copy of your input file with line numbers • Irrespective of errors • Error messages: • Coded error message inside the echo print on the line immediately following the scene of offense • Messages start with **** and contain a $ followed by a numerical error code • Explanation of numerical code after the echo prints
Reference maps • Include $onsymxrefas the first line in the script • Cross reference maps • This lists the named items (Sets, Parameters, Variables, Equations, Models, Files, Acronyms) in alphabetical order • identifies them as to type, shows the line numbers where the symbols appear, and classifies each appearance SYMBOL TYPE REFERENCES blend MODEL declared 55 defined 55 impl-asn 59 ref 59 cost PARAM declared 8 defined 9 ref 41 • List of model entities • All the model entities are grouped by type • Also lists documentary text associated with entities
Status and Solution reports S O L V E S U M M A R Y MODEL blend OBJECTIVE tot_cost TYPE LP DIRECTION MINIMIZE SOLVER CPLEX FROM LINE 59 **** SOLVER STATUS 1 Normal Completion **** MODEL STATUS 1 Optimal **** OBJECTIVE VALUE 45.2941 RESOURCE USAGE, LIMIT 0.000 1000.000 ITERATION COUNT, LIMIT 4 2000000000 ---- EQU min_spec_cons minimum Quality index constraint LOWER LEVEL UPPER MARGINAL G1 . 0.1765 1.0000 . G2 . 0.3529 1.0000 . G3 . . 1.0000 13.0000 G4 . 0.4706 1.0000 .
Summary • Generality of the model • Generation of closely related constraints in one statement • Self documentary script • Easy to read output • Easy to debug with reference maps
Useful links/Reference • Online GAMS documentation http://www.gams.com/docs/document.htm • GAMS World : http://www.gamsworld.org/
Try this ++ ++ (land) + + (labor) (nonnegativity) • Copy example files from /usr/global/seminar/econcp -r /usr/global/seminar/econ . (notice the dot at the end) • Load the module : module load gams • Run : gams blend_model.gms • Open the file: gvimblend_model.lst