PPL  1.2
MIP_Problem_defs.hh
Go to the documentation of this file.
1 /* MIP_Problem class declaration.
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_defs_hh
25 #define PPL_MIP_Problem_defs_hh 1
26 
27 #include "MIP_Problem_types.hh"
28 #include "globals_types.hh"
29 #include "Matrix_defs.hh"
31 #include "Constraint_types.hh"
33 #include "Generator_defs.hh"
34 #include "Variables_Set_defs.hh"
35 #include "Dense_Row_defs.hh"
36 #include "Sparse_Row_defs.hh"
37 #include <vector>
38 #include <deque>
39 #include <iterator>
40 #include <iosfwd>
41 
42 namespace Parma_Polyhedra_Library {
43 
44 namespace IO_Operators {
45 
47 
48 std::ostream&
49 operator<<(std::ostream& s, const MIP_Problem& mip);
50 
51 } // namespace IO_Operators
52 
54 
55 void swap(MIP_Problem& x, MIP_Problem& y);
56 
57 } // namespace Parma_Polyhedra_Library
58 
60 
88 public:
90 
102  explicit MIP_Problem(dimension_type dim = 0);
103 
140  template <typename In>
142  In first, In last,
143  const Variables_Set& int_vars,
146 
178  template <typename In>
180  In first, In last,
183 
210  const Constraint_System& cs,
213 
215  MIP_Problem(const MIP_Problem& y);
216 
218  ~MIP_Problem();
219 
221  MIP_Problem& operator=(const MIP_Problem& y);
222 
225 
228 
234 
235 private:
237  typedef std::vector<Constraint*> Constraint_Sequence;
238 
239 public:
242  private:
243  typedef Constraint_Sequence::const_iterator Base;
244  typedef std::iterator_traits<Base> Base_Traits;
245 
246  public:
247  typedef Base_Traits::iterator_category iterator_category;
248  typedef Base_Traits::difference_type difference_type;
249  typedef const Constraint value_type;
250  typedef const Constraint* pointer;
251  typedef const Constraint& reference;
252 
254  difference_type operator-(const const_iterator& y) const;
255 
258 
261 
264 
267 
269  const_iterator& operator+=(difference_type n);
270 
272  const_iterator& operator-=(difference_type n);
273 
275  const_iterator operator+(difference_type n) const;
276 
278  const_iterator operator-(difference_type n) const;
279 
281  reference operator*() const;
282 
284  pointer operator->() const;
285 
287 
291  bool operator==(const const_iterator& y) const;
292 
294 
298  bool operator!=(const const_iterator& y) const;
299 
300  private:
302  explicit const_iterator(Base b);
303 
305  Base itr;
306 
307  friend class MIP_Problem;
308  };
309 
315 
321 
323  const Linear_Expression& objective_function() const;
324 
327 
329 
332  void clear();
333 
349 
359 
367  void add_constraint(const Constraint& c);
368 
377  void add_constraints(const Constraint_System& cs);
378 
380 
385  void set_objective_function(const Linear_Expression& obj);
386 
389 
391 
395  bool is_satisfiable() const;
396 
398 
403  MIP_Problem_Status solve() const;
404 
423  void evaluate_objective_function(const Generator& evaluating_point,
424  Coefficient& numer,
425  Coefficient& denom) const;
426 
428 
432  const Generator& feasible_point() const;
433 
435 
440  const Generator& optimizing_point() const;
441 
451  void optimal_value(Coefficient& numer, Coefficient& denom) const;
452 
454  bool OK() const;
455 
457 
463  bool ascii_load(std::istream& s);
464 
467 
470 
472  void m_swap(MIP_Problem& y);
473 
478  };
479 
488  };
489 
493 
496 
497 private:
500 
506 
507 #if PPL_USE_SPARSE_MATRIX
508  typedef Sparse_Row Row;
509 #else
510  typedef Dense_Row Row;
511 #endif
512 
515 
516  typedef Row working_cost_type;
517 
519  working_cost_type working_cost;
520 
522 
530  std::vector<std::pair<dimension_type, dimension_type> > mapping;
531 
533  std::vector<dimension_type> base;
534 
536  enum Status {
551  };
552 
555 
556  // TODO: merge `status', `initialized', `pricing' and (maybe) `opt_mode'
557  // into a single bitset status word, so as to save space and allow
558  // for other control parameters.
559 
562 
569 
571  std::vector<Constraint*> input_cs;
572 
583 
586 
589 
592 
595 
601 
606 
608  : lp(mip), i_vars() {
609  // Turn mip into an LP problem (saving i_variables in i_vars).
610  using std::swap;
611  swap(i_vars, lp.i_variables);
612  }
613 
615  // Restore the original set of integer variables.
616  using std::swap;
617  swap(i_vars, lp.i_variables);
618  }
619  };
621 
624 
627 
629  void add_constraint_helper(const Constraint& c);
630 
633 
638  void second_phase();
639 
655  compute_tableau(std::vector<dimension_type>& worked_out_row);
656 
708  bool parse_constraints(dimension_type& additional_tableau_rows,
709  dimension_type& additional_slack_variables,
710  std::deque<bool>& is_tableau_constraint,
711  std::deque<bool>& is_satisfied_inequality,
712  std::deque<bool>& is_nonnegative_variable,
713  std::deque<bool>& is_remergeable_variable) const;
714 
726  get_exiting_base_index(dimension_type entering_var_index) const;
727 
729 
743  static void linear_combine(Row& x, const Row& y, const dimension_type k);
744 
745  // TODO: Remove this when the sparse working cost has been tested enough.
746 #if PPL_USE_SPARSE_MATRIX
747 
749 
763  static void linear_combine(Dense_Row& x, const Sparse_Row& y,
764  const dimension_type k);
765 
766 #endif // defined(PPL_USE_SPARSE_MATRIX)
767 
768  static bool is_unbounded_obj_function(
769  const Linear_Expression& obj_function,
770  const std::vector<std::pair<dimension_type, dimension_type> >& mapping,
772 
782  void pivot(dimension_type entering_var_index,
783  dimension_type exiting_base_index);
784 
795 
822 
835 
845 
856 
873  void erase_artificials(dimension_type begin_artificials,
874  dimension_type end_artificials);
875 
876  bool is_in_base(dimension_type var_index,
877  dimension_type& row_index) const;
878 
883  void compute_generator() const;
884 
898 
900  static bool is_satisfied(const Constraint& c, const Generator& g);
901 
903  static bool is_saturated(const Constraint& c, const Generator& g);
904 
924  static MIP_Problem_Status solve_mip(bool& have_incumbent_solution,
925  mpq_class& incumbent_solution_value,
926  Generator& incumbent_solution_point,
927  MIP_Problem& mip,
928  const Variables_Set& i_vars);
929 
933  bool is_lp_satisfiable() const;
934 
949  static bool is_mip_satisfiable(MIP_Problem& mip,
950  const Variables_Set& i_vars,
951  Generator& p);
952 
968  static bool choose_branching_variable(const MIP_Problem& mip,
969  const Variables_Set& i_vars,
970  dimension_type& branching_index);
971 };
972 
973 #include "MIP_Problem_inlines.hh"
974 #include "MIP_Problem_templates.hh"
975 
976 #endif // !defined(PPL_MIP_Problem_defs_hh)
MIP_Problem & operator=(const MIP_Problem &y)
Assignment operator.
bool operator==(const const_iterator &y) const
Compares *this with y.
bool is_satisfiable() const
Checks satisfiability of *this.
Definition: MIP_Problem.cc:247
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.
A finite sequence of coefficients.
size_t dimension_type
An unsigned integral type for representing space dimensions.
static void linear_combine(Row &x, const Row &y, const dimension_type k)
Linearly combines x with y so that *this[k] is 0.
Linear_Expression input_obj_function
The objective function to be optimized.
An std::set of variables' indexes.
A line, ray, point or closure point.
void add_constraints(const Constraint_System &cs)
Adds a copy of the constraints in cs to the MIP problem.
Definition: MIP_Problem.cc:184
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.
bool is_in_base(dimension_type var_index, dimension_type &row_index) const
Definition: MIP_Problem.cc:409
void swap(MIP_Problem &x, MIP_Problem &y)
Swaps x with y.
Control_Parameter_Value
Possible values for MIP problem's control parameters.
bool ascii_load(std::istream &s)
Loads from s an ASCII representation (as produced by ascii_dump(std::ostream&) const) and sets *this ...
std::ostream & operator<<(std::ostream &s, const Ask_Tell< D > &x)
A sparse matrix of Coefficient.
Definition: Matrix_defs.hh:37
working_cost_type working_cost
The working cost function.
A finite sparse sequence of coefficients.
const_iterator & operator-=(difference_type n)
Moves iterator backward of n positions.
MIP_Problem_Status compute_tableau(std::vector< dimension_type > &worked_out_row)
Assigns to this->tableau a simplex tableau representing the MIP problem, inserting into this->mapping...
A helper class to temporarily relax a MIP problem using RAII.
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...
void add_constraint(const Constraint &c)
Adds a copy of constraint c to the MIP problem.
Definition: MIP_Problem.cc:164
std::vector< dimension_type > base
The current basic solution.
bool is_lp_satisfiable() const
Returns true if and if only the LP problem is satisfiable.
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.
void erase_artificials(dimension_type begin_artificials, dimension_type end_artificials)
Drop unnecessary artificial variables from the tableau and get ready for the second phase of the simp...
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'.
dimension_type get_exiting_base_index(dimension_type entering_var_index) const
Computes the row index of the variable exiting the base of the MIP problem. Implemented with anti-cyc...
bool compute_simplex_using_steepest_edge_float()
Returns true if and if only the algorithm successfully computed a feasible solution.
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...
const_iterator(Base b)
Constructor from a Base iterator.
#define PPL_OUTPUT_DECLARATIONS
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.
std::vector< Constraint * > Constraint_Sequence
A type alias for a sequence of constraints.
dimension_type steepest_edge_float_entering_index() const
Same as steepest_edge_exact_entering_index, but using floating points.
bool OK() const
Checks if all the invariants are satisfied.
Optimization_Mode optimization_mode() const
Returns the optimization mode.
dimension_type merge_split_variable(dimension_type var_index)
Merges back the positive and negative part of a (previously split) variable after detecting a corresp...
Definition: MIP_Problem.cc:420
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.
static bool is_mip_satisfiable(MIP_Problem &mip, const Variables_Set &i_vars, Generator &p)
Returns true if and if only the MIP problem mip is satisfiable when variables in i_vars can only take...
void pivot(dimension_type entering_var_index, dimension_type exiting_base_index)
Performs the pivoting operation on the tableau.
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.
dimension_type steepest_edge_exact_entering_index() const
Computes the column index of the variable entering the base, using an exact steepest-edge algorithm w...
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.
static bool choose_branching_variable(const MIP_Problem &mip, const Variables_Set &i_vars, dimension_type &branching_index)
Returns true if and if only mip.last_generator satisfies all the integrality conditions implicitly st...
dimension_type row_index
Definition: PIP_Tree.cc:615
Coefficient value
Definition: PIP_Tree.cc:618
PPL_COEFFICIENT_TYPE Coefficient
An alias for easily naming the type of PPL coefficients.
static bool is_satisfied(const Constraint &c, const Generator &g)
Returns true if and only if c is satisfied by g.
Definition: MIP_Problem.cc:467
static MIP_Problem_Status solve_mip(bool &have_incumbent_solution, mpq_class &incumbent_solution_value, Generator &incumbent_solution_point, MIP_Problem &mip, const Variables_Set &i_vars)
Returns a status that encodes the solution of the MIP problem.
The feasible region of the MIP problem has been changed by adding new space dimensions or new constra...
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.
void second_phase()
Optimizes the MIP problem using the second phase of the primal simplex algorithm. ...
static bool is_unbounded_obj_function(const Linear_Expression &obj_function, const std::vector< std::pair< dimension_type, dimension_type > > &mapping, Optimization_Mode optimization_mode)
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
bool operator!=(const const_iterator &y) const
Compares *this with y.
dimension_type textbook_entering_index() const
Computes the column index of the variable entering the base, using the textbook algorithm with anti-c...
static bool is_saturated(const Constraint &c, const Generator &g)
Returns true if and only if c is saturated by g.
Definition: MIP_Problem.cc:478
const Generator & feasible_point() const
Returns a feasible point for *this, if it exists.
Definition: MIP_Problem.cc:225
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.
Maximization is requested.
Status
An enumerated type describing the internal status of the MIP problem.
void set_objective_function(const Linear_Expression &obj)
Sets the objective function to obj.
Definition: MIP_Problem.cc:208
Generator last_generator
The last successfully computed feasible or optimizing point.
MIP_Problem_Status solve() const
Optimizes the MIP problem.
Definition: MIP_Problem.cc:293
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.
void compute_generator() const
Computes a valid generator that satisfies all the constraints of the Linear Programming problem assoc...
bool parse_constraints(dimension_type &additional_tableau_rows, dimension_type &additional_slack_variables, std::deque< bool > &is_tableau_constraint, std::deque< bool > &is_satisfied_inequality, std::deque< bool > &is_nonnegative_variable, std::deque< bool > &is_remergeable_variable) const
Parses the pending constraints to gather information on how to resize the tableau.
Definition: MIP_Problem.cc:490
Steepest edge pricing method, using floating points (default).
Control_Parameter_Value pricing
The pricing method in use.
bool compute_simplex_using_exact_pricing()
Returns true if and if only the algorithm successfully computed a feasible solution.
void process_pending_constraints()
Processes the pending constraints of *this.
Definition: MIP_Problem.cc:681
size_t memory_size_type
An unsigned integral type for representing memory size in bytes.
Coefficient c
Definition: PIP_Tree.cc:64
Steepest edge pricing method, using Coefficient.
void set_control_parameter(Control_Parameter_Value value)
Sets control parameter value.
static const Linear_Expression & zero()
Returns the (zero-dimension space) constant 0.
dimension_type external_space_dim
The dimension of the vector space.
void add_space_dimensions_and_embed(dimension_type m)
Adds m new space dimensions and embeds the old MIP problem in the new vector space.
Definition: MIP_Problem.cc:374
memory_size_type external_memory_in_bytes() const
Returns the size in bytes of the memory managed by *this.
dimension_type first_pending_constraint
The first index of `input_cs' containing a pending constraint.
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
MIP_Problem_Status
Possible outcomes of the MIP_Problem solver.
void add_to_integer_space_dimensions(const Variables_Set &i_vars)
Sets the variables whose indexes are in set i_vars to be integer space dimensions.
Definition: MIP_Problem.cc:392
dimension_type space_dimension() const
Returns the space dimension of the MIP problem.
The MIP problem is satisfiable; a feasible solution has been computed.