deba@458
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
deba@458
|
2 |
*
|
deba@458
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
deba@458
|
4 |
*
|
deba@540
|
5 |
* Copyright (C) 2003-2009
|
deba@458
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@458
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@458
|
8 |
*
|
deba@458
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@458
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@458
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@458
|
12 |
*
|
deba@458
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@458
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@458
|
15 |
* purpose.
|
deba@458
|
16 |
*
|
deba@458
|
17 |
*/
|
deba@458
|
18 |
|
deba@458
|
19 |
#include "test_tools.h"
|
deba@458
|
20 |
|
deba@458
|
21 |
#ifdef HAVE_CONFIG_H
|
deba@458
|
22 |
#include <lemon/config.h>
|
deba@458
|
23 |
#endif
|
deba@458
|
24 |
|
deba@458
|
25 |
#ifdef HAVE_CPLEX
|
alpar@461
|
26 |
#include <lemon/cplex.h>
|
deba@458
|
27 |
#endif
|
deba@458
|
28 |
|
deba@458
|
29 |
#ifdef HAVE_GLPK
|
alpar@461
|
30 |
#include <lemon/glpk.h>
|
deba@458
|
31 |
#endif
|
deba@458
|
32 |
|
deba@559
|
33 |
#ifdef HAVE_CBC
|
deba@559
|
34 |
#include <lemon/cbc.h>
|
deba@559
|
35 |
#endif
|
deba@559
|
36 |
|
deba@458
|
37 |
|
deba@458
|
38 |
using namespace lemon;
|
deba@458
|
39 |
|
deba@459
|
40 |
void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat,
|
deba@458
|
41 |
double exp_opt) {
|
deba@458
|
42 |
using std::string;
|
deba@458
|
43 |
|
deba@459
|
44 |
mip.solve();
|
deba@458
|
45 |
//int decimal,sign;
|
deba@458
|
46 |
std::ostringstream buf;
|
deba@459
|
47 |
buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type());
|
deba@458
|
48 |
|
deba@458
|
49 |
|
deba@458
|
50 |
// itoa(stat,buf1, 10);
|
deba@459
|
51 |
check(mip.type()==stat, buf.str());
|
deba@458
|
52 |
|
deba@459
|
53 |
if (stat == MipSolver::OPTIMAL) {
|
deba@458
|
54 |
std::ostringstream sbuf;
|
deba@458
|
55 |
buf << "Wrong optimal value: the right optimum is " << exp_opt;
|
deba@459
|
56 |
check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str());
|
deba@458
|
57 |
//+ecvt(exp_opt,2)
|
deba@458
|
58 |
}
|
deba@458
|
59 |
}
|
deba@458
|
60 |
|
deba@459
|
61 |
void aTest(MipSolver& mip)
|
deba@458
|
62 |
{
|
deba@559
|
63 |
//The following example is very simple
|
deba@458
|
64 |
|
deba@458
|
65 |
|
deba@459
|
66 |
typedef MipSolver::Row Row;
|
deba@459
|
67 |
typedef MipSolver::Col Col;
|
deba@458
|
68 |
|
deba@458
|
69 |
|
deba@458
|
70 |
Col x1 = mip.addCol();
|
deba@458
|
71 |
Col x2 = mip.addCol();
|
deba@458
|
72 |
|
deba@458
|
73 |
|
deba@458
|
74 |
//Objective function
|
deba@458
|
75 |
mip.obj(x1);
|
deba@458
|
76 |
|
deba@458
|
77 |
mip.max();
|
deba@458
|
78 |
|
deba@458
|
79 |
//Unconstrained optimization
|
deba@458
|
80 |
mip.solve();
|
deba@458
|
81 |
//Check it out!
|
deba@458
|
82 |
|
deba@458
|
83 |
//Constraints
|
deba@559
|
84 |
mip.addRow(2 * x1 + x2 <= 2);
|
deba@559
|
85 |
Row y2 = mip.addRow(x1 - 2 * x2 <= 0);
|
deba@458
|
86 |
|
deba@458
|
87 |
//Nonnegativity of the variable x1
|
deba@458
|
88 |
mip.colLowerBound(x1, 0);
|
deba@458
|
89 |
|
deba@559
|
90 |
|
deba@458
|
91 |
//Maximization of x1
|
deba@458
|
92 |
//over the triangle with vertices (0,0),(4/5,2/5),(0,2)
|
deba@458
|
93 |
double expected_opt=4.0/5.0;
|
deba@459
|
94 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
|
deba@458
|
95 |
|
deba@559
|
96 |
|
deba@458
|
97 |
//Restrict x2 to integer
|
deba@459
|
98 |
mip.colType(x2,MipSolver::INTEGER);
|
deba@458
|
99 |
expected_opt=1.0/2.0;
|
deba@459
|
100 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
|
deba@458
|
101 |
|
deba@458
|
102 |
|
deba@458
|
103 |
//Restrict both to integer
|
deba@459
|
104 |
mip.colType(x1,MipSolver::INTEGER);
|
deba@458
|
105 |
expected_opt=0;
|
deba@459
|
106 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
|
deba@458
|
107 |
|
deba@559
|
108 |
//Erase a variable
|
deba@559
|
109 |
mip.erase(x2);
|
deba@559
|
110 |
mip.rowUpperBound(y2, 8);
|
deba@559
|
111 |
expected_opt=1;
|
deba@559
|
112 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
|
deba@458
|
113 |
|
deba@458
|
114 |
}
|
deba@458
|
115 |
|
deba@559
|
116 |
|
alpar@528
|
117 |
template<class MIP>
|
alpar@528
|
118 |
void cloneTest()
|
alpar@528
|
119 |
{
|
deba@540
|
120 |
|
alpar@528
|
121 |
MIP* mip = new MIP();
|
alpar@528
|
122 |
MIP* mipnew = mip->newSolver();
|
alpar@528
|
123 |
MIP* mipclone = mip->cloneSolver();
|
alpar@528
|
124 |
delete mip;
|
alpar@528
|
125 |
delete mipnew;
|
alpar@528
|
126 |
delete mipclone;
|
alpar@528
|
127 |
}
|
deba@458
|
128 |
|
deba@458
|
129 |
int main()
|
deba@458
|
130 |
{
|
deba@458
|
131 |
|
deba@458
|
132 |
#ifdef HAVE_GLPK
|
deba@459
|
133 |
{
|
alpar@462
|
134 |
GlpkMip mip1;
|
deba@459
|
135 |
aTest(mip1);
|
alpar@528
|
136 |
cloneTest<GlpkMip>();
|
deba@459
|
137 |
}
|
deba@458
|
138 |
#endif
|
deba@458
|
139 |
|
deba@458
|
140 |
#ifdef HAVE_CPLEX
|
deba@459
|
141 |
try {
|
alpar@462
|
142 |
CplexMip mip2;
|
deba@459
|
143 |
aTest(mip2);
|
deba@540
|
144 |
cloneTest<CplexMip>();
|
deba@459
|
145 |
} catch (CplexEnv::LicenseError& error) {
|
deba@459
|
146 |
#ifdef LEMON_FORCE_CPLEX_CHECK
|
deba@459
|
147 |
check(false, error.what());
|
deba@459
|
148 |
#else
|
deba@459
|
149 |
std::cerr << error.what() << std::endl;
|
deba@459
|
150 |
std::cerr << "Cplex license check failed, lp check skipped" << std::endl;
|
deba@459
|
151 |
#endif
|
deba@459
|
152 |
}
|
deba@458
|
153 |
#endif
|
deba@458
|
154 |
|
deba@559
|
155 |
#ifdef HAVE_CBC
|
deba@559
|
156 |
{
|
deba@559
|
157 |
CbcMip mip1;
|
deba@559
|
158 |
aTest(mip1);
|
deba@559
|
159 |
cloneTest<CbcMip>();
|
deba@559
|
160 |
}
|
deba@559
|
161 |
#endif
|
deba@559
|
162 |
|
deba@458
|
163 |
return 0;
|
deba@458
|
164 |
|
deba@458
|
165 |
}
|