GLPK/Output to GMPL data file

It may be desirable to output the result of a computation to a GMPL data file to use is as the input for another optimization. The example below shows how this can be accomplished using the printf command.


 * 1) Outputing results as GMPL data file
 * 2) Save model file as test.mod
 * 3) 1st run: glpsol -m test.mod
 * 4) 2nd run: glpsol -m test.mod -d test.dat

param f, symbolic := "test.dat"; set S, dimen 3 := { (0, 0, 1), (0, 1, 0), (1, 0, 0), (1, 1, 1) }; param p{(i, j, k) in S} := i + 2 * j + 4 * k;

set T, dimen 3; param q{T}; param r;

printf "data;\n" > f; printf "param r := %f;\n", card(S) >> f;
 * 1) Begin file output
 * 1) Output simple parameter

printf "set T :=\n" >> f; for {(i,j,k) in S} { printf "%d, %d, %d%s", i, j ,k, if card(setof{(l, m, n) in S : l > i or m > j or n > k }(l, m, n)) > 0 then "," else ";" >> f; printf "\n" >> f; }
 * 1) Output set

printf "param q :=\n" >> f; for {(i,j,k) in S} { printf "[%d, %d, %d] %f%s", i, j ,k, p[i, j, k], if card(setof{(l, m, n) in S : l > i or m > j or n > k }(l, m, n)) > 0 then "," else ";" >> f; printf "\n" >> f; }
 * 1) Output indexed parameter

printf "end;\n" >> f;
 * 1) File output is completed

display T; display q; display r; end;
 * 1) Display input from data file