26
Advanced GAMS Advanced GAMS 1. 1. Good Modeling Practices Good Modeling Practices 2. 2. Fixing Misbehaving Models Fixing Misbehaving Models 3. 3. Linking GAMS Linking GAMS 4. 4. Advanced GAMS Syntax Advanced GAMS Syntax

Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Embed Size (px)

Citation preview

Page 1: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Advanced GAMSAdvanced GAMS

1.1. Good Modeling PracticesGood Modeling Practices

2.2. Fixing Misbehaving ModelsFixing Misbehaving Models

3.3. Linking GAMSLinking GAMS

4.4. Advanced GAMS SyntaxAdvanced GAMS Syntax

Page 2: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

1 Good Modeling Practices1 Good Modeling Practices

DocumentDocument

Descriptive NamesDescriptive Names

Sensible StructureSensible Structure

Efficient ProgrammingEfficient Programming

Page 3: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

DocumentationDocumentation

Use “*” comments Use “*” comments

Symbol descriptions & units at declarationSymbol descriptions & units at declaration

$ontext $offtext $ontext $offtext

$oneolcom, $eolcom, $oninlinecom, …$oneolcom, $eolcom, $oninlinecom, …

Put all data in named parametersPut all data in named parameters

Keep original data files Keep original data files

Track gams program versionsTrack gams program versions

Page 4: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

NamingNaming Choose Descriptive Names up to 64 CharactersChoose Descriptive Names up to 64 Characters

Avoid Special CharactersAvoid Special Characters

Avoid set elements which look like numbersAvoid set elements which look like numbers

Use …_var, …_equ, …_dataUse …_var, …_equ, …_data

Use desired capitalization on 1st occurrence, be Use desired capitalization on 1st occurrence, be aware of unique element ordering aware of unique element ordering

Name files which need to be run sequentially 1_..., Name files which need to be run sequentially 1_..., 2_..., 3_...2_..., 3_...

Page 5: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

StructureStructure

Number and size of filesNumber and size of files

Number and organization of Number and organization of directoriesdirectories

Arrangement of sets, data, model, Arrangement of sets, data, model, and base reportand base report

Scenarios, Scenario ReportScenarios, Scenario Report

Page 6: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Efficient GAMS usageEfficient GAMS usage

Use indexes, keep order index Use indexes, keep order index consistentconsistent

Automate data processing (gdx, awk, Automate data processing (gdx, awk, put)put)

Experiment with small modelsExperiment with small models

Page 7: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Efficient GAMS usageEfficient GAMS usage

Taylor Taylor outputoutput

Check aggregate solutions firstCheck aggregate solutions first

Check speed and memoryCheck speed and memory

Avoid irrelevant computations - use Avoid irrelevant computations - use conditionsconditions

Page 8: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Efficiency and ErrorsEfficiency and Errors

Avoid copies of the same piece of Avoid copies of the same piece of code to facilitate model development code to facilitate model development and to reduce errors from incomplete and to reduce errors from incomplete updatingupdating– use indexesuse indexes– may introduce accounting variables to may introduce accounting variables to

reduce code copies in report statementsreduce code copies in report statements

Page 9: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

2 Fixing Misbehaving Models2 Fixing Misbehaving Models

UnboundednessUnboundedness

InfeasibilityInfeasibility

Bad solutionBad solution

Scaling, Memory, Speed ProblemsScaling, Memory, Speed Problems

Page 10: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Using GAMSCHKUsing GAMSCHK

Option LP=GAMSCHK;Option LP=GAMSCHK; Option MIP=GAMSCHK;Option MIP=GAMSCHK; Option NLP=GAMSCHK;Option NLP=GAMSCHK;

Place option before solve statementPlace option before solve statement

Works best for large LP models where Works best for large LP models where variables and equations have many variables and equations have many indexesindexes

Page 11: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

GAMSCHKGAMSCHK

Use on single solve statements (not Use on single solve statements (not in a scenario loop)in a scenario loop)

Start with small model versionsStart with small model versions

For larger models consider variable For larger models consider variable and equation selections and equation selections

Page 12: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Tayloring GAMSCHKTayloring GAMSCHK

Create <filename>.gck, where Create <filename>.gck, where filename is the name of the file that filename is the name of the file that is executedis executed

Create gamschk.opt and use Create gamschk.opt and use <modelname>.optfile = 1;<modelname>.optfile = 1;

Create/modify <defaultsolver>.optCreate/modify <defaultsolver>.opt

Page 13: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Content of <filename>.gckContent of <filename>.gck

Major keywords:Major keywords:– AnalysisAnalysis– PostoptPostopt– DisplaycrDisplaycr– BlockpicBlockpic

Variable/Equation selection possible Variable/Equation selection possible under each keywordunder each keyword

… … see solver manual or McCarl user see solver manual or McCarl user guide for more exampelsguide for more exampels

Page 14: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

UnboundednessUnboundedness

Place artificial bound on objective Place artificial bound on objective

or other variablesor other variables

Use GAMSCHK nonoptUse GAMSCHK nonopt

Set filter in gamschk.optSet filter in gamschk.opt

Page 15: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

InfeasibilityInfeasibility

Need artificial variablesNeed artificial variables

Use BLOCKPIC to find candidate Use BLOCKPIC to find candidate equations for artificial variablesequations for artificial variables

GAMSCHK analysisGAMSCHK analysis

GAMSCHK postopt on non-zero artificial GAMSCHK postopt on non-zero artificial variablesvariables

Page 16: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Equation Candidates for Artificial Equation Candidates for Artificial Variables using Blockpic OutputVariables using Blockpic Output

| V V V V | R | a a a a | H | 1 2 3 4 | S---------------------Equ_1 | + + + - | E 0Equ_2 | + | L -Equ_3 | - m m | L 0Equ_4 | + | G +--------------------- Typ | + + + u

Assume that all variables are zero and then check which equations would be violated (infeasible). Thus, look for “L –”, “G +”, “E -“, or “E +”

Page 17: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Artificial Variables, 1Artificial Variables, 1

Declare artificial variables as Declare artificial variables as nonnegative variablesnonnegative variables

Place a high penalty for artificial Place a high penalty for artificial variables in the objective function variables in the objective function – High negative values in max problemsHigh negative values in max problems– High positive values in min problemsHigh positive values in min problems

Use each artificial variable only in one Use each artificial variable only in one constraintconstraint

Page 18: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Artificial Variables, 2Artificial Variables, 2

For each =L= equation with a negative For each =L= equation with a negative r.h.s. value add the artificial variable to r.h.s. value add the artificial variable to the l.h.s with a coefficient of -1.the l.h.s with a coefficient of -1.

For each =G= equation with a positive For each =G= equation with a positive r.h.s. value add the artificial variable to r.h.s. value add the artificial variable to the l.h.s with a coefficient of 1.the l.h.s with a coefficient of 1.

For each =E= equation with a non-zero For each =E= equation with a non-zero r.h.s. value add two artificial variables r.h.s. value add two artificial variables to the l.h.s one with a coefficient of 1 to the l.h.s one with a coefficient of 1 and one with a coefficient of -1.and one with a coefficient of -1.

Page 19: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Bad solutionBad solution

GAMSCHK analysis routineGAMSCHK analysis routine

GAMSCHK postopt routineGAMSCHK postopt routine

Trace from symptom to cause(s)Trace from symptom to cause(s)

Page 20: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Tracing unrealistic solutionsTracing unrealistic solutions

No need to guess or remember all No need to guess or remember all changes from the last working model changes from the last working model

Decompose solutionDecompose solution

Get rid of generic errors using Get rid of generic errors using GAMSCHK ANALYSIS routineGAMSCHK ANALYSIS routine

Page 21: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Tracing unrealistic solutionsTracing unrealistic solutions

Alternate between variables and Alternate between variables and equations in GAMSCHKequations in GAMSCHK

Alternate between individual Alternate between individual GAMSCHK output and aggregated GAMSCHK output and aggregated model structure model structure

Use display statements (don‘t look at Use display statements (don‘t look at original data)original data)

Page 22: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Fixing Analysis ErrorsFixing Analysis Errors

First decide between 2 general casesFirst decide between 2 general cases

A) Variable/Equation is present but A) Variable/Equation is present but shouldn‘t be presentshouldn‘t be present

B) Variable/Equation is not present B) Variable/Equation is not present but should be presentbut should be present

Page 23: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Case A) Case A) Variable/Equation is Variable/Equation is present but shouldn‘t be presentpresent but shouldn‘t be present

Solution (99%):Solution (99%):

Impose conditionsImpose conditions

Page 24: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Case B) Case B) Variable/Equation is not Variable/Equation is not present but should be presentpresent but should be present

Solution:Solution:

Check conditions and dataCheck conditions and data

Display data right before solve Display data right before solve statementstatement

Page 25: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

Postopt OutputPostopt Output Output for VariablesOutput for Variables

– Aij coefficientsAij coefficients– Ui shadow pricesUi shadow prices– Aij * UiAij * Ui– True reduced costs True reduced costs

Output for EquationsOutput for Equations– Aij coefficientsAij coefficients– Xj variable levelsXj variable levels– Aij*XjAij*Xj– Shadow priceShadow price

Page 26: Advanced GAMS 1. Good Modeling Practices 2. Fixing Misbehaving Models 3. Linking GAMS 4. Advanced GAMS Syntax

ConditionsConditions

Rule of thumb: Every variable block Rule of thumb: Every variable block should have the same conditions in should have the same conditions in each equation blockeach equation block