Scippy

SoPlex

Sequential object-oriented simPlex

example.cpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the class library */
4 /* SoPlex --- the Sequential object-oriented simPlex. */
5 /* */
6 /* Copyright (C) 1996-2022 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file example.cpp
17  * @brief simple example of how to build up and solve an lp using the SoPlex callable library
18  *
19  * @author Ambros Gleixner
20  */
21 
22 #include <iostream>
23 #include "soplex.h"
24 
25 using namespace soplex;
26 
27 
28 void test_real()
29 {
30  SoPlex mysoplex;
31 
32  /* set the objective sense */
33  mysoplex.setIntParam(SoPlex::OBJSENSE, SoPlex::OBJSENSE_MINIMIZE);
34 
35  /* we first add variables */
36  DSVector dummycol(0);
37  mysoplex.addColReal(LPCol(3.0, dummycol, infinity, 1.0));
38  mysoplex.addColReal(LPCol(2.0, dummycol, infinity, 1.0));
39 
40  /* then constraints one by one */
41  DSVector row1(2);
42  row1.add(0, 0.2);
43  row1.add(1, 1.0);
44  mysoplex.addRowReal(LPRow(2.0, row1, infinity));
45 
46  /* NOTE: alternatively, we could have added the matrix nonzeros in dummycol already; nonexisting rows are then
47  * created automatically. */
48 
49  /* write LP in .lp format */
50  mysoplex.writeFileReal("dump_real.lp", NULL, NULL, NULL);
51 
52  /* solve LP */
53  SPxSolver::Status stat;
54  DVector prim(2);
55  DVector dual(1);
56  stat = mysoplex.optimize();
57 
58  /* get solution */
59  if(stat == SPxSolver::OPTIMAL)
60  {
61  mysoplex.getPrimal(prim);
62  mysoplex.getDual(dual);
63  std::cout << "LP solved to optimality.\n";
64  std::cout << "Objective value is " << mysoplex.objValueReal() << ".\n";
65  std::cout << "Primal solution is [" << prim[0] << ", " << prim[1] << "].\n";
66  std::cout << "Dual solution is [" << dual[0] << "].\n";
67  }
68  else
69  {
70  std::cout << "Error: SoPlex returned with status " << stat << ".\n";
71  }
72 }
73 
74 
76 {
77  SoPlex mysoplex;
78 
79  /* set parameters for exact solving */
80  mysoplex.setIntParam(SoPlex::READMODE, SoPlex::READMODE_RATIONAL);
81  mysoplex.setIntParam(SoPlex::SOLVEMODE, SoPlex::SOLVEMODE_RATIONAL);
82  mysoplex.setIntParam(SoPlex::CHECKMODE, SoPlex::CHECKMODE_RATIONAL);
83  mysoplex.setIntParam(SoPlex::SYNCMODE, SoPlex::SYNCMODE_AUTO);
84  mysoplex.setRealParam(SoPlex::FEASTOL, 0.0);
85  mysoplex.setRealParam(SoPlex::OPTTOL, 0.0);
86 
87  /* set the objective sense */
88  mysoplex.setIntParam(SoPlex::OBJSENSE, SoPlex::OBJSENSE_MINIMIZE);
89 
90  /* we first add variables (the integer data is converted to type Rational) */
91  DSVectorRational dummycol(0);
92  mysoplex.addColRational(LPColRational(3, dummycol, infinity, 1));
93  mysoplex.addColRational(LPColRational(2, dummycol, infinity, 1));
94 
95  /* then constraints one by one (here we show how Rationals can be used directly) */
96  DSVectorRational row1(2);
97  Rational r;
98  r = 1;
99  r /= 5;
100  row1.add(0, r);
101  r = 1;
102  row1.add(1, r);
103  r = 2;
104  mysoplex.addRowRational(LPRowRational(r, row1, infinity));
105 
106  /* NOTE: alternatively, we could have added the matrix nonzeros in dummycol already; nonexisting rows are then
107  * automatically created. */
108 
109  /* write LP in .lp format */
110  mysoplex.writeFileRational("dump_rational.lp", NULL, NULL, NULL);
111 
112  /* solve LP */
113  SPxSolver::Status stat;
114  DVectorRational prim(2);
115  DVectorRational dual(1);
116  stat = mysoplex.optimize();
117 
118  /* get solution */
119  if(stat == SPxSolver::OPTIMAL)
120  {
121  mysoplex.getPrimalRational(prim);
122  mysoplex.getDualRational(dual);
123  std::cout << "LP solved to optimality.\n";
124  std::cout << "Objective value is " << mysoplex.objValueRational() << ".\n";
125  std::cout << "Primal solution is [" << prim[0] << ", " << prim[1] << "].\n";
126  std::cout << "Dual solution is [" << dual[0] << "].\n";
127  }
128  else
129  {
130  std::cout << "Error: SoPlex returned with status " << stat << ".\n";
131  }
132 }
133 
134 
135 int main()
136 {
137  std::cout << "Testing SoPlex as floating-point LP solver:\n\n";
138  test_real();
139 
140  std::cout << "\nTesting SoPlex as exact rational LP solver:\n\n";
141  test_rational();
142 
143  return 0;
144 }
void test_real()
Definition: example.cpp:28
number< gmp_rational, et_off > Rational
Definition: rational.h:30
THREADLOCAL const Real infinity
Definition: spxdefines.cpp:32
LPRowBase< Rational > LPRowRational
Definition: lprow.h:29
Dense vector.Class VectorBase provides dense linear algebra vectors. Internally, VectorBase wraps std...
Definition: dsvectorbase.h:28
automatic sync of real and rational LP
Definition: soplex.h:1255
dual feasibility tolerance
Definition: soplex.h:1343
Dynamic sparse vectors.Class DSVectorBase implements dynamic sparse vectors, i.e. SVectorBases with a...
Definition: dsvectorbase.h:43
LP has been solved to optimality.
Definition: spxsolver.h:231
mode for a posteriori feasibility checks
Definition: soplex.h:1049
void add(const SVectorBase< S > &vec)
Append nonzeros of sv.
Definition: dsvectorbase.h:219
LPRowBase< Real > LPRow
Definition: lprow.h:27
objective sense
Definition: soplex.h:995
void test_rational()
Definition: example.cpp:75
primal feasibility tolerance
Definition: soplex.h:1340
int main()
Definition: example.cpp:135
LPColBase< Rational > LPColRational
Definition: lpcol.h:31
Preconfigured SoPlex LP solver.
Preconfigured SoPlex LP-solver.
Everything should be within this namespace.
force iterative refinement
Definition: soplex.h:1281
mode for iterative refinement strategy
Definition: soplex.h:1046
mode for reading LP files
Definition: soplex.h:1043
LPColBase< Real > LPCol
Definition: lpcol.h:29
mode for synchronizing real and rational LP
Definition: soplex.h:1040