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 |
1277 |
SygusUnif::SygusUnif(Env& env) |
31 |
1277 |
: EnvObj(env), d_tds(nullptr), d_enableMinimality(false) |
32 |
|
{ |
33 |
1277 |
} |
34 |
|
|
35 |
1275 |
SygusUnif::~SygusUnif() {} |
36 |
|
|
37 |
61 |
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 |
61 |
d_tds = tds; |
44 |
61 |
d_candidates.push_back(f); |
45 |
|
// initialize the strategy |
46 |
61 |
d_strategy.emplace(f, SygusUnifStrategy(d_env)); |
47 |
61 |
d_strategy.at(f).initialize(tds, f, enums); |
48 |
61 |
} |
49 |
|
|
50 |
240 |
Node SygusUnif::getMinimalTerm(const std::vector<Node>& terms) |
51 |
|
{ |
52 |
240 |
unsigned minSize = 0; |
53 |
240 |
Node minTerm; |
54 |
240 |
std::map<Node, unsigned>::iterator it; |
55 |
604 |
for (const Node& n : terms) |
56 |
|
{ |
57 |
364 |
it = d_termToSize.find(n); |
58 |
364 |
unsigned ssize = 0; |
59 |
364 |
if (it == d_termToSize.end()) |
60 |
|
{ |
61 |
128 |
ssize = datatypes::utils::getSygusTermSize(n); |
62 |
128 |
d_termToSize[n] = ssize; |
63 |
|
} |
64 |
|
else |
65 |
|
{ |
66 |
236 |
ssize = it->second; |
67 |
|
} |
68 |
364 |
if (minTerm.isNull() || ssize < minSize) |
69 |
|
{ |
70 |
276 |
minTerm = n; |
71 |
276 |
minSize = ssize; |
72 |
|
} |
73 |
|
} |
74 |
240 |
return minTerm; |
75 |
|
} |
76 |
|
|
77 |
226 |
Node SygusUnif::constructBestSolvedTerm(Node e, const std::vector<Node>& solved) |
78 |
|
{ |
79 |
226 |
Assert(!solved.empty()); |
80 |
226 |
if (d_enableMinimality) |
81 |
|
{ |
82 |
103 |
return getMinimalTerm(solved); |
83 |
|
} |
84 |
123 |
return solved[0]; |
85 |
|
} |
86 |
|
|
87 |
358 |
Node SygusUnif::constructBestConditional(Node ce, |
88 |
|
const std::vector<Node>& conds) |
89 |
|
{ |
90 |
358 |
Assert(!conds.empty()); |
91 |
358 |
double r = Random::getRandom().pickDouble(0.0, 1.0); |
92 |
358 |
unsigned cindex = r * conds.size(); |
93 |
358 |
if (cindex > conds.size()) |
94 |
|
{ |
95 |
89 |
cindex = conds.size() - 1; |
96 |
|
} |
97 |
358 |
return conds[cindex]; |
98 |
|
} |
99 |
|
|
100 |
1097 |
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 |
1097 |
Assert(!strs.empty()); |
106 |
2194 |
std::vector<Node> strs_tmp = strs; |
107 |
1097 |
std::shuffle(strs_tmp.begin(), strs_tmp.end(), Random::getRandom()); |
108 |
|
// prefer one that has incremented by more than 0 |
109 |
1187 |
for (const Node& ns : strs_tmp) |
110 |
|
{ |
111 |
1157 |
const std::map<Node, size_t>::const_iterator iti = total_inc.find(ns); |
112 |
1157 |
if (iti != total_inc.end() && iti->second > 0) |
113 |
|
{ |
114 |
1067 |
return ns; |
115 |
|
} |
116 |
|
} |
117 |
30 |
return strs_tmp[0]; |
118 |
|
} |
119 |
|
|
120 |
24255 |
void SygusUnif::indent(const char* c, int ind) |
121 |
|
{ |
122 |
24255 |
if (Trace.isOn(c)) |
123 |
|
{ |
124 |
|
for (int i = 0; i < ind; i++) |
125 |
|
{ |
126 |
|
Trace(c) << " "; |
127 |
|
} |
128 |
|
} |
129 |
24255 |
} |
130 |
|
|
131 |
76 |
void SygusUnif::print_val(const char* c, std::vector<Node>& vals, bool pol) |
132 |
|
{ |
133 |
76 |
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 |
76 |
} |
143 |
|
|
144 |
|
} // namespace quantifiers |
145 |
|
} // namespace theory |
146 |
29505 |
} // namespace cvc5 |