Modifying Parameter in MCP ModelInstance Topic is solved

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
Jan
User
User
Posts: 10
Joined: 7 years ago

Modifying Parameter in MCP ModelInstance

Post by Jan »

Dear all,

I try to modify a parameter of a ModelInstance representing and MCP model. A minimal example based on the MCP transport model from the library is given below. Instantiating the model, an error is raised from "gmoLoadDataLegacy" telling me that I have an unmatched column and no unmatched row (which reminds me on the unmatched variable/equation error of MCP models).
*** gmoLoadDataLegacy: fillMatches failed: matching error: nUnmatchedCols = 1 but nUnmatchedRows = 0
*** Could not load data from file: matching error: nUnmatchedCols = 1 but nUnmatchedRows = 0
Against the background, that the documentation states, that modifiers are converted to variables in the ModelInstance, I have two questions:

(1) Is it possible to manipulate MCP models using ModelInstances?
(2) If yes, can you provide me a hint what is going wrong with the code below?

Best regards
Jan

P.S. Versions: Python 2.7, Windows 7, GAMS 24.8.3

Code: Select all

# coding: utf-8
import gams

model_text = """$Title Transportation model as equilibrium problem (TRANSMCP,SEQ=126)
$Ontext
   Dantzig's original transportation model (TRNSPORT) is
   reformulated as a linear complementarity problem.  We first
   solve the model with fixed demand and supply quantities, and
   then we incorporate price-responsiveness on both sides of the
   market.


Dantzig, G B, Chapter 3.3. In Linear Programming and Extensions.
Princeton University Press, Princeton, New Jersey, 1963.

$Offtext

  Sets
       i   canning plants   / seattle, san-diego /
       j   markets          / new-york, chicago, topeka / ;

  Parameters

       a(i)  capacity of plant i in cases (when prices are unity)
         /    seattle     350
              san-diego   600  /,

       b(j)  demand at market j in cases (when prices equal unity)
         /    new-york    325
              chicago     300
              topeka      275  /,

        esub(j)  price elasticity of demand (at prices equal to unity)
         /    new-york    1.5
              chicago     1.2
              topeka      2.0  /
* ADDED DEMAND MULTIPLIER              
        multdem    demand mulitplier   /1/  
              
              ;

  Table d(i,j)  distance in thousands of miles
                    new-york       chicago      topeka
      seattle          2.5           1.7          1.8
      san-diego        2.5           1.8          1.4  ;

  Scalar f  freight in dollars per case per thousand miles  /90/ ;

  Parameter c(i,j)  transport cost in thousands of dollars per case ;

            c(i,j) = f * d(i,j) / 1000 ;

  Parameter pbar(j) reference price at demand node j;

  Positive variables
       w(i)             shadow price at supply node i,
       p(j)             shadow price at demand node j,
       x(i,j)           shipment quantities in cases;

  Equations
       supply(i)        supply limit at plant i,
       fxdemand(j)      fixed demand at market j,
       prdemand(j)      price-responsive demand at market j,
       profit(i,j)      zero profit conditions;

profit(i,j)..   w(i) + c(i,j)   =g= p(j);

supply(i)..     a(i) =g= sum(j, x(i,j));

* ADDED DEMAND MULTIPLIER
fxdemand(j)..   sum(i, x(i,j))  =g=  b(j)*multdem;

prdemand(j)..   sum(i, x(i,j))  =g=  b(j) * (pbar(j)/p(j))**esub(j);

*       declare models including specification of equation-variable
*       association:

  Model fixedqty / profit.x, supply.w, fxdemand.p/ ;
  Model equilqty / profit.x, supply.w, prdemand.p/ """

# initialize workspace, options, and checkpoint
ws = gams.GamsWorkspace(debug=2)
opt = ws.add_options()
opt.mcp = "PATH" 
cp = ws.add_checkpoint()

# create checkpoint running GAMS file 
j_modeltext = ws.add_job_from_string(model_text)
j_modeltext.run(checkpoint=cp)

# intitialize GAMSModelInstance
mi = cp.add_modelinstance()

# add changing parameter to sync_db
multdem = mi.sync_db.add_parameter("multdem", 0, "uniform demand multiplier")

# instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare bmult mutable
mi.instantiate("fixedqty using MCP", gams.GamsModifier(multdem), opt)
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Modifying Parameter in MCP ModelInstance

Post by bussieck »

The Python API has come a far way. The model provided by Jan works with GAMS 37 (I added a mi.solve()):

Code: Select all

D:\Users\mbussieck\Documents\GAMS\Studio\workspace>\gams\37\GMSPython\python.exe mcp.py
--- Job _gams_py_gjo0.gms Start 12/08/21 00:07:12 37.1.0 r07954d5 WEX-WEI x86 64bit/MS Windows
--- Applying:
    D:\GAMS\37\gmsprmNT.txt
    D:\Users\mbussieck\Documents\GAMS\gamsconfig.yaml
--- GAMS Parameters defined
    LP CPLEX
    MIP CPLEX
    RMIP CPLEX
    NLP CONOPT
    MCP PATH
    MPEC NLPEC
    RMPEC NLPEC
    CNS CONOPT
    DNLP CONOPT
    RMINLP CONOPT
    MINLP DICOPT
    QCP CONOPT
    MIQCP SBB
    RMIQCP CONOPT
    EMP JAMS
    Input C:\Users\MBUSSI~1\AppData\Local\Temp\tmpp0xoxup0\_gams_py_gjo0.gms
    Output C:\Users\MBUSSI~1\AppData\Local\Temp\tmpp0xoxup0\_gams_py_gjo0.lst
    Save C:\Users\MBUSSI~1\AppData\Local\Temp\tmpp0xoxup0\_gams_py_gcp0.g00
    ScrDir C:\Users\MBUSSI~1\AppData\Local\Temp\tmpp0xoxup0\225a\
    SysDir D:\GAMS\37\
    CurDir C:\Users\MBUSSI~1\AppData\Local\Temp\tmpp0xoxup0\
    LogOption 3
    LogFile C:\Users\MBUSSI~1\AppData\Local\Temp\tmpp0xoxup0\_gams_py_gjo0.log
    GDX C:\Users\MBUSSI~1\AppData\Local\Temp\tmpp0xoxup0\_gams_py_gdb0.gdx
Licensee: Eval License                                   S211111|0002CB-GEN
          GAMS Software GmbH, Braunschweig Office                    DCE751
          D:\GAMS\37\gamslice.txt
          License Admin: Michael Bussieck, MBussieck@gams.com
          Evaluation license: Not for commercial or production use
Processor information: 1 socket(s), 8 core(s), and 16 thread(s) available
GAMS 37.1.0   Copyright (C) 1987-2021 GAMS Development. All rights reserved
--- Starting compilation
--- _gams_py_gjo0.gms(76) 3 Mb
--- Starting execution: elapsed 0:00:00.018
--- _gams_py_gjo0.gms(48) 4 Mb
--- GDX File C:\Users\MBUSSI~1\AppData\Local\Temp\tmpp0xoxup0\_gams_py_gdb0.gdx
*** Status: Normal completion
--- Job _gams_py_gjo0.gms Stop 12/08/21 00:07:12 elapsed 0:00:00.058
--- Job _gams_py_gjo1.gms Start 12/08/21 00:07:12 37.1.0 r07954d5 WEX-WEI x86 64bit/MS Windows
--- Applying:
    D:\GAMS\37\gmsprmNT.txt
    D:\Users\mbussieck\Documents\GAMS\gamsconfig.yaml
--- GAMS Parameters defined
    LP CPLEX
    MIP CPLEX
    RMIP CPLEX
    NLP CONOPT
    MCP PATH
    MPEC NLPEC
    RMPEC NLPEC
    CNS CONOPT
    DNLP CONOPT
    RMINLP CONOPT
    MINLP DICOPT
    QCP CONOPT
    MIQCP SBB
    RMIQCP CONOPT
    EMP JAMS
    Restart C:\Users\MBUSSI~1\AppData\Local\Temp\tmpp0xoxup0\_gams_py_gcp0.g00
    Input C:\Users\MBUSSI~1\AppData\Local\Temp\tmpp0xoxup0\_gams_py_gjo1.gms
    Output C:\Users\MBUSSI~1\AppData\Local\Temp\tmpp0xoxup0\_gams_py_gjo1.lst
    ScrDir C:\Users\MBUSSI~1\AppData\Local\Temp\tmpp0xoxup0\_gams_py_gmi0\
    SysDir D:\GAMS\37\
    CurDir C:\Users\MBUSSI~1\AppData\Local\Temp\tmpp0xoxup0\
    LogOption 3
    LogFile C:\Users\MBUSSI~1\AppData\Local\Temp\tmpp0xoxup0\_gams_py_gmi0\gamslog.dat
    SolverCntr gamscntr.dat
Licensee: Eval License                                   S211111|0002CB-GEN
          GAMS Software GmbH, Braunschweig Office                    DCE751
          D:\GAMS\37\gamslice.txt
          License Admin: Michael Bussieck, MBussieck@gams.com
          Evaluation license: Not for commercial or production use
Processor information: 1 socket(s), 8 core(s), and 16 thread(s) available
GAMS 37.1.0   Copyright (C) 1987-2021 GAMS Development. All rights reserved
--- Starting continued compilation
--- Workfile was generated under GAMS version WEX371-371
--- _gams_py_gjo1.gms(8) 3 Mb
--- Starting execution: elapsed 0:00:00.007
--- _gams_py_gjo1.gms(84) 4 Mb
---
--- collecting and writing gdx file
---
--- Generating MCP model fixedqty
--- _gams_py_gjo1.gms(84) 6 Mb
---   11 rows  12 columns  27 non-zeroes
---   1 fixed-and-unmatched column(s) will be treated as constant
---   0 nl-code  0 nl-non-zeroes
--- _gams_py_gjo1.gms(84) 4 Mb
--- Reading solution for model fixedqty
--- Executing after solve: elapsed 0:00:00.050
*** Status: Normal completion
--- Job _gams_py_gjo1.gms Stop 12/08/21 00:07:13 elapsed 0:00:00.050
Reading dictionary...
Reading row data...
Evaluating functions...
Checking model...
Calculating Jacobian...

PATH             37.1.0 r07954d5 Released Nov 11, 2021 WEI x86 64bit/MS Window

11 row/cols, 24 non-zeros, 19.83% dense.

Path 5.0.04 (Thu Nov 11 10:42:20 2021)
Written by Todd Munson, Steven Dirkse, Youngdae Kim, and Michael Ferris


INITIAL POINT STATISTICS
Maximum of X. . . . . . . . . .  0.0000e+00 var: (x(seattle,new-york))
Maximum of F. . . . . . . . . .  6.0000e+02 eqn: (supply(san-diego))
Maximum of Grad F . . . . . . .  1.0000e+00 eqn: (supply(seattle))
                                            var: (x(seattle,new-york))

INITIAL JACOBIAN NORM STATISTICS
Maximum Row Norm. . . . . . . .  3.0000e+00 eqn: (supply(seattle))
Minimum Row Norm. . . . . . . .  2.0000e+00 eqn: (profit(seattle,new-york))
Maximum Column Norm . . . . . .  3.0000e+00 var: (w(seattle))
Minimum Column Norm . . . . . .  2.0000e+00 var: (x(seattle,new-york))

Crash Log
major  func  diff  size  residual    step       prox   (label)
    0     0             1.0416e+03             0.0e+00 (fxdemand(new-york))
    1     1     9     3 1.0029e+03  1.0e+00    1.0e+01 (fxdemand(new-york))
    2     2     6     9 9.4289e+02  1.0e+00    9.0e+00 (fxdemand(new-york))
    3     3     0     9 8.8483e+02  1.0e+00    8.1e+00 (fxdemand(new-york))
pn_search terminated: no basis change.

Major Iteration Log
major minor  func  grad  residual    step  type prox    inorm  (label)
    0     0     4     4 8.8483e+02           I 7.3e+00 4.9e+02 (fxdemand(new-yo)
    1     1     5     5 7.2119e+02  1.0e+00 SO 2.9e+00 2.6e+02 (profit(seattle,)
    2     2     6     6 3.6245e+02  1.0e+00 SO 1.2e+00 1.9e+02 (profit(san-dieg)
    3     2     7     7 5.9071e+01  1.0e+00 SO 4.7e-01 3.3e+01 (supply(san-dieg)
    4     1     8     8 1.6118e+01  1.0e+00 SO 1.9e-01 7.7e+00 (supply(seattle))
    5     6     9     9 4.3905e-01  1.0e+00 SO 7.5e-02 2.2e-01 (profit(san-dieg)
    6     4    10    10 2.0855e-01  1.0e+00 SO 3.0e-02 1.1e-01 (profit(seattle,)
    7     1    11    11 2.6297e-02  1.0e+00 SO 1.2e-02 1.8e-02 (profit(san-dieg)
    8     1    12    12 2.6239e-02  1.0e+00 SO 2.6e-03 1.8e-02 (profit(san-dieg)
    9     1    13    13 2.6238e-02  1.0e+00 SO 1.1e-03 1.8e-02 (profit(san-dieg)
   10     2    14    14 2.3838e-02  1.0e+00 SO 4.2e-04 1.4e-02 (profit(san-dieg)
   11     2    15    15 9.5865e-03  1.0e+00 SO 1.7e-04 6.0e-03 (profit(san-dieg)
   12     3    16    16 5.6196e-03  1.0e+00 SO 6.7e-05 3.9e-03 (profit(seattle,)
   13     2    17    17 3.5355e-03  1.0e+00 SO 2.7e-05 2.0e-03 (profit(san-dieg)
   14     2    18    18 6.9529e-08  1.0e+00 SO 1.1e-05 6.6e-08 (fxdemand(chicag)

FINAL STATISTICS
Inf-Norm of Complementarity . .  1.5198e-08 eqn: (profit(seattle,chicago))
Inf-Norm of Normal Map. . . . .  6.5961e-08 eqn: (fxdemand(chicago))
Inf-Norm of Minimum Map . . . .  6.5961e-08 eqn: (fxdemand(chicago))
Inf-Norm of Fischer Function. .  6.5961e-08 eqn: (fxdemand(chicago))
Inf-Norm of Grad Fischer Fcn. .  6.5961e-08 eqn: (profit(seattle,chicago))
Two-Norm of Grad Fischer Fcn. .  9.8329e-08

FINAL POINT STATISTICS
Maximum of X. . . . . . . . . .  3.0000e+02 var: (x(seattle,chicago))
Maximum of F. . . . . . . . . .  5.0000e+01 eqn: (supply(san-diego))
Maximum of Grad F . . . . . . .  1.0000e+00 eqn: (supply(seattle))
                                            var: (x(seattle,new-york))

 ** EXIT - solution found.

Major Iterations. . . . 14
Minor Iterations. . . . 30
Restarts. . . . . . . . 0
Crash Iterations. . . . 3
Gradient Steps. . . . . 0
Function Evaluations. . 18
Gradient Evaluations. . 18
Basis Time. . . . . . . 0.000000
Total Time. . . . . . . 0.031000
Residual. . . . . . . . 6.952922e-08
-Michael
Post Reply