PPL  1.2
MIP_Problem_inlines.hh
Go to the documentation of this file.
1 /* MIP_Problem class implementation: inline functions.
2  Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3  Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4 
5 This file is part of the Parma Polyhedra Library (PPL).
6 
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20 
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23 
24 #ifndef PPL_MIP_Problem_inlines_hh
25 #define PPL_MIP_Problem_inlines_hh 1
26 
27 #include "Constraint_defs.hh"
28 #include <stdexcept>
29 
30 namespace Parma_Polyhedra_Library {
31 
32 inline dimension_type
35 }
36 
37 inline dimension_type
39  return external_space_dim;
40 }
41 
42 
43 inline
45  : external_space_dim(y.external_space_dim),
46  internal_space_dim(y.internal_space_dim),
47  tableau(y.tableau),
48  working_cost(y.working_cost),
49  mapping(y.mapping),
50  base(y.base),
51  status(y.status),
52  pricing(y.pricing),
53  initialized(y.initialized),
54  input_cs(),
55  inherited_constraints(0),
56  first_pending_constraint(),
57  input_obj_function(y.input_obj_function),
58  opt_mode(y.opt_mode),
59  last_generator(y.last_generator),
60  i_variables(y.i_variables) {
61  input_cs.reserve(y.input_cs.size());
62  for (Constraint_Sequence::const_iterator i = y.input_cs.begin(),
63  i_end = y.input_cs.end(); i != i_end; ++i) {
64  add_constraint_helper(*(*i));
65  }
66  PPL_ASSERT(OK());
67 }
68 
69 inline
71  : external_space_dim(y.external_space_dim),
72  internal_space_dim(y.internal_space_dim),
73  tableau(y.tableau),
74  working_cost(y.working_cost),
75  mapping(y.mapping),
76  base(y.base),
77  status(y.status),
78  pricing(y.pricing),
79  initialized(y.initialized),
80  input_cs(y.input_cs),
81  // NOTE: The constraints are inherited, NOT copied!
82  inherited_constraints(y.input_cs.size()),
83  first_pending_constraint(y.first_pending_constraint),
84  input_obj_function(y.input_obj_function),
85  opt_mode(y.opt_mode),
86  last_generator(y.last_generator),
87  i_variables(y.i_variables) {
88  PPL_ASSERT(OK());
89 }
90 
91 inline void
93  // For exception safety, reserve space for the new element.
94  const dimension_type size = input_cs.size();
95  if (size == input_cs.capacity()) {
96  const dimension_type max_size = input_cs.max_size();
97  if (size == max_size) {
98  throw std::length_error("MIP_Problem::add_constraint(): "
99  "too many constraints");
100  }
101  // Use an exponential grow policy to avoid too many reallocations.
102  input_cs.reserve(compute_capacity(size + 1, max_size));
103  }
104 
105  // This operation does not throw, because the space for the new element
106  // has already been reserved: hence the new-ed Constraint is safe.
107  input_cs.push_back(new Constraint(c));
108 }
109 
110 inline
112  // NOTE: do NOT delete inherited constraints; they are owned
113  // (and will eventually be deleted) by ancestors.
114  for (Constraint_Sequence::const_iterator
116  i_end = input_cs.end(); i != i_end; ++i) {
117  delete *i;
118  }
119 }
120 
121 
122 inline void
124  if (opt_mode != mode) {
125  opt_mode = mode;
126  if (status == UNBOUNDED || status == OPTIMIZED) {
128  }
129  PPL_ASSERT(OK());
130  }
131 }
132 
133 inline const Linear_Expression&
135  return input_obj_function;
136 }
137 
138 inline Optimization_Mode
140  return opt_mode;
141 }
142 
143 inline void
145  const Generator& g = optimizing_point();
146  evaluate_objective_function(g, numer, denom);
147 }
148 
151  return const_iterator(input_cs.begin());
152 }
153 
156  return const_iterator(input_cs.end());
157 }
158 
159 inline const Variables_Set&
161  return i_variables;
162 }
163 
166  PPL_USED(name);
167  PPL_ASSERT(name == PRICING);
168  return pricing;
169 }
170 
171 inline void
173  pricing = value;
174 }
175 
176 inline void
178  using std::swap;
181  swap(tableau, y.tableau);
183  swap(mapping, y.mapping);
185  swap(base, y.base);
186  swap(status, y.status);
187  swap(pricing, y.pricing);
188  swap(input_cs, y.input_cs);
192  swap(opt_mode, y.opt_mode);
195 }
196 
197 inline MIP_Problem&
199  MIP_Problem tmp(y);
200  m_swap(tmp);
201  return *this;
202 }
203 
204 inline void
206  MIP_Problem tmp;
207  m_swap(tmp);
208 }
209 
210 
211 inline memory_size_type
215  + tableau.external_memory_in_bytes()
218 
219  // Adding the external memory for `input_cs'.
220  // NOTE: disregard inherited constraints, as they are owned by ancestors.
221  n += input_cs.capacity() * sizeof(Constraint*);
222  for (Constraint_Sequence::const_iterator
224  i_end = input_cs.end(); i != i_end; ++i) {
225  n += ((*i)->total_memory_in_bytes());
226  }
227 
228  // Adding the external memory for `base'.
229  n += base.capacity() * sizeof(dimension_type);
230  // Adding the external memory for `mapping'.
231  n += mapping.capacity() * sizeof(std::pair<dimension_type, dimension_type>);
232  return n;
233 }
234 
235 inline memory_size_type
237  return sizeof(*this) + external_memory_in_bytes();
238 }
239 
240 inline
242  : itr(b) {
243 }
244 
247  return itr - y.itr;
248 }
249 
252  ++itr;
253  return *this;
254 }
255 
258  --itr;
259  return *this;
260 }
261 
264  const_iterator x = *this;
265  operator++();
266  return x;
267 }
268 
271  const_iterator x = *this;
272  operator--();
273  return x;
274 }
275 
278  return const_iterator(itr + n);
279 }
280 
283  return const_iterator(itr - n);
284 }
285 
288  itr += n;
289  return *this;
290 }
291 
294  itr -= n;
295  return *this;
296 }
297 
300  return *(*itr);
301 }
302 
305  return *itr;
306 }
307 
308 inline bool
310  return itr == y.itr;
311 }
312 
313 inline bool
315  return itr != y.itr;
316 }
317 
319 inline void
321  x.m_swap(y);
322 }
323 
324 } // namespace Parma_Polyhedra_Library
325 
326 #endif // !defined(PPL_MIP_Problem_inlines_hh)
MIP_Problem & operator=(const MIP_Problem &y)
Assignment operator.
bool operator==(const const_iterator &y) const
Compares *this with y.
void set_optimization_mode(Optimization_Mode mode)
Sets the optimization mode to mode.
A linear equality or inequality.
void swap(CO_Tree &x, CO_Tree &y)
Optimization_Mode
Possible optimization modes.
Optimization_Mode opt_mode
The optimization mode requested.
memory_size_type external_memory_in_bytes() const
Returns the size in bytes of the memory managed by *this.
size_t dimension_type
An unsigned integral type for representing space dimensions.
Linear_Expression input_obj_function
The objective function to be optimized.
An std::set of variables' indexes.
A line, ray, point or closure point.
const_iterator constraints_end() const
Returns a past-the-end read-only iterator to the sequence of constraints defining the feasible region...
const_iterator & operator+=(difference_type n)
Moves iterator forward of n positions.
void swap(MIP_Problem &x, MIP_Problem &y)
Swaps x with y.
Control_Parameter_Value
Possible values for MIP problem's control parameters.
working_cost_type working_cost
The working cost function.
const_iterator & operator-=(difference_type n)
Moves iterator backward of n positions.
std::vector< Constraint * > input_cs
The sequence of constraints describing the feasible region.
void evaluate_objective_function(const Generator &evaluating_point, Coefficient &numer, Coefficient &denom) const
Sets num and denom so that is the result of evaluating the objective function on evaluating_point...
std::vector< dimension_type > base
The current basic solution.
A tag type to distinguish normal vs. inheriting copy constructor.
const Variables_Set & integer_space_dimensions() const
Returns a set containing all the variables' indexes constrained to be integral.
A read-only iterator on the constraints defining the feasible region.
RA_Container::iterator nth_iter(RA_Container &cont, dimension_type n)
const Linear_Expression & objective_function() const
Returns the objective function.
const Generator & optimizing_point() const
Returns an optimal point for *this, if it exists.
Definition: MIP_Problem.cc:236
std::vector< std::pair< dimension_type, dimension_type > > mapping
A map between the variables of `input_cs' and `tableau'.
Control_Parameter_Value get_control_parameter(Control_Parameter_Name name) const
Returns the value of the control parameter name.
pointer operator->() const
Returns the address of the "pointed" object.
Variables_Set i_variables
A set containing all the indexes of variables that are constrained to have an integer value...
dimension_type compute_capacity(dimension_type requested_size, dimension_type maximum_size)
Speculative allocation function.
const_iterator(Base b)
Constructor from a Base iterator.
const_iterator constraints_begin() const
Returns a read-only iterator to the first constraint defining the feasible region.
Matrix< Row > tableau
The matrix encoding the current feasible region in tableau form.
bool OK() const
Checks if all the invariants are satisfied.
Optimization_Mode optimization_mode() const
Returns the optimization mode.
const_iterator operator+(difference_type n) const
Returns an iterator n positions forward.
Control_Parameter_Name
Names of MIP problems' control parameters.
The MIP problem is optimized; an optimal solution has been computed.
void optimal_value(Coefficient &numer, Coefficient &denom) const
Sets numer and denom so that is the solution of the optimization problem.
Base itr
The Base iterator on the Constraint_Sequence.
A Mixed Integer (linear) Programming problem.
memory_size_type total_memory_in_bytes() const
Returns the total size in bytes of the memory occupied by *this.
Coefficient value
Definition: PIP_Tree.cc:618
PPL_COEFFICIENT_TYPE Coefficient
An alias for easily naming the type of PPL coefficients.
void m_swap(MIP_Problem &y)
Swaps *this with y.
bool initialized
A Boolean encoding whether or not internal data structures have already been properly sized and popul...
void clear()
Resets *this to be equal to the trivial MIP problem.
Status status
The internal state of the MIP problem.
dimension_type inherited_constraints
The number of constraints that are inherited from our parent in the recursion tree built when solving...
The entire library is confined to this namespace.
Definition: version.hh:61
memory_size_type external_memory_in_bytes() const
Returns the size in bytes of the memory managed by *this.
bool operator!=(const const_iterator &y) const
Compares *this with y.
void add_constraint_helper(const Constraint &c)
Helper method: implements exception safe addition.
difference_type operator-(const const_iterator &y) const
Iterator difference: computes distances.
Generator last_generator
The last successfully computed feasible or optimizing point.
dimension_type internal_space_dim
The space dimension of the current (partial) solution of the MIP problem; it may be smaller than exte...
The MIP problem is unbounded; a feasible solution has been computed.
#define PPL_USED(v)
No-op macro that allows to avoid unused variable warnings from the compiler.
Definition: compiler.hh:39
Control_Parameter_Value pricing
The pricing method in use.
size_t memory_size_type
An unsigned integral type for representing memory size in bytes.
Coefficient c
Definition: PIP_Tree.cc:64
void set_control_parameter(Control_Parameter_Value value)
Sets control parameter value.
dimension_type external_space_dim
The dimension of the vector space.
memory_size_type external_memory_in_bytes() const
Returns the size in bytes of the memory managed by *this.
static dimension_type max_space_dimension()
Returns the maximum space dimension a Constraint can handle.
dimension_type first_pending_constraint
The first index of `input_cs' containing a pending constraint.
memory_size_type external_memory_in_bytes() const
Returns the size in bytes of the memory managed by *this.
reference operator*() const
Returns a reference to the "pointed" object.
static dimension_type max_space_dimension()
Returns the maximum space dimension an MIP_Problem can handle.
MIP_Problem(dimension_type dim=0)
Builds a trivial MIP problem.
Definition: MIP_Problem.cc:78
dimension_type space_dimension() const
Returns the space dimension of the MIP problem.
The MIP problem is satisfiable; a feasible solution has been computed.