Wednesday, May 27, 2009

Convert GAMS to OML

A simple way to generate scalar OML from GAMS is to use the CONVERT “dummy” solver. Use an option file convert.opt with

sfs

E.g. for the model trnsport.gms from the GAMS model library we do:

> gams trnsport lp=convert optfile=1

This will show:

--- Job trnsport.gms Start 05/27/09 17:03:47 WEX-VIS 23.0.2 x86/MS Windows       
GAMS Rev 230  Copyright (C) 1987-2009 GAMS Development. All rights reserved
Licensee: Erwin Kalvelagen                               G090213/0001CV-WIN
          Amsterdam Optimization Modeling Group                      DC4572
--- Starting compilation
--- trnsport.gms(69) 3 Mb
--- Starting execution: elapsed 0:00:00.003
--- trnsport.gms(45) 4 Mb
--- Generating LP model transport
--- trnsport.gms(66) 4 Mb
---   6 rows  7 columns  19 non-zeroes
--- Executing CONVERT: elapsed 0:00:00.009

Convert 2.0      Feb 14, 2009 23.0.2 WIN 9388.9411 VIS x86/MS Windows        

--- Using Option File
Reading parameter(s) from "C:\projects\ms\convert.opt"
>>  sfs
>>
Finished reading from "C:\projects\ms\convert.opt"
--- Writing SFS       : sfs.oml

--- Restarting execution
--- trnsport.gms(66) 0 Mb
--- Reading solution for model transport
--- Executing after solve: elapsed 0:00:00.063
--- trnsport.gms(68) 3 Mb
*** Status: Normal completion
--- Job trnsport.gms Stop 05/27/09 17:03:47 elapsed 0:00:00.083

The output will look like:

//  LP written by GAMS Convert at 05/27/09 17:03:47
//
//  Equation counts
//      Total        E        G        L        N        X        C
//          6        1        3        2        0        0        0
//
//  Variable counts
//                   x        b        i      s1s      s2s       sc       si
//      Total     cont   binary  integer     sos1     sos2    scont     sint
//          7        7        0        0        0        0        0        0
//  FX      0        0        0        0        0        0        0        0
//
//  Nonzero counts
//      Total    const       NL      DLL
//         19       19        0        0
//
//  Reformulation has removed 1 variable and 1 equation

Model[
  Decisions[Reals[0, Infinity],x1],
  Decisions[Reals[0, Infinity],x2],
  Decisions[Reals[0, Infinity],x3],
  Decisions[Reals[0, Infinity],x4],
  Decisions[Reals[0, Infinity],x5],
  Decisions[Reals[0, Infinity],x6],

Goals[ Minimize[    0.225*x1 + 0.153*x2 + 0.162*x3 + 0.225*x4 + 0.162*x5 + 0.126*x6 ]],

Constraints[

e2 ->   x1 + x2 + x3 <= 350,

e3 ->   x4 + x5 + x6 <= 600,

e4 ->   x1 + x4 >= 325,

e5 ->   x2 + x5 >= 300,

e6 ->   x3 + x6 >= 275 ]]

This format is called “scalar” model opposed to an “indexed” model. I produced by hand the following “indexed” model, which is preferable if you need to maintain the model in OML or if large amounts of data needs to be imported from data sources:

Model[
  Parameters[Sets,Plants,Markets],
  Parameters[Reals,Capacity[Plants],Demand[Markets],Cost[Plants,Markets]],

  Decisions[Reals[0,Infinity],x[Plants,Markets],TotalCost],

  Constraints[
     TotalCost == Sum[{i,Plants},{j,Markets},Cost[i,j]*x[i,j]],
     Foreach[{i,Plants}, Sum[{j,Markets},x[i,j]]<=Capacity[i]],
     Foreach[{j,Markets}, Sum[{i,Plants},x[i,j]]>=Demand[j]]
  ],

  Goals[Minimize[TotalCost]]
]

The scalar model may be especially useful for experimentation and evaluation.

Notes:

8 comments:

  1. extremely impressive !
    Can the user use such functions or it is your own addin ?
    Thanks

    ReplyDelete
  2. This is part of GAMS 23.0 (the currently shipping GAMS system).

    ReplyDelete
  3. Erwin, I am one your GAMS follower, is that possible for you to publish some GAMS and mathematical programming note you wrote before ?
    There are a number of pdf notes about LP/NLP and MILP in your personal site at GAMS before, we cant see them now.
    Thanks

    ReplyDelete
  4. Any document in particular you need?

    ReplyDelete
  5. Just some MILP modelling notes you have written before, thanks.

    ReplyDelete
  6. Need helps in GAMS code to performed constrained Linear regression, ... such that

    Min Y(j,t) = sum(Y(i,t-1)*P(i,j)) i =1,2,3 j = 1, 2,3 and t = 1, 2, 3,4,5. P(i,j) are parameter, and Y(j,t) are proportion observed of item j
    S.t 0 <= P(i,j) <= 1 and sum(P(i,j))= 1 sum over j.
    Year y1 y2 y3
    1 0.2794 0.4008 0.3198
    2 0.3418 0.3301 0.3281
    3 0.3867 0.3013 0.312
    4 0.4074 0.2906 0.302

    Thanks

    ReplyDelete
  7. Constrained regression is very easy to model in GAMS. The simplest way is to put a sum of squared residuals in the objective.

    ReplyDelete
  8. thanks, please am novice in gams, can u help with sample codes, please?

    ReplyDelete