Thursday, July 21, 2011

Pyomo

For a thoughtful comment on the design of Python based modeling system see: http://yetanothermathprogrammingconsultant.blogspot.com/2011/04/performance-of-python-based-modeling.html?showComment=1310240113570#c3299569615459362578.

My assessment was clearly not convincing. To summarize: essentially the modeling framework Pyomo is a scalar system. This compares somewhat to the difference between AMPL and GLPK (http://www.gnu.org/software/glpk/glpk.html). Both systems are written in C so we have taken out the language issue (i.e. Python is slow compared to C). The modeling system in GLPK (called Mathprog) is dense (except for the final generated matrix which is of course sparse). This actually work in many cases just fine: many sub-matrices used in the model are often small enough that dense handling is not really a problem. I have seen this in my practice: the majority of the models implemented in GLPK/Mathprog just work fine. Of course there are some models (the big sparse suckers) that bring GLPK/Mathprog on its knees while AMPL will happily generate the model very quickly. If I see this I generally suggest some reformulation or more often just give the advise: buy AMPL.

I suspect this can also happen with Pyomo. For some models a simple dense/scalar structure where we iterate over the carthesian product of indices will not perform. Of course with a system like Pyomo the modeler has more possibilities to implement better data-structures so we can skip zeros quickly and efficiently. But that means we have moved the burden from modeling system to the modeler.

Patent Application

One of my clients is applying for a patent on a model I helped them with. I am mentioned as co-inventor. That is mostly just an ego-booster I suspect, as this was work-for-hire. From what I understand the patent application is quite narrowly defined, so hopefully I am still able to use my bag of LP tricks for other clients.

Tuesday, July 5, 2011

Min sum or min num

In an advertisement model we may have to deal with underdelivery: the delivery of less impressions, visitors, GRPs than contractually agreed upon. So what to do. We can minimize the SUM of this quantity with the possible effect of many orders with small underdelivery numbers (spreading the pain). When minimizing the MAX this effect is even guaranteed. Or we can minimize the NUMber of orders with underdelivery. 

image

In practice, a good approach is to minimize a linear combination of both the SUM and the NUM objectives.

GAMS: GDXDUMP and Equations

There is a fairly deep issue when comparing GDX files if one of the GDX files has the following properties:

  1. It is produced by GDXDUMP
  2. It contains Equation records

The following illustrates the problem:

C:\projects\tmp>gamslib indus89
Copy ASCII: indus89.gms

C:\projects\tmp>gams indus89 lo=0 gdx=1

C:\projects\tmp>gdxdump 1.gdx output=2.gms
*  GDX dump of 1.gdx
*  Library in use : C:\PROGRA~1\GAMS23.7
*  Library version: GDX Library      BETA  8Jun11 23.7.0 WEX 25828.25830 WEI x86_64/MS Windows
*  File version   : GDX Library      BETA  8Jun11 23.7.0 WEX 25828.25830 WEI x86_64/MS Windows
*  Producer       : GAMS Base Module BETA  8Jun11 23.7.0 WEX 25828.25830 WEI x86_64/MS Windows
*  File format    :    7
*  Compression    :    0
*  Symbols        :  281
*  Unique Elements:  245

C:\projects\tmp>gams 2.gms lo=0 gdx=2

C:\projects\tmp>gdxdiff 1.gdx 2.gdx eps=1.0e-6
GDXDIFF          BETA  8Jun11 23.7.0 WEX 25828.25830 WEI x86_64/MS Windows
File1 : 1.gdx
File2 : 2.gdx
Summary of differences:
   bdraft   Data are different
   brepco   Data are different
  ccombal   Data are different
  consbal   Data are different
     cost   Data are different
   demnat   Data are different
divcnlsea   Data are different
   divsea   Data are different
   fodder   Data are different
   grnfdr   Data are different
   laborc   Data are different
     nbal   Data are different
  nwfpalc   Data are different
     objn   Data are different
    objnn   Data are different
     objz   Data are different
    objzn   Data are different
  protein   Data are different
   prseaw   Data are different
  qcombal   Data are different
  subirrc   Data are different
   tdraft   Data are different
watalcpro   Data are different
watalcsea   Data are different
  watalcz   Data are different
waterbaln   Data are different
Output: diffile.gdx
GDXDiff finished

C:\projects\tmp>gdxdump diffile.gdx Symbols
*  GDX dump of diffile.gdx
*  Library in use : C:\PROGRA~1\GAMS23.7
*  Library version: GDX Library      BETA  8Jun11 23.7.0 WEX 25828.25830 WEI x86_64/MS Windows
*  File version   : GDX Library      BETA  8Jun11 23.7.0 WEX 25828.25830 WEI x86_64/MS Windows
*  Producer       : GDXDIFF
*  File format    :    7
*  Compression    :    0
*  Symbols        :   26
*  Unique Elements:  249
   Symbol    Dim Type  Explanatory text
1 bdraft      4  Equ  Differences
2 brepco      3  Equ  Differences
3 ccombal     4  Equ  Differences
4 consbal     4  Equ  Differences
5 cost        3  Equ  Differences
6 demnat      3  Equ  Differences
7 divcnlsea   3  Equ  Differences
8 divsea      2  Equ  Differences
9 fodder      4  Equ  Differences
10 grnfdr      4  Equ  Differences
11 laborc      4  Equ  Differences
12 nbal        3  Equ  Differences
13 nwfpalc     2  Equ  Differences
14 objn        1  Equ  Differences
15 objnn       1  Equ  Differences
16 objz        1  Equ  Differences
17 objzn       1  Equ  Differences
18 protein     4  Equ  Differences
19 prseaw      3  Equ  Differences
20 qcombal     4  Equ  Differences
21 subirrc     4  Equ  Differences
22 tdraft      4  Equ  Differences
23 watalcpro   3  Equ  Differences
24 watalcsea   3  Equ  Differences
25 watalcz     4  Equ  Differences
26 waterbaln   4  Equ  Differences

C:\projects\tmp>

Apart from a small bug related to $ONEMPTY, it turns out that the way GDXDUMP exports equations records, it looses information about the equation type. This leads to the above results. Luckily in practice this will not be an issue, as equations are not often part of such an exercise.

Background.

During a course I taught about GAMS I was asked if it is possible to edit GDX files. The answer is: GDXDUMP will give you a GAMS representation of a GDX file which you can edit. Using the call:

GAMS gmsfile gdx=newgdxfile

you can make a new GDX file with the changes incorporated. The tool GDXDIFF will allow you to monitor the changes.

Although this is correct, I chose the INDUS89 example as illustration. That was unfortunate, as that failed because of the exotic Equation problem.

Excel issue

I was doing some reporting on the results of a large, complex MIP model. I made the unfortunate error of having some spurious blank cells. With the =SUM() function, skipping such cells is identical as considering them as zero, so there is no problem. With a =MIN() function however this is not the case. Even =MINA() will not help there:

image

It is sometimes good to work with different people on a project so that one of your collaborators can catch this before submitting results to the customer!

In this case the result was beneficial for us. With this, our estimated improvement in monthly revenue when using a MIP based model, increased from 250K euros to 300K euros.

Friday, July 1, 2011

How bumblebees tackle the traveling salesman problem

http://www.eurekalert.org/pub_releases/2011-06/qmuo-hbt062811.php

Sorting in GAMS

This question came up during a workshop I taught at a large power company:

I want to generate a number of load-duration and price-duration curves, but it takes too long.

Here is a small example demonstrating some formulations for sorting a 1d parameter:

option profile=1;
set i /i1*i10000/
;
parameter
p(i);
p(i) = uniform(1,100);

display
p;

execute_unload "p"
,p;
execute "gdxrank p.gdx p2.gdx"
;

parameter
pindex(i);
execute_load "p2.gdx"
,pindex=p;
display
pindex;


* slow

parameter p2(i);
alias
(i,j);
p2(i)=
sum(j$(ord
(i)=pindex(j)),p(j));
display
p2;

* almost as slow

parameter p3(i);
alias
(i,j);
loop((i,j)$(ord
(i)=pindex(j)),
   p3(i)=p(j);
);

display
p3;

* fast using trick:

parameter p4(i);
p4(i+(pindex(i)-
ord
(i))) = p(i);
display
p4;

The timings are:

Method Time (seconds)
SUM (assignment to p2) 28.407
LOOP (assignment to p3) 22.230
TRICK (assignment to p4) 0.265

Thursday, June 30, 2011

Cplex polishing

For some difficult large models, the Polishing facility in Cplex can sometimes do extremely well. This is an example:

Elapsed real time = 874.90 sec. (tree size = 11.31 MB, solutions = 2)
    233   235     -158.9737  3551      621.0895     -159.3086   258914  125.65%
    234   236     -158.9448  3578      621.0895     -159.3086   258964  125.65%
    266   268     -158.9498  3578      621.0895     -159.3086   265158  125.65%
    306   308     -158.9338  3568      621.0895     -159.3086   271534  125.65%
    310   312     -158.9315  3511      621.0895     -159.3086   272354  125.65%
    312   314     -158.9489  3581      621.0895     -159.3086   272367  125.65%
    322   324     -158.9310  3574      621.0895     -159.3086   273646  125.65%
    346   348     -158.8857  3561      621.0895     -159.3086   277217  125.65%
    354   356     -158.8368  3532      621.0895     -159.3086   278697  125.65%
    370   372     -158.8741  3567      621.0895     -159.3086   281105  125.65%
Elapsed real time = 995.15 sec. (tree size = 49.71 MB, solutions = 2)

Starting condition for polishing reached (PolishAfterTime).
Starting solution polishing.

*   390+  390                          360.3876     -159.3086   285698  144.20%
*   390+  390                          231.7287     -159.3086   285698  168.75%
*   390+  390                          100.2251     -159.3086   285698  258.95%
    390   392     -158.8957  3503      100.2251     -159.3086   285698  258.95%
    391   393     -158.9880  3601      100.2251     -159.3086   286729  258.95%
    392   394     -158.9935  3613      100.2251     -159.3086   286790  258.95%
    393   395     -158.9735  3574      100.2251     -159.3086   287216  258.95%
    394   396     -158.9763  3595      100.2251     -159.3086   287247  258.95%
    395   397     -158.9980  3601      100.2251     -159.3086   287328  258.95%
    396   398     -158.9806  3600      100.2251     -159.3086   287599  258.95%
    397   399     -159.0641  3615      100.2251     -159.3086   296751  258.95%

GUB cover cuts applied:  854
Clique cuts applied:  14
Cover cuts applied:  1540
Implied bound cuts applied:  20
Flow cuts applied:  922
Mixed integer rounding cuts applied:  67
Zero-half cuts applied:  1169
Gomory fractional cuts applied:  236

Root node processing (before b&c):
  Real time             =  127.44
Parallel b&c, 8 threads:
  Real time             = 7070.70
  Sync time (average)   =  168.52
  Wait time (average)   =    0.00
                          -------
Total (root+branch&cut) = 7198.14 sec.
MIP status(107): time limit exceeded

Although the relative gap looks bad, this is in fact a very good solution. The reason for the gap problem is that we are relatively close to zero in our objective (compared to solutions found by heuristics) and the fact that we are crossing zero in the gap calculation does not help.

Saturday, June 18, 2011

Advanced basis with Cplex

Usually Cplex behaves very predictable with LP’s that have an advanced basis to start from. But now I have something that puzzles me. Basically I do a solve from an optimal solution. This should normally run in just a few iterations. But with the default setting I see:

First model

Iteration log . . .
Iteration:     1   Dual objective     =             1.005320
Iteration:   581   Dual objective     =           296.751221
Iteration:  1247   Dual objective     =           511.623938
Iteration:  1681   Dual objective     =           514.072257
LP status(1): optimal

Restart

Iteration log . . .
Iteration:     1    Objective     =           515.104554
Perturbation started.
Iteration:   402    Objective     =           515.104554
Iteration:   806    Objective     =           515.104088
Iteration:  1107    Objective     =           515.103794
Iteration:  1406    Objective     =           515.103557
Iteration:  1684    Objective     =           515.103314
Iteration:  1939    Objective     =           515.103216
Iteration:  2186    Objective     =           515.103124
Iteration:  2434    Objective     =           515.103037
Iteration:  2672    Objective     =           515.102875
Iteration:  2951    Objective     =           515.102790
Iteration:  3210    Objective     =           515.102730
Iteration:  3472    Objective     =           515.102670
Iteration:  3709    Objective     =           515.102630
Iteration:  3943    Objective     =           515.102592
Iteration:  4199    Objective     =           515.102525
Iteration:  4445    Objective     =           515.102493
Iteration:  4692    Objective     =           515.102468
Iteration:  4933    Objective     =           515.102448
Iteration:  5181    Objective     =           515.102428
Iteration:  5431    Objective     =           515.102411
Iteration:  5689    Objective     =           515.102395
Iteration:  5962    Objective     =           515.102379
Iteration:  6224    Objective     =           515.102362
Iteration:  6487    Objective     =           515.102353
Iteration:  6765    Objective     =           515.102342
Iteration:  7041    Objective     =           515.102335
Removing perturbation.
LP status(1): optimal

The restart from an optimal basis takes more iterations than solving from scratch. This is with a default setting of LPMETHOD=0 (automatic). When I use LPMETHOD=1 (Primal Simplex) or LPMETHOD=2 (Dual Simplex) I see:

Restart with LPMETHOD=1

Primal crossover.
  Primal:  Fixed no variables.
  Dual:  Fixing 993 variables.
      992 DMoves:  Infeasibility 6.76704726e+000  Objective 5.15104554e+002
      394 DMoves:  Infeasibility 1.33823843e+000  Objective 5.15104554e+002
        0 DMoves:  Infeasibility 7.17019911e-001  Objective 5.15104554e+002
  Dual:  Pushed 0, exchanged 993.

Iteration log . . .
Iteration:     1    Objective     =           515.104554
LP status(1): optimal

Restart with LPMETHOD=2

Dual crossover.
  Dual:  Fixing 994 variables.
      993 DMoves:  Infeasibility 2.07167616e-013  Objective 5.15104554e+002
      414 DMoves:  Infeasibility 7.97140132e-014  Objective 5.15104554e+002
        0 DMoves:  Infeasibility 0.00000000e+000  Objective 5.15104554e+002
  Dual:  Pushed 1, exchanged 993.
  Primal:  Fixed no variables.
LP status(1): optimal

These seem indeed to take fewer iterations to solve the restarted model. I always assumed LPMETHOD=0 would just choose primal or dual simplex, but apparently it also does other things, and in this case that is not helping.

Update

I did some more experiments. With LPMETHOD=0 I can now also achieve a restart if I set all variable levels to zero. I.e.

solve m minimizing z using lp;
solve
m minimizing z using lp;

gives a bad restart, while:

solve m minimizing z using lp;
x.l(i,j) = 0;
z.l=0;

solve
m minimizing z using lp;

gives me zero iterations for the restart. Something is not completely right. As Bo suggested in his comments, the reason may be related to Cplex trying to do a presolve on an advanced basis. (I have heard about some ancient solvers that tried a basis preserving presolve, may be more limited than what current presolvers do). The difference between LPMETHOD=0 and both LPMETHOD=1 and LPMETHOD=2 is still a mystery to me.

Update 2

The behavior can be partially explained by looking at the Cplex documentation describing the Advind parameter:

For continuous models solved with simplex, setting 1 (one) will use the currently loaded basis. If a basis is available only for the original, unpresolved model, or if CPLEX has a start vector rather than a simplex basis, then the simplex algorithm will proceed on the unpresolved model. With setting 2, CPLEX will first perform presolve on the model and on the basis or start vector, and then proceed with optimization on the presolved problem.

I was using the model from a GAMS environment. There the default is 2. The documentation for GAMS/Cplex is somewhat more limited than the above paragraph:

Advind (Integer)
Use an Advanced Basis. GAMS/Cplex will automatically use an advanced basis from a previous solve
statement. The GAMS Bratio option can be used to specify when not to use an advanced basis. The Cplex
option advind can be used to ignore a basis passed on by GAMS (it overrides Bratio).
(default = determined by GAMS Bratio)
0 Do not use advanced basis
1 Use advanced basis if available
2 Crash an advanced basis if available (use basis with presolve)

Looks like 1 is better for a very good basis, 2 may be better for models with a basis that is not very good (in that case presolving will pay off even when destroying some basis information).