be17c4b83a85da1a903b6be27261caaf82fc01d2
[satune.git] / src / Tuner / comptuner.cc
1 #include "comptuner.h"
2 #include <float.h>
3 #include <math.h>
4 #include "searchtuner.h"
5 #include <iostream>
6 #include <fstream>
7 #include "solver_interface.h"
8
9 CompTuner::CompTuner(uint _budget, uint _timeout) :
10         BasicTuner(_budget, _timeout)
11 {
12 }
13
14 CompTuner::~CompTuner(){
15         
16 }
17
18 void CompTuner::findBestThreeTuners() {
19         if (allTuners.getSize() < 3) {
20                 printData();
21                 return;
22         }
23         TunerRecord *bestTuners[3];
24         double score = DBL_MAX;
25         for (uint i = 0; i < allTuners.getSize() - 2; i++) {
26                 for (uint j = i + 1; j < allTuners.getSize() - 1; j++) {
27                         for (uint k = j + 1; k < allTuners.getSize(); k++) {
28                                 TunerRecord *tuner1 = allTuners.get(i);
29                                 TunerRecord *tuner2 = allTuners.get(j);
30                                 TunerRecord *tuner3 = allTuners.get(k);
31                                 double mintimes[problems.getSize()];
32                                 for (uint l = 0; l < problems.getSize(); l++) {
33                                         Problem *problem = problems.get(l);
34                                         mintimes[l] = pow(min(tuner1->getTime(problem), tuner2->getTime(problem), tuner3->getTime(problem)), (double)1 / problems.getSize());
35                                 }
36                                 double result = 1;
37                                 for (uint l = 0; l < problems.getSize(); l++) {
38                                         result *= mintimes[l];
39                                 }
40                                 if (result < score) {
41                                         score = result;
42                                         bestTuners[0] = tuner1;
43                                         bestTuners[1] = tuner2;
44                                         bestTuners[2] = tuner3;
45                                 }
46                         }
47                 }
48         }
49         model_print("Best 3 tuners:\n");
50         for (uint i = 0; i < 3; i++) {
51                 TunerRecord *tuner = bestTuners[i];
52                 SearchTuner *stun = tuner->getTuner();
53                 char buffer[512];
54                 snprintf(buffer, sizeof(buffer), "best%u.tuner", i);
55                 stun->serialize(buffer);
56                 model_print("Tuner %u\n", tuner->getTunerNumber());
57                 stun->print();
58                 tuner->printProblemsInfo();
59                 model_print("----------------------------------\n\n\n");
60         }
61 }
62
63 void CompTuner::readData(uint numRuns) {
64         for (uint i = 0; i < numRuns; i++) {
65                 ifstream myfile;
66                 char buffer[512];
67                 uint tunernumber;
68                 snprintf(buffer, sizeof(buffer), "tunernum%u", i);
69                 myfile.open (buffer, ios::in);
70                 myfile >> tunernumber;
71                 myfile.close();
72                 if (allTuners.getSize() <= tunernumber)
73                         allTuners.setSize(tunernumber + 1);
74                 if (allTuners.get(tunernumber) == NULL) {
75                         snprintf(buffer, sizeof(buffer), "tuner%u", i);
76                         allTuners.set(tunernumber, new TunerRecord(new SearchTuner(buffer), tunernumber));
77                 }
78                 //Add any new used records
79                 snprintf(buffer, sizeof(buffer), "tuner%uused", i);
80                 TunerRecord *tuner = allTuners.get(tunernumber);
81                 tuner->getTuner()->addUsed(buffer);
82
83                 char problemname[512];
84                 uint problemnumber;
85                 snprintf(buffer, sizeof(buffer), "problem%u", i);
86                 myfile.open(buffer, ios::in);
87                 myfile.getline(problemname, sizeof(problemname));
88                 myfile >> problemnumber;
89                 myfile.close();
90                 if (problems.getSize() <= problemnumber)
91                         problems.setSize(problemnumber + 1);
92                 if (problems.get(problemnumber) == NULL)
93                         problems.set(problemnumber, new Problem(problemname));
94                 Problem *problem = problems.get(problemnumber);
95                 long long metric = -1;
96                 int sat = IS_INDETER;
97                 //Read data in from results file
98                 snprintf(buffer, sizeof(buffer), "result%u", i);
99
100                 myfile.open (buffer, ios::in);
101
102
103                 if (myfile.is_open()) {
104                         myfile >> metric;
105                         myfile >> sat;
106                         myfile.close();
107                 }
108                 if (problem->getResult() == TUNERUNSETVALUE && sat != IS_INDETER) {
109                         problem->setResult(sat);
110                 } else if (problem->getResult() != sat && sat != IS_INDETER) {
111                         model_print("******** Result has changed ********\n");
112                 }
113
114                 if (metric != -1) {
115                         if (tuner->getTime(problem) == -1)
116                                 tuner->addProblem(problem);
117                         tuner->setTime(problem, metric);
118                 }
119
120         }
121
122 }
123
124 void CompTuner::tune() {
125         Vector<TunerRecord *> *tunerV = new Vector<TunerRecord *>(&tuners);
126         for (uint b = 0; b < budget; b++) {
127                 model_print("Round %u of %u\n", b, budget);
128                 uint tSize = tunerV->getSize();
129                 for (uint i = 0; i < tSize; i++) {
130                         SearchTuner *tmpTuner = mutateTuner(tunerV->get(i)->getTuner(), b);
131                         TunerRecord *tmp = new TunerRecord(tmpTuner);
132                         tmp->setTunerNumber(allTuners.getSize());
133                         model_print("Mutated tuner %u to generate tuner %u\n", tunerV->get(i)->getTunerNumber(), tmp->getTunerNumber());
134                         allTuners.push(tmp);
135                         tunerV->push(tmp);
136                 }
137
138                 Hashtable<TunerRecord *, int, uint64_t> scores;
139                 for (uint i = 0; i < problems.getSize(); i++) {
140                         Problem *problem = problems.get(i);
141                         Vector<TunerRecord *> places;
142                         for (uint j = 0; j < tunerV->getSize(); j++) {
143                                 TunerRecord *tuner = tunerV->get(j);
144                                 long long metric = tuner->getTime(problem);
145                                 if (metric == -1) {
146                                         metric = evaluate(problem, tuner);
147                                         if (tuner->getTime(problem) == -1) {
148                                                 tuner->addProblem(problem);
149                                         }
150                                         model_print("%u.Problem<%s>\tTuner<%p, %d>\tMetric<%lld>\n", i, problem->getProblem(),tuner, tuner->getTunerNumber(), metric);
151                                         model_print("*****************************\n");
152                                         if (metric != -1)
153                                                 tuner->setTime(problem, metric);
154                                         else
155                                                 tuner->setTime(problem, -2);
156                                 }
157                                 if (metric >= 0) {
158                                         uint k = 0;
159                                         for (; k < places.getSize(); k++) {
160                                                 if (metric < places.get(k)->getTime(problem))
161                                                         break;
162                                         }
163                                         model_print("place[%u]=Tuner<%p,%d>\n", k, tuner, tuner->getTunerNumber());
164                                         places.insertAt(k, tuner);
165                                 }
166                         }
167                         int points = 9;
168                         for (uint k = 0; k < places.getSize() && points; k++) {
169                                 TunerRecord *tuner = places.get(k);
170                                 int currScore = 0;
171                                 if (scores.contains(tuner))
172                                         currScore = scores.get(tuner);
173                                 currScore += points;
174                                 model_print("Problem<%s>\tTuner<%p,%d>\tmetric<%d>\n", problem->getProblem(), tuner, tuner->getTunerNumber(),  currScore);
175                                 model_print("**************************\n");
176                                 scores.put(tuner, currScore);
177                                 points = points / 3;
178                         }
179                 }
180                 Vector<TunerRecord *> ranking;
181                 for (uint i = 0; i < tunerV->getSize(); i++) {
182                         TunerRecord *tuner = tunerV->get(i);
183                         int score = 0;
184                         if (scores.contains(tuner))
185                                 score = scores.get(tuner);
186                         uint j = 0;
187                         for (; j < ranking.getSize(); j++) {
188                                 TunerRecord *t = ranking.get(j);
189                                 int tscore = 0;
190                                 if (scores.contains(t))
191                                         tscore = scores.get(t);
192                                 if (score > tscore)
193                                         break;
194                         }
195                         model_print("ranking[%u]=tuner<%p,%u>(Score=%d)\n", j, tuner, tuner->getTunerNumber(), score);
196                         model_print("************************\n");
197                         ranking.insertAt(j, tuner);
198                 }
199                 model_print("tunerSize=%u\trankingSize=%u\ttunerVSize=%u\n", tuners.getSize(), ranking.getSize(), tunerV->getSize());
200                 for (uint i = tuners.getSize(); i < ranking.getSize(); i++) {
201                         TunerRecord *tuner = ranking.get(i);
202                         model_print("Removing tuner %u\n", tuner->getTunerNumber());
203                         for (uint j = 0; j < tunerV->getSize(); j++) {
204                                 if (tunerV->get(j) == tuner)
205                                         tunerV->removeAt(j);
206                         }
207                 }
208         }
209         printData();
210 }