GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/nl/nl_model.cpp Lines: 597 745 80.1 %
Date: 2021-09-29 Branches: 1383 3539 39.1 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Gereon Kremer, Tim King
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
 * Model object for the non-linear extension class.
14
 */
15
16
#include "theory/arith/nl/nl_model.h"
17
18
#include "expr/node_algorithm.h"
19
#include "options/arith_options.h"
20
#include "options/smt_options.h"
21
#include "options/theory_options.h"
22
#include "theory/arith/arith_msum.h"
23
#include "theory/arith/arith_utilities.h"
24
#include "theory/arith/nl/nl_lemma_utils.h"
25
#include "theory/theory_model.h"
26
#include "theory/rewriter.h"
27
28
using namespace cvc5::kind;
29
30
namespace cvc5 {
31
namespace theory {
32
namespace arith {
33
namespace nl {
34
35
3429
NlModel::NlModel() : d_used_approx(false)
36
{
37
3429
  d_true = NodeManager::currentNM()->mkConst(true);
38
3429
  d_false = NodeManager::currentNM()->mkConst(false);
39
3429
  d_zero = NodeManager::currentNM()->mkConst(Rational(0));
40
3429
  d_one = NodeManager::currentNM()->mkConst(Rational(1));
41
3429
  d_two = NodeManager::currentNM()->mkConst(Rational(2));
42
3429
}
43
44
4687
NlModel::~NlModel() {}
45
46
2110
void NlModel::reset(TheoryModel* m, std::map<Node, Node>& arithModel)
47
{
48
2110
  d_model = m;
49
2110
  d_mv[0].clear();
50
2110
  d_mv[1].clear();
51
2110
  d_arithVal.clear();
52
  // process arithModel
53
2110
  std::map<Node, Node>::iterator it;
54
42634
  for (const std::pair<const Node, Node>& m2 : arithModel)
55
  {
56
40524
    d_arithVal[m2.first] = m2.second;
57
  }
58
2110
}
59
60
2126
void NlModel::resetCheck()
61
{
62
2126
  d_used_approx = false;
63
2126
  d_check_model_solved.clear();
64
2126
  d_check_model_bounds.clear();
65
2126
  d_check_model_witnesses.clear();
66
2126
  d_check_model_vars.clear();
67
2126
  d_check_model_subs.clear();
68
2126
}
69
70
324351
Node NlModel::computeConcreteModelValue(Node n)
71
{
72
324351
  return computeModelValue(n, true);
73
}
74
75
153860
Node NlModel::computeAbstractModelValue(Node n)
76
{
77
153860
  return computeModelValue(n, false);
78
}
79
80
1437257
Node NlModel::computeModelValue(Node n, bool isConcrete)
81
{
82
1437257
  unsigned index = isConcrete ? 0 : 1;
83
1437257
  std::map<Node, Node>::iterator it = d_mv[index].find(n);
84
1437257
  if (it != d_mv[index].end())
85
  {
86
878023
    return it->second;
87
  }
88
1118468
  Trace("nl-ext-mv-debug") << "computeModelValue " << n << ", index=" << index
89
559234
                           << std::endl;
90
1118468
  Node ret;
91
559234
  Kind nk = n.getKind();
92
559234
  if (n.isConst())
93
  {
94
29609
    ret = n;
95
  }
96
529625
  else if (!isConcrete && hasTerm(n))
97
  {
98
    // use model value for abstraction
99
34138
    ret = getRepresentative(n);
100
  }
101
495487
  else if (n.getNumChildren() == 0)
102
  {
103
    // we are interested in the exact value of PI, which cannot be computed.
104
    // hence, we return PI itself when asked for the concrete value.
105
15693
    if (nk == PI)
106
    {
107
148
      ret = n;
108
    }
109
    else
110
    {
111
15545
      ret = getValueInternal(n);
112
    }
113
  }
114
  else
115
  {
116
    // otherwise, compute true value
117
479794
    TheoryId ctid = theory::kindToTheoryId(nk);
118
479794
    if (ctid != THEORY_ARITH && ctid != THEORY_BOOL && ctid != THEORY_BUILTIN)
119
    {
120
      // we directly look up terms not belonging to arithmetic
121
12715
      ret = getValueInternal(n);
122
    }
123
    else
124
    {
125
934158
      std::vector<Node> children;
126
467079
      if (n.getMetaKind() == metakind::PARAMETERIZED)
127
      {
128
245
        children.push_back(n.getOperator());
129
      }
130
1325784
      for (unsigned i = 0, nchild = n.getNumChildren(); i < nchild; i++)
131
      {
132
1717410
        Node mc = computeModelValue(n[i], isConcrete);
133
858705
        children.push_back(mc);
134
      }
135
467079
      ret = NodeManager::currentNM()->mkNode(nk, children);
136
467079
      ret = Rewriter::rewrite(ret);
137
    }
138
  }
139
1118468
  Trace("nl-ext-mv-debug") << "computed " << (index == 0 ? "M" : "M_A") << "["
140
559234
                           << n << "] = " << ret << std::endl;
141
559234
  d_mv[index][n] = ret;
142
559234
  return ret;
143
}
144
145
103597
bool NlModel::hasTerm(Node n) const
146
{
147
103597
  return d_arithVal.find(n) != d_arithVal.end();
148
}
149
150
34138
Node NlModel::getRepresentative(Node n) const
151
{
152
34138
  if (n.isConst())
153
  {
154
    return n;
155
  }
156
34138
  std::map<Node, Node>::const_iterator it = d_arithVal.find(n);
157
34138
  if (it != d_arithVal.end())
158
  {
159
34138
    AlwaysAssert(it->second.isConst());
160
34138
    return it->second;
161
  }
162
  return d_model->getRepresentative(n);
163
}
164
165
28260
Node NlModel::getValueInternal(Node n)
166
{
167
28260
  if (n.isConst())
168
  {
169
    return n;
170
  }
171
28260
  std::map<Node, Node>::const_iterator it = d_arithVal.find(n);
172
28260
  if (it != d_arithVal.end())
173
  {
174
28249
    AlwaysAssert(it->second.isConst());
175
28249
    return it->second;
176
  }
177
  // It is unconstrained in the model, return 0. We additionally add it
178
  // to mapping from the linear solver. This ensures that if the nonlinear
179
  // solver assumes that n = 0, then this assumption is recorded in the overall
180
  // model.
181
11
  d_arithVal[n] = d_zero;
182
11
  return d_zero;
183
}
184
185
46734
int NlModel::compare(Node i, Node j, bool isConcrete, bool isAbsolute)
186
{
187
93468
  Node ci = computeModelValue(i, isConcrete);
188
93468
  Node cj = computeModelValue(j, isConcrete);
189
46734
  if (ci.isConst())
190
  {
191
46734
    if (cj.isConst())
192
    {
193
46734
      return compareValue(ci, cj, isAbsolute);
194
    }
195
    return 1;
196
  }
197
  return cj.isConst() ? -1 : 0;
198
}
199
200
53607
int NlModel::compareValue(Node i, Node j, bool isAbsolute) const
201
{
202
53607
  Assert(i.isConst() && j.isConst());
203
  int ret;
204
53607
  if (i == j)
205
  {
206
9861
    ret = 0;
207
  }
208
43746
  else if (!isAbsolute)
209
  {
210
296
    ret = i.getConst<Rational>() < j.getConst<Rational>() ? 1 : -1;
211
  }
212
  else
213
  {
214
130350
    ret = (i.getConst<Rational>().abs() == j.getConst<Rational>().abs()
215
84325
               ? 0
216
166075
               : (i.getConst<Rational>().abs() < j.getConst<Rational>().abs()
217
122625
                      ? 1
218
                      : -1));
219
  }
220
53607
  return ret;
221
}
222
223
151
bool NlModel::checkModel(const std::vector<Node>& assertions,
224
                         unsigned d,
225
                         std::vector<NlLemma>& lemmas)
226
{
227
151
  Trace("nl-ext-cm-debug") << "  solve for equalities..." << std::endl;
228
2407
  for (const Node& atom : assertions)
229
  {
230
    // see if it corresponds to a univariate polynomial equation of degree two
231
2256
    if (atom.getKind() == EQUAL)
232
    {
233
234
      if (!solveEqualitySimple(atom, d, lemmas))
234
      {
235
        // no chance we will satisfy this equality
236
126
        Trace("nl-ext-cm") << "...check-model : failed to solve equality : "
237
63
                           << atom << std::endl;
238
      }
239
    }
240
  }
241
242
  // all remaining variables are constrained to their exact model values
243
302
  Trace("nl-ext-cm-debug") << "  set exact bounds for remaining variables..."
244
151
                           << std::endl;
245
302
  std::unordered_set<TNode> visited;
246
302
  std::vector<TNode> visit;
247
302
  TNode cur;
248
2407
  for (const Node& a : assertions)
249
  {
250
2256
    visit.push_back(a);
251
8104
    do
252
    {
253
10360
      cur = visit.back();
254
10360
      visit.pop_back();
255
10360
      if (visited.find(cur) == visited.end())
256
      {
257
6115
        visited.insert(cur);
258
6115
        if (cur.getType().isReal() && !cur.isConst())
259
        {
260
1547
          Kind k = cur.getKind();
261
2606
          if (k != MULT && k != PLUS && k != NONLINEAR_MULT
262
2048
              && !isTranscendentalKind(k))
263
          {
264
            // if we have not set an approximate bound for it
265
340
            if (!hasCheckModelAssignment(cur))
266
            {
267
              // set its exact model value in the substitution
268
390
              Node curv = computeConcreteModelValue(cur);
269
195
              if (Trace.isOn("nl-ext-cm"))
270
              {
271
                Trace("nl-ext-cm")
272
                    << "check-model-bound : exact : " << cur << " = ";
273
                printRationalApprox("nl-ext-cm", curv);
274
                Trace("nl-ext-cm") << std::endl;
275
              }
276
195
              bool ret = addCheckModelSubstitution(cur, curv);
277
195
              AlwaysAssert(ret);
278
            }
279
          }
280
        }
281
14219
        for (const Node& cn : cur)
282
        {
283
8104
          visit.push_back(cn);
284
        }
285
      }
286
10360
    } while (!visit.empty());
287
  }
288
289
151
  Trace("nl-ext-cm-debug") << "  check assertions..." << std::endl;
290
302
  std::vector<Node> check_assertions;
291
2407
  for (const Node& a : assertions)
292
  {
293
2256
    if (d_check_model_solved.find(a) == d_check_model_solved.end())
294
    {
295
4170
      Node av = a;
296
      // apply the substitution to a
297
2085
      if (!d_check_model_vars.empty())
298
      {
299
1576
        av = arithSubstitute(av, d_check_model_vars, d_check_model_subs);
300
1576
        av = Rewriter::rewrite(av);
301
      }
302
      // simple check literal
303
2085
      if (!simpleCheckModelLit(av))
304
      {
305
416
        Trace("nl-ext-cm") << "...check-model : assertion failed : " << a
306
208
                           << std::endl;
307
208
        check_assertions.push_back(av);
308
416
        Trace("nl-ext-cm-debug")
309
208
            << "...check-model : failed assertion, value : " << av << std::endl;
310
      }
311
    }
312
  }
313
314
151
  if (!check_assertions.empty())
315
  {
316
99
    Trace("nl-ext-cm") << "...simple check failed." << std::endl;
317
    // TODO (#1450) check model for general case
318
99
    return false;
319
  }
320
52
  Trace("nl-ext-cm") << "...simple check succeeded!" << std::endl;
321
52
  return true;
322
}
323
324
361
bool NlModel::addCheckModelSubstitution(TNode v, TNode s)
325
{
326
  // should not substitute the same variable twice
327
722
  Trace("nl-ext-model") << "* check model substitution : " << v << " -> " << s
328
361
                        << std::endl;
329
  // should not set exact bound more than once
330
1083
  if (std::find(d_check_model_vars.begin(), d_check_model_vars.end(), v)
331
1083
      != d_check_model_vars.end())
332
  {
333
    Trace("nl-ext-model") << "...ERROR: already has value." << std::endl;
334
    // this should never happen since substitutions should be applied eagerly
335
    Assert(false);
336
    return false;
337
  }
338
  // if we previously had an approximate bound, the exact bound should be in its
339
  // range
340
  std::map<Node, std::pair<Node, Node>>::iterator itb =
341
361
      d_check_model_bounds.find(v);
342
361
  if (itb != d_check_model_bounds.end())
343
  {
344
    if (s.getConst<Rational>() >= itb->second.first.getConst<Rational>()
345
        || s.getConst<Rational>() <= itb->second.second.getConst<Rational>())
346
    {
347
      Trace("nl-ext-model")
348
          << "...ERROR: already has bound which is out of range." << std::endl;
349
      return false;
350
    }
351
  }
352
361
  Assert(d_check_model_witnesses.find(v) == d_check_model_witnesses.end())
353
      << "We tried to add a substitution where we already had a witness term."
354
      << std::endl;
355
722
  std::vector<Node> varsTmp;
356
361
  varsTmp.push_back(v);
357
722
  std::vector<Node> subsTmp;
358
361
  subsTmp.push_back(s);
359
1163
  for (unsigned i = 0, size = d_check_model_subs.size(); i < size; i++)
360
  {
361
1604
    Node ms = d_check_model_subs[i];
362
1604
    Node mss = arithSubstitute(ms, varsTmp, subsTmp);
363
802
    if (mss != ms)
364
    {
365
27
      mss = Rewriter::rewrite(mss);
366
    }
367
802
    d_check_model_subs[i] = mss;
368
  }
369
361
  d_check_model_vars.push_back(v);
370
361
  d_check_model_subs.push_back(s);
371
361
  return true;
372
}
373
374
225
bool NlModel::addCheckModelBound(TNode v, TNode l, TNode u)
375
{
376
450
  Trace("nl-ext-model") << "* check model bound : " << v << " -> [" << l << " "
377
225
                        << u << "]" << std::endl;
378
225
  if (l == u)
379
  {
380
    // bound is exact, can add as substitution
381
    return addCheckModelSubstitution(v, l);
382
  }
383
  // should not set a bound for a value that is exact
384
675
  if (std::find(d_check_model_vars.begin(), d_check_model_vars.end(), v)
385
675
      != d_check_model_vars.end())
386
  {
387
    Trace("nl-ext-model")
388
        << "...ERROR: setting bound for variable that already has exact value."
389
        << std::endl;
390
    Assert(false);
391
    return false;
392
  }
393
225
  Assert(l.isConst());
394
225
  Assert(u.isConst());
395
225
  Assert(l.getConst<Rational>() <= u.getConst<Rational>());
396
225
  d_check_model_bounds[v] = std::pair<Node, Node>(l, u);
397
225
  if (Trace.isOn("nl-ext-cm"))
398
  {
399
    Trace("nl-ext-cm") << "check-model-bound : approximate : ";
400
    printRationalApprox("nl-ext-cm", l);
401
    Trace("nl-ext-cm") << " <= " << v << " <= ";
402
    printRationalApprox("nl-ext-cm", u);
403
    Trace("nl-ext-cm") << std::endl;
404
  }
405
225
  return true;
406
}
407
408
7
bool NlModel::addCheckModelWitness(TNode v, TNode w)
409
{
410
14
  Trace("nl-ext-model") << "* check model witness : " << v << " -> " << w
411
7
                        << std::endl;
412
  // should not set a witness for a value that is already set
413
21
  if (std::find(d_check_model_vars.begin(), d_check_model_vars.end(), v)
414
21
      != d_check_model_vars.end())
415
  {
416
    Trace("nl-ext-model") << "...ERROR: setting witness for variable that "
417
                             "already has a constant value."
418
                          << std::endl;
419
    Assert(false);
420
    return false;
421
  }
422
7
  d_check_model_witnesses.emplace(v, w);
423
7
  return true;
424
}
425
426
416
bool NlModel::hasCheckModelAssignment(Node v) const
427
{
428
416
  if (d_check_model_bounds.find(v) != d_check_model_bounds.end())
429
  {
430
3
    return true;
431
  }
432
413
  if (d_check_model_witnesses.find(v) != d_check_model_witnesses.end())
433
  {
434
    return true;
435
  }
436
826
  return std::find(d_check_model_vars.begin(), d_check_model_vars.end(), v)
437
1239
         != d_check_model_vars.end();
438
}
439
440
167
void NlModel::setUsedApproximate() { d_used_approx = true; }
441
442
16
bool NlModel::usedApproximate() const { return d_used_approx; }
443
444
280
bool NlModel::solveEqualitySimple(Node eq,
445
                                  unsigned d,
446
                                  std::vector<NlLemma>& lemmas)
447
{
448
560
  Node seq = eq;
449
280
  if (!d_check_model_vars.empty())
450
  {
451
225
    seq = arithSubstitute(eq, d_check_model_vars, d_check_model_subs);
452
225
    seq = Rewriter::rewrite(seq);
453
225
    if (seq.isConst())
454
    {
455
72
      if (seq.getConst<bool>())
456
      {
457
        // already true
458
72
        d_check_model_solved[eq] = Node::null();
459
72
        return true;
460
      }
461
      return false;
462
    }
463
  }
464
208
  Trace("nl-ext-cms") << "simple solve equality " << seq << "..." << std::endl;
465
208
  Assert(seq.getKind() == EQUAL);
466
416
  std::map<Node, Node> msum;
467
208
  if (!ArithMSum::getMonomialSumLit(seq, msum))
468
  {
469
    Trace("nl-ext-cms") << "...fail, could not determine monomial sum."
470
                        << std::endl;
471
    return false;
472
  }
473
208
  bool is_valid = true;
474
  // the variable we will solve a quadratic equation for
475
416
  Node var;
476
416
  Node a = d_zero;
477
416
  Node b = d_zero;
478
416
  Node c = d_zero;
479
208
  NodeManager* nm = NodeManager::currentNM();
480
  // the list of variables that occur as a monomial in msum, and whose value
481
  // is so far unconstrained in the model.
482
416
  std::unordered_set<Node> unc_vars;
483
  // the list of variables that occur as a factor in a monomial, and whose
484
  // value is so far unconstrained in the model.
485
416
  std::unordered_set<Node> unc_vars_factor;
486
600
  for (std::pair<const Node, Node>& m : msum)
487
  {
488
784
    Node v = m.first;
489
784
    Node coeff = m.second.isNull() ? d_one : m.second;
490
392
    if (v.isNull())
491
    {
492
107
      c = coeff;
493
    }
494
285
    else if (v.getKind() == NONLINEAR_MULT)
495
    {
496
177
      if (v.getNumChildren() == 2 && v[0].isVar() && v[0] == v[1]
497
127
          && (var.isNull() || var == v[0]))
498
      {
499
        // may solve quadratic
500
9
        a = coeff;
501
9
        var = v[0];
502
      }
503
      else
504
      {
505
50
        is_valid = false;
506
100
        Trace("nl-ext-cms-debug")
507
50
            << "...invalid due to non-linear monomial " << v << std::endl;
508
        // may wish to set an exact bound for a factor and repeat
509
150
        for (const Node& vc : v)
510
        {
511
100
          unc_vars_factor.insert(vc);
512
        }
513
      }
514
    }
515
226
    else if (!v.isVar() || (!var.isNull() && var != v))
516
    {
517
204
      Trace("nl-ext-cms-debug")
518
102
          << "...invalid due to factor " << v << std::endl;
519
      // cannot solve multivariate
520
102
      if (is_valid)
521
      {
522
82
        is_valid = false;
523
        // if b is non-zero, then var is also an unconstrained variable
524
82
        if (b != d_zero)
525
        {
526
25
          unc_vars.insert(var);
527
25
          unc_vars_factor.insert(var);
528
        }
529
      }
530
      // if v is unconstrained, we may turn this equality into a substitution
531
102
      unc_vars.insert(v);
532
102
      unc_vars_factor.insert(v);
533
    }
534
    else
535
    {
536
      // set the variable to solve for
537
124
      b = coeff;
538
124
      var = v;
539
    }
540
  }
541
208
  if (!is_valid)
542
  {
543
    // see if we can solve for a variable?
544
205
    for (const Node& uv : unc_vars)
545
    {
546
104
      Trace("nl-ext-cm-debug") << "check subs var : " << uv << std::endl;
547
      // cannot already have a bound
548
104
      if (uv.isVar() && !hasCheckModelAssignment(uv))
549
      {
550
27
        Node slv;
551
27
        Node veqc;
552
27
        if (ArithMSum::isolate(uv, msum, veqc, slv, EQUAL) != 0)
553
        {
554
27
          Assert(!slv.isNull());
555
          // Currently do not support substitution-with-coefficients.
556
          // We also ensure types are correct here, which avoids substituting
557
          // a term of non-integer type for a variable of integer type.
558
81
          if (veqc.isNull() && !expr::hasSubterm(slv, uv)
559
81
              && slv.getType().isSubtypeOf(uv.getType()))
560
          {
561
54
            Trace("nl-ext-cm")
562
27
                << "check-model-subs : " << uv << " -> " << slv << std::endl;
563
27
            bool ret = addCheckModelSubstitution(uv, slv);
564
27
            if (ret)
565
            {
566
54
              Trace("nl-ext-cms") << "...success, model substitution " << uv
567
27
                                  << " -> " << slv << std::endl;
568
27
              d_check_model_solved[eq] = uv;
569
            }
570
27
            return ret;
571
          }
572
        }
573
      }
574
    }
575
    // see if we can assign a variable to a constant
576
182
    for (const Node& uvf : unc_vars_factor)
577
    {
578
127
      Trace("nl-ext-cm-debug") << "check set var : " << uvf << std::endl;
579
      // cannot already have a bound
580
127
      if (uvf.isVar() && !hasCheckModelAssignment(uvf))
581
      {
582
92
        Node uvfv = computeConcreteModelValue(uvf);
583
46
        if (Trace.isOn("nl-ext-cm"))
584
        {
585
          Trace("nl-ext-cm") << "check-model-bound : exact : " << uvf << " = ";
586
          printRationalApprox("nl-ext-cm", uvfv);
587
          Trace("nl-ext-cm") << std::endl;
588
        }
589
46
        bool ret = addCheckModelSubstitution(uvf, uvfv);
590
        // recurse
591
46
        return ret ? solveEqualitySimple(eq, d, lemmas) : false;
592
      }
593
    }
594
110
    Trace("nl-ext-cms") << "...fail due to constrained invalid terms."
595
55
                        << std::endl;
596
55
    return false;
597
  }
598
80
  else if (var.isNull() || var.getType().isInteger())
599
  {
600
    // cannot solve quadratic equations for integer variables
601
8
    Trace("nl-ext-cms") << "...fail due to variable to solve for." << std::endl;
602
8
    return false;
603
  }
604
605
  // we are linear, it is simple
606
72
  if (a == d_zero)
607
  {
608
69
    if (b == d_zero)
609
    {
610
      Trace("nl-ext-cms") << "...fail due to zero a/b." << std::endl;
611
      Assert(false);
612
      return false;
613
    }
614
138
    Node val = nm->mkConst(-c.getConst<Rational>() / b.getConst<Rational>());
615
69
    if (Trace.isOn("nl-ext-cm"))
616
    {
617
      Trace("nl-ext-cm") << "check-model-bound : exact : " << var << " = ";
618
      printRationalApprox("nl-ext-cm", val);
619
      Trace("nl-ext-cm") << std::endl;
620
    }
621
69
    bool ret = addCheckModelSubstitution(var, val);
622
69
    if (ret)
623
    {
624
69
      Trace("nl-ext-cms") << "...success, solved linear." << std::endl;
625
69
      d_check_model_solved[eq] = var;
626
    }
627
69
    return ret;
628
  }
629
3
  Trace("nl-ext-quad") << "Solve quadratic : " << seq << std::endl;
630
3
  Trace("nl-ext-quad") << "  a : " << a << std::endl;
631
3
  Trace("nl-ext-quad") << "  b : " << b << std::endl;
632
3
  Trace("nl-ext-quad") << "  c : " << c << std::endl;
633
6
  Node two_a = nm->mkNode(MULT, d_two, a);
634
3
  two_a = Rewriter::rewrite(two_a);
635
  Node sqrt_val = nm->mkNode(
636
6
      MINUS, nm->mkNode(MULT, b, b), nm->mkNode(MULT, d_two, two_a, c));
637
3
  sqrt_val = Rewriter::rewrite(sqrt_val);
638
3
  Trace("nl-ext-quad") << "Will approximate sqrt " << sqrt_val << std::endl;
639
3
  Assert(sqrt_val.isConst());
640
  // if it is negative, then we are in conflict
641
3
  if (sqrt_val.getConst<Rational>().sgn() == -1)
642
  {
643
    Node conf = seq.negate();
644
    Trace("nl-ext-lemma") << "NlModel::Lemma : quadratic no root : " << conf
645
                          << std::endl;
646
    lemmas.emplace_back(InferenceId::ARITH_NL_CM_QUADRATIC_EQ, conf);
647
    Trace("nl-ext-cms") << "...fail due to negative discriminant." << std::endl;
648
    return false;
649
  }
650
3
  if (hasCheckModelAssignment(var))
651
  {
652
    Trace("nl-ext-cms") << "...fail due to bounds on variable to solve for."
653
                        << std::endl;
654
    // two quadratic equations for same variable, give up
655
    return false;
656
  }
657
  // approximate the square root of sqrt_val
658
6
  Node l, u;
659
3
  if (!getApproximateSqrt(sqrt_val, l, u, 15 + d))
660
  {
661
    Trace("nl-ext-cms") << "...fail, could not approximate sqrt." << std::endl;
662
    return false;
663
  }
664
3
  d_used_approx = true;
665
6
  Trace("nl-ext-quad") << "...got " << l << " <= sqrt(" << sqrt_val
666
3
                       << ") <= " << u << std::endl;
667
6
  Node negb = nm->mkConst(-b.getConst<Rational>());
668
6
  Node coeffa = nm->mkConst(Rational(1) / two_a.getConst<Rational>());
669
  // two possible bound regions
670
6
  Node bounds[2][2];
671
6
  Node diff_bound[2];
672
6
  Node m_var = computeConcreteModelValue(var);
673
3
  Assert(m_var.isConst());
674
9
  for (unsigned r = 0; r < 2; r++)
675
  {
676
18
    for (unsigned b2 = 0; b2 < 2; b2++)
677
    {
678
24
      Node val = b2 == 0 ? l : u;
679
      // (-b +- approx_sqrt( b^2 - 4ac ))/2a
680
      Node approx = nm->mkNode(
681
24
          MULT, coeffa, nm->mkNode(r == 0 ? MINUS : PLUS, negb, val));
682
12
      approx = Rewriter::rewrite(approx);
683
12
      bounds[r][b2] = approx;
684
12
      Assert(approx.isConst());
685
    }
686
6
    if (bounds[r][0].getConst<Rational>() > bounds[r][1].getConst<Rational>())
687
    {
688
      // ensure bound is (lower, upper)
689
6
      Node tmp = bounds[r][0];
690
3
      bounds[r][0] = bounds[r][1];
691
3
      bounds[r][1] = tmp;
692
    }
693
    Node diff =
694
        nm->mkNode(MINUS,
695
                   m_var,
696
24
                   nm->mkNode(MULT,
697
12
                              nm->mkConst(Rational(1) / Rational(2)),
698
24
                              nm->mkNode(PLUS, bounds[r][0], bounds[r][1])));
699
6
    if (Trace.isOn("nl-ext-cm-debug"))
700
    {
701
      Trace("nl-ext-cm-debug") << "Bound option #" << r << " : ";
702
      printRationalApprox("nl-ext-cm-debug", bounds[r][0]);
703
      Trace("nl-ext-cm-debug") << "...";
704
      printRationalApprox("nl-ext-cm-debug", bounds[r][1]);
705
      Trace("nl-ext-cm-debug") << std::endl;
706
    }
707
6
    diff = Rewriter::rewrite(diff);
708
6
    Assert(diff.isConst());
709
6
    diff = nm->mkConst(diff.getConst<Rational>().abs());
710
6
    diff_bound[r] = diff;
711
6
    if (Trace.isOn("nl-ext-cm-debug"))
712
    {
713
      Trace("nl-ext-cm-debug") << "...diff from model value (";
714
      printRationalApprox("nl-ext-cm-debug", m_var);
715
      Trace("nl-ext-cm-debug") << ") is ";
716
      printRationalApprox("nl-ext-cm-debug", diff_bound[r]);
717
      Trace("nl-ext-cm-debug") << std::endl;
718
    }
719
  }
720
  // take the one that var is closer to in the model
721
6
  Node cmp = nm->mkNode(GEQ, diff_bound[0], diff_bound[1]);
722
3
  cmp = Rewriter::rewrite(cmp);
723
3
  Assert(cmp.isConst());
724
3
  unsigned r_use_index = cmp == d_true ? 1 : 0;
725
3
  if (Trace.isOn("nl-ext-cm"))
726
  {
727
    Trace("nl-ext-cm") << "check-model-bound : approximate (sqrt) : ";
728
    printRationalApprox("nl-ext-cm", bounds[r_use_index][0]);
729
    Trace("nl-ext-cm") << " <= " << var << " <= ";
730
    printRationalApprox("nl-ext-cm", bounds[r_use_index][1]);
731
    Trace("nl-ext-cm") << std::endl;
732
  }
733
  bool ret =
734
3
      addCheckModelBound(var, bounds[r_use_index][0], bounds[r_use_index][1]);
735
3
  if (ret)
736
  {
737
3
    d_check_model_solved[eq] = var;
738
3
    Trace("nl-ext-cms") << "...success, solved quadratic." << std::endl;
739
  }
740
3
  return ret;
741
}
742
743
2472
bool NlModel::simpleCheckModelLit(Node lit)
744
{
745
4944
  Trace("nl-ext-cms") << "*** Simple check-model lit for " << lit << "..."
746
2472
                      << std::endl;
747
2472
  if (lit.isConst())
748
  {
749
1223
    Trace("nl-ext-cms") << "  return constant." << std::endl;
750
1223
    return lit.getConst<bool>();
751
  }
752
1249
  NodeManager* nm = NodeManager::currentNM();
753
1249
  bool pol = lit.getKind() != kind::NOT;
754
2498
  Node atom = lit.getKind() == kind::NOT ? lit[0] : lit;
755
756
1249
  if (atom.getKind() == EQUAL)
757
  {
758
    // x = a is ( x >= a ^ x <= a )
759
358
    for (unsigned i = 0; i < 2; i++)
760
    {
761
518
      Node lit2 = nm->mkNode(GEQ, atom[i], atom[1 - i]);
762
358
      if (!pol)
763
      {
764
338
        lit2 = lit2.negate();
765
      }
766
358
      lit2 = Rewriter::rewrite(lit2);
767
358
      bool success = simpleCheckModelLit(lit2);
768
358
      if (success != pol)
769
      {
770
        // false != true -> one conjunct of equality is false, we fail
771
        // true != false -> one disjunct of disequality is true, we succeed
772
198
        return success;
773
      }
774
    }
775
    // both checks passed and polarity is true, or both checks failed and
776
    // polarity is false
777
    return pol;
778
  }
779
1051
  else if (atom.getKind() != GEQ)
780
  {
781
    Trace("nl-ext-cms") << "  failed due to unknown literal." << std::endl;
782
    return false;
783
  }
784
  // get the monomial sum
785
2102
  std::map<Node, Node> msum;
786
1051
  if (!ArithMSum::getMonomialSumLit(atom, msum))
787
  {
788
    Trace("nl-ext-cms") << "  failed due to get msum." << std::endl;
789
    return false;
790
  }
791
  // simple interval analysis
792
1051
  if (simpleCheckModelMsum(msum, pol))
793
  {
794
730
    return true;
795
  }
796
  // can also try reasoning about univariate quadratic equations
797
642
  Trace("nl-ext-cms-debug")
798
321
      << "* Try univariate quadratic analysis..." << std::endl;
799
642
  std::vector<Node> vs_invalid;
800
642
  std::unordered_set<Node> vs;
801
642
  std::map<Node, Node> v_a;
802
642
  std::map<Node, Node> v_b;
803
  // get coefficients...
804
959
  for (std::pair<const Node, Node>& m : msum)
805
  {
806
1276
    Node v = m.first;
807
638
    if (!v.isNull())
808
    {
809
357
      if (v.isVar())
810
      {
811
28
        v_b[v] = m.second.isNull() ? d_one : m.second;
812
28
        vs.insert(v);
813
      }
814
687
      else if (v.getKind() == NONLINEAR_MULT && v.getNumChildren() == 2
815
687
               && v[0] == v[1] && v[0].isVar())
816
      {
817
29
        v_a[v[0]] = m.second.isNull() ? d_one : m.second;
818
29
        vs.insert(v[0]);
819
      }
820
      else
821
      {
822
300
        vs_invalid.push_back(v);
823
      }
824
    }
825
  }
826
  // solve the valid variables...
827
321
  Node invalid_vsum = vs_invalid.empty() ? d_zero
828
280
                                         : (vs_invalid.size() == 1
829
260
                                                ? vs_invalid[0]
830
1182
                                                : nm->mkNode(PLUS, vs_invalid));
831
  // substitution to try
832
642
  std::vector<Node> qvars;
833
642
  std::vector<Node> qsubs;
834
362
  for (const Node& v : vs)
835
  {
836
    // is it a valid variable?
837
    std::map<Node, std::pair<Node, Node>>::iterator bit =
838
41
        d_check_model_bounds.find(v);
839
41
    if (!expr::hasSubterm(invalid_vsum, v) && bit != d_check_model_bounds.end())
840
    {
841
41
      std::map<Node, Node>::iterator it = v_a.find(v);
842
41
      if (it != v_a.end())
843
      {
844
58
        Node a = it->second;
845
29
        Assert(a.isConst());
846
29
        int asgn = a.getConst<Rational>().sgn();
847
29
        Assert(asgn != 0);
848
58
        Node t = nm->mkNode(MULT, a, v, v);
849
58
        Node b = d_zero;
850
29
        it = v_b.find(v);
851
29
        if (it != v_b.end())
852
        {
853
16
          b = it->second;
854
16
          t = nm->mkNode(PLUS, t, nm->mkNode(MULT, b, v));
855
        }
856
29
        t = Rewriter::rewrite(t);
857
58
        Trace("nl-ext-cms-debug") << "Trying to find min/max for quadratic "
858
29
                                  << t << "..." << std::endl;
859
29
        Trace("nl-ext-cms-debug") << "    a = " << a << std::endl;
860
29
        Trace("nl-ext-cms-debug") << "    b = " << b << std::endl;
861
        // find maximal/minimal value on the interval
862
        Node apex = nm->mkNode(
863
58
            DIVISION, nm->mkNode(UMINUS, b), nm->mkNode(MULT, d_two, a));
864
29
        apex = Rewriter::rewrite(apex);
865
29
        Assert(apex.isConst());
866
        // for lower, upper, whether we are greater than the apex
867
        bool cmp[2];
868
58
        Node boundn[2];
869
87
        for (unsigned r = 0; r < 2; r++)
870
        {
871
58
          boundn[r] = r == 0 ? bit->second.first : bit->second.second;
872
116
          Node cmpn = nm->mkNode(GT, boundn[r], apex);
873
58
          cmpn = Rewriter::rewrite(cmpn);
874
58
          Assert(cmpn.isConst());
875
58
          cmp[r] = cmpn.getConst<bool>();
876
        }
877
29
        Trace("nl-ext-cms-debug") << "  apex " << apex << std::endl;
878
58
        Trace("nl-ext-cms-debug")
879
29
            << "  lower " << boundn[0] << ", cmp: " << cmp[0] << std::endl;
880
58
        Trace("nl-ext-cms-debug")
881
29
            << "  upper " << boundn[1] << ", cmp: " << cmp[1] << std::endl;
882
29
        Assert(boundn[0].getConst<Rational>()
883
               <= boundn[1].getConst<Rational>());
884
58
        Node s;
885
29
        qvars.push_back(v);
886
29
        if (cmp[0] != cmp[1])
887
        {
888
          Assert(!cmp[0] && cmp[1]);
889
          // does the sign match the bound?
890
          if ((asgn == 1) == pol)
891
          {
892
            // the apex is the max/min value
893
            s = apex;
894
            Trace("nl-ext-cms-debug") << "  ...set to apex." << std::endl;
895
          }
896
          else
897
          {
898
            // it is one of the endpoints, plug in and compare
899
            Node tcmpn[2];
900
            for (unsigned r = 0; r < 2; r++)
901
            {
902
              qsubs.push_back(boundn[r]);
903
              Node ts = arithSubstitute(t, qvars, qsubs);
904
              tcmpn[r] = Rewriter::rewrite(ts);
905
              qsubs.pop_back();
906
            }
907
            Node tcmp = nm->mkNode(LT, tcmpn[0], tcmpn[1]);
908
            Trace("nl-ext-cms-debug")
909
                << "  ...both sides of apex, compare " << tcmp << std::endl;
910
            tcmp = Rewriter::rewrite(tcmp);
911
            Assert(tcmp.isConst());
912
            unsigned bindex_use = (tcmp.getConst<bool>() == pol) ? 1 : 0;
913
            Trace("nl-ext-cms-debug")
914
                << "  ...set to " << (bindex_use == 1 ? "upper" : "lower")
915
                << std::endl;
916
            s = boundn[bindex_use];
917
          }
918
        }
919
        else
920
        {
921
          // both to one side of the apex
922
          // we figure out which bound to use (lower or upper) based on
923
          // three factors:
924
          // (1) whether a's sign is positive,
925
          // (2) whether we are greater than the apex of the parabola,
926
          // (3) the polarity of the constraint, i.e. >= or <=.
927
          // there are 8 cases of these factors, which we test here.
928
29
          unsigned bindex_use = (((asgn == 1) == cmp[0]) == pol) ? 0 : 1;
929
58
          Trace("nl-ext-cms-debug")
930
29
              << "  ...set to " << (bindex_use == 1 ? "upper" : "lower")
931
29
              << std::endl;
932
29
          s = boundn[bindex_use];
933
        }
934
29
        Assert(!s.isNull());
935
29
        qsubs.push_back(s);
936
58
        Trace("nl-ext-cms") << "* set bound based on quadratic : " << v
937
29
                            << " -> " << s << std::endl;
938
      }
939
    }
940
  }
941
321
  if (!qvars.empty())
942
  {
943
29
    Assert(qvars.size() == qsubs.size());
944
58
    Node slit = arithSubstitute(lit, qvars, qsubs);
945
29
    slit = Rewriter::rewrite(slit);
946
29
    return simpleCheckModelLit(slit);
947
  }
948
292
  return false;
949
}
950
951
1051
bool NlModel::simpleCheckModelMsum(const std::map<Node, Node>& msum, bool pol)
952
{
953
1051
  Trace("nl-ext-cms-debug") << "* Try simple interval analysis..." << std::endl;
954
1051
  NodeManager* nm = NodeManager::currentNM();
955
  // map from transcendental functions to whether they were set to lower
956
  // bound
957
1051
  bool simpleSuccess = true;
958
2102
  std::map<Node, bool> set_bound;
959
2102
  std::vector<Node> sum_bound;
960
3105
  for (const std::pair<const Node, Node>& m : msum)
961
  {
962
4124
    Node v = m.first;
963
2070
    if (v.isNull())
964
    {
965
925
      sum_bound.push_back(m.second.isNull() ? d_one : m.second);
966
    }
967
    else
968
    {
969
1145
      Trace("nl-ext-cms-debug") << "- monomial : " << v << std::endl;
970
      // --- whether we should set a lower bound for this monomial
971
      bool set_lower =
972
1145
          (m.second.isNull() || m.second.getConst<Rational>().sgn() == 1)
973
1145
          == pol;
974
2290
      Trace("nl-ext-cms-debug")
975
1145
          << "set bound to " << (set_lower ? "lower" : "upper") << std::endl;
976
977
      // --- Collect variables and factors in v
978
2274
      std::vector<Node> vars;
979
2274
      std::vector<unsigned> factors;
980
1145
      if (v.getKind() == NONLINEAR_MULT)
981
      {
982
55
        unsigned last_start = 0;
983
165
        for (unsigned i = 0, nchildren = v.getNumChildren(); i < nchildren; i++)
984
        {
985
          // are we at the end?
986
110
          if (i + 1 == nchildren || v[i + 1] != v[i])
987
          {
988
55
            unsigned vfact = 1 + (i - last_start);
989
55
            last_start = (i + 1);
990
55
            vars.push_back(v[i]);
991
55
            factors.push_back(vfact);
992
          }
993
        }
994
      }
995
      else
996
      {
997
1090
        vars.push_back(v);
998
1090
        factors.push_back(1);
999
      }
1000
1001
      // --- Get the lower and upper bounds and sign information.
1002
      // Whether we have an (odd) number of negative factors in vars, apart
1003
      // from the variable at choose_index.
1004
1145
      bool has_neg_factor = false;
1005
1145
      int choose_index = -1;
1006
2274
      std::vector<Node> ls;
1007
2274
      std::vector<Node> us;
1008
      // the relevant sign information for variables with odd exponents:
1009
      //   1: both signs of the interval of this variable are positive,
1010
      //  -1: both signs of the interval of this variable are negative.
1011
2274
      std::vector<int> signs;
1012
1145
      Trace("nl-ext-cms-debug") << "get sign information..." << std::endl;
1013
2290
      for (unsigned i = 0, size = vars.size(); i < size; i++)
1014
      {
1015
2290
        Node vc = vars[i];
1016
1145
        unsigned vcfact = factors[i];
1017
1145
        if (Trace.isOn("nl-ext-cms-debug"))
1018
        {
1019
          Trace("nl-ext-cms-debug") << "-- " << vc;
1020
          if (vcfact > 1)
1021
          {
1022
            Trace("nl-ext-cms-debug") << "^" << vcfact;
1023
          }
1024
          Trace("nl-ext-cms-debug") << " ";
1025
        }
1026
        std::map<Node, std::pair<Node, Node>>::iterator bit =
1027
1145
            d_check_model_bounds.find(vc);
1028
        // if there is a model bound for this term
1029
1145
        if (bit != d_check_model_bounds.end())
1030
        {
1031
2290
          Node l = bit->second.first;
1032
2290
          Node u = bit->second.second;
1033
1145
          ls.push_back(l);
1034
1145
          us.push_back(u);
1035
1145
          int vsign = 0;
1036
1145
          if (vcfact % 2 == 1)
1037
          {
1038
1090
            vsign = 1;
1039
1090
            int lsgn = l.getConst<Rational>().sgn();
1040
1090
            int usgn = u.getConst<Rational>().sgn();
1041
2180
            Trace("nl-ext-cms-debug")
1042
1090
                << "bound_sign(" << lsgn << "," << usgn << ") ";
1043
1090
            if (lsgn == -1)
1044
            {
1045
93
              if (usgn < 1)
1046
              {
1047
                // must have a negative factor
1048
93
                has_neg_factor = !has_neg_factor;
1049
93
                vsign = -1;
1050
              }
1051
              else if (choose_index == -1)
1052
              {
1053
                // set the choose index to this
1054
                choose_index = i;
1055
                vsign = 0;
1056
              }
1057
              else
1058
              {
1059
                // ambiguous, can't determine the bound
1060
                Trace("nl-ext-cms")
1061
                    << "  failed due to ambiguious monomial." << std::endl;
1062
                return false;
1063
              }
1064
            }
1065
          }
1066
1145
          Trace("nl-ext-cms-debug") << " -> " << vsign << std::endl;
1067
1145
          signs.push_back(vsign);
1068
        }
1069
        else
1070
        {
1071
          Assert(d_check_model_witnesses.find(vc)
1072
                 == d_check_model_witnesses.end())
1073
              << "No variable should be assigned a witness term if we get "
1074
                 "here. "
1075
              << vc << " is, though." << std::endl;
1076
          Trace("nl-ext-cms-debug") << std::endl;
1077
          Trace("nl-ext-cms")
1078
              << "  failed due to unknown bound for " << vc << std::endl;
1079
          // should either assign a model bound or eliminate the variable
1080
          // via substitution
1081
          Assert(false);
1082
          return false;
1083
        }
1084
      }
1085
      // whether we will try to minimize/maximize (-1/1) the absolute value
1086
1145
      int setAbs = (set_lower == has_neg_factor) ? 1 : -1;
1087
2290
      Trace("nl-ext-cms-debug")
1088
1145
          << "set absolute value to " << (setAbs == 1 ? "maximal" : "minimal")
1089
1145
          << std::endl;
1090
1091
2274
      std::vector<Node> vbs;
1092
1145
      Trace("nl-ext-cms-debug") << "set bounds..." << std::endl;
1093
2274
      for (unsigned i = 0, size = vars.size(); i < size; i++)
1094
      {
1095
2274
        Node vc = vars[i];
1096
1145
        unsigned vcfact = factors[i];
1097
2274
        Node l = ls[i];
1098
2274
        Node u = us[i];
1099
        bool vc_set_lower;
1100
1145
        int vcsign = signs[i];
1101
2290
        Trace("nl-ext-cms-debug")
1102
1145
            << "Bounds for " << vc << " : " << l << ", " << u
1103
1145
            << ", sign : " << vcsign << ", factor : " << vcfact << std::endl;
1104
1145
        if (l == u)
1105
        {
1106
          // by convention, always say it is lower if they are the same
1107
          vc_set_lower = true;
1108
          Trace("nl-ext-cms-debug")
1109
              << "..." << vc << " equal bound, set to lower" << std::endl;
1110
        }
1111
        else
1112
        {
1113
1145
          if (vcfact % 2 == 0)
1114
          {
1115
            // minimize or maximize its absolute value
1116
110
            Rational la = l.getConst<Rational>().abs();
1117
110
            Rational ua = u.getConst<Rational>().abs();
1118
55
            if (la == ua)
1119
            {
1120
              // by convention, always say it is lower if abs are the same
1121
              vc_set_lower = true;
1122
              Trace("nl-ext-cms-debug")
1123
                  << "..." << vc << " equal abs, set to lower" << std::endl;
1124
            }
1125
            else
1126
            {
1127
55
              vc_set_lower = (la > ua) == (setAbs == 1);
1128
            }
1129
          }
1130
1090
          else if (signs[i] == 0)
1131
          {
1132
            // we choose this index to match the overall set_lower
1133
            vc_set_lower = set_lower;
1134
          }
1135
          else
1136
          {
1137
1090
            vc_set_lower = (signs[i] != setAbs);
1138
          }
1139
2290
          Trace("nl-ext-cms-debug")
1140
1145
              << "..." << vc << " set to " << (vc_set_lower ? "lower" : "upper")
1141
1145
              << std::endl;
1142
        }
1143
        // check whether this is a conflicting bound
1144
1145
        std::map<Node, bool>::iterator itsb = set_bound.find(vc);
1145
1145
        if (itsb == set_bound.end())
1146
        {
1147
1125
          set_bound[vc] = vc_set_lower;
1148
        }
1149
20
        else if (itsb->second != vc_set_lower)
1150
        {
1151
32
          Trace("nl-ext-cms")
1152
16
              << "  failed due to conflicting bound for " << vc << std::endl;
1153
16
          return false;
1154
        }
1155
        // must over/under approximate based on vc_set_lower, computed above
1156
2258
        Node vb = vc_set_lower ? l : u;
1157
2297
        for (unsigned i2 = 0; i2 < vcfact; i2++)
1158
        {
1159
1168
          vbs.push_back(vb);
1160
        }
1161
      }
1162
1129
      if (!simpleSuccess)
1163
      {
1164
        break;
1165
      }
1166
2258
      Node vbound = vbs.size() == 1 ? vbs[0] : nm->mkNode(MULT, vbs);
1167
1129
      sum_bound.push_back(ArithMSum::mkCoeffTerm(m.second, vbound));
1168
    }
1169
  }
1170
  // if the exact bound was computed via simple analysis above
1171
  // make the bound
1172
2070
  Node bound;
1173
1035
  if (sum_bound.size() > 1)
1174
  {
1175
961
    bound = nm->mkNode(kind::PLUS, sum_bound);
1176
  }
1177
74
  else if (sum_bound.size() == 1)
1178
  {
1179
74
    bound = sum_bound[0];
1180
  }
1181
  else
1182
  {
1183
    bound = d_zero;
1184
  }
1185
  // make the comparison
1186
2070
  Node comp = nm->mkNode(kind::GEQ, bound, d_zero);
1187
1035
  if (!pol)
1188
  {
1189
631
    comp = comp.negate();
1190
  }
1191
1035
  Trace("nl-ext-cms") << "  comparison is : " << comp << std::endl;
1192
1035
  comp = Rewriter::rewrite(comp);
1193
1035
  Assert(comp.isConst());
1194
1035
  Trace("nl-ext-cms") << "  returned : " << comp << std::endl;
1195
1035
  return comp == d_true;
1196
}
1197
1198
3
bool NlModel::getApproximateSqrt(Node c, Node& l, Node& u, unsigned iter) const
1199
{
1200
3
  Assert(c.isConst());
1201
3
  if (c == d_one || c == d_zero)
1202
  {
1203
    l = c;
1204
    u = c;
1205
    return true;
1206
  }
1207
6
  Rational rc = c.getConst<Rational>();
1208
1209
6
  Rational rl = rc < Rational(1) ? rc : Rational(1);
1210
6
  Rational ru = rc < Rational(1) ? Rational(1) : rc;
1211
3
  unsigned count = 0;
1212
6
  Rational half = Rational(1) / Rational(2);
1213
117
  while (count < iter)
1214
  {
1215
114
    Rational curr = half * (rl + ru);
1216
114
    Rational curr_sq = curr * curr;
1217
57
    if (curr_sq == rc)
1218
    {
1219
      rl = curr;
1220
      ru = curr;
1221
      break;
1222
    }
1223
57
    else if (curr_sq < rc)
1224
    {
1225
27
      rl = curr;
1226
    }
1227
    else
1228
    {
1229
30
      ru = curr;
1230
    }
1231
57
    count++;
1232
  }
1233
1234
3
  NodeManager* nm = NodeManager::currentNM();
1235
3
  l = nm->mkConst(rl);
1236
3
  u = nm->mkConst(ru);
1237
3
  return true;
1238
}
1239
1240
53637
void NlModel::printModelValue(const char* c, Node n, unsigned prec) const
1241
{
1242
53637
  if (Trace.isOn(c))
1243
  {
1244
    Trace(c) << "  " << n << " -> ";
1245
    for (int i = 1; i >= 0; --i)
1246
    {
1247
      std::map<Node, Node>::const_iterator it = d_mv[i].find(n);
1248
      Assert(it != d_mv[i].end());
1249
      if (it->second.isConst())
1250
      {
1251
        printRationalApprox(c, it->second, prec);
1252
      }
1253
      else
1254
      {
1255
        Trace(c) << "?";
1256
      }
1257
      Trace(c) << (i == 1 ? " [actual: " : " ]");
1258
    }
1259
    Trace(c) << std::endl;
1260
  }
1261
53637
}
1262
1263
360
void NlModel::getModelValueRepair(
1264
    std::map<Node, Node>& arithModel,
1265
    std::map<Node, std::pair<Node, Node>>& approximations,
1266
    std::map<Node, Node>& witnesses,
1267
    bool witnessToValue)
1268
{
1269
360
  Trace("nl-model") << "NlModel::getModelValueRepair:" << std::endl;
1270
  // If we extended the model with entries x -> 0 for unconstrained values,
1271
  // we first update the map to the extended one.
1272
360
  if (d_arithVal.size() > arithModel.size())
1273
  {
1274
4
    arithModel = d_arithVal;
1275
  }
1276
  // Record the approximations we used. This code calls the
1277
  // recordApproximation method of the model, which overrides the model
1278
  // values for variables that we solved for, using techniques specific to
1279
  // this class.
1280
360
  NodeManager* nm = NodeManager::currentNM();
1281
22
  for (const std::pair<const Node, std::pair<Node, Node>>& cb :
1282
360
       d_check_model_bounds)
1283
  {
1284
44
    Node l = cb.second.first;
1285
44
    Node u = cb.second.second;
1286
44
    Node pred;
1287
44
    Node v = cb.first;
1288
22
    if (l != u)
1289
    {
1290
22
      pred = nm->mkNode(AND, nm->mkNode(GEQ, v, l), nm->mkNode(GEQ, u, v));
1291
22
      Trace("nl-model") << v << " approximated as " << pred << std::endl;
1292
44
      Node witness;
1293
22
      if (witnessToValue)
1294
      {
1295
        // witness is the midpoint
1296
        witness = nm->mkNode(
1297
            MULT, nm->mkConst(Rational(1, 2)), nm->mkNode(PLUS, l, u));
1298
        witness = Rewriter::rewrite(witness);
1299
        Trace("nl-model") << v << " witness is " << witness << std::endl;
1300
      }
1301
22
      approximations[v] = std::pair<Node, Node>(pred, witness);
1302
    }
1303
    else
1304
    {
1305
      // overwrite
1306
      arithModel[v] = l;
1307
      Trace("nl-model") << v << " exact approximation is " << l << std::endl;
1308
    }
1309
  }
1310
367
  for (const auto& vw : d_check_model_witnesses)
1311
  {
1312
7
    Trace("nl-model") << vw.first << " witness is " << vw.second << std::endl;
1313
7
    witnesses.emplace(vw.first, vw.second);
1314
  }
1315
  // Also record the exact values we used. An exact value can be seen as a
1316
  // special kind approximation of the form (witness x. x = exact_value).
1317
  // Notice that the above term gets rewritten such that the choice function
1318
  // is eliminated.
1319
558
  for (size_t i = 0, num = d_check_model_vars.size(); i < num; i++)
1320
  {
1321
396
    Node v = d_check_model_vars[i];
1322
396
    Node s = d_check_model_subs[i];
1323
    // overwrite
1324
198
    arithModel[v] = s;
1325
198
    Trace("nl-model") << v << " solved is " << s << std::endl;
1326
  }
1327
1328
  // multiplication terms should not be given values; their values are
1329
  // implied by the monomials that they consist of
1330
720
  std::vector<Node> amErase;
1331
8002
  for (const std::pair<const Node, Node>& am : arithModel)
1332
  {
1333
7642
    if (am.first.getKind() == NONLINEAR_MULT)
1334
    {
1335
1581
      amErase.push_back(am.first);
1336
    }
1337
  }
1338
1941
  for (const Node& ae : amErase)
1339
  {
1340
1581
    arithModel.erase(ae);
1341
  }
1342
360
}
1343
1344
}  // namespace nl
1345
}  // namespace arith
1346
}  // namespace theory
1347
22746
}  // namespace cvc5