GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/nl/ext/monomial_bounds_check.cpp Lines: 168 294 57.1 %
Date: 2021-05-22 Branches: 353 1198 29.5 %

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
 * Check for monomial bound inference lemmas.
14
 */
15
16
#include "theory/arith/nl/ext/monomial_bounds_check.h"
17
18
#include "expr/node.h"
19
#include "expr/proof.h"
20
#include "options/arith_options.h"
21
#include "theory/arith/arith_msum.h"
22
#include "theory/arith/arith_utilities.h"
23
#include "theory/arith/inference_manager.h"
24
#include "theory/arith/nl/ext/ext_state.h"
25
#include "theory/arith/nl/nl_model.h"
26
#include "theory/rewriter.h"
27
28
namespace cvc5 {
29
namespace theory {
30
namespace arith {
31
namespace nl {
32
33
namespace {
34
void debugPrintBound(const char* c, Node coeff, Node x, Kind type, Node rhs)
35
{
36
  Node t = ArithMSum::mkCoeffTerm(coeff, x);
37
  Trace(c) << t << " " << type << " " << rhs;
38
}
39
40
2218
bool hasNewMonomials(Node n, const std::vector<Node>& existing)
41
{
42
4436
  std::set<Node> visited;
43
44
4436
  std::vector<Node> worklist;
45
2218
  worklist.push_back(n);
46
47928
  while (!worklist.empty())
47
  {
48
47164
    Node current = worklist.back();
49
24309
    worklist.pop_back();
50
24309
    if (visited.find(current) == visited.end())
51
    {
52
21860
      visited.insert(current);
53
21860
      if (current.getKind() == Kind::NONLINEAR_MULT)
54
      {
55
10002
        if (std::find(existing.begin(), existing.end(), current)
56
10002
            == existing.end())
57
        {
58
1454
          return true;
59
        }
60
      }
61
      else
62
      {
63
18526
        worklist.insert(worklist.end(), current.begin(), current.end());
64
      }
65
    }
66
  }
67
764
  return false;
68
}
69
}  // namespace
70
71
4914
MonomialBoundsCheck::MonomialBoundsCheck(ExtState* data)
72
4914
    : d_data(data), d_cdb(d_data->d_mdb)
73
{
74
4914
}
75
76
2829
void MonomialBoundsCheck::init()
77
{
78
2829
  d_ci.clear();
79
2829
  d_ci_exp.clear();
80
2829
  d_ci_max.clear();
81
2829
}
82
83
528
void MonomialBoundsCheck::checkBounds(const std::vector<Node>& asserts,
84
                                      const std::vector<Node>& false_asserts)
85
{
86
  // sort monomials by degree
87
528
  Trace("nl-ext-proc") << "Sort monomials by degree..." << std::endl;
88
528
  d_data->d_mdb.sortByDegree(d_data->d_ms);
89
  // all monomials
90
2112
  d_data->d_mterms.insert(d_data->d_mterms.end(),
91
528
                          d_data->d_ms_vars.begin(),
92
2112
                          d_data->d_ms_vars.end());
93
1056
  d_data->d_mterms.insert(
94
1056
      d_data->d_mterms.end(), d_data->d_ms.begin(), d_data->d_ms.end());
95
96
  const std::map<Node, std::map<Node, ConstraintInfo> >& cim =
97
528
      d_cdb.getConstraints();
98
99
528
  NodeManager* nm = NodeManager::currentNM();
100
  // register constraints
101
528
  Trace("nl-ext-debug") << "Register bound constraints..." << std::endl;
102
34340
  for (const Node& lit : asserts)
103
  {
104
33812
    bool polarity = lit.getKind() != Kind::NOT;
105
67624
    Node atom = lit.getKind() == Kind::NOT ? lit[0] : lit;
106
33812
    d_cdb.registerConstraint(atom);
107
    bool is_false_lit =
108
67624
        std::find(false_asserts.begin(), false_asserts.end(), lit)
109
101436
        != false_asserts.end();
110
    // add information about bounds to variables
111
    std::map<Node, std::map<Node, ConstraintInfo> >::const_iterator itc =
112
33812
        cim.find(atom);
113
33812
    if (itc == cim.end())
114
    {
115
      continue;
116
    }
117
95474
    for (const std::pair<const Node, ConstraintInfo>& itcc : itc->second)
118
    {
119
123324
      Node x = itcc.first;
120
123324
      Node coeff = itcc.second.d_coeff;
121
123324
      Node rhs = itcc.second.d_rhs;
122
61662
      Kind type = itcc.second.d_type;
123
123324
      Node exp = lit;
124
61662
      if (!polarity)
125
      {
126
        // reverse
127
34420
        if (type == Kind::EQUAL)
128
        {
129
          // we will take the strict inequality in the direction of the
130
          // model
131
26692
          Node lhs = ArithMSum::mkCoeffTerm(coeff, x);
132
26692
          Node query = nm->mkNode(Kind::GT, lhs, rhs);
133
26692
          Node query_mv = d_data->d_model.computeAbstractModelValue(query);
134
13346
          if (query_mv == d_data->d_true)
135
          {
136
8454
            exp = query;
137
8454
            type = Kind::GT;
138
          }
139
          else
140
          {
141
4892
            Assert(query_mv == d_data->d_false);
142
4892
            exp = nm->mkNode(Kind::LT, lhs, rhs);
143
4892
            type = Kind::LT;
144
          }
145
        }
146
        else
147
        {
148
21074
          type = negateKind(type);
149
        }
150
      }
151
      // add to status if maximal degree
152
61662
      d_ci_max[x][coeff][rhs] = d_cdb.isMaximal(atom, x);
153
61662
      if (Trace.isOn("nl-ext-bound-debug2"))
154
      {
155
        Node t = ArithMSum::mkCoeffTerm(coeff, x);
156
        Trace("nl-ext-bound-debug2") << "Add Bound: " << t << " " << type << " "
157
                                     << rhs << " by " << exp << std::endl;
158
      }
159
61662
      bool updated = true;
160
61662
      std::map<Node, Kind>::iterator its = d_ci[x][coeff].find(rhs);
161
61662
      if (its == d_ci[x][coeff].end())
162
      {
163
57821
        d_ci[x][coeff][rhs] = type;
164
57821
        d_ci_exp[x][coeff][rhs] = exp;
165
      }
166
3841
      else if (type != its->second)
167
      {
168
208
        Trace("nl-ext-bound-debug2")
169
104
            << "Joining kinds : " << type << " " << its->second << std::endl;
170
104
        Kind jk = joinKinds(type, its->second);
171
104
        if (jk == Kind::UNDEFINED_KIND)
172
        {
173
          updated = false;
174
        }
175
104
        else if (jk != its->second)
176
        {
177
79
          if (jk == type)
178
          {
179
79
            d_ci[x][coeff][rhs] = type;
180
79
            d_ci_exp[x][coeff][rhs] = exp;
181
          }
182
          else
183
          {
184
            d_ci[x][coeff][rhs] = jk;
185
            d_ci_exp[x][coeff][rhs] =
186
                nm->mkNode(Kind::AND, d_ci_exp[x][coeff][rhs], exp);
187
          }
188
        }
189
        else
190
        {
191
25
          updated = false;
192
        }
193
      }
194
61662
      if (Trace.isOn("nl-ext-bound"))
195
      {
196
        if (updated)
197
        {
198
          Trace("nl-ext-bound") << "Bound: ";
199
          debugPrintBound("nl-ext-bound", coeff, x, d_ci[x][coeff][rhs], rhs);
200
          Trace("nl-ext-bound") << " by " << d_ci_exp[x][coeff][rhs];
201
          if (d_ci_max[x][coeff][rhs])
202
          {
203
            Trace("nl-ext-bound") << ", is max degree";
204
          }
205
          Trace("nl-ext-bound") << std::endl;
206
        }
207
      }
208
      // compute if bound is not satisfied, and store what is required
209
      // for a possible refinement
210
61662
      if (options::nlExtTangentPlanes())
211
      {
212
7976
        if (is_false_lit)
213
        {
214
1778
          d_data->d_tplane_refine.insert(x);
215
        }
216
      }
217
    }
218
  }
219
  // reflexive constraints
220
1056
  Node null_coeff;
221
4876
  for (unsigned j = 0; j < d_data->d_mterms.size(); j++)
222
  {
223
8696
    Node n = d_data->d_mterms[j];
224
4348
    d_ci[n][null_coeff][n] = Kind::EQUAL;
225
4348
    d_ci_exp[n][null_coeff][n] = d_data->d_true;
226
4348
    d_ci_max[n][null_coeff][n] = false;
227
  }
228
229
528
  Trace("nl-ext") << "Get inferred bound lemmas..." << std::endl;
230
  const std::map<Node, std::vector<Node> >& cpMap =
231
528
      d_data->d_mdb.getContainsParentMap();
232
4876
  for (unsigned k = 0; k < d_data->d_mterms.size(); k++)
233
  {
234
6314
    Node x = d_data->d_mterms[k];
235
8696
    Trace("nl-ext-bound-debug")
236
4348
        << "Process bounds for " << x << " : " << std::endl;
237
4348
    std::map<Node, std::vector<Node> >::const_iterator itm = cpMap.find(x);
238
6730
    if (itm == cpMap.end())
239
    {
240
2382
      Trace("nl-ext-bound-debug") << "...has no parent monomials." << std::endl;
241
2382
      continue;
242
    }
243
3932
    Trace("nl-ext-bound-debug")
244
1966
        << "...has " << itm->second.size() << " parent monomials." << std::endl;
245
    // check derived bounds
246
    std::map<Node, std::map<Node, std::map<Node, Kind> > >::iterator itc =
247
1966
        d_ci.find(x);
248
1966
    if (itc == d_ci.end())
249
    {
250
      continue;
251
    }
252
2509
    for (std::map<Node, std::map<Node, Kind> >::iterator itcc =
253
1966
             itc->second.begin();
254
4475
         itcc != itc->second.end();
255
         ++itcc)
256
    {
257
5018
      Node coeff = itcc->first;
258
5018
      Node t = ArithMSum::mkCoeffTerm(coeff, x);
259
28106
      for (std::map<Node, Kind>::iterator itcr = itcc->second.begin();
260
28106
           itcr != itcc->second.end();
261
           ++itcr)
262
      {
263
39241
        Node rhs = itcr->first;
264
        // only consider this bound if maximal degree
265
25597
        if (!d_ci_max[x][coeff][rhs])
266
        {
267
11953
          continue;
268
        }
269
13644
        Kind type = itcr->second;
270
58916
        for (unsigned j = 0; j < itm->second.size(); j++)
271
        {
272
88252
          Node y = itm->second[j];
273
88252
          Node mult = d_data->d_mdb.getContainsDiff(x, y);
274
          // x <k> t => m*x <k'> t  where y = m*x
275
          // get the sign of mult
276
88252
          Node mmv = d_data->d_model.computeConcreteModelValue(mult);
277
90544
          Trace("nl-ext-bound-debug2")
278
45272
              << "Model value of " << mult << " is " << mmv << std::endl;
279
45430
          if (!mmv.isConst())
280
          {
281
316
            Trace("nl-ext-bound-debug")
282
158
                << "     ...coefficient " << mult
283
158
                << " is non-constant (probably transcendental)." << std::endl;
284
158
            continue;
285
          }
286
45114
          int mmv_sign = mmv.getConst<Rational>().sgn();
287
90228
          Trace("nl-ext-bound-debug2")
288
45114
              << "  sign of " << mmv << " is " << mmv_sign << std::endl;
289
47248
          if (mmv_sign == 0)
290
          {
291
4268
            Trace("nl-ext-bound-debug")
292
2134
                << "     ...coefficient " << mult << " is zero." << std::endl;
293
2134
            continue;
294
          }
295
85960
          Trace("nl-ext-bound-debug")
296
42980
              << "  from " << x << " * " << mult << " = " << y << " and " << t
297
42980
              << " " << type << " " << rhs << ", infer : " << std::endl;
298
42980
          Kind infer_type = mmv_sign == -1 ? reverseRelationKind(type) : type;
299
85960
          Node infer_lhs = nm->mkNode(Kind::MULT, mult, t);
300
85960
          Node infer_rhs = nm->mkNode(Kind::MULT, mult, rhs);
301
85960
          Node infer = nm->mkNode(infer_type, infer_lhs, infer_rhs);
302
42980
          Trace("nl-ext-bound-debug") << "     " << infer << std::endl;
303
85960
          Node infer_mv = d_data->d_model.computeAbstractModelValue(Rewriter::rewrite(infer));
304
85960
          Trace("nl-ext-bound-debug")
305
42980
              << "       ...infer model value is " << infer_mv << std::endl;
306
42980
          if (infer_mv == d_data->d_false)
307
          {
308
            Node exp = nm->mkNode(
309
                Kind::AND,
310
6654
                nm->mkNode(
311
4436
                    mmv_sign == 1 ? Kind::GT : Kind::LT, mult, d_data->d_zero),
312
8872
                d_ci_exp[x][coeff][rhs]);
313
4436
            Node iblem = nm->mkNode(Kind::IMPLIES, exp, infer);
314
4436
            Node iblem_rw = Rewriter::rewrite(iblem);
315
2218
            bool introNewTerms = hasNewMonomials(iblem_rw, d_data->d_ms);
316
4436
            Trace("nl-ext-bound-lemma")
317
2218
                << "*** Bound inference lemma : " << iblem_rw
318
2218
                << " (pre-rewrite : " << iblem << ")" << std::endl;
319
2218
            CDProof* proof = nullptr;
320
4436
            Node orig = d_ci_exp[x][coeff][rhs];
321
2218
            if (d_data->isProofEnabled())
322
            {
323
530
              proof = d_data->getProof();
324
              // this is iblem, but uses (type t rhs) instead of the original
325
              // variant (which is identical under rewriting)
326
              // we first infer the "clean" version of the lemma and then
327
              // use MACRO_SR_PRED_TRANSFORM to rewrite
328
              Node tmplem = nm->mkNode(
329
                  Kind::IMPLIES,
330
2120
                  nm->mkNode(Kind::AND,
331
1060
                             nm->mkNode(mmv_sign == 1 ? Kind::GT : Kind::LT,
332
                                        mult,
333
530
                                        d_data->d_zero),
334
1060
                             nm->mkNode(type, t, rhs)),
335
2120
                  infer);
336
2120
              proof->addStep(tmplem,
337
530
                             mmv_sign == 1 ? PfRule::ARITH_MULT_POS
338
                                           : PfRule::ARITH_MULT_NEG,
339
                             {},
340
1060
                             {mult, nm->mkNode(type, t, rhs)});
341
1590
              proof->addStep(
342
1060
                  iblem, PfRule::MACRO_SR_PRED_TRANSFORM, {tmplem}, {iblem});
343
            }
344
2218
            d_data->d_im.addPendingLemma(iblem,
345
                                         InferenceId::ARITH_NL_INFER_BOUNDS_NT,
346
                                         proof,
347
                                         introNewTerms);
348
          }
349
        }
350
      }
351
    }
352
  }
353
528
}
354
355
void MonomialBoundsCheck::checkResBounds()
356
{
357
  NodeManager* nm = NodeManager::currentNM();
358
  Trace("nl-ext") << "Get monomial resolution inferred bound lemmas..."
359
                  << std::endl;
360
  size_t nmterms = d_data->d_mterms.size();
361
  for (unsigned j = 0; j < nmterms; j++)
362
  {
363
    Node a = d_data->d_mterms[j];
364
    std::map<Node, std::map<Node, std::map<Node, Kind> > >::iterator itca =
365
        d_ci.find(a);
366
    if (itca == d_ci.end())
367
    {
368
      continue;
369
    }
370
    for (unsigned k = (j + 1); k < nmterms; k++)
371
    {
372
      Node b = d_data->d_mterms[k];
373
      std::map<Node, std::map<Node, std::map<Node, Kind> > >::iterator itcb =
374
          d_ci.find(b);
375
      if (itcb == d_ci.end())
376
      {
377
        continue;
378
      }
379
      Trace("nl-ext-rbound-debug") << "resolution inferences : compare " << a
380
                                   << " and " << b << std::endl;
381
      // if they have common factors
382
      std::map<Node, Node>::iterator ita = d_data->d_mono_diff[a].find(b);
383
      if (ita == d_data->d_mono_diff[a].end())
384
      {
385
        continue;
386
      }
387
      Trace("nl-ext-rbound") << "Get resolution inferences for [a] " << a
388
                             << " vs [b] " << b << std::endl;
389
      std::map<Node, Node>::iterator itb = d_data->d_mono_diff[b].find(a);
390
      Assert(itb != d_data->d_mono_diff[b].end());
391
      Node mv_a = d_data->d_model.computeAbstractModelValue(ita->second);
392
      Assert(mv_a.isConst());
393
      int mv_a_sgn = mv_a.getConst<Rational>().sgn();
394
      if (mv_a_sgn == 0)
395
      {
396
        // we don't compare monomials whose current model value is zero
397
        continue;
398
      }
399
      Node mv_b = d_data->d_model.computeAbstractModelValue(itb->second);
400
      Assert(mv_b.isConst());
401
      int mv_b_sgn = mv_b.getConst<Rational>().sgn();
402
      if (mv_b_sgn == 0)
403
      {
404
        // we don't compare monomials whose current model value is zero
405
        continue;
406
      }
407
      Trace("nl-ext-rbound") << "  [a] factor is " << ita->second
408
                             << ", sign in model = " << mv_a_sgn << std::endl;
409
      Trace("nl-ext-rbound") << "  [b] factor is " << itb->second
410
                             << ", sign in model = " << mv_b_sgn << std::endl;
411
412
      std::vector<Node> exp;
413
      // bounds of a
414
      for (std::map<Node, std::map<Node, Kind> >::iterator itcac =
415
               itca->second.begin();
416
           itcac != itca->second.end();
417
           ++itcac)
418
      {
419
        Node coeff_a = itcac->first;
420
        for (std::map<Node, Kind>::iterator itcar = itcac->second.begin();
421
             itcar != itcac->second.end();
422
             ++itcar)
423
        {
424
          Node rhs_a = itcar->first;
425
          Node rhs_a_res_base = nm->mkNode(Kind::MULT, itb->second, rhs_a);
426
          rhs_a_res_base = Rewriter::rewrite(rhs_a_res_base);
427
          if (hasNewMonomials(rhs_a_res_base, d_data->d_ms))
428
          {
429
            continue;
430
          }
431
          Kind type_a = itcar->second;
432
          exp.push_back(d_ci_exp[a][coeff_a][rhs_a]);
433
434
          // bounds of b
435
          for (std::map<Node, std::map<Node, Kind> >::iterator itcbc =
436
                   itcb->second.begin();
437
               itcbc != itcb->second.end();
438
               ++itcbc)
439
          {
440
            Node coeff_b = itcbc->first;
441
            Node rhs_a_res = ArithMSum::mkCoeffTerm(coeff_b, rhs_a_res_base);
442
            for (std::map<Node, Kind>::iterator itcbr = itcbc->second.begin();
443
                 itcbr != itcbc->second.end();
444
                 ++itcbr)
445
            {
446
              Node rhs_b = itcbr->first;
447
              Node rhs_b_res = nm->mkNode(Kind::MULT, ita->second, rhs_b);
448
              rhs_b_res = ArithMSum::mkCoeffTerm(coeff_a, rhs_b_res);
449
              rhs_b_res = Rewriter::rewrite(rhs_b_res);
450
              if (hasNewMonomials(rhs_b_res, d_data->d_ms))
451
              {
452
                continue;
453
              }
454
              Kind type_b = itcbr->second;
455
              exp.push_back(d_ci_exp[b][coeff_b][rhs_b]);
456
              if (Trace.isOn("nl-ext-rbound"))
457
              {
458
                Trace("nl-ext-rbound") << "* try bounds : ";
459
                debugPrintBound("nl-ext-rbound", coeff_a, a, type_a, rhs_a);
460
                Trace("nl-ext-rbound") << std::endl;
461
                Trace("nl-ext-rbound") << "               ";
462
                debugPrintBound("nl-ext-rbound", coeff_b, b, type_b, rhs_b);
463
                Trace("nl-ext-rbound") << std::endl;
464
              }
465
              Kind types[2];
466
              for (unsigned r = 0; r < 2; r++)
467
              {
468
                Node pivot_factor = r == 0 ? itb->second : ita->second;
469
                int pivot_factor_sign = r == 0 ? mv_b_sgn : mv_a_sgn;
470
                types[r] = r == 0 ? type_a : type_b;
471
                if (pivot_factor_sign == (r == 0 ? 1 : -1))
472
                {
473
                  types[r] = reverseRelationKind(types[r]);
474
                }
475
                if (pivot_factor_sign == 1)
476
                {
477
                  exp.push_back(
478
                      nm->mkNode(Kind::GT, pivot_factor, d_data->d_zero));
479
                }
480
                else
481
                {
482
                  exp.push_back(
483
                      nm->mkNode(Kind::LT, pivot_factor, d_data->d_zero));
484
                }
485
              }
486
              Kind jk = transKinds(types[0], types[1]);
487
              Trace("nl-ext-rbound-debug")
488
                  << "trans kind : " << types[0] << " + " << types[1] << " = "
489
                  << jk << std::endl;
490
              if (jk != Kind::UNDEFINED_KIND)
491
              {
492
                Node conc = nm->mkNode(jk, rhs_a_res, rhs_b_res);
493
                Node conc_mv = d_data->d_model.computeAbstractModelValue(conc);
494
                if (conc_mv == d_data->d_false)
495
                {
496
                  Node rblem = nm->mkNode(Kind::IMPLIES, nm->mkAnd(exp), conc);
497
                  Trace("nl-ext-rbound-lemma-debug")
498
                      << "Resolution bound lemma "
499
                         "(pre-rewrite) "
500
                         ": "
501
                      << rblem << std::endl;
502
                  rblem = Rewriter::rewrite(rblem);
503
                  Trace("nl-ext-rbound-lemma")
504
                      << "Resolution bound lemma : " << rblem << std::endl;
505
                  d_data->d_im.addPendingLemma(
506
                      rblem, InferenceId::ARITH_NL_RES_INFER_BOUNDS);
507
                }
508
              }
509
              exp.pop_back();
510
              exp.pop_back();
511
              exp.pop_back();
512
            }
513
          }
514
          exp.pop_back();
515
        }
516
      }
517
    }
518
  }
519
}
520
521
}  // namespace nl
522
}  // namespace arith
523
}  // namespace theory
524
28191
}  // namespace cvc5