PandA-2024.02
meilp_solver.cpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
46 #include <utility>
47 
48 #include "dbgPrintHelper.hpp"
49 #include "exceptions.hpp"
50 #include "meilp_solver.hpp"
51 
52 #if HAVE_GLPK
53 #include "glpk_solver.hpp"
54 #endif
55 
56 #if HAVE_COIN_OR
57 #include "cbc_solver.hpp"
58 #endif
59 
60 #if HAVE_LP_SOLVE
61 #include "lp_solve_solver.hpp"
62 #endif
63 
65  : nel(0), real_buffer(nullptr), int_buffer(nullptr), unique_column_id(0), MAX_time(0), debug_level(DEBUG_LEVEL_NONE)
66 {
67 }
68 
70 {
71  if(nel)
72  {
73  delete[] real_buffer;
74  delete[] int_buffer;
75  }
76 }
77 
78 void meilp_solver::resize(size_t count)
79 {
80  if(count > nel)
81  {
82  delete[] real_buffer;
83  delete[] int_buffer;
84  THROW_ASSERT(count > 0, "expected a positive number of variables");
85  real_buffer = new double[count];
86  int_buffer = new int[count];
87  nel = count;
88  }
89 }
90 
92 {
93  debug_level = dl;
94 }
95 
96 void meilp_solver::set_priority(const std::map<int, int>& _priority)
97 {
98  priority = _priority;
99 }
100 
101 void meilp_solver::copy(const std::map<int, double>& i_coeffs)
102 {
103  resize(i_coeffs.size());
104  auto i_end = i_coeffs.end();
105  int index = 0;
106  for(auto i = i_coeffs.begin(); i != i_end; ++i, index++)
107  {
108  this->real_buffer[index] = i->second;
109  this->int_buffer[index] = i->first + 1;
110  }
111 }
112 
113 meilp_solverRef meilp_solver::create_solver(supported_solvers solver_type)
114 {
115  switch(solver_type)
116  {
117 #if HAVE_GLPK
118  case GLPK:
119  return meilp_solverRef(new glpk_solver());
120 #endif
121 #if HAVE_COIN_OR
122  case COIN_OR:
123  return meilp_solverRef(new cbc_solver());
124 #endif
125 #if HAVE_LP_SOLVE
126  case LP_SOLVE:
127  return meilp_solverRef(new lp_solve_solver());
128 #endif
129  default:
130  THROW_ERROR("not supported solver type");
131  }
132  // not reachable point
133  return meilp_solverRef();
134 }
135 
137 {
138  set_int(i);
139  set_bnds(i, 0, 1);
140 }
141 
142 void meilp_solver::set_bnds(int var, double lowbo, double upbo)
143 {
144  lower_bounds[var] = lowbo;
145  upper_bounds[var] = upbo;
146 }
147 
148 void meilp_solver::set_lowbo(int var, double bound)
149 {
150  lower_bounds[var] = bound;
151 }
152 
153 void meilp_solver::set_upbo(int var, double bound)
154 {
155  upper_bounds[var] = bound;
156 }
virtual void set_bnds(int var, double lowbo, double upbo)
Set lower and upper bound of a variable.
virtual void copy(const std::map< int, double > &i_coeffs)
File containing functions and utilities to support the printing of debug messagges.
CustomUnorderedMap< int, double > upper_bounds
The upper bound of the variables. They will be really set by solve method.
CustomUnorderedMap< int, double > lower_bounds
The lower bound of the variables. They will be really set by solve method.
virtual void set_upbo(int var, double bound)
Set upper bound of a variable.
exceptions managed by PandA
meilp_solver()
Constructor.
virtual void set_binary(int i)
Set a variable to have only binary values.
void resize(size_t count)
std::map< int, int > priority
variables priority
void set_priority(const std::map< int, int > &_priority)
Set the variable priority.
int dl
Definition: adpcm.c:212
int debug_level
debug_level
virtual ~meilp_solver()
virtual destructor
int * int_buffer
indexes in the constraint buffer
#define index(x, y)
Definition: Keccak.c:74
Autoheader include.
Definition: glpk_solver.hpp:63
void set_debug_level(int dl)
Set the verbosity (debug_level)
supported_solvers
List of currently supported solvers.
#define DEBUG_LEVEL_NONE
no debugging print is performed.
#define THROW_ERROR(str_expr)
helper function used to throw an error in a standard way
Definition: exceptions.hpp:263
virtual void set_int(int i)=0
Set a variable to have only integer values.
static meilp_solverRef create_solver(supported_solvers solver_type)
Factory static member function.
size_t nel
number of elements in the constraint buffer
double * real_buffer
values in the constraint buffer
virtual void set_lowbo(int var, double bound)
Set lower bound of a variable.
Linear Programming solver according to the newer syntax (from version 4.35) of the GLPK solver...
This class provide an interface to different solvers.
#define THROW_ASSERT(cond, str_expr)
helper function used to check an assert and if needed to throw an error in a standard way ...
Definition: exceptions.hpp:289

Generated on Mon Feb 12 2024 13:02:54 for PandA-2024.02 by doxygen 1.8.13