Thursday, June 13, 2013

Creating sparse data

To test certain modeling constructs it may be useful to generate some random data. In GAMS this is usually done through something like:

a(i,j,k) = uniform(-100,100);

When we want to have a sparse data set we can use the following:

a(i,j,k) = uniform(-100,100)$(uniform(0,1)<0.1);

i.e. with probability 0.1 generate an entry in A with a random value between -100 and 100.

This can also be written as:

a(i,j,k)$(uniform(0,1)<0.1) = uniform(-100,100);

For very large, very sparse structures it may be even better to generate a random sparsity pattern first. E.g. using something like:

image