GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/sygus_unif.cpp Lines: 50 55 90.9 %
Date: 2021-05-22 Branches: 49 140 35.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Aina Niemetz, Haniel Barbosa
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
 * Implementation of sygus_unif.
14
 */
15
16
#include "theory/quantifiers/sygus/sygus_unif.h"
17
18
#include "theory/quantifiers/sygus/term_database_sygus.h"
19
#include "theory/quantifiers/term_util.h"
20
#include "util/random.h"
21
22
using namespace std;
23
using namespace cvc5::kind;
24
25
namespace cvc5 {
26
namespace theory {
27
namespace quantifiers {
28
29
1576
SygusUnif::SygusUnif() : d_tds(nullptr), d_enableMinimality(false) {}
30
1576
SygusUnif::~SygusUnif() {}
31
32
59
void SygusUnif::initializeCandidate(
33
    TermDbSygus* tds,
34
    Node f,
35
    std::vector<Node>& enums,
36
    std::map<Node, std::vector<Node>>& strategy_lemmas)
37
{
38
59
  d_tds = tds;
39
59
  d_candidates.push_back(f);
40
  // initialize the strategy
41
59
  d_strategy[f].initialize(tds, f, enums);
42
59
}
43
44
247
Node SygusUnif::getMinimalTerm(const std::vector<Node>& terms)
45
{
46
247
  unsigned minSize = 0;
47
247
  Node minTerm;
48
247
  std::map<Node, unsigned>::iterator it;
49
623
  for (const Node& n : terms)
50
  {
51
376
    it = d_termToSize.find(n);
52
376
    unsigned ssize = 0;
53
376
    if (it == d_termToSize.end())
54
    {
55
131
      ssize = d_tds->getSygusTermSize(n);
56
131
      d_termToSize[n] = ssize;
57
    }
58
    else
59
    {
60
245
      ssize = it->second;
61
    }
62
376
    if (minTerm.isNull() || ssize < minSize)
63
    {
64
285
      minTerm = n;
65
285
      minSize = ssize;
66
    }
67
  }
68
247
  return minTerm;
69
}
70
71
226
Node SygusUnif::constructBestSolvedTerm(Node e, const std::vector<Node>& solved)
72
{
73
226
  Assert(!solved.empty());
74
226
  if (d_enableMinimality)
75
  {
76
105
    return getMinimalTerm(solved);
77
  }
78
121
  return solved[0];
79
}
80
81
352
Node SygusUnif::constructBestConditional(Node ce,
82
                                         const std::vector<Node>& conds)
83
{
84
352
  Assert(!conds.empty());
85
352
  double r = Random::getRandom().pickDouble(0.0, 1.0);
86
352
  unsigned cindex = r * conds.size();
87
352
  if (cindex > conds.size())
88
  {
89
88
    cindex = conds.size() - 1;
90
  }
91
352
  return conds[cindex];
92
}
93
94
1096
Node SygusUnif::constructBestStringToConcat(
95
    const std::vector<Node>& strs,
96
    const std::map<Node, size_t>& total_inc,
97
    const std::map<Node, std::vector<size_t>>& incr)
98
{
99
1096
  Assert(!strs.empty());
100
2192
  std::vector<Node> strs_tmp = strs;
101
1096
  std::shuffle(strs_tmp.begin(), strs_tmp.end(), Random::getRandom());
102
  // prefer one that has incremented by more than 0
103
1185
  for (const Node& ns : strs_tmp)
104
  {
105
1156
    const std::map<Node, size_t>::const_iterator iti = total_inc.find(ns);
106
1156
    if (iti != total_inc.end() && iti->second > 0)
107
    {
108
1067
      return ns;
109
    }
110
  }
111
29
  return strs_tmp[0];
112
}
113
114
24165
void SygusUnif::indent(const char* c, int ind)
115
{
116
24165
  if (Trace.isOn(c))
117
  {
118
    for (int i = 0; i < ind; i++)
119
    {
120
      Trace(c) << "  ";
121
    }
122
  }
123
24165
}
124
125
77
void SygusUnif::print_val(const char* c, std::vector<Node>& vals, bool pol)
126
{
127
77
  if (Trace.isOn(c))
128
  {
129
    for (unsigned i = 0; i < vals.size(); i++)
130
    {
131
      Trace(c) << ((pol ? vals[i].getConst<bool>() : !vals[i].getConst<bool>())
132
                       ? "1"
133
                       : "0");
134
    }
135
  }
136
77
}
137
138
}  // namespace quantifiers
139
}  // namespace theory
140
28194
}  // namespace cvc5