4ccd29f1f1af3123213e7e70a92ea916e5c7543d
[satune.git] / src / Tuner / multituner.cc
1 #include "multituner.h"
2 #include "csolver.h"
3 #include "searchtuner.h"
4 #include <math.h>
5 #include <stdlib.h>
6 #include <float.h>
7 #include <string.h>
8 #include <iostream>
9 #include <fstream>
10
11 #define UNSETVALUE -1
12
13 Problem::Problem(const char *_problem) : result(UNSETVALUE) {
14         uint len = strlen(_problem);
15         problem = (char *) ourmalloc(len + 1);
16         memcpy(problem, _problem, len + 1);
17 }
18
19 Problem::~Problem() {
20         ourfree(problem);
21 }
22
23 void TunerRecord::setTime(Problem *problem, long long time) {
24         timetaken.put(problem, time);
25 }
26
27 long long TunerRecord::getTime(Problem *problem) {
28         if (timetaken.contains(problem))
29                 return timetaken.get(problem);
30         else return -1;
31 }
32
33 TunerRecord *TunerRecord::changeTuner(SearchTuner *_newtuner) {
34         TunerRecord *tr = new TunerRecord(_newtuner);
35         for (uint i = 0; i < problems.getSize(); i++) {
36                 tr->problems.push(problems.get(i));
37         }
38         return tr;
39 }
40
41 MultiTuner::MultiTuner(uint _budget, uint _rounds, uint _timeout) :
42         budget(_budget), rounds(_rounds), timeout(_timeout), execnum(0) {
43 }
44
45 MultiTuner::~MultiTuner() {
46         for (uint i = 0; i < problems.getSize(); i++)
47                 ourfree(problems.get(i));
48         for (uint i = 0; i < allTuners.getSize(); i++)
49                 delete allTuners.get(i);
50 }
51
52 void MultiTuner::addProblem(const char *filename) {
53         problems.push(new Problem(filename));
54 }
55
56 void MultiTuner::addTuner(SearchTuner *tuner) {
57         TunerRecord *t = new TunerRecord(tuner);
58         tuners.push(t);
59         allTuners.push(t);
60 }
61
62 long long MultiTuner::evaluate(Problem *problem, SearchTuner *tuner) {
63         char buffer[512];
64         //Write out the tuner
65         snprintf(buffer, sizeof(buffer), "tuner%u", execnum);
66         tuner->serialize(buffer);
67
68         //Do run
69         snprintf(buffer, sizeof(buffer), "deserializerun %s %u tuner%u result%s%u > log%u", problem->getProblem(), timeout, execnum, problem->getProblem(), execnum, execnum);
70         int status = system(buffer);
71
72         long long metric = -1;
73         int sat = IS_INDETER;
74         if (status == 0) {
75                 //Read data in from results file
76                 snprintf(buffer, sizeof(buffer), "result%s%u", problem->getProblem(), execnum);
77
78                 ifstream myfile;
79                 myfile.open (buffer, ios::in);
80
81
82                 if (myfile.is_open()) {
83                         myfile >> metric;
84                         myfile >> sat;
85                         myfile.close();
86                 }
87         }
88         //Increment execution count
89         execnum++;
90
91         if (problem->result == UNSETVALUE && sat != IS_INDETER) {
92                 problem->result = sat;
93         } else if (problem->result != sat && sat != IS_INDETER) {
94                 model_print("******** Result has changed ********\n");
95         }
96         return metric;
97 }
98
99 void MultiTuner::tuneComp() {
100         Vector<TunerRecord *> *tunerV = new Vector<TunerRecord *>(&tuners);
101         for (uint b = 0; b < budget; b++) {
102
103                 uint tSize = tunerV->getSize();
104                 for (uint i = 0; i < tSize; i++) {
105                         SearchTuner *tmpTuner = mutateTuner(tunerV->get(i)->getTuner(), b);
106                         tunerV->push(new TunerRecord(tmpTuner));
107                 }
108
109                 Hashtable<TunerRecord *, int, uint64_t> scores;
110                 for (uint i = 0; i < problems.getSize(); i++) {
111                         Problem *problem = problems.get(i);
112                         Vector<TunerRecord *> places;
113                         for (uint j = 0; j < tunerV->getSize(); j++) {
114                                 TunerRecord *tuner = tunerV->get(j);
115                                 long long metric = tuner->getTime(problem);
116                                 if (metric == -1) {
117                                         metric = evaluate(problem, tuner->getTuner());
118                                         if (metric != -1)
119                                                 tuner->setTime(problem, metric);
120                                 }
121                                 if (metric != -1) {
122                                         uint k = 0;
123                                         for (; k < places.getSize(); k++) {
124                                                 if (metric < places.get(k)->getTime(problem))
125                                                         break;
126                                         }
127                                         places.insertAt(k, tuner);
128                                 }
129                         }
130                         int points = 4;
131                         for (uint k = 0; k < places.getSize() && points; k++) {
132                                 TunerRecord *tuner = places.get(k);
133                                 points = points / 2;
134                                 int currScore = 0;
135                                 if (scores.contains(tuner))
136                                         currScore = scores.get(tuner);
137                                 currScore += points;
138                                 scores.put(tuner, currScore);
139                         }
140                 }
141                 Vector<TunerRecord *> ranking;
142                 for (uint i = 0; i < tunerV->getSize(); i++) {
143                         TunerRecord *tuner = tunerV->get(i);
144                         int score = 0;
145                         if (scores.contains(tuner))
146                                 score = scores.get(tuner);
147                         uint j = 0;
148                         for (; j < ranking.getSize(); j++) {
149                                 TunerRecord *t = ranking.get(j);
150                                 int tscore = 0;
151                                 if (scores.contains(t))
152                                         tscore = scores.get(t);
153                                 if (score > tscore)
154                                         break;
155                         }
156                         ranking.insertAt(j, tuner);
157                 }
158         }
159 }
160
161 void MultiTuner::mapProblemsToTuners(Vector<TunerRecord *> *tunerV) {
162         for (uint i = 0; i < problems.getSize(); i++) {
163                 Problem *problem = problems.get(i);
164                 TunerRecord *bestTuner = NULL;
165                 long long bestscore = 0;
166                 for (uint j = 0; j < tunerV->getSize(); j++) {
167                         TunerRecord *tuner = tunerV->get(j);
168                         long long metric = tuner->getTime(problem);
169                         if (metric == -1) {
170                                 metric = evaluate(problem, tuner->getTuner());
171                                 if (metric != -1)
172                                         tuner->setTime(problem, metric);
173                         }
174                         if ((bestTuner == NULL && metric != -1) ||
175                                         (metric < bestscore && metric != -1)) {
176                                 bestTuner = tuner;
177                                 bestscore = metric;
178                         }
179                 }
180                 if (bestTuner != NULL)
181                         bestTuner->problems.push(problem);
182         }
183 }
184
185 void clearVector(Vector<TunerRecord *> *tunerV) {
186         for (uint j = 0; j < tunerV->getSize(); j++) {
187                 TunerRecord *tuner = tunerV->get(j);
188                 tuner->problems.clear();
189         }
190 }
191
192 void MultiTuner::tuneK() {
193         Vector<TunerRecord *> *tunerV = new Vector<TunerRecord *>(&tuners);
194         for (uint i = 0; i < rounds; i++) {
195                 clearVector(tunerV);
196                 mapProblemsToTuners(tunerV);
197                 improveTuners(tunerV);
198         }
199         model_print("Best tuners\n");
200         for (uint j = 0; j < tunerV->getSize(); j++) {
201                 TunerRecord *tuner = tunerV->get(j);
202                 char buffer[256];
203                 sprintf(buffer, "tuner%u.conf", j);
204                 tuner->getTuner()->serialize(buffer);
205                 tuner->getTuner()->print();
206         }
207         delete tunerV;
208 }
209
210 void MultiTuner::improveTuners(Vector<TunerRecord *> *tunerV) {
211         for (uint j = 0; j < tunerV->getSize(); j++) {
212                 TunerRecord *tuner = tunerV->get(j);
213                 TunerRecord *newtuner = tune(tuner);
214                 tunerV->set(j, newtuner);
215         }
216 }
217
218 double MultiTuner::evaluateAll(TunerRecord *tuner) {
219         double product = 1;
220         for (uint i = 0; i < tuner->problems.getSize(); i++) {
221                 Problem *problem = tuner->problems.get(i);
222                 long long metric = tuner->getTime(problem);
223                 if (metric == -1) {
224                         metric = evaluate(problem, tuner->getTuner());
225                         if (metric != -1)
226                                 tuner->setTime(problem, metric);
227                 }
228
229                 double score = metric;
230                 product *= score;
231         }
232         return pow(product, 1 / ((double)tuner->problems.getSize()));
233 }
234
235 SearchTuner *MultiTuner::mutateTuner(SearchTuner *oldTuner, uint k) {
236         SearchTuner *newTuner = oldTuner->copyUsed();
237         uint numSettings = oldTuner->getSize();
238         uint settingsToMutate = (uint)(AUTOTUNERFACTOR * (((double)numSettings) * (budget - k)) / (budget));
239         if (settingsToMutate < 1)
240                 settingsToMutate = 1;
241         model_print("Mutating %u settings\n", settingsToMutate);
242         while (settingsToMutate-- != 0) {
243                 newTuner->randomMutate();
244         }
245         return newTuner;
246 }
247
248 TunerRecord *MultiTuner::tune(TunerRecord *tuner) {
249         TunerRecord *bestTuner = NULL;
250         double bestScore = DBL_MAX;
251
252         TunerRecord *oldTuner = tuner;
253         double base_temperature = evaluateAll(oldTuner);
254         double oldScore = base_temperature;
255
256         for (uint i = 0; i < budget; i++) {
257                 SearchTuner *tmpTuner = mutateTuner(oldTuner->getTuner(), i);
258                 TunerRecord *newTuner = oldTuner->changeTuner(tmpTuner);
259                 allTuners.push(newTuner);
260                 double newScore = evaluateAll(newTuner);
261                 newTuner->tuner->printUsed();
262                 model_print("Received score %f\n", newScore);
263                 double scoreDiff = newScore - oldScore; //smaller is better
264                 if (newScore < bestScore) {
265                         bestScore = newScore;
266                         bestTuner = newTuner;
267                 }
268
269                 double acceptanceP;
270                 if (scoreDiff < 0) {
271                         acceptanceP = 1;
272                 } else {
273                         double currTemp = base_temperature * (((double)budget - i) / budget);
274                         acceptanceP = exp(-scoreDiff / currTemp);
275                 }
276                 double ran = ((double)random()) / RAND_MAX;
277                 if (ran <= acceptanceP) {
278                         oldScore = newScore;
279                         oldTuner = newTuner;
280                 }
281         }
282
283         return bestTuner;
284 }
285