GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/nl/cad/cdcac.h Lines: 2 2 100.0 %
Date: 2021-05-22 Branches: 0 0 0.0 %

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 the CDCAC approach as described in
14
 * https://arxiv.org/pdf/2003.05633.pdf.
15
 */
16
17
#include "cvc5_private.h"
18
19
#ifndef CVC5__THEORY__ARITH__NL__CAD__CDCAC_H
20
#define CVC5__THEORY__ARITH__NL__CAD__CDCAC_H
21
22
#ifdef CVC5_POLY_IMP
23
24
#include <poly/polyxx.h>
25
26
#include <vector>
27
28
#include "theory/arith/nl/cad/cdcac_utils.h"
29
#include "theory/arith/nl/cad/constraints.h"
30
#include "theory/arith/nl/cad/proof_generator.h"
31
#include "theory/arith/nl/cad/variable_ordering.h"
32
33
namespace cvc5 {
34
namespace theory {
35
namespace arith {
36
namespace nl {
37
38
class NlModel;
39
40
namespace cad {
41
42
/**
43
 * This class implements Cylindrical Algebraic Coverings as presented in
44
 * https://arxiv.org/pdf/2003.05633.pdf
45
 */
46
4914
class CDCAC
47
{
48
 public:
49
  /** Initialize this method with the given variable ordering. */
50
  CDCAC(context::Context* ctx,
51
        ProofNodeManager* pnm,
52
        const std::vector<poly::Variable>& ordering = {});
53
54
  /** Reset this instance. */
55
  void reset();
56
57
  /** Collect variables from the constraints and compute a variable ordering. */
58
  void computeVariableOrdering();
59
60
  /**
61
   * Extract an initial assignment from the given model.
62
   * This initial assignment is used to guide sampling if possible.
63
   * The ran_variable should be the variable used to encode real algebraic
64
   * numbers in the model and is simply passed on to node_to_value.
65
   */
66
  void retrieveInitialAssignment(NlModel& model, const Node& ran_variable);
67
68
  /**
69
   * Returns the constraints as a non-const reference. Can be used to add new
70
   * constraints.
71
   */
72
  Constraints& getConstraints();
73
  /** Returns the constraints as a const reference. */
74
  const Constraints& getConstraints() const;
75
76
  /**
77
   * Returns the current assignment. This is a satisfying model if
78
   * get_unsat_cover() returned an empty vector.
79
   */
80
  const poly::Assignment& getModel() const;
81
82
  /** Returns the current variable ordering. */
83
  const std::vector<poly::Variable>& getVariableOrdering() const;
84
85
  /**
86
   * Collect all unsatisfiable intervals for the given variable.
87
   * Combines unsatisfiable regions from d_constraints evaluated over
88
   * d_assignment. Implements Algorithm 2.
89
   */
90
  std::vector<CACInterval> getUnsatIntervals(std::size_t cur_variable);
91
92
  /**
93
   * Sample outside of the set of intervals.
94
   * Uses a given initial value from mInitialAssignment if possible.
95
   * Returns whether a sample was found (true) or the infeasible intervals cover
96
   * the whole real line (false).
97
   */
98
  bool sampleOutsideWithInitial(const std::vector<CACInterval>& infeasible,
99
                                poly::Value& sample,
100
                                std::size_t cur_variable);
101
102
  /**
103
   * Collects the coefficients required for projection from the given
104
   * polynomial. Implements Algorithm 6.
105
   */
106
  PolyVector requiredCoefficients(const poly::Polynomial& p) const;
107
108
  /**
109
   * Constructs a characterization of the given covering.
110
   * A characterization contains polynomials whose roots bound the region around
111
   * the current assignment. Implements Algorithm 4.
112
   */
113
  PolyVector constructCharacterization(std::vector<CACInterval>& intervals);
114
115
  /**
116
   * Constructs an infeasible interval from a characterization.
117
   * Implements Algorithm 5.
118
   */
119
  CACInterval intervalFromCharacterization(const PolyVector& characterization,
120
                                           std::size_t cur_variable,
121
                                           const poly::Value& sample);
122
123
  /**
124
   * Main method that checks for the satisfiability of the constraints.
125
   * Recursively explores possible assignments and excludes regions based on the
126
   * coverings. Returns either a covering for the lowest dimension or an empty
127
   * vector. If the covering is empty, the result is SAT and an assignment can
128
   * be obtained from d_assignment. If the covering is not empty, the result is
129
   * UNSAT and an infeasible subset can be extracted from the returned covering.
130
   * Implements Algorithm 2.
131
   * @param curVariable The id of the variable (within d_variableOrdering) to
132
   * be considered. This argument is used to manage the recursion internally and
133
   * should always be zero if called externally.
134
   * @param returnFirstInterval If true, the function returns after the first
135
   * interval obtained from a recursive call. The result is not (necessarily) an
136
   * unsat cover, but merely a list of infeasible intervals.
137
   */
138
  std::vector<CACInterval> getUnsatCover(std::size_t curVariable = 0,
139
                                         bool returnFirstInterval = false);
140
141
  void startNewProof();
142
  /**
143
   * Finish the generated proof (if proofs are enabled) with a scope over the
144
   * given assertions.
145
   */
146
  ProofGenerator* closeProof(const std::vector<Node>& assertions);
147
148
  /** Get the proof generator */
149
  CADProofGenerator* getProof() { return d_proof.get(); }
150
151
 private:
152
  /** Check whether proofs are enabled */
153
1450
  bool isProofEnabled() const { return d_proof != nullptr; }
154
155
  /**
156
   * Check whether the current sample satisfies the integrality condition of the
157
   * current variable. Returns true if the variable is not integral or the
158
   * sample is integral.
159
   */
160
  bool checkIntegrality(std::size_t cur_variable, const poly::Value& value);
161
  /**
162
   * Constructs an interval that excludes the non-integral region around the
163
   * current sample. Assumes !check_integrality(cur_variable, value).
164
   */
165
  CACInterval buildIntegralityInterval(std::size_t cur_variable,
166
                                       const poly::Value& value);
167
168
  /**
169
   * Check whether the polynomial has a real root above the given value (when
170
   * evaluated over the current assignment).
171
   */
172
  bool hasRootAbove(const poly::Polynomial& p, const poly::Value& val) const;
173
  /**
174
   * Check whether the polynomial has a real root below the given value (when
175
   * evaluated over the current assignment).
176
   */
177
  bool hasRootBelow(const poly::Polynomial& p, const poly::Value& val) const;
178
179
  /**
180
   * Sort intervals according to section 4.4.1. and removes fully redundant
181
   * intervals as in 4.5. 1. by calling out to cleanIntervals.
182
   * Additionally makes sure to prune proofs for removed intervals.
183
   */
184
  void pruneRedundantIntervals(std::vector<CACInterval>& intervals);
185
186
  /**
187
   * The current assignment. When the method terminates with SAT, it contains a
188
   * model for the input constraints.
189
   */
190
  poly::Assignment d_assignment;
191
192
  /** The set of input constraints to be checked for consistency. */
193
  Constraints d_constraints;
194
195
  /** The computed variable ordering used for this method. */
196
  std::vector<poly::Variable> d_variableOrdering;
197
198
  /** The object computing the variable ordering. */
199
  VariableOrdering d_varOrder;
200
201
  /** The linear assignment used as an initial guess. */
202
  std::vector<poly::Value> d_initialAssignment;
203
204
  /** The proof generator */
205
  std::unique_ptr<CADProofGenerator> d_proof;
206
};
207
208
}  // namespace cad
209
}  // namespace nl
210
}  // namespace arith
211
}  // namespace theory
212
}  // namespace cvc5
213
214
#endif
215
216
#endif