GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/sygus_unif.cpp Lines: 53 58 91.4 %
Date: 2021-11-07 Branches: 50 140 35.7 %

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
1984
SygusUnif::SygusUnif(Env& env)
31
1984
    : EnvObj(env), d_tds(nullptr), d_enableMinimality(false)
32
{
33
1984
}
34
35
1980
SygusUnif::~SygusUnif() {}
36
37
107
void SygusUnif::initializeCandidate(
38
    TermDbSygus* tds,
39
    Node f,
40
    std::vector<Node>& enums,
41
    std::map<Node, std::vector<Node>>& strategy_lemmas)
42
{
43
107
  d_tds = tds;
44
107
  d_candidates.push_back(f);
45
  // initialize the strategy
46
107
  d_strategy.emplace(f, SygusUnifStrategy(d_env));
47
107
  d_strategy.at(f).initialize(tds, f, enums);
48
107
}
49
50
425
Node SygusUnif::getMinimalTerm(const std::vector<Node>& terms)
51
{
52
425
  unsigned minSize = 0;
53
425
  Node minTerm;
54
425
  std::map<Node, unsigned>::iterator it;
55
1073
  for (const Node& n : terms)
56
  {
57
648
    it = d_termToSize.find(n);
58
648
    unsigned ssize = 0;
59
648
    if (it == d_termToSize.end())
60
    {
61
223
      ssize = datatypes::utils::getSygusTermSize(n);
62
223
      d_termToSize[n] = ssize;
63
    }
64
    else
65
    {
66
425
      ssize = it->second;
67
    }
68
648
    if (minTerm.isNull() || ssize < minSize)
69
    {
70
494
      minTerm = n;
71
494
      minSize = ssize;
72
    }
73
  }
74
425
  return minTerm;
75
}
76
77
389
Node SygusUnif::constructBestSolvedTerm(Node e, const std::vector<Node>& solved)
78
{
79
389
  Assert(!solved.empty());
80
389
  if (d_enableMinimality)
81
  {
82
184
    return getMinimalTerm(solved);
83
  }
84
205
  return solved[0];
85
}
86
87
597
Node SygusUnif::constructBestConditional(Node ce,
88
                                         const std::vector<Node>& conds)
89
{
90
597
  Assert(!conds.empty());
91
597
  double r = Random::getRandom().pickDouble(0.0, 1.0);
92
597
  unsigned cindex = r * conds.size();
93
597
  if (cindex > conds.size())
94
  {
95
152
    cindex = conds.size() - 1;
96
  }
97
597
  return conds[cindex];
98
}
99
100
2082
Node SygusUnif::constructBestStringToConcat(
101
    const std::vector<Node>& strs,
102
    const std::map<Node, size_t>& total_inc,
103
    const std::map<Node, std::vector<size_t>>& incr)
104
{
105
2082
  Assert(!strs.empty());
106
4164
  std::vector<Node> strs_tmp = strs;
107
2082
  std::shuffle(strs_tmp.begin(), strs_tmp.end(), Random::getRandom());
108
  // prefer one that has incremented by more than 0
109
2256
  for (const Node& ns : strs_tmp)
110
  {
111
2196
    const std::map<Node, size_t>::const_iterator iti = total_inc.find(ns);
112
2196
    if (iti != total_inc.end() && iti->second > 0)
113
    {
114
2022
      return ns;
115
    }
116
  }
117
60
  return strs_tmp[0];
118
}
119
120
45762
void SygusUnif::indent(const char* c, int ind)
121
{
122
45762
  if (Trace.isOn(c))
123
  {
124
    for (int i = 0; i < ind; i++)
125
    {
126
      Trace(c) << "  ";
127
    }
128
  }
129
45762
}
130
131
145
void SygusUnif::print_val(const char* c, std::vector<Node>& vals, bool pol)
132
{
133
145
  if (Trace.isOn(c))
134
  {
135
    for (unsigned i = 0; i < vals.size(); i++)
136
    {
137
      Trace(c) << ((pol ? vals[i].getConst<bool>() : !vals[i].getConst<bool>())
138
                       ? "1"
139
                       : "0");
140
    }
141
  }
142
145
}
143
144
}  // namespace quantifiers
145
}  // namespace theory
146
31137
}  // namespace cvc5