GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/nl/cad/cdcac.h Lines: 2 2 100.0 %
Date: 2021-09-17 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 "smt/env.h"
29
#include "smt/env_obj.h"
30
#include "theory/arith/nl/cad/cdcac_utils.h"
31
#include "theory/arith/nl/cad/constraints.h"
32
#include "theory/arith/nl/cad/proof_generator.h"
33
#include "theory/arith/nl/cad/variable_ordering.h"
34
35
namespace cvc5 {
36
namespace theory {
37
namespace arith {
38
namespace nl {
39
40
class NlModel;
41
42
namespace cad {
43
44
/**
45
 * This class implements Cylindrical Algebraic Coverings as presented in
46
 * https://arxiv.org/pdf/2003.05633.pdf
47
 */
48
5213
class CDCAC : protected EnvObj
49
{
50
 public:
51
  /** Initialize this method with the given variable ordering. */
52
  CDCAC(Env& env, 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, depending on the command line
105
   * arguments. Either directly implements Algorithm 6, or improved variants
106
   * based on Lazard's projection.
107
   */
108
  PolyVector requiredCoefficients(const poly::Polynomial& p);
109
110
  /**
111
   * Constructs a characterization of the given covering.
112
   * A characterization contains polynomials whose roots bound the region around
113
   * the current assignment. Implements Algorithm 4.
114
   */
115
  PolyVector constructCharacterization(std::vector<CACInterval>& intervals);
116
117
  /**
118
   * Constructs an infeasible interval from a characterization.
119
   * Implements Algorithm 5.
120
   */
121
  CACInterval intervalFromCharacterization(const PolyVector& characterization,
122
                                           std::size_t cur_variable,
123
                                           const poly::Value& sample);
124
125
  /**
126
   * Main method that checks for the satisfiability of the constraints.
127
   * Recursively explores possible assignments and excludes regions based on the
128
   * coverings. Returns either a covering for the lowest dimension or an empty
129
   * vector. If the covering is empty, the result is SAT and an assignment can
130
   * be obtained from d_assignment. If the covering is not empty, the result is
131
   * UNSAT and an infeasible subset can be extracted from the returned covering.
132
   * Implements Algorithm 2.
133
   * @param curVariable The id of the variable (within d_variableOrdering) to
134
   * be considered. This argument is used to manage the recursion internally and
135
   * should always be zero if called externally.
136
   * @param returnFirstInterval If true, the function returns after the first
137
   * interval obtained from a recursive call. The result is not (necessarily) an
138
   * unsat cover, but merely a list of infeasible intervals.
139
   */
140
  std::vector<CACInterval> getUnsatCover(std::size_t curVariable = 0,
141
                                         bool returnFirstInterval = false);
142
143
  void startNewProof();
144
  /**
145
   * Finish the generated proof (if proofs are enabled) with a scope over the
146
   * given assertions.
147
   */
148
  ProofGenerator* closeProof(const std::vector<Node>& assertions);
149
150
  /** Get the proof generator */
151
  CADProofGenerator* getProof() { return d_proof.get(); }
152
153
 private:
154
  /** Check whether proofs are enabled */
155
2539
  bool isProofEnabled() const { return d_proof != nullptr; }
156
157
  /**
158
   * Check whether the current sample satisfies the integrality condition of the
159
   * current variable. Returns true if the variable is not integral or the
160
   * sample is integral.
161
   */
162
  bool checkIntegrality(std::size_t cur_variable, const poly::Value& value);
163
  /**
164
   * Constructs an interval that excludes the non-integral region around the
165
   * current sample. Assumes !check_integrality(cur_variable, value).
166
   */
167
  CACInterval buildIntegralityInterval(std::size_t cur_variable,
168
                                       const poly::Value& value);
169
170
  /**
171
   * Check whether the polynomial has a real root above the given value (when
172
   * evaluated over the current assignment).
173
   */
174
  bool hasRootAbove(const poly::Polynomial& p, const poly::Value& val) const;
175
  /**
176
   * Check whether the polynomial has a real root below the given value (when
177
   * evaluated over the current assignment).
178
   */
179
  bool hasRootBelow(const poly::Polynomial& p, const poly::Value& val) const;
180
181
  /**
182
   * Sort intervals according to section 4.4.1. and removes fully redundant
183
   * intervals as in 4.5. 1. by calling out to cleanIntervals.
184
   * Additionally makes sure to prune proofs for removed intervals.
185
   */
186
  void pruneRedundantIntervals(std::vector<CACInterval>& intervals);
187
188
  /**
189
   * The current assignment. When the method terminates with SAT, it contains a
190
   * model for the input constraints.
191
   */
192
  poly::Assignment d_assignment;
193
194
  /** The set of input constraints to be checked for consistency. */
195
  Constraints d_constraints;
196
197
  /** The computed variable ordering used for this method. */
198
  std::vector<poly::Variable> d_variableOrdering;
199
200
  /** The object computing the variable ordering. */
201
  VariableOrdering d_varOrder;
202
203
  /** The linear assignment used as an initial guess. */
204
  std::vector<poly::Value> d_initialAssignment;
205
206
  /** The proof generator */
207
  std::unique_ptr<CADProofGenerator> d_proof;
208
};
209
210
}  // namespace cad
211
}  // namespace nl
212
}  // namespace arith
213
}  // namespace theory
214
}  // namespace cvc5
215
216
#endif
217
218
#endif