GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/nl/cad/variable_ordering.cpp Lines: 33 55 60.0 %
Date: 2021-05-22 Branches: 25 78 32.1 %

Line Exec Source
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
47
std::vector<poly_utils::VariableInformation> collectInformation(
29
    const Constraints::ConstraintVector& polys, bool with_totals)
30
{
31
94
  poly::VariableCollector vc;
32
789
  for (const auto& c : polys)
33
  {
34
742
    vc(std::get<0>(c));
35
  }
36
47
  std::vector<poly_utils::VariableInformation> res;
37
142
  for (const auto& v : vc.get_variables())
38
  {
39
95
    res.emplace_back();
40
95
    res.back().var = v;
41
1677
    for (const auto& c : polys)
42
    {
43
1582
      poly_utils::getVariableInformation(res.back(), std::get<0>(c));
44
    }
45
  }
46
47
  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
94
  return res;
55
}
56
57
47
std::vector<poly::Variable> getVariables(
58
    const std::vector<poly_utils::VariableInformation>& vi)
59
{
60
47
  std::vector<poly::Variable> res;
61
142
  for (const auto& v : vi)
62
  {
63
95
    res.emplace_back(v.var);
64
  }
65
47
  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
47
std::vector<poly::Variable> sortBrown(
80
    const Constraints::ConstraintVector& polys)
81
{
82
94
  auto vi = collectInformation(polys);
83
47
  std::sort(vi.begin(),
84
            vi.end(),
85
            [](const poly_utils::VariableInformation& a,
86
73
               const poly_utils::VariableInformation& b) {
87
73
              if (a.max_degree != b.max_degree)
88
56
                return a.max_degree > b.max_degree;
89
17
              if (a.max_terms_tdegree != b.max_terms_tdegree)
90
2
                return a.max_terms_tdegree > b.max_terms_tdegree;
91
15
              return a.num_terms > b.num_terms;
92
            });
93
94
  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
4914
VariableOrdering::VariableOrdering() {}
114
4914
VariableOrdering::~VariableOrdering() {}
115
116
47
std::vector<poly::Variable> VariableOrdering::operator()(
117
    const Constraints::ConstraintVector& polys,
118
    VariableOrderingStrategy vos) const
119
{
120
47
  switch (vos)
121
  {
122
    case VariableOrderingStrategy::BYID: return sortByid(polys);
123
47
    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
28191
}  // namespace cvc5
135
136
#endif