1    	#include "soplex.h"
2    	#include "soplex_interface.h"
3    	#include <iostream>
4    	
5    	using namespace soplex;
6    	
7    	/** creates new SoPlex struct **/
8    	void* SoPlex_create()
9    	{
10   	   SoPlex* so = new SoPlex();
11   	   return so;
12   	}
13   	
14   	/** frees SoPlex struct **/
15   	void SoPlex_free(void* soplex)
16   	{
17   	   SoPlex* so = (SoPlex*)(soplex);
18   	   delete so;
19   	}
20   	
21   	/** clears the (floating point) LP **/
22   	void SoPlex_clearLPReal(void* soplex)
23   	{
24   	   SoPlex* so = (SoPlex*)(soplex);
25   	   so->clearLPReal();
26   	}
27   	
28   	/** returns number of rows **/
29   	int SoPlex_numRows(void* soplex)
30   	{
31   	   SoPlex* so = (SoPlex*)(soplex);
32   	   return so->numRows();
33   	}
34   	
35   	/** returns number of columns **/
36   	int SoPlex_numCols(void* soplex)
37   	{
38   	   SoPlex* so = (SoPlex*)(soplex);
39   	   return so->numCols();
40   	}
41   	
42   	/** enables rational solving mode **/
43   	void SoPlex_setRational(void* soplex)
44   	{
45   	#ifndef SOPLEX_WITH_BOOST
46   	   throw SPxException("Rational functions cannot be used when built without Boost.");
47   	#endif
48   	   SoPlex* so = (SoPlex*)(soplex);
49   	   so->setIntParam(SoPlex::READMODE, SoPlex::READMODE_RATIONAL);
50   	   so->setIntParam(SoPlex::SOLVEMODE, SoPlex::SOLVEMODE_RATIONAL);
51   	   so->setIntParam(SoPlex::CHECKMODE, SoPlex::CHECKMODE_RATIONAL);
52   	   so->setIntParam(SoPlex::SYNCMODE, SoPlex::SYNCMODE_AUTO);
53   	   so->setRealParam(SoPlex::FEASTOL, 0.0);
54   	   so->setRealParam(SoPlex::OPTTOL, 0.0);
55   	}
56   	
57   	/** sets integer parameter value **/
58   	void SoPlex_setIntParam(void* soplex, int paramcode, int paramvalue)
59   	{
60   	   SoPlex* so = (SoPlex*)(soplex);
61   	   so->setIntParam((SoPlex::IntParam)paramcode, paramvalue);
62   	}
63   	
64   	/** returns value of integer parameter **/
65   	int SoPlex_getIntParam(void* soplex, int paramcode)
66   	{
67   	   SoPlex* so = (SoPlex*)(soplex);
68   	   return so->intParam((SoPlex::IntParam)paramcode);
69   	}
70   	
71   	/** adds a single (floating point) column **/
72   	void SoPlex_addColReal(
73   	   void* soplex,
74   	   double* colentries,
75   	   int colsize,
76   	   int nnonzeros,
77   	   double objval,
78   	   double lb,
79   	   double ub
80   	)
81   	{
82   	   SoPlex* so = (SoPlex*)(soplex);
83   	   DSVector col(nnonzeros);
84   	
85   	   /* add nonzero entries to column vector */
86   	   for(int i = 0; i < colsize; ++i)
87   	   {
88   	      if(colentries[i] != 0.0)
89   	         col.add(i, colentries[i]);
90   	   }
91   	
92   	   so->addColReal(LPCol(objval, col, ub, lb));
93   	}
94   	
95   	/** adds a single rational column **/
96   	void SoPlex_addColRational(
97   	   void* soplex,
98   	   long* colnums,
99   	   long* coldenoms,
100  	   int colsize,
101  	   int nnonzeros,
102  	   long objvalnum,
103  	   long objvaldenom,
104  	   long lbnum,
105  	   long lbdenom,
106  	   long ubnum,
107  	   long ubdenom
108  	)
109  	{
110  	#ifndef SOPLEX_WITH_BOOST
111  	   throw SPxException("Rational functions cannot be used when built without Boost.");
112  	#endif
113  	   SoPlex* so = (SoPlex*)(soplex);
114  	   DSVectorRational col(nnonzeros);
115  	
116  	   /* get rational lower bound */
117  	   Rational lower(lbnum, lbdenom);
118  	
119  	   /* get rational upper bound */
120  	   Rational upper(ubnum, ubdenom);
121  	
122  	   /* get rational objective value */
123  	   Rational objval(objvalnum, objvaldenom);
124  	
125  	   /* add nonzero entries to column vector */
126  	   for(int i = 0; i < colsize; ++i)
127  	   {
128  	      if(colnums[i] != 0)
129  	      {
130  	         /* get rational nonzero entry */
131  	         Rational colentry(colnums[i], coldenoms[i]);
132  	         col.add(i, colentry);
133  	      }
134  	   }
135  	
136  	   so->addColRational(LPColRational(objval, col, upper, lower));
137  	}
138  	
139  	/** adds a single (floating point) row **/
140  	void SoPlex_addRowReal(
141  	   void* soplex,
142  	   double* rowentries,
143  	   int rowsize,
144  	   int nnonzeros,
145  	   double lb,
146  	   double ub
147  	)
148  	{
149  	   SoPlex* so = (SoPlex*)(soplex);
150  	   DSVector row(nnonzeros);
151  	
152  	   /* add nonzero entries to row vector */
153  	   for(int i = 0; i < rowsize; ++i)
154  	   {
155  	      if(rowentries[i] != 0.0)
156  	         row.add(i, rowentries[i]);
157  	   }
158  	
159  	   so->addRowReal(LPRow(lb, row, ub));
160  	}
161  	
162  	/** adds a single rational row **/
163  	void SoPlex_addRowRational(
164  	   void* soplex,
165  	   long* rownums,
166  	   long* rowdenoms,
167  	   int rowsize,
168  	   int nnonzeros,
169  	   long lbnum,
170  	   long lbdenom,
171  	   long ubnum,
172  	   long ubdenom
173  	)
174  	{
175  	#ifndef SOPLEX_WITH_BOOST
176  	   throw SPxException("Rational functions cannot be used when built without Boost.");
177  	#endif
178  	   SoPlex* so = (SoPlex*)(soplex);
179  	   DSVectorRational row(nnonzeros);
180  	
181  	   /* get rational lower bound */
182  	   Rational lower(lbnum, lbdenom);
183  	
184  	   /* get rational upper bound */
185  	   Rational upper(ubnum, ubdenom);
186  	
187  	   /* add nonzero entries to row vector */
188  	   for(int i = 0; i < rowsize; ++i)
189  	   {
190  	      if(rownums[i] != 0)
191  	      {
192  	         /* get rational nonzero entry */
193  	         Rational rowentry(rownums[i], rowdenoms[i]);
194  	         row.add(i, rowentry);
195  	      }
196  	   }
197  	
198  	   so->addRowRational(LPRowRational(lower, row, upper));
199  	}
200  	
201  	/** gets primal solution **/
202  	void SoPlex_getPrimalReal(void* soplex, double* primal, int dim)
203  	{
204  	   SoPlex* so = (SoPlex*)(soplex);
205  	   so->getPrimalReal(primal, dim);
206  	}
207  	
208  	/** Returns rational primal solution in a char pointer.
209  	*   The caller needs to ensure the char array is freed.
210  	**/
211  	char* SoPlex_getPrimalRationalString(void* soplex, int dim)
212  	{
213  	#ifndef SOPLEX_WITH_BOOST
214  	   throw SPxException("Rational functions cannot be used when built without Boost.");
215  	#endif
(1) Event unreachable: This code cannot be reached: "soplex::SoPlex *so = (sople...".
216  	   SoPlex* so = (SoPlex*)(soplex);
217  	   VectorRational primal(dim);
218  	   std::string primalstring;
219  	   char* rawstring;
220  	
221  	   so->getPrimalRational(primal);
222  	
223  	   for(int i = 0; i < dim; ++i)
224  	   {
225  	      primalstring.append(primal[i].str());
226  	      primalstring.append(" ");
227  	   }
228  	
229  	   rawstring = new char[strlen(primalstring.c_str()) + 1];
230  	   strcpy(rawstring, primalstring.c_str());
231  	   return rawstring;
232  	}
233  	
234  	/** gets dual solution **/
235  	void SoPlex_getDualReal(void* soplex, double* dual, int dim)
236  	{
237  	   SoPlex* so = (SoPlex*)(soplex);
238  	   so->getDualReal(dual, dim);
239  	}
240  	
241  	/** optimizes the given LP **/
242  	int SoPlex_optimize(void* soplex)
243  	{
244  	   SoPlex* so = (SoPlex*)(soplex);
245  	   return so->optimize();
246  	}
247  	
248  	/** changes objective function vector to obj **/
249  	void SoPlex_changeObjReal(void* soplex, double* obj, int dim)
250  	{
251  	   SoPlex* so = (SoPlex*)(soplex);
252  	   Vector objective(dim, obj);
253  	   return so->changeObjReal(objective);
254  	}
255  	
256  	/** changes rational objective function vector to obj **/
257  	void SoPlex_changeObjRational(void* soplex, long* objnums, long* objdenoms, int dim)
258  	{
259  	#ifndef SOPLEX_WITH_BOOST
260  	   throw SPxException("Rational functions cannot be used when built without Boost.");
261  	#endif
262  	   SoPlex* so = (SoPlex*)(soplex);
263  	   Rational* objrational = new Rational [dim];
264  	
265  	   /* create rational objective vector */
266  	   for(int i = 0; i < dim; ++i)
267  	   {
268  	      Rational objentry(objnums[i], objdenoms[i]);
269  	      objrational[i] = objentry;
270  	   }
271  	
272  	   VectorRational objective(dim, objrational);
273  	   return so->changeObjRational(objective);
274  	}
275  	
276  	/** changes left-hand side vector for constraints to lhs **/
277  	void SoPlex_changeLhsReal(void* soplex, double* lhs, int dim)
278  	{
279  	   SoPlex* so = (SoPlex*)(soplex);
280  	   Vector lhsvec(dim, lhs);
281  	   return so->changeLhsReal(lhsvec);
282  	}
283  	
284  	/** changes rational left-hand side vector for constraints to lhs **/
285  	void SoPlex_changeLhsRational(void* soplex, long* lhsnums, long* lhsdenoms, int dim)
286  	{
287  	#ifndef SOPLEX_WITH_BOOST
288  	   throw SPxException("Rational functions cannot be used when built without Boost.");
289  	#endif
290  	   SoPlex* so = (SoPlex*)(soplex);
291  	   Rational* lhsrational = new Rational [dim];
292  	
293  	   /* create rational lhs vector */
294  	   for(int i = 0; i < dim; ++i)
295  	   {
296  	      Rational lhsentry(lhsnums[i], lhsdenoms[i]);
297  	      lhsrational[i] = lhsentry;
298  	   }
299  	
300  	   VectorRational lhs(dim, lhsrational);
301  	   return so->changeLhsRational(lhs);
302  	}
303  	
304  	/** changes right-hand side vector for constraints to rhs **/
305  	void SoPlex_changeRhsReal(void* soplex, double* rhs, int dim)
306  	{
307  	   SoPlex* so = (SoPlex*)(soplex);
308  	   Vector rhsvec(dim, rhs);
309  	   return so->changeRhsReal(rhsvec);
310  	}
311  	
312  	/** changes rational right-hand side vector for constraints to rhs **/
313  	void SoPlex_changeRhsRational(void* soplex, long* rhsnums, long* rhsdenoms, int dim)
314  	{
315  	#ifndef SOPLEX_WITH_BOOST
316  	   throw SPxException("Rational functions cannot be used when built without Boost.");
317  	#endif
318  	   SoPlex* so = (SoPlex*)(soplex);
319  	   Rational* rhsrational = new Rational [dim];
320  	
321  	   /* create rational rhs vector */
322  	   for(int i = 0; i < dim; ++i)
323  	   {
324  	      Rational rhsentry(rhsnums[i], rhsdenoms[i]);
325  	      rhsrational[i] = rhsentry;
326  	   }
327  	
328  	   VectorRational rhs(dim, rhsrational);
329  	   return so->changeRhsRational(rhs);
330  	}
331  	
332  	/** write LP to file **/
333  	void SoPlex_writeFileReal(void* soplex, char* filename)
334  	{
335  	   SoPlex* so = (SoPlex*)(soplex);
336  	   so->writeFile(filename);
337  	}
338  	
339  	/** returns the objective value if a primal solution is available **/
340  	double SoPlex_objValueReal(void* soplex)
341  	{
342  	   SoPlex* so = (SoPlex*)(soplex);
343  	   return so->objValueReal();
344  	}
345  	
346  	/** Returns the rational objective value (as a string) if a primal solution is available.
347  	*   The caller needs to ensure the char array is freed.
348  	**/
349  	char* SoPlex_objValueRationalString(void* soplex)
350  	{
351  	#ifndef SOPLEX_WITH_BOOST
352  	   throw SPxException("Rational functions cannot be used when built without Boost.");
353  	#endif
354  	   char* value;
355  	   std::string objstring;
356  	   SoPlex* so = (SoPlex*)(soplex);
357  	   objstring = so->objValueRational().str();
358  	   value = new char[strlen(objstring.c_str()) + 1];
359  	   strcpy(value, objstring.c_str());
360  	   return value;
361  	}
362  	
363  	/** changes vectors of column bounds to lb and ub **/
364  	void SoPlex_changeBoundsReal(void* soplex, double* lb, double* ub, int dim)
365  	{
366  	   SoPlex* so = (SoPlex*)(soplex);
367  	   Vector lbvec(dim, lb);
368  	   Vector ubvec(dim, ub);
369  	   return so->changeBoundsReal(lbvec, ubvec);
370  	}
371  	
372  	/** changes bounds of a column to lb and ub **/
373  	void SoPlex_changeVarBoundsReal(void* soplex, int colidx, double lb, double ub)
374  	{
375  	   SoPlex* so = (SoPlex*)(soplex);
376  	   return so->changeBoundsReal(colidx, lb, ub);
377  	}
378  	
379  	/** changes rational bounds of a column to lbnum/lbdenom and ubnum/ubdenom **/
380  	void SoPlex_changeVarBoundsRational(
381  	   void* soplex,
382  	   int colidx,
383  	   long lbnum,
384  	   long lbdenom,
385  	   long ubnum,
386  	   long ubdenom
387  	)
388  	{
389  	#ifndef SOPLEX_WITH_BOOST
390  	   throw SPxException("Rational functions cannot be used when built without Boost.");
391  	#endif
392  	   SoPlex* so = (SoPlex*)(soplex);
393  	
394  	   /* get rational lower bound */
395  	   Rational lower(lbnum, lbdenom);
396  	
397  	   /* get rational upper bound */
398  	   Rational upper(ubnum, ubdenom);
399  	
400  	   return so->changeBoundsRational(colidx, lower, upper);
401  	}
402  	
403  	/** changes upper bound of column to ub **/
404  	void SoPlex_changeVarUpperReal(void* soplex, int colidx, double ub)
405  	{
406  	   SoPlex* so = (SoPlex*)(soplex);
407  	   return so->changeLowerReal(colidx, ub);
408  	}
409  	
410  	/** changes upper bound vector of columns to ub **/
411  	void SoPlex_getUpperReal(void* soplex, double* ub, int dim)
412  	{
413  	   SoPlex* so = (SoPlex*)(soplex);
414  	   Vector ubvec(dim, ub);
415  	
416  	   so->getLowerReal(ubvec);
417  	
418  	   for(int i = 0; i < dim; ++i)
419  	      ub[i] = ubvec[i];
420  	}
421