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 |
1237 |
SygusUnif::SygusUnif() : d_tds(nullptr), d_enableMinimality(false) {} |
31 |
1235 |
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 |
243 |
Node SygusUnif::getMinimalTerm(const std::vector<Node>& terms) |
46 |
|
{ |
47 |
243 |
unsigned minSize = 0; |
48 |
243 |
Node minTerm; |
49 |
243 |
std::map<Node, unsigned>::iterator it; |
50 |
612 |
for (const Node& n : terms) |
51 |
|
{ |
52 |
369 |
it = d_termToSize.find(n); |
53 |
369 |
unsigned ssize = 0; |
54 |
369 |
if (it == d_termToSize.end()) |
55 |
|
{ |
56 |
129 |
ssize = datatypes::utils::getSygusTermSize(n); |
57 |
129 |
d_termToSize[n] = ssize; |
58 |
|
} |
59 |
|
else |
60 |
|
{ |
61 |
240 |
ssize = it->second; |
62 |
|
} |
63 |
369 |
if (minTerm.isNull() || ssize < minSize) |
64 |
|
{ |
65 |
279 |
minTerm = n; |
66 |
279 |
minSize = ssize; |
67 |
|
} |
68 |
|
} |
69 |
243 |
return minTerm; |
70 |
|
} |
71 |
|
|
72 |
229 |
Node SygusUnif::constructBestSolvedTerm(Node e, const std::vector<Node>& solved) |
73 |
|
{ |
74 |
229 |
Assert(!solved.empty()); |
75 |
229 |
if (d_enableMinimality) |
76 |
|
{ |
77 |
104 |
return getMinimalTerm(solved); |
78 |
|
} |
79 |
125 |
return solved[0]; |
80 |
|
} |
81 |
|
|
82 |
361 |
Node SygusUnif::constructBestConditional(Node ce, |
83 |
|
const std::vector<Node>& conds) |
84 |
|
{ |
85 |
361 |
Assert(!conds.empty()); |
86 |
361 |
double r = Random::getRandom().pickDouble(0.0, 1.0); |
87 |
361 |
unsigned cindex = r * conds.size(); |
88 |
361 |
if (cindex > conds.size()) |
89 |
|
{ |
90 |
89 |
cindex = conds.size() - 1; |
91 |
|
} |
92 |
361 |
return conds[cindex]; |
93 |
|
} |
94 |
|
|
95 |
1097 |
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 |
1097 |
Assert(!strs.empty()); |
101 |
2194 |
std::vector<Node> strs_tmp = strs; |
102 |
1097 |
std::shuffle(strs_tmp.begin(), strs_tmp.end(), Random::getRandom()); |
103 |
|
// prefer one that has incremented by more than 0 |
104 |
1187 |
for (const Node& ns : strs_tmp) |
105 |
|
{ |
106 |
1157 |
const std::map<Node, size_t>::const_iterator iti = total_inc.find(ns); |
107 |
1157 |
if (iti != total_inc.end() && iti->second > 0) |
108 |
|
{ |
109 |
1067 |
return ns; |
110 |
|
} |
111 |
|
} |
112 |
30 |
return strs_tmp[0]; |
113 |
|
} |
114 |
|
|
115 |
24456 |
void SygusUnif::indent(const char* c, int ind) |
116 |
|
{ |
117 |
24456 |
if (Trace.isOn(c)) |
118 |
|
{ |
119 |
|
for (int i = 0; i < ind; i++) |
120 |
|
{ |
121 |
|
Trace(c) << " "; |
122 |
|
} |
123 |
|
} |
124 |
24456 |
} |
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 |
29505 |
} // namespace cvc5 |