s_gams_ar.pdf

Upload: karen-dejo

Post on 02-Jun-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 s_gams_ar.pdf

    1/48

    Algebraic modeling languages

    Andrs Ramos

    Universidad Pontificia Comillashttp://www.iit.upcomillas.es/aramos/

    [email protected]

  • 8/11/2019 s_gams_ar.pdf

    2/48

    Operations Research (OR) definition

    Application of scientific methods in improving the

    effectiveness in operations, decisions and management.

    Design and improvements in operations and decisions

    Problem solving and support in management, planning or forecasting

    functions

    Provide knowledge and help in taking decisions

    Algebraic modeling languages. GAMS - 2

    Tasks: Collect and analyze data

    Develop and test mathematical models

    Propose solutions and recommendations

    Interpret the information Help to implement improvement actions

    Results: computer applications, systems, services or

    products.

  • 8/11/2019 s_gams_ar.pdf

    3/48

    Shortest path problem

    Algebraic modeling languages. GAMS - 3

  • 8/11/2019 s_gams_ar.pdf

    4/48

    OR History

    Origin in the beginning of World War II (due to the urgent

    assignment of scarce resources in military operations, in

    tactical and strategic problems). The same techniques were

    then applied to companies.

    Very quick initial algorithmic progress (many techniques

    LP, DP were developed before 1960).

    Algebraic modeling languages. GAMS - 4

    Game Theory: von Neumann y Morgenstern 1944 Simplex Method: Dantzig 1947

    Optimality Principle: Bellman 1957

    Constant relation with computer hardware advance. Today it

    is possible to solve an LP problem with 200.000 equationsand 200.000 variables in a PC.

  • 8/11/2019 s_gams_ar.pdf

    5/48

    In the last decade, new advances in algorithms have been as

    important as the impressive advances in computertechnology George L. Nemhauser (1994)

    Last decade in optimization

    Algebraic modeling languages. GAMS - 5

    The technology improvements in algorithms, mo elinglanguages, software, and hardware have made the

    methodology accessible, easy to use, and fast. So the Age of

    Optimization has arrived George L. Nemhauser (1994)

  • 8/11/2019 s_gams_ar.pdf

    6/48

    Logarithmic advances

    Between CPLEX 1.0 from 1988 and CPLEX 9 del 2004 a

    total improvement (software and algorithms) of 3300 times

    has been observed in LP solver. As a reference, theperformance improvement in hardware has been of 1600

    times in the same period

    Algebraic modeling languages. GAMS - 6

    .

    total improvement (software and algorithms) of 29000 timeshas been observed in MIP solver

    Today it can be solved in seconds what a decade ago

    would have taken years.

  • 8/11/2019 s_gams_ar.pdf

    7/48

    What is optimization?

    To find the value of the variables that make optimal the

    objective function satisfying the set of constraints.

    Algebraic modeling languages. GAMS - 7

  • 8/11/2019 s_gams_ar.pdf

    8/48

    Components of an optimization problem

    Objective function

    Quantitative performance function (fitness) of a system that we want

    to maximize or minimize

    Variables

    Decisions that influence the ob ective function

    Algebraic modeling languages. GAMS - 8

    Constraints

    Set of relations that variables are forced to satisfy

  • 8/11/2019 s_gams_ar.pdf

    9/48

    Alternatives for developing optimization models

    General purpose programming languages (C, C++, Java, Visual Basic,

    FORTRAN 90)

    C (CPLEX from ILOG, , Gurobi from Gurobi Optimization, Xpress-Optimizer

    from FICO)

    C++ (ILOG Concert from IBM, LINDO API from LINDO Systems, OptiMax

    2000 from Maximal Software, FLOPC++ from Universidade de Aveiro)

    Public domain [GNU Linear Programming Toolkit GLPK

    www. nu.or /software/ l k , Com utational Infrastructure for O erations

    Algebraic modeling languages. GAMS - 9

    Research COIN-OR (www.coin-or.org), LP solver SoPlex (http://soplex.zib.de)and MIP framework SCIP (http://scip.zib.de)]

    Numerical or symbolic languages or environments (spreadsheet, Matlab,

    Mathematica)

    Algebraic modeling languages [GAMS, OPL Studio, AMPL, AIMMS,XPRESS-MP, MPL, Zimpl (http://zimpl.zib.de) (last one of public

    domain)]

    Once a year in OR/MS Today (www.orms-today.com) there are summary

    papers of the different optimization environments and their characteristics

  • 8/11/2019 s_gams_ar.pdf

    10/48

    Solvers in spreadsheets

    Advantages

    Easy to use

    Total integration with the spreadsheet Familiarity with the environment that eases the explanation of the

    model and its results

    Easy representation of graphical results

    Algebraic modeling languages. GAMS - 10

    Drawbacks

    Do not induce good programming practices

    Difficulties for verification, updating and model documentation

    Do not allow to model complex or large-scale problems

  • 8/11/2019 s_gams_ar.pdf

    11/48

    Optimization library in C, C++

    Advantages

    Solution time is critical

    Allow the use of specific optimization algorithms Possibility of model implementation in a special software or hardware

    environment

    Algebraic modeling languages. GAMS - 11

    Drawbacks Greater difficulty and resources consumption for model maintenance

  • 8/11/2019 s_gams_ar.pdf

    12/48

    Algebraic modeling languages advantages (i)

    High level languages for compact formulation of large-scale

    and complex models

    Easy prototype development Improve modelers productivity

    Structure good modeling habits

    Algebraic modeling languages. GAMS - 12

    Separation of interface, data, model and solver Formulation independent of model size

    Model independent of solvers

  • 8/11/2019 s_gams_ar.pdf

    13/48

    Algebraic modeling languages advantages (ii)

    Easy continuous reformulation

    Documentation is made simultaneously to modeling

    Allow to build large maintainable models that can be adaptedquickly to new situations

    Allow advanced algorithm implementation

    Algebraic modeling languages. GAMS - 13

    Easy implementation of NLP, MIP, and MCP Open architecture with interfaces to other systems

    Platform independence and portability among platforms and

    operating systems (MS Windows, Linux, Sun Solaris, HPUX, Digital True64Unix, IBM AIX, SGI IRIX, Mac OS X)

  • 8/11/2019 s_gams_ar.pdf

    14/48

    Algebraic modeling languages drawbacks

    Not adequate for sporadic use with small problems

    Not adequate for direct resolution of very large-scale

    problems (1.000.000 x 1.000.000)

    Algebraic modeling languages. GAMS - 14

  • 8/11/2019 s_gams_ar.pdf

    15/48

    Future tendencies

    Visual interface for formulating

    Closer interface with spreadsheet and databases

    Interface with external functions written in general purposelanguages

    Direct resolution of stochastic optimization problems

    Algebraic modeling languages. GAMS - 15

    (OSLSE, DECIS) Automatic selection of optimization method and solver

  • 8/11/2019 s_gams_ar.pdf

    16/48

    Real applications

    GAMS is been used extensively to develop decision support

    models. It was developed in 1987. More than 10000 users in

    100 countries Problems with up to 190.000 constraints, 630.000 variables

    and 1.900.000 non zero elements easily solved in a PC with

    Algebraic modeling languages. GAMS - 16

    Development of advanced algorithms (like stochastic nested

    Benders decomposition) in some models

  • 8/11/2019 s_gams_ar.pdf

    17/48

    Transportation model

    There are i can factories and j consumption markets. Each

    factory has a maximum capacity of ai cases and each market

    demands a quantity of bj cases (it is assumed that the total

    production capacity is greater than the total market demand forthe problem to be feasible). The transportation cost between

    each factory i and each marketj for each case is cij. The

    Algebraic modeling languages. GAMS - 17

    demand must be satisfied at minimum cost.The decision variables of the problem will be cases transportedbetween each factory i and each marketj,xij.

  • 8/11/2019 s_gams_ar.pdf

    18/48

    Mathematical formulation

    Objective function

    Production limit for each factory i

    minij ij

    i j

    c x

    ij i

    j

    x a i

    Algebraic modeling languages. GAMS - 18

    Consumption in each marketj

    Quantity to send from each

    factory i to each marketj

    ij j

    i

    x b j

    0ijx i j

  • 8/11/2019 s_gams_ar.pdf

    19/48

    Transportation model in GAMS (i)

    ET

    I fbricas de envasado / VIGO, ALGECIRAS /J mercados de consumo / MARI, !ARCELO"A, VALE"CIA /

    #ARAMETER

    A$i% ca&acidad de &roducci'n de (a fbrica i )ca*as+/ VIGO -.ALGECIRAS .. /

    Algebraic modeling languages. GAMS - 19

    / MARI 0..!ARCELO"A 0-.VALE"CIA 1-. /

    TA!LE

    C$i,*% cos2e 2rans&or2e en2re i 3 * )4 &or ca*a+MARI !ARCELO"A VALE"CIA

    VIGO .5.6 .517 .5.8ALGECIRAS .5.- .51- .511

  • 8/11/2019 s_gams_ar.pdf

    20/48

    Transportation model in GAMS (ii)

    VARIA!LE

    9$i,*% ca*as 2rans&or2adas en2re fbrica i 3 mercado * )ca*as+CT cos2e de 2rans&or2e )4+

    #O ITIVE VARIA!LE

    9

    E:;ATIO"

    COSTE cos2e 2o2a( de 2rans&or2e )4+CA#ACIA$i% ca&acidad mE>;M

    $$i,*%, C$i,*% ? 9$i,*%% =

    CA#ACIA$i% 55;M

    $*, 9$i,*%% >L> A$i% =

    EMA"A$*% 55 ;M$i, 9$i,*%% >G> !$*% =

    MO EL

    TRA"S#ORTE / COSTE, CA#ACIA, EMA"A /

    OLVE

    TRA"S#ORTE ;SI"G L# MI"IMI@I"G CT

  • 8/11/2019 s_gams_ar.pdf

    21/48

    Problem library (+ 250 models)

    gamside > File > Model Library > Open GAMS Model Library

    http://www.doi.icai.upcomillas.es/simio/apuntes/a_casos.pdf

    Production management

    Agricultural economics

    Chemical engineering

    Forest engineering

    International commerce

    Algebraic modeling languages. GAMS - 21

    Economic development

    Micro and macroeconomics

    General equilibrium models

    Energy economics

    Finance Statistics, econometrics

    Management science and operations research

  • 8/11/2019 s_gams_ar.pdf

    22/48

    GAMS (General Algebraic Modeling System)

    Development environment gamside

    Users guide Help > GAMS Users Guide Solvers guide Help > docs > solvers

    Algebraic modeling languages. GAMS - 22

    Model: file_name.gms Results: file_name.lst

    Process log: file_name.log

  • 8/11/2019 s_gams_ar.pdf

    23/48

    General structure of GAMS sentences

    Lines with * in the first column are comments

    No distinction between uppercase and lowercase letters

    Parenthesis (), square bracket [] or braces {} can be usedindistinctly to distinguish levels

    Language reserved words appear in bold

    Algebraic modeling languages. GAMS - 23

    Sentences end with a ; (that can be suppressed when thefollowing word is a reserved one)

  • 8/11/2019 s_gams_ar.pdf

    24/48

    General structure of a model

    Set declaration. Initialization.

    Include and manipulate input data and parameters.

    Variables

    Equations

    Algebraic modeling languages. GAMS - 24

    Model

    Bounds and initialization of variables

    Solve the optimization problem Output of the results

  • 8/11/2019 s_gams_ar.pdf

    25/48

    Blocks in a GAMS model

    Mandatoryvariables

    equations

    model

    solve

    Algebraic modeling languages. GAMS - 25

    Optionalsets: (alias) alias (i,j) i and j can be used indistinctly

    Checking of domain indexes

    data: scalars, parameters, table

    Values of inf, eps are valid as data

  • 8/11/2019 s_gams_ar.pdf

    26/48

    variables

    There must always be a free variable to represent the value of theobjective function. The value of the variables are always kept.

    Type:free (by default) - to +

    positive 0 to +

    negative - to 0

    binar 0 or 1

    Algebraic modeling languages. GAMS - 26

    integer 0 to 100

    Suffixes:

    var_name.lo lower bound

    var_name.up upper bound

    var_name.l initial value before and optimal value after

    var_name.m marginal value (reduced cost)

    var_name.fx fixes a variable to a constant

  • 8/11/2019 s_gams_ar.pdf

    27/48

    equations

    Blocks:

    Declaration with explicative comment

    Mathematical expressions

    Types: =e= =, =l= , =g=

    Algebraic modeling languages. GAMS - 27

    Suffixes:

    eq_name.lo lower bound

    eq_name.up upper bound

    eq_name.l initial value before and optimal value after eq_name.m marginal value (dual variable or shadow price)

  • 8/11/2019 s_gams_ar.pdf

    28/48

    mode and solve

    model model_name / equation_names /

    model model_name / all/

    solve model_name using problem_type minimizing

    (maximizing) o.f._variable

    Algebraic modeling languages. GAMS - 28

  • 8/11/2019 s_gams_ar.pdf

    29/48

    Types of problems and solvers

    LP, RMIP (linear programming): BDMLP, CLP MILP (mixed integer linear programming): CPLEX, CBC, Gurobi,

    XPRESS

    NLP (non linear programming): CONOPT, MINOS, SNOPT,

    PATHNLP, LGO, MOSEK

    DNLP (non linear programming with non continuous derivatives):

    CONOPT, MINOS, SNOPT, BARON, LGO, OQNLP, MOSEK

    Algebraic modeling languages. GAMS - 29

    m xe nteger non near programm ng : , ,

    BARON, OQNLP

    SP (stochastic programming): DECIS, OSLSE

    MCP (mixed complementarity problem): MILES, PATH, NLPEC

    MPEC (mathematical programming with equilibrium constraints):

    CNS (constrained non lineal systems): CONOPT, PATH

    MPSGE (generalized equilibrium)

    GAMS Solvers (http://www.gams.com/solvers/index.htm)

  • 8/11/2019 s_gams_ar.pdf

    30/48

    $ Operator in assignments, summations, constraints

    Sets a condition

    $(VALUE > 0) $(NUMBER1 NUMBER2)

    On the left of an assignment: it does the assignment ONLY

    ifthe condition is satisfiedif (condition,

    DO THE ASSIGNMENT

    Algebraic modeling languages. GAMS - 30

    ;

    On the right of an assignment: it does the assignment

    ALWAYS and if the condition is not satisfied it assigns

    value 0

    if (condition,DO THE ASSIGNMENT

    else

    ASIGNS VALUE 0

    ) ;

  • 8/11/2019 s_gams_ar.pdf

    31/48

    Relational operator

    lt , eq =, ne , le =

    not, and, or, xor

    diag(set_element,set_element)={1,0}

    Algebraic modeling languages. GAMS - 31

    sameas(set_element,set_element)={T,F}

  • 8/11/2019 s_gams_ar.pdf

    32/48

    Functions

    Elementary: +, -, *,/, ** or power(x,n)

    ord, card ordinal and cardinal of a set

    With indexes: sum, prod, smax, smin

    Algebraic modeling languages. GAMS - 32

    Other functions: abs, arctan, sin, cos, ceil,floor, exp, log, log10, max, min, mod,

    round, sign, sqr, sqrt, trunc, normal,

    uniform

    Time functions: gyear, gmonth, gday, ghour,gminute, gsecond, gdow, gleap, jdate,

    jnow, jstart, jtime

  • 8/11/2019 s_gams_ar.pdf

    33/48

    Dynamic sets

    Subsets of static sets whose content may change byassignments

    sets M months / 1 * 12 /

    MP(m) even months ;

    display m ;

    MP(m) $[MOD(ord(m),2) = 0] = YES ;

    display mp ;

    Algebraic modeling languages. GAMS - 33

    =

    display mp ;

    MP(m) $(ord(m) = 3) = NO ;

    display mp ;

    Fundamental elements in developing GAMS models

    They must be used systematically to avoid the formulation of

    superfluous equations, variables or assignments

  • 8/11/2019 s_gams_ar.pdf

    34/48

    Operations with sets

    IntersectionD(a) = B(a) * C(a)

    UnionD(a) = B(a) + C(a)

    Complementary=

    Algebraic modeling languages. GAMS - 34

    DifferenceD(a) = B(a) - C(a)

  • 8/11/2019 s_gams_ar.pdf

    35/48

    Order of the sets can be relevant

    Algebraic modeling languages. GAMS - 35

  • 8/11/2019 s_gams_ar.pdf

    36/48

    Transshipment model

    Lets have a set of nodes connected by arcs. A node doesnt

    have to be connected with the remaining nodes. A node can

    generate, demand or transship according to its function. Total

    offer exceeds total demand. Maximum offer capacity and

    maximum demand in each node are assumed to be known.

    Also it is known the unit trans ortation cost of each roduct

    Algebraic modeling languages. GAMS - 36

    for each arc. We want to supply the demand whileminimizing the total transportation cost.

    Extend the former problem adding non directed arcs.

  • 8/11/2019 s_gams_ar.pdf

    37/48

    Index shifting. Lag and lead

    t=J,F,M,A,MA,J,JU,AU,S,O,N,DvReserve(t)+pInflow(t)-vOutflow(t) =e= vReserve(t+1)

    Vector values out of the domain are 0vReserve(D) + pInflow(D) - vOutflow(D) =e= 0

    Circular sequence of an index (++, --)t=1,...,12

    vReserve(t)+pInflow(t)-vOutflow(t) =e= vReserve(t++1)

    Algebraic modeling languages. GAMS - 37

    vReserve(12)+pInflow(12)-vOutflow(12) =e=

    vReserve(1)

    Inverted order sequence ofPP index even though i is traversed inincreasing orderPP(t+[card(t)-2*ord(t)+1])

  • 8/11/2019 s_gams_ar.pdf

    38/48

    Underground sellers

    The sales booths of an underground station need the following people for

    the 24 hours of each day

    Interval Sellers

    00 06 2

    06 10 8

    10 12 4

    12 16 3

    Algebraic modeling languages. GAMS - 38

    16 18 6

    18 22 5

    22 24 3

    Each seller works 8 hours in two blocks of 4 hours with 1 free hour after

    the first block. The shift may begin in every hour. Determine the

    minimum number of salespersons to be hired.

  • 8/11/2019 s_gams_ar.pdf

    39/48

    Lag and lead exercises

    Distances among crosses

    Suppose a city with a complete square grid with a unit length in each

    side. Compute analytically the distance between any two crosses of

    the city

    Maximum number of knights (queens, castles)

    Algebraic modeling languages. GAMS - 39

    Determine by an optimization problem the maximum number ofknights (queens, castles) that can be in a chessboard without threat

    among them

  • 8/11/2019 s_gams_ar.pdf

    40/48

    Repetitions

    loop (set,

    ) ;

    while (condition,

    ) ;

    repeat

    Algebraic modeling languages. GAMS - 40

    if (condition,

    else

    ) ;

    for (i=beginning to/downto end by increment,

    ) ;

  • 8/11/2019 s_gams_ar.pdf

    41/48

    GAMS features

    Use the good features of a language

    GAMS works in parallel (no loops except when needed)

    Use $ instead of if, then, else

    Replace repetitive expressions by calls to a common function

    GAMS allows the classical control structures as LOOP,

    Algebraic modeling languages. GAMS - 41

    , , , .

    Make your programs read from top to bottom.

    Use the fundamental control flow constructs.

    Write first in an easy-to-understand pseudo-language; then

    translate into whatever language you have to use.

    Follow each decision as closely as possible with its

    associated action.

    Use data arrays to avoid repetitive control sequences.

  • 8/11/2019 s_gams_ar.pdf

    42/48

    Data input/output

    Data input from a file$include file_name

    display identifier_name (shows its content or value)

    Algebraic modeling languages. GAMS - 42

    file internal_name / external_name /put internal_name

    put identifier_name

    putclose internal_name There are specific options to control the output format

  • 8/11/2019 s_gams_ar.pdf

    43/48

    TABLE (i)

    Table with multiple columns

    sets i / MAD, BCN /

    j / A1, A2, A3, A4, A5, A6 /

    table CAPACITY(i,j) maximum capacity

    Algebraic modeling languages. GAMS - 43

    MAD 1 0 3BCN 2 1 2

    +

    A4 A5 A6

    MAD 2 1 3

    BCN 3 2 2

    TABLE (ii)

  • 8/11/2019 s_gams_ar.pdf

    44/48

    TABLE (ii)

    Tables with more than 2 dimensionssets i / MAD, BCN /

    j / A1, A2, A3, A4, A5, A6 /

    K / A, B, C /

    table CAPACITY(i,j,k) maximum capacity

    A B C

    Algebraic modeling languages. GAMS - 44

    MAD.A2 2 1 2

    table CAPACITY(i,j,k) maximum capacity

    A1.A A1.B A1.C A2.A A2.B

    MAD 1 0 3 6 8

    BCN 2 1 2 2 4

    I t d O t t

  • 8/11/2019 s_gams_ar.pdf

    45/48

    Input and Output

    Use simple input formats. Make input easy to prepare and

    adapted to the user.

    Test input for validity and plausibility. This can be done ininput/output tool (e.g., using conditional formatting in Excel)

    or in GAMS

    Algebraic modeling languages. GAMS - 45

    , ,

    execution giving information to the user

    It is difficult to determine infeasibility of an optimization

    program. It is better to check bad data.

    Produce self-explanatory output. Modularity: data, model structure and results in different

    files

    E h t l h ifi

  • 8/11/2019 s_gams_ar.pdf

    46/48

    Each tool has a specific purpose

    Input/output data and simple graphs (Excel, Access)

    Advanced graphs (GNUPlot)

    Gdx utilities to interface with other applications

    Algebraic modeling languages. GAMS - 46

    S li

  • 8/11/2019 s_gams_ar.pdf

    47/48

    Scaling

    Solvers are powerful but not magic

    Input data and output results must be in commonly used

    scales but internally variables, equations, parameters must be

    around 1 (i.e., from 0.01 to 100)

    Algebraic modeling languages. GAMS - 47

  • 8/11/2019 s_gams_ar.pdf

    48/48

    Algebraic modeling languages. GAMS - 48

    Andrs Ramos

    http://www.iit.upcomillas.es/aramos/

    [email protected]