forked from stanford-centaur/pono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_searcher.cpp
209 lines (176 loc) · 6.21 KB
/
local_searcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "engines/local_searcher.h"
#include "engines/options_handler.h"
#include "utils/logger.h"
#include "smt-switch/smtlib_reader.h"
#include "smt/available_solvers.h"
#include <chrono>
#include <filesystem>
#include <cmath>
using namespace smt;
namespace pono {
LocalSearcher::LocalSearcher(const PonoOptions &opt)
: opt_(opt)
{
initializeConfig();
}
LocalSearcher::~LocalSearcher(){
for (auto &option : options_)
delete option;
options_.clear();
}
void LocalSearcher::initializeConfig() {
OptionsHandler::loadOptions(opt_.config_input_, options_);
sumOfWeights_ = 0;
for (auto &option : options_) {
currentConfig_[option] = option->default_value_;
if (option->weight_)
sumOfWeights_ += std::pow(WEIGHT_BASE_, option->weight_);
}
logger.log(2, "Config initialized");
dumpCurrentConfig();
}
void LocalSearcher::localSearch(std::string filename, unsigned iterations) {
logger.log(1, "Optimizing local search for {} iterations (repeat: {})", iterations, opt_.solver_runs_);
double currentCost = evaluateCurrentConfig(filename, 3);
double lastAcceptedCost = currentCost;
acceptCurrentConfig();
bestConfig_ = currentConfig_; bestIter_ = 0; bestCost_ = currentCost;
initialCost_ = currentCost;
printStatus(0, currentCost);
unsigned printEvery = 5;
// MCMC Sampling
for (unsigned iter = 1; iter <= iterations; ++iter) {
proposeConfigUpdate();
currentCost = evaluateCurrentConfig(filename, opt_.solver_runs_);
if (decideToAcceptCurrentProposal(currentCost, lastAcceptedCost)) {
lastAcceptedCost = currentCost;
acceptCurrentConfig();
if (currentCost < bestCost_) {
bestConfig_ = currentConfig_; bestIter_ = iter; bestCost_ = currentCost;
}
}
if (iter % printEvery == 0)
printStatus(iter, currentCost);
}
}
void LocalSearcher::writeBestConfig(std::string filename) {
logger.log(1, "Writing best configuration to {}", filename);
logger.log(1, "\tIteration: {}, initial cost: {}, best cost: {}",
bestIter_, initialCost_, bestCost_);
std::vector<SolverOption *> options;
for (const auto &option : options_) {
options.push_back(new SolverOption
(option->option_name_,
lastAcceptedConfig_[option],
option->values_,
option->weight_));
}
OptionsHandler::writeOptions(filename, options);
for (auto &option : options)
delete option;
}
void LocalSearcher::proposeConfigUpdate() {
logger.log(2, "Proposing configuration update");
currentConfig_ = lastAcceptedConfig_;
// Select an option
int rnd = (int) rand() % sumOfWeights_;
SolverOption *option = nullptr;
for (auto &o : options_) {
if (o->weight_) {
int v = std::pow(WEIGHT_BASE_, o->weight_);
if (rnd < v) {
option = o;
break;
}
rnd -= v;
}
}
if (option->weight_ == 0) {
logger.log(0, "Cannot select option with zero weights");
exit(0);
}
// Select a value
std::string val1 = currentConfig_[option];
std::string val2 = option->alternative(val1);
currentConfig_[option] = val2;
logger.log(2, "Proposed update for {}: {} -> {} ",
option->option_name_, val1, val2);
}
double LocalSearcher::evaluateCurrentConfig(std::string filename, unsigned solverRuns) {
logger.log(2, "Evaluating configuration...");
std::string configStr = currentConfigtoString();
if (configToCost_.count(configStr)) {
double score = configToCost_[configStr];
logger.log(2, "Configuration cached, cost: {}", score);
return score;
} else {
unsigned long long sum = 0;
unsigned long long runtime;
for (unsigned i = 0; i < solverRuns; ++i) {
SmtSolver s = create_solver_for(opt_.smt_solver_,
opt_.engine_,
false,
opt_.ceg_prophecy_arrays_);
loadConfig(s, currentConfig_);
SmtLibReader reader(s);
auto begin = std::chrono::high_resolution_clock::now();
reader.parse(opt_.filename_);
auto end = std::chrono::high_resolution_clock::now();
runtime = std::chrono::duration_cast<std::chrono::milliseconds>(end-begin).count();
logger.log(2, "\tAttempt {}: cost {}", i, runtime);
sum += runtime;
}
sum /= solverRuns;
configToCost_[configStr] = sum;
logger.log(2, "Evaluating configuration - done, cost: {}", sum);
return sum;
}
}
void LocalSearcher::loadConfig(smt::SmtSolver &solver,
const std::unordered_map<SolverOption *, std::string> &config) const {
for (const auto &pair : config) {
solver->set_opt(pair.first->option_name_, pair.second);
}
}
bool LocalSearcher::decideToAcceptCurrentProposal(double currentCost,
double lastAcceptedCost) const {
double mcmcBeta = 1;
logger.log(2, "Current cost: {}, last accepted cost: {}",
currentCost, lastAcceptedCost);
if (currentCost < lastAcceptedCost) {
logger.log(2, "\tPobability to accept: {}", 1);
return true;
} else {
// The smaller the difference between the proposed phase pattern and the
// current phase pattern, the more likely to accept the proposal.
double prob = std::exp(- currentCost / lastAcceptedCost * mcmcBeta);
logger.log(2, "\tPobability to accept: {}", prob);
return ((double)rand() / RAND_MAX) < prob;
}
}
void LocalSearcher::acceptCurrentConfig() {
lastAcceptedConfig_ = currentConfig_;
logger.log(2, "\tCurrent configuration accepted");
}
void LocalSearcher::dumpCurrentConfig() const {
logger.log(0, "Dumping current configuration:");
for (auto &pair : currentConfig_) {
std::string s;
pair.first->dump(s);
logger.log(0, s);
logger.log(0, " current value: {}", pair.second);
}
}
std::string LocalSearcher::currentConfigtoString() const {
std::string s = "";
for (auto &pair : currentConfig_) {
s += pair.second + ",";
}
return s;
}
void LocalSearcher::printStatus(unsigned iter,
double currentCost) const {
logger.log(1, "---- Iteration: {}, current cost: {}, best cost: {} ----\n",
iter, currentCost, bestCost_);
}
} // namespace pono