I am a full-time consultant and provide services related to the design, implementation and deployment of mathematical programming, optimization and data-science applications. I also teach courses and workshops. Usually I cannot blog about projects I am doing, but there are many technical notes I'd like to share. Not in the least so I have an easy way to search and find them again myself. You can reach me at erwin@amsterdamoptimization.com.
Friday, June 28, 2013
Thursday, June 27, 2013
Calling R from GAMS to produce high quality graphs
Here is a small demo script that shows how we can call R from GAMS to produce graphs using ggplot.
* name of the R script to execute |
Results look like:
We reproduced these graphs from from:
Friday, June 21, 2013
Monday, June 17, 2013
Neural Networks in GAMS
I was running some neural networks with a back propagation algorithm using a rather simple optimization tool (gradient descent). This actually ran pretty decent. Now lets if we can formulate something like this in GAMS. I tried something like:
I know the formulation is not great but may be better high-performance sparse solvers would help me out here and still do a decent job. Unfortunately, the result was dramatically poor. Most solvers ran out of memory, and MINOS was still running after 3 hours.
The basic problem is that we really only want THETA2 and THETA3 as variables and all the rest just as temporary intermediate values. That makes the problem very much smaller (the thetas are not depending on i which is the observation number of the training set). With AMPL we probably could have used defined variables to make this work but GAMS does not offer this.
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:
Thursday, June 6, 2013
RAS Algorithm Refinement
In the post http://yetanothermathprogrammingconsultant.blogspot.com/2013/06/simple-data-set-to-show-equivalence-of.html a simple RAS algorithm was shown. A slightly better way is to use a convergence criterion.
I got some feedback that users were not really familiar with how to write this in GAMS. So I thought this would be a good thing to share.
$ontext |
I often use a large set to loop over: “loop(iter,”. This will make sure we always terminate (no infinite loop). The $ condition on the loop can be used to terminate the loop when convergence is observed: “loop(iter$continue,”. Finally we easily can check whether the loop terminated with or without convergence and give an appropriate message.
SQL2GMS and Stored Procedures
Yes, that works. See:
The stored procedure should return a result set which SQL2GMS will then store in the GDX file.
Monday, June 3, 2013
GRAS (Generalized RAS) Example
$ontext |
Notes:
- We can declare z(i,j)=x(i,j)/a(i,j) as a positive variable.
- This makes things sign preserving.
- z(i,j)’s will not be generated if a(i,j)=0. Some papers mention we should set z(i,j)=1 if a(i,j)=0 but here we just omit all these entries.
- In the results below GAMS is truncating labels while there is in fact enough room to print them completely (or: counting is difficult!). I even reported this little fact to the GAMS people.
Results:
---- 89 PARAMETER IO_0 Initial, old input–output table Goods Services Consumpti~ NetExports TotalOutp~ Goods 7.000 3.000 5.000 -3.000 12.000 Goods Services Consumpti~ NetExports TotalOutp~ Goods 8.976 3.743 5.722 -3.441 15.000 |
Saturday, June 1, 2013
Simple Data Set to show equivalence of RAS algorithm and Entropy model
For the matrix balancing problem it is well known that the RAS algorithm and an Entropy model give the same results. Here is a small example with the data taken from http://www.lexjansen.com/mwsug/1992/MWSUG92013.pdf.
In the models below the row and column totals are known while the inner part of the matrix is estimated. The goal is to find a nearby matrix such that the row and column sums are equal to the given row and column totals. For many economic problems it is important we preserve the zeros.
The RAS algorithm implementation can easily be improved by iterating until convergence is observed instead of the fixed number of iterations. One big advantage of using an Entropy model instead of an RAS-like algorithm is that it is easy to add side-constraints. This can often be very important in practical situations.
RAS Algorithm
$ontext |
Entropy Model
$ontext |
Results
---- 73 ------------------------------------------------------------------- ---- 73 PARAMETER A1 s1 s2 s3 s4 s5 s6 s7 PA 229.652 374.869 375.099 100.028 686.238 212.542 + s8 s9 s10 rowTotal sum diff PA 50.572 2029.000 2029.000 -2.2737E-13 |