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-2016 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 int main()
29 {
30  SoPlex mysoplex;
31 
32  /* set the objective sense */
34 
35  /* we first add variables */
36  DSVector dummycol(0);
37  mysoplex.addColReal(LPCol(2.0, dummycol, infinity, 15.0));
38  mysoplex.addColReal(LPCol(3.0, dummycol, infinity, 20.0));
39 
40  /* then constraints one by one */
41  DSVector row1(2);
42  row1.add(0, 1.0);
43  row1.add(1, 5.0);
44  mysoplex.addRowReal(LPRow(100.0, row1, infinity));
45 
46  /* NOTE: alternatively, we could have added the matrix nonzeros in dummycol already; nonexisting rows are then
47  * automatically created. */
48 
49  /* write LP in .lp format */
50  mysoplex.writeFileReal("dump.lp", NULL, NULL, NULL);
51 
52  /* solve LP */
53  SPxSolver::Status stat;
54  DVector prim(2);
55  DVector dual(1);
56  stat = mysoplex.solve();
57 
58  /* get solution */
59  if( stat == SPxSolver::OPTIMAL )
60  {
61  mysoplex.getPrimalReal(prim);
62  mysoplex.getDualReal(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 
69  return 0;
70 }
bool getPrimalReal(VectorReal &vector)
gets the primal solution vector if available; returns true on success
Definition: soplex.cpp:2865
SPxSolver::Status solve()
solves the LP
Definition: soplex.cpp:2722
objective sense
Definition: soplex.h:849
bool setIntParam(const IntParam param, const int value, const bool quiet=false, const bool init=false)
sets integer parameter value; returns true on success
Definition: soplex.cpp:4939
void addColReal(const LPCol &lpcol)
adds a single column
Definition: soplex.cpp:1277
void add(const SVectorBase< S > &vec)
Append nonzeros of sv.
Definition: dsvectorbase.h:216
LPRowBase< Real > LPRow
Definition: lprow.h:27
LP has been solved to optimality.
Definition: spxsolver.h:206
int main()
Definition: example.cpp:28
Preconfigured SoPlex LP solver.
Everything should be within this namespace.
bool getDualReal(VectorReal &vector)
gets the dual solution vector if available; returns true on success
Definition: soplex.cpp:2910
Preconfigured SoPlex LP-solver.
Definition: soplex.h:86
bool writeFileReal(const char *filename, const NameSet *rowNames=0, const NameSet *colNames=0, const DIdxSet *intvars=0) const
writes real LP to file; LP or MPS format is chosen from the extension in filename; if rowNames and co...
Definition: soplex.cpp:4486
LPColBase< Real > LPCol
Definition: lpcol.h:29
const Real infinity
Definition: spxdefines.cpp:26
void addRowReal(const LPRowReal &lprow)
adds a single row
Definition: soplex.cpp:1240
Real objValueReal()
returns the objective value if a primal solution is available
Definition: soplex.cpp:2839