Gröbner basis project
Codebase for research into Gröbner basis computation
ppl_solver.cpp
1 /*****************************************************************************\
2 * This file is part of DynGB. *
3 * *
4 * DynGB is free software: you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation, either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * Foobar is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with DynGB. If not, see <http://www.gnu.org/licenses/>. *
16 \*****************************************************************************/
17 
18 #include <gmp.h>
19 
20 #include "ppl_solver.hpp"
21 
22 #ifndef __PPL_SOLVER_CPP
23 #define __PPL_SOLVER_CPP
24 
25 using PPL::Variable;
26 using PPL::Generator_System;
27 using PPL::Linear_Expression;
28 using PPL::Polyhedron;
29 using PPL::NNC_Polyhedron;
30 using PPL::Constraint_System;
31 using PPL::raw_value;
32 
33 typedef PPL::Constraint PPL_Constraint;
34 typedef PPL::Generator PPL_Generator;
35 
36 unsigned PPL_Solver::instances = 0;
37 
39  rays.clear();
40  if (not lp->is_empty()) {
41  const Generator_System & G = lp->generators();
42  for (const PPL_Generator & g : G) {
43  if (g.is_ray()) {
44  for (NVAR_TYPE i = 0; i < n; ++i)
45  ray_data[i] = g.coefficient(*(X[i])).get_ui();
46  rays.emplace(n, ray_data);
47  }
48  }
49  }
50 }
51 
52 PPL_Solver::PPL_Solver(NVAR_TYPE num_vars) {
53  n = num_vars;
54  ray_data = new RAYENT_TYPE[n];
55  if (instances == 0)
56  Parma_Polyhedra_Library::initialize();
57  ++instances;
58  lp = new NNC_Polyhedron(n);
59  X = (Variable **)malloc(sizeof(Variable *)*n);
60  for (NVAR_TYPE i = 0; i < n; ++i)
61  X[i] = new Variable(i);
62  Constraint_System cs;
63  for (NVAR_TYPE i = 0; i < n; ++i) {
64  cs.insert(*(X[i]) >= 0);
65  }
66  lp->refine_with_constraints(cs);
67  m = n;
68  setup_rays();
69 }
70 
72  n = other.n;
73  rays = other.rays;
74  ray_data = new RAYENT_TYPE[n];
75  lp = new NNC_Polyhedron(*(other.lp));
76  X = (Variable **)malloc(sizeof(Variable *)*n);
77  for (NVAR_TYPE i = 0; i < n; ++i)
78  X[i] = new Variable(i);
79  ++instances;
80 }
81 
82 bool PPL_Solver::copy(const LP_Solver * old_solver) {
83  const PPL_Solver * other = dynamic_cast<const PPL_Solver *>(old_solver);
84  if (other != nullptr) {
85  n = other->n;
86  rays = other->rays;
87  delete [] ray_data;
88  delete lp;
89  //for (NVAR_TYPE i = 0; i < n; ++i)
90  // delete X[i];
91  //free(X);
92  ray_data = new RAYENT_TYPE[n];
93  lp = new NNC_Polyhedron(*(other->lp));
94  //X = (Variable **)malloc(sizeof(Variable *)*n);
95  //for (NVAR_TYPE i = 0; i < n; ++i)
96  // X[i] = new Variable(i);
97  ++instances;
98  }
99  return (other != nullptr);
100 }
101 
102 PPL_Solver::~PPL_Solver() {
103  delete [] ray_data;
104  delete lp;
105  for (NVAR_TYPE i = 0; i < n; ++i)
106  delete X[i];
107  free(X);
108  --instances;
109  if (instances == 0)
110  Parma_Polyhedra_Library::finalize();
111 }
112 
114  Linear_Expression ineq;
115  for (NVAR_TYPE i = 0; i < n; ++i) {
116  if (c[i] != 0)
117  ineq += Linear_Expression(c[i]*(*X[i]));
118  }
119  PPL_Constraint pc(ineq >= 0);
120  lp->refine_with_constraint(pc);
121  setup_rays();
122  return (rays.size() > 0);
123 }
124 
125 bool PPL_Solver::solve(vector<constraint> &C) {
126  Constraint_System cs;
127  for (constraint c : C) {
128  Linear_Expression ineq;
129  for (NVAR_TYPE i = 0; i < n; ++i) {
130  if (c[i] != 0)
131  ineq += Linear_Expression(c[i]*(*X[i]));
132  }
133  PPL_Constraint pc(ineq >= 0);
134  cs.insert(pc);
135  }
136  lp->refine_with_constraints(cs);
137  setup_rays();
138  return (rays.size() > 0);
139 }
140 
141 #endif
virtual bool solve(constraint &)
Adds the indicated constraint (singular!) and re-computes the solution.
Definition: ppl_solver.cpp:113
set< ray > rays
Definition: lp_solver.hpp:602
approximate skeleton of a polyhedral cone, using PPL linear solver
Definition: ppl_solver.hpp:38
NVAR_TYPE n
number of variables
Definition: ppl_solver.hpp:66
a constraint
Definition: lp_solver.hpp:53
exact or approximate polyhedral cone solution, with methods allowing definition and refinement ...
Definition: lp_solver.hpp:504
RAYENT_TYPE * ray_data
used to retrieve rays
Definition: ppl_solver.hpp:70
unsigned m
number of constraints
Definition: ppl_solver.hpp:67
PPL_Solver(NVAR_TYPE n)
initializes solver for variables
Definition: ppl_solver.cpp:52
PPL::Variable ** X
array of variables
Definition: ppl_solver.hpp:69
static unsigned instances
number of PPL instances
Definition: ppl_solver.hpp:68
virtual bool copy(const LP_Solver *)
performs a deep copy, similar to a copy constructor
Definition: ppl_solver.cpp:82
PPL::NNC_Polyhedron * lp
PPL problem interface.
Definition: ppl_solver.hpp:65
virtual void setup_rays()
clear the current set of rays and extracts the ones contained in lp
Definition: ppl_solver.cpp:38