-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmeilp_solver.cpp
156 lines (140 loc) · 3.84 KB
/
meilp_solver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
*
* _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
* _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
* _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
* _/ _/ _/ _/ _/ _/ _/ _/ _/
* _/ _/ _/ _/ _/ _/_/_/ _/ _/
*
* ***********************************************
* PandA Project
* URL: http://panda.dei.polimi.it
* Politecnico di Milano - DEIB
* System Architectures Group
* ***********************************************
* Copyright (C) 2004-2024 Politecnico di Milano
*
* This file is part of the PandA framework.
*
* The PandA framework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* @file meilp_solver.cpp
* @brief This class provide an interface to lp_solve and glpk solvers.
*
* This class provide an interface to lp_solve and glpk solvers.
*
* @author Fabrizio Ferrandi <[email protected]>
* $Revision$
* $Date$
* Last modified by $Author$
*
*/
#include <utility>
#include "dbgPrintHelper.hpp"
#include "exceptions.hpp"
#include "meilp_solver.hpp"
#if HAVE_GLPK
#include "glpk_solver.hpp"
#endif
#if HAVE_COIN_OR
#include "cbc_solver.hpp"
#endif
#if HAVE_LP_SOLVE
#include "lp_solve_solver.hpp"
#endif
meilp_solver::meilp_solver()
: nel(0), real_buffer(nullptr), int_buffer(nullptr), unique_column_id(0), MAX_time(0), debug_level(DEBUG_LEVEL_NONE)
{
}
meilp_solver::~meilp_solver()
{
if(nel)
{
delete[] real_buffer;
delete[] int_buffer;
}
}
void meilp_solver::resize(size_t count)
{
if(count > nel)
{
delete[] real_buffer;
delete[] int_buffer;
THROW_ASSERT(count > 0, "expected a positive number of variables");
real_buffer = new double[count];
int_buffer = new int[count];
nel = count;
}
}
void meilp_solver::set_debug_level(int dl)
{
debug_level = dl;
}
void meilp_solver::set_priority(const std::map<int, int>& _priority)
{
priority = _priority;
}
void meilp_solver::copy(const std::map<int, double>& i_coeffs)
{
resize(i_coeffs.size());
auto i_end = i_coeffs.end();
int index = 0;
for(auto i = i_coeffs.begin(); i != i_end; ++i, index++)
{
this->real_buffer[index] = i->second;
this->int_buffer[index] = i->first + 1;
}
}
meilp_solverRef meilp_solver::create_solver(supported_solvers solver_type)
{
switch(solver_type)
{
#if HAVE_GLPK
case GLPK:
return meilp_solverRef(new glpk_solver());
#endif
#if HAVE_COIN_OR
case COIN_OR:
return meilp_solverRef(new cbc_solver());
#endif
#if HAVE_LP_SOLVE
case LP_SOLVE:
return meilp_solverRef(new lp_solve_solver());
#endif
default:
THROW_ERROR("not supported solver type");
}
// not reachable point
return meilp_solverRef();
}
void meilp_solver::set_binary(int i)
{
set_int(i);
set_bnds(i, 0, 1);
}
void meilp_solver::set_bnds(int var, double lowbo, double upbo)
{
lower_bounds[var] = lowbo;
upper_bounds[var] = upbo;
}
void meilp_solver::set_lowbo(int var, double bound)
{
lower_bounds[var] = bound;
}
void meilp_solver::set_upbo(int var, double bound)
{
upper_bounds[var] = bound;
}