1 // Compile it with
2 // g++ -O -I/path/to/coin/include -L/path/to/coin/lib -lCoinUtils -o lp2compact lp2compact.cpp
3 //
4 // then call with
5 //
6 // lp2compact < myfile.lp | bzip2 > myfile.bz2
7 
8 #include "CoinFileIO.hpp"
9 #include "CoinLpIO.hpp"
10 #include "CoinPackedMatrix.hpp"
11 #include "CoinPackedVectorBase.hpp"
12 
d2s(const double d)13 const char* d2s(const double d)
14 {
15   static char str[1000];
16   if (d > 1e29) {
17     sprintf(str, "1e30");
18   } else if (d < -1e29) {
19     sprintf(str, "-1e30");
20   } else {
21     sprintf(str, "%f", d);
22     int i;
23     for (i = strlen(str) - 1; i > 0; --i) {
24       if (str[i] != '0')
25         break;
26       str[i] = 0;
27     }
28     if (str[i] == '.')
29       str[i] = 0;
30   }
31   return str;
32 }
33 
main(int argc,char * argv[])34 int main(int argc, char* argv[])
35 {
36   CoinLpIO lp;
37   lp.readLp(stdin);
38   const CoinPackedMatrix& m = *lp.getMatrixByRow();
39   printf("%i\t%i\t%i\t0\n",
40          m.getNumCols(), m.getNumRows(), m.getNumElements());
41   const int ncol = lp.getNumCols();
42   const double* clb = lp.getColLower();
43   const double* cub = lp.getColUpper();
44   for (int i = 0; i < ncol; ++i) {
45     printf("%s", d2s(clb[i]));
46     printf("\t%s", d2s(cub[i]));
47     //		printf("\t%i", i);
48     //		printf("\t%s", lp.getColNames()[i]);
49     printf("\n");
50   }
51   const int nrow = lp.getNumRows();
52   const double* rlb = lp.getRowLower();
53   const double* rub = lp.getRowUpper();
54   for (int i = 0; i < nrow; ++i) {
55     const CoinPackedVectorBase& v = m.getVector(i);
56     const int size = v.getNumElements();
57     const int* ind = v.getIndices();
58     const double* val = v.getElements();
59     printf("%i", size);
60     printf("\t%s", d2s(rlb[i]));
61     printf("\t%s", d2s(rub[i]));
62     for (int j = 0; j < size; ++j) {
63       printf("\t%i\t%s", ind[j], d2s(val[j]));
64     }
65     printf("\n");
66   }
67   return 0;
68 }
69