fbf66c7f1b9f9b462d6d1e559a98e3239b963b5e
[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), "./run.sh 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                 uint tSize = tunerV->getSize();
103                 for (uint i = 0; i < tSize; i++) {
104                         SearchTuner *tmpTuner = mutateTuner(tunerV->get(i)->getTuner(), b);
105                         tunerV->push(new TunerRecord(tmpTuner));
106                 }
107
108                 Hashtable<TunerRecord *, int, uint64_t> scores;
109                 for (uint i = 0; i < problems.getSize(); i++) {
110                         Problem *problem = problems.get(i);
111                         Vector<TunerRecord *> places;
112                         for (uint j = 0; j < tunerV->getSize(); j++) {
113                                 TunerRecord *tuner = tunerV->get(j);
114                                 long long metric = tuner->getTime(problem);
115                                 if (metric == -1) {
116                                         metric = evaluate(problem, tuner->getTuner());
117                                         if (metric != -1)
118                                                 tuner->setTime(problem, metric);
119                                 }
120                                 if (metric != -1) {
121                                         uint k = 0;
122                                         for (; k < places.getSize(); k++) {
123                                                 if (metric < places.get(k)->getTime(problem))
124                                                         break;
125                                         }
126                                         places.insertAt(k, tuner);
127                                 }
128                         }
129                         int points = 9;
130                         for (uint k = 0; k < places.getSize() && points; k++) {
131                                 TunerRecord *tuner = places.get(k);
132                                 points = points / 3;
133                                 int currScore = 0;
134                                 if (scores.contains(tuner))
135                                         currScore = scores.get(tuner);
136                                 currScore += points;
137                                 scores.put(tuner, currScore);
138                         }
139                 }
140                 Vector<TunerRecord *> ranking;
141                 for (uint i = 0; i < tunerV->getSize(); i++) {
142                         TunerRecord *tuner = tunerV->get(i);
143                         int score = 0;
144                         if (scores.contains(tuner))
145                                 score = scores.get(tuner);
146                         uint j = 0;
147                         for (; j < ranking.getSize(); j++) {
148                                 TunerRecord *t = ranking.get(j);
149                                 int tscore = 0;
150                                 if (scores.contains(t))
151                                         tscore = scores.get(t);
152                                 if (score > tscore)
153                                         break;
154                         }
155                         ranking.insertAt(j, tuner);
156                 }
157                 for (uint i = tuners.getSize(); i < ranking.getSize(); i++) {
158                         TunerRecord *tuner = ranking.get(i);
159                         for (uint j = 0; j < tunerV->getSize(); j++) {
160                                 if (tunerV->get(j) == tuner)
161                                         tunerV->removeAt(j);
162                         }
163                 }
164         }
165 }
166
167 void MultiTuner::mapProblemsToTuners(Vector<TunerRecord *> *tunerV) {
168         for (uint i = 0; i < problems.getSize(); i++) {
169                 Problem *problem = problems.get(i);
170                 TunerRecord *bestTuner = NULL;
171                 long long bestscore = 0;
172                 for (uint j = 0; j < tunerV->getSize(); j++) {
173                         TunerRecord *tuner = tunerV->get(j);
174                         long long metric = tuner->getTime(problem);
175                         if (metric == -1) {
176                                 metric = evaluate(problem, tuner->getTuner());
177                                 if (metric != -1)
178                                         tuner->setTime(problem, metric);
179                         }
180                         if ((bestTuner == NULL && metric != -1) ||
181                                         (metric < bestscore && metric != -1)) {
182                                 bestTuner = tuner;
183                                 bestscore = metric;
184                         }
185                 }
186                 if (bestTuner != NULL)
187                         bestTuner->problems.push(problem);
188         }
189 }
190
191 void clearVector(Vector<TunerRecord *> *tunerV) {
192         for (uint j = 0; j < tunerV->getSize(); j++) {
193                 TunerRecord *tuner = tunerV->get(j);
194                 tuner->problems.clear();
195         }
196 }
197
198 void MultiTuner::tuneK() {
199         Vector<TunerRecord *> *tunerV = new Vector<TunerRecord *>(&tuners);
200         for (uint i = 0; i < rounds; i++) {
201                 clearVector(tunerV);
202                 mapProblemsToTuners(tunerV);
203                 improveTuners(tunerV);
204         }
205         model_print("Best tuners\n");
206         for (uint j = 0; j < tunerV->getSize(); j++) {
207                 TunerRecord *tuner = tunerV->get(j);
208                 char buffer[256];
209                 sprintf(buffer, "tuner%u.conf", j);
210                 tuner->getTuner()->serialize(buffer);
211                 tuner->getTuner()->print();
212         }
213         delete tunerV;
214 }
215
216 void MultiTuner::improveTuners(Vector<TunerRecord *> *tunerV) {
217         for (uint j = 0; j < tunerV->getSize(); j++) {
218                 TunerRecord *tuner = tunerV->get(j);
219                 TunerRecord *newtuner = tune(tuner);
220                 tunerV->set(j, newtuner);
221         }
222 }
223
224 double MultiTuner::evaluateAll(TunerRecord *tuner) {
225         double product = 1;
226         for (uint i = 0; i < tuner->problems.getSize(); i++) {
227                 Problem *problem = tuner->problems.get(i);
228                 long long metric = tuner->getTime(problem);
229                 if (metric == -1) {
230                         metric = evaluate(problem, tuner->getTuner());
231                         if (metric != -1)
232                                 tuner->setTime(problem, metric);
233                 }
234
235                 double score = metric;
236                 product *= score;
237         }
238         return pow(product, 1 / ((double)tuner->problems.getSize()));
239 }
240
241 SearchTuner *MultiTuner::mutateTuner(SearchTuner *oldTuner, uint k) {
242         SearchTuner *newTuner = oldTuner->copyUsed();
243         uint numSettings = oldTuner->getSize();
244         uint settingsToMutate = (uint)(AUTOTUNERFACTOR * (((double)numSettings) * (budget - k)) / (budget));
245         if (settingsToMutate < 1)
246                 settingsToMutate = 1;
247         model_print("Mutating %u settings\n", settingsToMutate);
248         while (settingsToMutate-- != 0) {
249                 newTuner->randomMutate();
250         }
251         return newTuner;
252 }
253
254 TunerRecord *MultiTuner::tune(TunerRecord *tuner) {
255         TunerRecord *bestTuner = NULL;
256         double bestScore = DBL_MAX;
257
258         TunerRecord *oldTuner = tuner;
259         double base_temperature = evaluateAll(oldTuner);
260         double oldScore = base_temperature;
261
262         for (uint i = 0; i < budget; i++) {
263                 SearchTuner *tmpTuner = mutateTuner(oldTuner->getTuner(), i);
264                 TunerRecord *newTuner = oldTuner->changeTuner(tmpTuner);
265                 allTuners.push(newTuner);
266                 double newScore = evaluateAll(newTuner);
267                 newTuner->tuner->printUsed();
268                 model_print("Received score %f\n", newScore);
269                 double scoreDiff = newScore - oldScore; //smaller is better
270                 if (newScore < bestScore) {
271                         bestScore = newScore;
272                         bestTuner = newTuner;
273                 }
274
275                 double acceptanceP;
276                 if (scoreDiff < 0) {
277                         acceptanceP = 1;
278                 } else {
279                         double currTemp = base_temperature * (((double)budget - i) / budget);
280                         acceptanceP = exp(-scoreDiff / currTemp);
281                 }
282                 double ran = ((double)random()) / RAND_MAX;
283                 if (ran <= acceptanceP) {
284                         oldScore = newScore;
285                         oldTuner = newTuner;
286                 }
287         }
288
289         return bestTuner;
290 }
291