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