Monday, July 26, 2010

Scheduling problem formulation

A prototype model was sent to me that was basically a job-shop model. It was formulated with a time index. Unfortunately this model had a large number of time periods, and thus a large model resulted:

* Rows:       4942
* Columns:    10210 (10210 integer, 10210 binary)
* Non-zeros:  137091

The number of time periods needed was estimated as T = sum of processing times. This number is obviously a somewhat generous upper bound and as a result we have a large number of binary variables x[jobstep,timeperiod]. Running some heuristic before solving the MIP can help reducing the estimate for T.

Furthermore, this formulation contained quite a few equations of the form 0 >= 0. The reason was that a particular way was chosen to turn off certain equations:

eq{i in I}: p[i]*(…) >= 0;

where p is a parameter with 0,1 values. Although the LP/MIP pre-solver can take care of these, it is better not to generate those equations at all. I.e. use:

eq{i in I: p[i]=1}: … >= 0;

Even after the presolver removes a lot of rows and columns, the remaining MIP is still quite large:

   presolved MIP has 3302 rows, 7339 columns, 124084 non-zeros
   7339 integer columns, all of which are binary

With a continuous time formulation (no time index) and better care of generating only equations that are really needed, a much smaller model can be formed:

* Rows:       150
* Columns:    91 (36 integer, 36 binary)
* Non-zeros:  371

In this model T is no longer used to determine the number of variables. However it is used to form some big-M values which are present in “job step i is executed before or after job step j” (i.e. no overlap) constraints.

image

Large scale example:

tankchart1 tankchart2

Tuesday, July 20, 2010

Tough little MIP's

These are two instances (related to http://yetanothermathprogrammingconsultant.blogspot.com/2010/07/multiple-length-cutting-stock-problem.html) of very small MIP models that are very tough to solve to optimality. Size is very small: 26 rows and 73 columns. The first data set solves slowly, but the second one is really tough even for the best MIP solvers (at least with default settings).

Usually models of this size solve very quickly, but with MIP’s one should always be prepared for surprises.

Workaround: just stop after a little while: the obj is most likely optimal just not proven optimal. This behavior is not unusual: the mip solvers have good heuristics so they actually find the (near) optimal solution very quickly. But proving optimality by working on the best bound is not that easy and turns out to be very expensive. A second approach would be to use the alternative formulation mentioned in http://yetanothermathprogrammingconsultant.blogspot.com/2010/07/multiple-length-cutting-stock-problem.html.

To prove optimality it makes sense to use an option like MipEmphasis (Cplex) or MipFocus (Gurobi) to move the best bound. Dataset 1 then solves quite quickly but dataset 2 is still terrible.

Point two filenames to the same file?

In Windows there are many ways to describe the same file: UNC, old 8.3 notation, relative, absolute. Unfortunately it is not trivial to detect whether two filenames are pointing to the same file. Here is some code that does this: http://blogs.msdn.com/b/vbteam/archive/2008/09/22/to-compare-two-filenames-lucian-wischik.aspx.

(Under Unix I guess one would use the inode to check this)

Monday, July 12, 2010

Max Availability in Cutting Stock Model

In http://yetanothermathprogrammingconsultant.blogspot.com/2010/07/multiple-length-cutting-stock-problem.html an extension of the standard cutting stock model is discussed. A question was posed if it would be difficult to add some availability restriction to this model. I.e. example data can look like:
image
This can be implemented as follows. Our notation is again:
imageThen the master problem in the column generation approach can look like:
imagewhere we have the following constants:
imageThe subproblem can be formulated as:
image The initialization of the algorithm needs a little bit more sophistication than I had before, to make sure a feasible initial solution is created. Alternatively it may be more convenient to make the master elastic with respect to the availability constraint, so that it is easier to construct a feasible solution.

An example solution can look like:
 

Sunday, July 4, 2010

GAMS: display

While playing with my old column generation example http://www.amsterdamoptimization.com/pdf/colgen.pdf I wanted to change the display of of the patterns a little bit:

----    181 PARAMETER pat  pattern usage

                 p1         p15         p18         p21         p25         p26         p28         p31         p34

width1       12.000                               2.000                               1.000
width2                    1.000       3.000       2.000                   1.000       1.000
width3                                            2.000       2.000       3.000                               1.000
width4                                3.000
width5                                                                                            2.000
width6                                                        1.000       1.000                   4.000       5.000
width7                                                                                3.000
width9                    3.000
width10                                                                                           1.000       1.000
width13                                                       2.000                                           1.000
Count         4.000      27.000      49.000      61.000       1.000     222.000     479.000      61.000       3.000

      +         p36         p37         p38         p39         p40         p42         p43         p44         p45

width1                                            1.000                   4.000
width2                                2.000                   1.000                                           4.000
width3        1.000       2.000       1.000       1.000                   1.000       2.000       2.000
width4                                            3.000                               1.000
width6        1.000       5.000       1.000       2.000       4.000                               1.000
width7        1.000                                                                   1.000                   1.000
width8                                1.000
width9                                                                                            1.000
width10                                                                               2.000                   1.000
width11                               2.000                   3.000       3.000                   1.000
width12                   1.000
width13       2.000
Count       191.000     101.000     103.000       1.000      79.000       1.000       1.000     347.000      20.000

      +       Total

width1      654.000
width2     1362.000
width3     1987.000
width4      151.000
width5      122.000
width6     1946.000
width7     1649.000
width8      103.000
width9      428.000
width10      86.000
width11     793.000
width12     101.000
width13     387.000
Count      1751.000

Now I wanted to drop the zero’s after the decimal point, so I did:

option pat:0;
display pat;

This gave as result:

----    183 PARAMETER pat  pattern usage

                 p1         p15         p18         p21         p25         p26         p28         p31         p34

width1           12                                   2                        ***********1
width2                        1           3           2                       1           1
width3                                                2           2           3                                   1
width4                                    3
width5                                                                                                2
width6                                                            1           1                       4           5
width7                                                                                    3
width9                        3
width10                                                                                               1           1
width13                                                           2                                               1
Count             4          27          49          61           1         222         479          61           3

      +         p36         p37         p38         p39         p40         p42         p43         p44         p45

width1                                                1                       4
width2                                    2                       1                                               4
width3            1           2           1           1                       1           2           2
width4                                                3                                   1
width6            1           5           1           2           4                                   1
width7            1                                                                       1                       1
width8                                    1
width9                                                                                                1
width10                                                                                   2                       1
width11                                   2                       3           3                       1
width12                       1
width13           2
Count           191         101         103           1          79           1           1         347          20

      +       Total

width1          654
width2         1362
width3         1987
width4          151
width5          122
width6         1946
width7         1649
width8          103
width9          428
width10          86
width11         793
width12         101
width13         387
Count          1751

It is not clear what the stars are doing there. May be some debugging output left in the code.

Multiple length cutting stock problem

I received some test data to run against a cutting stock model (see http://yetanothermathprogrammingconsultant.blogspot.com/2009/09/vstosolver-foundation.html). The data approximately looks like:
image This cannot be solved using the standard model mentioned here and in http://www.amsterdamoptimization.com/pdf/colgen.pdf. The standard model assumes there is just one raw material length, and we have here a number of different lengths in stock. Note that we assume that stock availability is not a limiting factor. The question was: can we adapt the column generation approach to handle this.
The first document I found with google was this: http://www.aimms.com/aimms/download/manuals/aimms3om_cuttingstock.pdf. At first reading this did not look very promising for different reasons. First they use as objective in the multiple length version: minimize the cost of each stock length. I don’t have this data available, and we are aiming to minimize waste.  Note that in the standard cutting stock problem we can simply use the number of rolls of raw material as proxy to find the minimum waste. In this case this is not a good objective and we really need to model the wasted material explicitly.
A second problem was the simple way they dealt with the multiple lengths in the sub problem. They just run the subproblem several times:
image
In our case we have a somewhat large set of stock lengths, so that is not attractive. It was argued one could stop as soon as a good candidate was found. (Note: There is some confusion about what the sub and the master is as they use the term submodel for what usually is called the master problem.)
I believe there is a more elegant way to do this: let the pattern generation model (this is usually called the subproblem) find a candidate pattern and a candidate raw material length directly. A similar approach is written up here.
So with the notation:
image
we have:
image
and we can form a master problem as follows:
image
Note that I used an equality constraint here. That is to make sure we don’t produce more products with zero waste than needed. The subproblem can now look like:
image
Here lambda are the duals of the master problem. So the subproblem both finds a new pattern y(i) and which stock length k is to be used for this pattern. This actually solved the problem quickly (approx 10 seconds). A further improvement was added by preprocessing the data. If a demand length is equal to a stock length, we can immediately choose that stock length, and remove this demand from the model. If the problem is larger, it may be wise to add a tolerance, i.e. if a demanded length is within say 5% of a stock length, use that stock length.
It is interesting to see that the column generation algorithm actually is not that sensitive to the preprocessing step:
image
We stop a little bit earlier with preprocessing, but the expected better performance is not uniformly over the whole run. It is noted I used a simple minded assignment for the initial solution. Probably a simple bin-packing heuristic to find an initial solution can help to start the column generation algorithm with a better objective.
An alternative formulation would be: use inequality constraint in the master together with a cost coefficient of c(p)=L(k(p)). The subproblem needs to be updated slightly: drop U from the objective. This formulation gave similar results.

The solution looks like:

Friday, June 25, 2010

GAMS augmenting a set

Ina project we encountered the problem where we loaded a GDX file and subsequently wanted to augment one of the sets in that GDX file with some elements. The following trick will do this:

set i(*);
$gdxin data.gdx
$load i
* i has now elements a,b

$onmulti
set i /c/;
$offmulti
* i has now elements a,b,c

parameter p(i) /
 
a 1
 
b 2
 
c 3
/;
display i,p;

In the GDX file the set i has elements a and b. In the GAMS code we add the element c. This is not very intuitive, but it works. The advantage of this method is that we do this at compile time, i.e. we can declare parameters and variables over the set i. Note that we cannot use DISPLAY to debug this:

set i(*);
$gdxin data.gdx
$load i

display i;

$onmulti
set i /c/;
$offmulti

display i;

This code will show the same output for both display statements:

----      5 SET i 

a,    b,    c

----     11 SET i 

a,    b,    c

This is because the display statement is executed at execution time while the sets are formed at compile time.

Monday, June 21, 2010

Spreadsheets

http://eusprig.org/ has some interesting info on spreadsheet usage. Especially: http://eusprig.org/horror-stories.htm

Friday, June 18, 2010

NA in GAMS

Q: Does the value of A make sense; why is NA in there? There is no NA in B

set i /i1*i10/;
parameter A(i) /i1=1, i2=NA, i3=1e-10/;
display A;
set tiny(i);

tiny(i)$[  A(i)
and
          (Abs(A(i)) < 1e-5)
and
          (A(i) <>
NA)
       ] =
yes;
display tiny;

parameter B(i);
B(i)$(A(i) <>
NA) = A(i);
display B;

The purpose is to find the small values and store their index in set tiny. The result is:

----      5 PARAMETER A 

i1       1.000,    i2          NA,    i3 1.00000E-10

----     12 SET tiny 

i2,    i3

----     16 PARAMETER B 

i1       1.000,    i3 1.00000E-10

We see element i2 is present, although we included A(i)<>NA as condition. This is not completely intuitive, but for element i2, with A(‘i2’)=NA, the condition reads as:

tiny(‘i2’)$(NA and NA and NO) =  yes;

Now the rule is that any operation on NA (including ‘and’) results in NA. So the whole condition becomes:

NA and NA and NO => NA

As $NA evaluates to $1, we actually get that element tiny(i) set to YES.

Working with special values requires special attention, especially in complex expressions. Usually it is best to split this in parts:

set tiny2(i);
tiny2(i)$[A(i)
and (Abs(A(i)) < 1e-5)] = yes;
tiny2(i)$(A(i)=
NA) = no;
display tiny2;

Now this set will only contain the element i3.

Thursday, June 17, 2010

sparse matmul revisited

In http://yetanothermathprogrammingconsultant.blogspot.com/2009/02/timing-of-sparse-matrix-multiplication.html we time a (sparse and dense) matrix multiplication of two matrices (parameters). In OPL this can look like:
int N = 250;
{int} I = asSet(1..N);
float A[i in I][j in I] = (i==j)?1:0;
float B[i in I][j in I] = (i==j)?1:0;
//float A[i in I][j in I] = 1;
//float B[i in I][j in I] = 1;
float C[i in I][j in I] = sum(k in I) A[i,k]*B[k,j];
float s = sum(i in I, j in I) C[i,j];

execute{
  writeln(s);
}
The timings are included in the following table:

Model GNU Mathprog AMPL GAMS OPL
sparse (identity) 50 sec 3 sec 0.1 sec 17 sec
dense (all ones) 50 sec 3 sec 3 sec 17 sec
So in IBM OPL: no sparse processing, and a little bit slower than the other commercial modeling systems (but substantial faster than GNU's Mathprog).