1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Gereon Kremer |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* Implements variable orderings tailored to CAD. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "theory/arith/nl/cad/variable_ordering.h" |
17 |
|
|
18 |
|
#ifdef CVC5_POLY_IMP |
19 |
|
|
20 |
|
#include "util/poly_util.h" |
21 |
|
|
22 |
|
namespace cvc5 { |
23 |
|
namespace theory { |
24 |
|
namespace arith { |
25 |
|
namespace nl { |
26 |
|
namespace cad { |
27 |
|
|
28 |
68 |
std::vector<poly_utils::VariableInformation> collectInformation( |
29 |
|
const Constraints::ConstraintVector& polys, bool with_totals) |
30 |
|
{ |
31 |
136 |
poly::VariableCollector vc; |
32 |
1088 |
for (const auto& c : polys) |
33 |
|
{ |
34 |
1020 |
vc(std::get<0>(c)); |
35 |
|
} |
36 |
68 |
std::vector<poly_utils::VariableInformation> res; |
37 |
223 |
for (const auto& v : vc.get_variables()) |
38 |
|
{ |
39 |
155 |
res.emplace_back(); |
40 |
155 |
res.back().var = v; |
41 |
2403 |
for (const auto& c : polys) |
42 |
|
{ |
43 |
2248 |
poly_utils::getVariableInformation(res.back(), std::get<0>(c)); |
44 |
|
} |
45 |
|
} |
46 |
68 |
if (with_totals) |
47 |
|
{ |
48 |
|
res.emplace_back(); |
49 |
|
for (const auto& c : polys) |
50 |
|
{ |
51 |
|
poly_utils::getVariableInformation(res.back(), std::get<0>(c)); |
52 |
|
} |
53 |
|
} |
54 |
136 |
return res; |
55 |
|
} |
56 |
|
|
57 |
68 |
std::vector<poly::Variable> getVariables( |
58 |
|
const std::vector<poly_utils::VariableInformation>& vi) |
59 |
|
{ |
60 |
68 |
std::vector<poly::Variable> res; |
61 |
223 |
for (const auto& v : vi) |
62 |
|
{ |
63 |
155 |
res.emplace_back(v.var); |
64 |
|
} |
65 |
68 |
return res; |
66 |
|
} |
67 |
|
|
68 |
|
std::vector<poly::Variable> sortByid(const Constraints::ConstraintVector& polys) |
69 |
|
{ |
70 |
|
auto vi = collectInformation(polys); |
71 |
|
std::sort( |
72 |
|
vi.begin(), |
73 |
|
vi.end(), |
74 |
|
[](const poly_utils::VariableInformation& a, |
75 |
|
const poly_utils::VariableInformation& b) { return a.var < b.var; }); |
76 |
|
return getVariables(vi); |
77 |
|
}; |
78 |
|
|
79 |
68 |
std::vector<poly::Variable> sortBrown( |
80 |
|
const Constraints::ConstraintVector& polys) |
81 |
|
{ |
82 |
136 |
auto vi = collectInformation(polys); |
83 |
68 |
std::sort(vi.begin(), |
84 |
|
vi.end(), |
85 |
|
[](const poly_utils::VariableInformation& a, |
86 |
159 |
const poly_utils::VariableInformation& b) { |
87 |
159 |
if (a.max_degree != b.max_degree) |
88 |
100 |
return a.max_degree > b.max_degree; |
89 |
59 |
if (a.max_terms_tdegree != b.max_terms_tdegree) |
90 |
24 |
return a.max_terms_tdegree > b.max_terms_tdegree; |
91 |
35 |
return a.num_terms > b.num_terms; |
92 |
|
}); |
93 |
136 |
return getVariables(vi); |
94 |
|
}; |
95 |
|
|
96 |
|
std::vector<poly::Variable> sortTriangular( |
97 |
|
const Constraints::ConstraintVector& polys) |
98 |
|
{ |
99 |
|
auto vi = collectInformation(polys); |
100 |
|
std::sort(vi.begin(), |
101 |
|
vi.end(), |
102 |
|
[](const poly_utils::VariableInformation& a, |
103 |
|
const poly_utils::VariableInformation& b) { |
104 |
|
if (a.max_degree != b.max_degree) |
105 |
|
return a.max_degree > b.max_degree; |
106 |
|
if (a.max_lc_degree != b.max_lc_degree) |
107 |
|
return a.max_lc_degree > b.max_lc_degree; |
108 |
|
return a.sum_poly_degree > b.sum_poly_degree; |
109 |
|
}); |
110 |
|
return getVariables(vi); |
111 |
|
}; |
112 |
|
|
113 |
5220 |
VariableOrdering::VariableOrdering() {} |
114 |
5218 |
VariableOrdering::~VariableOrdering() {} |
115 |
|
|
116 |
68 |
std::vector<poly::Variable> VariableOrdering::operator()( |
117 |
|
const Constraints::ConstraintVector& polys, |
118 |
|
VariableOrderingStrategy vos) const |
119 |
|
{ |
120 |
68 |
switch (vos) |
121 |
|
{ |
122 |
|
case VariableOrderingStrategy::BYID: return sortByid(polys); |
123 |
68 |
case VariableOrderingStrategy::BROWN: return sortBrown(polys); |
124 |
|
case VariableOrderingStrategy::TRIANGULAR: return sortTriangular(polys); |
125 |
|
default: Assert(false) << "Unsupported variable ordering."; |
126 |
|
} |
127 |
|
return {}; |
128 |
|
} |
129 |
|
|
130 |
|
} // namespace cad |
131 |
|
} // namespace nl |
132 |
|
} // namespace arith |
133 |
|
} // namespace theory |
134 |
29502 |
} // namespace cvc5 |
135 |
|
|
136 |
|
#endif |