Gröbner basis project
Codebase for research into Gröbner basis computation
lp_solver.hpp
1 #ifndef __LP_SOLVER_HPP_
2 #define __LP_SOLVER_HPP_
3 
4 /*****************************************************************************\
5 * This file is part of DynGB. *
6 * *
7 * DynGB is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * Foobar is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with DynGB. If not, see <http://www.gnu.org/licenses/>. *
19 \*****************************************************************************/
20 
21 #include <set>
22 using std::set;
23 #include <vector>
24 using std::vector;
25 #include <iostream>
26 using std::ostream; using std::cout; using std::endl;
27 
28 #include "system_constants.hpp"
29 
30 #include "monomial.hpp"
31 
54 {
55 
56 public:
57 
59 
70  constraint(NVAR_TYPE, CONSTR_TYPE []);
71 
80  constraint(vector<CONSTR_TYPE> &);
81 
86  constraint(const constraint &);
87 
89 
91 
98  ~constraint();
99 
101 
103 
108  inline NVAR_TYPE get_number_of_variables() const { return nvars; }
109 
113  inline CONSTR_TYPE operator[](NVAR_TYPE index) const { return coefficients[index]; };
114 
116  inline const CONSTR_TYPE * coeffs() const { return coefficients; }
117 
125  friend bool operator<(const constraint &a, const constraint &b);
126 
133  friend bool operator==(const constraint &a, const constraint &b);
140  friend bool operator!=(const constraint &a, const constraint &b);
141 
148  friend bool operator!=(constraint &a, constraint &b);
149 
151 
153 
160  friend ostream & operator<<(ostream &, const constraint &);
161 
163 
164 private:
165 
167  NVAR_TYPE nvars;
169  CONSTR_TYPE * coefficients;
170 
171 };
172 
190 class ray
191 {
192 
193 public:
194 
196 
209  ray(NVAR_TYPE, long = -1);
210 
217  ray(NVAR_TYPE, const RAYENT_TYPE []);
218 
225  ray(NVAR_TYPE, const EXP_TYPE []);
226 
232  ray(const vector<RAYENT_TYPE> &);
233 
239  ray(const ray &);
240 
242 
244 
251  ~ray();
252 
254 
256 
259  inline NVAR_TYPE get_dimension() const { return dim; };
260 
262  inline RAYENT_TYPE operator[](NVAR_TYPE index) const { return coords[index]; };
263 
265  inline const RAYENT_TYPE * weights() const { return coords; };
266 
268  inline const RAYENT_TYPE coordinate(NVAR_TYPE index) { return coords[index]; };
269 
279  inline bool is_active_at(const constraint &hyperplane) const
280  {
281  return 0 == obtain_dot_product(hyperplane);
282  };
283 
293  inline bool is_above(constraint &hyperplane)
294  {
295  return 0 < obtain_dot_product(hyperplane);
296  };
297 
307  inline bool is_below(constraint &hyperplane)
308  {
309  return 0 > obtain_dot_product(hyperplane);
310  };
311 
313 
315 
322  friend bool operator==(const ray &, const ray &);
328  friend bool operator!=(const ray &, const ray &);
329 
337  inline friend bool operator!=(ray &r, ray &s) { return !(r==s); }
338 
343  friend bool operator == (const ray &, const ray &);
344 
350  friend bool operator < (const ray &, const ray &);
351 
353 
355 
364  DOTPROD_TYPE obtain_dot_product(const constraint &) const;
365 
367 
369 
375  void simplify_ray();
376 
383  ray & operator=(const ray &);
384 
391  void swap(ray &);
392 
394 
396 
399  friend ostream & operator<<(ostream &, const ray &);
400 
402 
403 private:
404 
405  NVAR_TYPE dim;
407  RAYENT_TYPE * coords;
409 };
410 
418 ray operator*(RAYENT_TYPE, ray &);
419 
427 ray operator+(ray &, ray &);
428 
436 ray operator-(const ray &, const ray &);
437 
445 ray ray_sum(const set<ray> &);
446 
454 RAYENT_TYPE operator*(const ray &, const ray &);
455 
465 inline DOTPROD_TYPE operator*(const ray &r, const constraint &c)
466 { return r.obtain_dot_product(c); }
467 
477 inline DOTPROD_TYPE operator*(constraint &c, ray &r)
478 { return r.obtain_dot_product(c); }
479 
504 class LP_Solver {
505 public:
507 
515  virtual bool copy(const LP_Solver *) = 0;
517 
518 
520  virtual ~LP_Solver() { }
522 
523 
539  virtual bool solve(constraint &) = 0;
555  virtual bool solve(vector<constraint> &) = 0;
556 
562 
565 
567  virtual NVAR_TYPE get_dimension() const = 0;
569  virtual unsigned long get_number_of_rays() { return rays.size(); }
571  virtual const set<ray> & get_rays();
572  virtual unsigned long get_number_of_constraints() = 0;
574 
576 
578  virtual inline bool makes_consistent_constraint(
579  const Monomial & t, const Monomial & u, bool show_data = false
580  ) {
581  bool inconsistent = true;
582  for (
583  auto riter = rays.begin();
584  inconsistent and riter != rays.end();
585  ++riter
586  ) {
587  int d = 0;
588  for (int i = 0; i < riter->get_dimension(); ++i)
589  d += (*riter)[i] * (t.degree(i) - u.degree(i));
590  if (d > 0)
591  inconsistent = false;
592  if (show_data) {
593  cout << d << ' ';
594  if (!inconsistent) cout << *riter << endl;
595  }
596  }
597  if (show_data) cout << endl;
598  return not inconsistent;
599  }
601 protected:
602  set<ray> rays;
603 };
604 
605 #endif
ray ray_sum(const set< ray > &)
Add all the rays in a set.
Definition: lp_solver.cpp:281
ray operator-(const ray &, const ray &)
Subtract the two rays.
Definition: lp_solver.cpp:270
set< ray > rays
Definition: lp_solver.hpp:602
friend bool operator!=(ray &r, ray &s)
Indicates whether the two rays are unequal.
Definition: lp_solver.hpp:337
RAYENT_TYPE operator[](NVAR_TYPE index) const
Returns the entry indicated. Numbering starts at 0.
Definition: lp_solver.hpp:262
ray operator+(ray &, ray &)
Add the two rays.
Definition: lp_solver.cpp:259
friend bool operator!=(const constraint &a, const constraint &b)
check for constraint inequality
friend bool operator<(const constraint &a, const constraint &b)
Lexicographic comparison of constraints.
Definition: lp_solver.cpp:48
~constraint()
Deletes memory allocated by the constructor.
Definition: lp_solver.cpp:111
friend ostream & operator<<(ostream &, const constraint &)
print a representation of the constraint to the stream
Definition: lp_solver.cpp:81
virtual ~LP_Solver()
the default destructor does nothing (this is an abstract class)
Definition: lp_solver.hpp:520
ray operator*(RAYENT_TYPE, ray &)
Multiply every coordinate in the given ray by the given scalar.
Definition: lp_solver.cpp:222
constraint(NVAR_TYPE, CONSTR_TYPE [])
Initialize constraint to the given coefficients.
Definition: lp_solver.cpp:23
DOTPROD_TYPE obtain_dot_product(const constraint &) const
Convenience function to compute dot product between ray and the given constraint. ...
Definition: lp_solver.cpp:212
NVAR_TYPE get_number_of_variables() const
Returns the number of variables in the constraint.
Definition: lp_solver.hpp:108
virtual unsigned long get_number_of_rays()
Returns the number of rays defining the skeleton.
Definition: lp_solver.hpp:569
const RAYENT_TYPE coordinate(NVAR_TYPE index)
Synonym for []. I have no idea why I added this.
Definition: lp_solver.hpp:268
a constraint
Definition: lp_solver.hpp:53
bool is_above(constraint &hyperplane)
Returns true if and only if this ray is above the hyperplane.
Definition: lp_solver.hpp:293
exact or approximate polyhedral cone solution, with methods allowing definition and refinement ...
Definition: lp_solver.hpp:504
friend bool operator==(const constraint &a, const constraint &b)
check for constraint equality
Definition: lp_solver.cpp:61
virtual bool makes_consistent_constraint(const Monomial &t, const Monomial &u, bool show_data=false)
tests for consistency of a constraint generated by two monomials.
Definition: lp_solver.hpp:578
Implementation of monomials.
Definition: monomial.hpp:69
const CONSTR_TYPE * coeffs() const
Returns the coefficients that determine this constraints.
Definition: lp_solver.hpp:116
NVAR_TYPE get_dimension() const
Returns the dimension of this ray.
Definition: lp_solver.hpp:259
DEG_TYPE degree(NVAR_TYPE i) const
Degree of th variable.
Definition: monomial.cpp:183
CONSTR_TYPE operator[](NVAR_TYPE index) const
Returns the coefficient indicated. Numbering starts at 0.
Definition: lp_solver.hpp:113
bool is_below(constraint &hyperplane)
Returns true if and only if this ray is below the hyperplane.
Definition: lp_solver.hpp:307
const RAYENT_TYPE * weights() const
Returns the weights.
Definition: lp_solver.hpp:265
bool is_active_at(const constraint &hyperplane) const
Returns true if and only if the hyperplane is active at this ray.
Definition: lp_solver.hpp:279
a ray defined by nonnegative coordinates
Definition: lp_solver.hpp:190