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-2024 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
34using namespace soplex;
35
36
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 */
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 */
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
144int 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";
151
152 return 0;
153}
Preconfigured SoPlex LP-solver.
Dynamic sparse vectors.
Definition: dsvectorbase.h:53
void add(const SVectorBase< S > &vec)
Append nonzeros of sv.
Definition: dsvectorbase.h:228
Dense vector.
Definition: vectorbase.h:86
void test_rational()
Definition: example.cpp:84
void test_real()
Definition: example.cpp:37
int main()
Definition: example.cpp:144
Everything should be within this namespace.
LPRowBase< Real > LPRow
Definition: lprow.h:36
LPColBase< Rational > LPColRational
Definition: lpcol.h:40
number< gmp_rational, et_off > Rational
Definition: rational.h:29
LPRowBase< Rational > LPRowRational
Definition: lprow.h:38
LPColBase< Real > LPCol
Definition: lpcol.h:38
SOPLEX_THREADLOCAL const Real infinity
Definition: spxdefines.cpp:41
Preconfigured SoPlex LP solver.