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-2023 Zuse Institute Berlin (ZIB) */
7 /* */
8 /* Licensed under the Apache License, Version 2.0 (the "License"); */
9 /* you may not use this file except in compliance with the License. */
10 /* You may obtain a copy of the License at */
11 /* */
12 /* http://www.apache.org/licenses/LICENSE-2.0 */
13 /* */
14 /* Unless required by applicable law or agreed to in writing, software */
15 /* distributed under the License is distributed on an "AS IS" BASIS, */
16 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17 /* See the License for the specific language governing permissions and */
18 /* limitations under the License. */
19 /* */
20 /* You should have received a copy of the Apache-2.0 license */
21 /* along with SoPlex; see the file LICENSE. If not email to soplex@zib.de. */
22 /* */
23 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24 
25 /**@file example.cpp
26  * @brief simple example of how to build up and solve an lp using the SoPlex callable library
27  *
28  * @author Ambros Gleixner
29  */
30 
31 #include <iostream>
32 #include "soplex.h"
33 
34 using namespace soplex;
35 
36 
37 void test_real()
38 {
39  SoPlex mysoplex;
40 
41  /* set the objective sense */
42  mysoplex.setIntParam(SoPlex::OBJSENSE, SoPlex::OBJSENSE_MINIMIZE);
43 
44  /* we first add variables */
45  DSVector dummycol(0);
46  mysoplex.addColReal(LPCol(3.0, dummycol, infinity, 1.0));
47  mysoplex.addColReal(LPCol(2.0, dummycol, infinity, 1.0));
48 
49  /* then constraints one by one */
50  DSVector row1(2);
51  row1.add(0, 0.2);
52  row1.add(1, 1.0);
53  mysoplex.addRowReal(LPRow(2.0, row1, infinity));
54 
55  /* NOTE: alternatively, we could have added the matrix nonzeros in dummycol already; nonexisting rows are then
56  * created automatically. */
57 
58  /* write LP in .lp format */
59  mysoplex.writeFileReal("dump_real.lp", NULL, NULL, NULL);
60 
61  /* solve LP */
62  SPxSolver::Status stat;
63  DVector prim(2);
64  DVector dual(1);
65  stat = mysoplex.optimize();
66 
67  /* get solution */
68  if(stat == SPxSolver::OPTIMAL)
69  {
70  mysoplex.getPrimal(prim);
71  mysoplex.getDual(dual);
72  std::cout << "LP solved to optimality.\n";
73  std::cout << "Objective value is " << mysoplex.objValueReal() << ".\n";
74  std::cout << "Primal solution is [" << prim[0] << ", " << prim[1] << "].\n";
75  std::cout << "Dual solution is [" << dual[0] << "].\n";
76  }
77  else
78  {
79  std::cout << "Error: SoPlex returned with status " << stat << ".\n";
80  }
81 }
82 
83 
85 {
86  SoPlex mysoplex;
87 
88  /* set parameters for exact solving */
89  mysoplex.setIntParam(SoPlex::READMODE, SoPlex::READMODE_RATIONAL);
90  mysoplex.setIntParam(SoPlex::SOLVEMODE, SoPlex::SOLVEMODE_RATIONAL);
91  mysoplex.setIntParam(SoPlex::CHECKMODE, SoPlex::CHECKMODE_RATIONAL);
92  mysoplex.setIntParam(SoPlex::SYNCMODE, SoPlex::SYNCMODE_AUTO);
93  mysoplex.setRealParam(SoPlex::FEASTOL, 0.0);
94  mysoplex.setRealParam(SoPlex::OPTTOL, 0.0);
95 
96  /* set the objective sense */
97  mysoplex.setIntParam(SoPlex::OBJSENSE, SoPlex::OBJSENSE_MINIMIZE);
98 
99  /* we first add variables (the integer data is converted to type Rational) */
100  DSVectorRational dummycol(0);
101  mysoplex.addColRational(LPColRational(3, dummycol, infinity, 1));
102  mysoplex.addColRational(LPColRational(2, dummycol, infinity, 1));
103 
104  /* then constraints one by one (here we show how Rationals can be used directly) */
105  DSVectorRational row1(2);
106  Rational r;
107  r = 1;
108  r /= 5;
109  row1.add(0, r);
110  r = 1;
111  row1.add(1, r);
112  r = 2;
113  mysoplex.addRowRational(LPRowRational(r, row1, infinity));
114 
115  /* NOTE: alternatively, we could have added the matrix nonzeros in dummycol already; nonexisting rows are then
116  * automatically created. */
117 
118  /* write LP in .lp format */
119  mysoplex.writeFileRational("dump_rational.lp", NULL, NULL, NULL);
120 
121  /* solve LP */
122  SPxSolver::Status stat;
123  DVectorRational prim(2);
124  DVectorRational dual(1);
125  stat = mysoplex.optimize();
126 
127  /* get solution */
128  if(stat == SPxSolver::OPTIMAL)
129  {
130  mysoplex.getPrimalRational(prim);
131  mysoplex.getDualRational(dual);
132  std::cout << "LP solved to optimality.\n";
133  std::cout << "Objective value is " << mysoplex.objValueRational() << ".\n";
134  std::cout << "Primal solution is [" << prim[0] << ", " << prim[1] << "].\n";
135  std::cout << "Dual solution is [" << dual[0] << "].\n";
136  }
137  else
138  {
139  std::cout << "Error: SoPlex returned with status " << stat << ".\n";
140  }
141 }
142 
143 
144 int main()
145 {
146  std::cout << "Testing SoPlex as floating-point LP solver:\n\n";
147  test_real();
148 
149  std::cout << "\nTesting SoPlex as exact rational LP solver:\n\n";
150  test_rational();
151 
152  return 0;
153 }
void test_real()
Definition: example.cpp:37
number< gmp_rational, et_off > Rational
Definition: rational.h:29
LPRowBase< Rational > LPRowRational
Definition: lprow.h:38
Dense vector.Class VectorBase provides dense linear algebra vectors. Internally, VectorBase wraps std...
Definition: dsvectorbase.h:37
dual feasibility tolerance
Definition: soplex.h:1385
Dynamic sparse vectors.Class DSVectorBase implements dynamic sparse vectors, i.e. SVectorBases with a...
Definition: dsvectorbase.h:52
LP has been solved to optimality.
Definition: spxsolver.h:226
mode for a posteriori feasibility checks
Definition: soplex.h:1096
void add(const SVectorBase< S > &vec)
Append nonzeros of sv.
Definition: dsvectorbase.h:228
LPRowBase< Real > LPRow
Definition: lprow.h:36
objective sense
Definition: soplex.h:1042
void test_rational()
Definition: example.cpp:84
primal feasibility tolerance
Definition: soplex.h:1382
automatic sync of real and rational LP
Definition: soplex.h:1297
SOPLEX_THREADLOCAL const Real infinity
Definition: spxdefines.cpp:41
int main()
Definition: example.cpp:144
LPColBase< Rational > LPColRational
Definition: lpcol.h:40
Preconfigured SoPlex LP solver.
Preconfigured SoPlex LP-solver.
Everything should be within this namespace.
mode for iterative refinement strategy
Definition: soplex.h:1093
mode for reading LP files
Definition: soplex.h:1090
LPColBase< Real > LPCol
Definition: lpcol.h:38
mode for synchronizing real and rational LP
Definition: soplex.h:1087
force iterative refinement
Definition: soplex.h:1323