SoPlex
Toggle main menu visibility
Loading...
Searching...
No Matches
src
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-2026 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
84
void
test_rational
()
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
}
Rational
Definition
rational.h:255
SoPlex
Preconfigured SoPlex LP-solver.
soplex::DSVectorBase::add
void add(const SVectorBase< S > &vec)
Append nonzeros of sv.
Definition
dsvectorbase.h:228
soplex::SPxSolverBase< Real >::Status
Status
Definition
spxsolver.h:209
soplex::SPxSolverBase< Real >::OPTIMAL
@ OPTIMAL
Definition
spxsolver.h:224
soplex::SoPlexBase< Real >::OPTTOL
@ OPTTOL
Definition
soplex.h:1429
soplex::SoPlexBase< Real >::FEASTOL
@ FEASTOL
Definition
soplex.h:1426
soplex::SoPlexBase< Real >::CHECKMODE_RATIONAL
@ CHECKMODE_RATIONAL
Definition
soplex.h:1380
soplex::SoPlexBase< Real >::CHECKMODE
@ CHECKMODE
Definition
soplex.h:1140
soplex::SoPlexBase< Real >::READMODE
@ READMODE
Definition
soplex.h:1134
soplex::SoPlexBase< Real >::SYNCMODE
@ SYNCMODE
Definition
soplex.h:1131
soplex::SoPlexBase< Real >::OBJSENSE
@ OBJSENSE
Definition
soplex.h:1086
soplex::SoPlexBase< Real >::SOLVEMODE
@ SOLVEMODE
Definition
soplex.h:1137
soplex::SoPlexBase< Real >::SOLVEMODE_RATIONAL
@ SOLVEMODE_RATIONAL
Definition
soplex.h:1367
soplex::SoPlexBase< Real >::READMODE_RATIONAL
@ READMODE_RATIONAL
Definition
soplex.h:1354
soplex::SoPlexBase< Real >::SYNCMODE_AUTO
@ SYNCMODE_AUTO
Definition
soplex.h:1341
soplex::SoPlexBase< Real >::OBJSENSE_MINIMIZE
@ OBJSENSE_MINIMIZE
Definition
soplex.h:1178
test_rational
void test_rational()
Definition
example.cpp:84
test_real
void test_real()
Definition
example.cpp:37
main
int main()
Definition
example.cpp:144
soplex
Everything should be within this namespace.
soplex::DSVector
DSVectorBase< Real > DSVector
Definition
dsvector.h:37
soplex::LPRow
LPRowBase< Real > LPRow
Definition
lprow.h:36
soplex::LPColRational
LPColBase< Rational > LPColRational
Definition
lpcol.h:40
soplex::DVector
VectorBase< Real > DVector
Definition
dvector.h:40
soplex::DVectorRational
VectorBase< Rational > DVectorRational
Definition
dvector.h:42
soplex::LPRowRational
LPRowBase< Rational > LPRowRational
Definition
lprow.h:38
soplex::LPCol
LPColBase< Real > LPCol
Definition
lpcol.h:38
soplex::infinity
SOPLEX_THREADLOCAL const Real infinity
Definition
spxdefines.cpp:41
soplex::DSVectorRational
DSVectorBase< Rational > DSVectorRational
Definition
dsvector.h:39
soplex.h
Preconfigured SoPlex LP solver.
Generated by
1.17.0