Fix result file
[satune.git] / src / Tuner / multituner.cc
index 8ad7a32..1cdeee2 100644 (file)
@@ -8,9 +8,7 @@
 #include <iostream>
 #include <fstream>
 
-
 #define UNSETVALUE -1
-#define TIMEOUTSEC 5000
 
 Problem::Problem(const char *_problem) : result(UNSETVALUE) {
        uint len = strlen(_problem);
@@ -22,13 +20,33 @@ Problem::~Problem() {
        ourfree(problem);
 }
 
-MultiTuner::MultiTuner(uint _budget, uint _timeout) :
-       budget(_budget), timeout(_timeout), execnum(0) {
+void TunerRecord::setTime(Problem *problem, long long time) {
+       timetaken.put(problem, time);
+}
+
+long long TunerRecord::getTime(Problem *problem) {
+       if (timetaken.contains(problem))
+               return timetaken.get(problem);
+       else return -1;
+}
+
+TunerRecord *TunerRecord::changeTuner(SearchTuner *_newtuner) {
+       TunerRecord *tr = new TunerRecord(_newtuner);
+       for (uint i = 0; i < problems.getSize(); i++) {
+               tr->problems.push(problems.get(i));
+       }
+       return tr;
+}
+
+MultiTuner::MultiTuner(uint _budget, uint _rounds, uint _timeout) :
+       budget(_budget), rounds(_rounds), timeout(_timeout), execnum(0) {
 }
 
 MultiTuner::~MultiTuner() {
        for (uint i = 0; i < problems.getSize(); i++)
                ourfree(problems.get(i));
+       for (uint i = 0; i < allTuners.getSize(); i++)
+               delete allTuners.get(i);
 }
 
 void MultiTuner::addProblem(const char *filename) {
@@ -36,24 +54,27 @@ void MultiTuner::addProblem(const char *filename) {
 }
 
 void MultiTuner::addTuner(SearchTuner *tuner) {
-       tuners.push(new TunerRecord(tuner));
+       TunerRecord *t = new TunerRecord(tuner);
+       tuners.push(t);
+       allTuners.push(t);
 }
 
-long long MultiTuner::evaluate(Problem *problem, SearchTuner *tuner) {
+long long MultiTuner::evaluate(Problem *problem, TunerRecord *tuner) {
        char buffer[512];
        //Write out the tuner
        snprintf(buffer, sizeof(buffer), "tuner%u", execnum);
-       tuner->serialize(buffer);
+       tuner->getTuner()->serialize(buffer);
 
        //Do run
-       snprintf(buffer, sizeof(buffer), "deserializerun %s %u tuner%u result%s%u > log%u", problem->getProblem(), timeout, execnum, problem->getProblem(), execnum, execnum);
+       snprintf(buffer, sizeof(buffer), "./run.sh deserializerun %s %u tuner%u result%u > log%u", problem->getProblem(), timeout, execnum, execnum, execnum);
        int status = system(buffer);
 
        long long metric = -1;
        int sat = IS_INDETER;
+
        if (status == 0) {
                //Read data in from results file
-               snprintf(buffer, sizeof(buffer), "result%s%u", problem->getProblem(), execnum);
+               snprintf(buffer, sizeof(buffer), "result%u", execnum);
 
                ifstream myfile;
                myfile.open (buffer, ios::in);
@@ -64,6 +85,9 @@ long long MultiTuner::evaluate(Problem *problem, SearchTuner *tuner) {
                        myfile >> sat;
                        myfile.close();
                }
+
+               snprintf(buffer, sizeof(buffer), "tuner%uused", execnum);
+               tuner->getTuner()->addUsed(buffer);
        }
        //Increment execution count
        execnum++;
@@ -76,6 +100,74 @@ long long MultiTuner::evaluate(Problem *problem, SearchTuner *tuner) {
        return metric;
 }
 
+void MultiTuner::tuneComp() {
+       Vector<TunerRecord *> *tunerV = new Vector<TunerRecord *>(&tuners);
+       for (uint b = 0; b < budget; b++) {
+               uint tSize = tunerV->getSize();
+               for (uint i = 0; i < tSize; i++) {
+                       SearchTuner *tmpTuner = mutateTuner(tunerV->get(i)->getTuner(), b);
+                       tunerV->push(new TunerRecord(tmpTuner));
+               }
+
+               Hashtable<TunerRecord *, int, uint64_t> scores;
+               for (uint i = 0; i < problems.getSize(); i++) {
+                       Problem *problem = problems.get(i);
+                       Vector<TunerRecord *> places;
+                       for (uint j = 0; j < tunerV->getSize(); j++) {
+                               TunerRecord *tuner = tunerV->get(j);
+                               long long metric = tuner->getTime(problem);
+                               if (metric == -1) {
+                                       metric = evaluate(problem, tuner);
+                                       if (metric != -1)
+                                               tuner->setTime(problem, metric);
+                               }
+                               if (metric != -1) {
+                                       uint k = 0;
+                                       for (; k < places.getSize(); k++) {
+                                               if (metric < places.get(k)->getTime(problem))
+                                                       break;
+                                       }
+                                       places.insertAt(k, tuner);
+                               }
+                       }
+                       int points = 9;
+                       for (uint k = 0; k < places.getSize() && points; k++) {
+                               TunerRecord *tuner = places.get(k);
+                               points = points / 3;
+                               int currScore = 0;
+                               if (scores.contains(tuner))
+                                       currScore = scores.get(tuner);
+                               currScore += points;
+                               scores.put(tuner, currScore);
+                       }
+               }
+               Vector<TunerRecord *> ranking;
+               for (uint i = 0; i < tunerV->getSize(); i++) {
+                       TunerRecord *tuner = tunerV->get(i);
+                       int score = 0;
+                       if (scores.contains(tuner))
+                               score = scores.get(tuner);
+                       uint j = 0;
+                       for (; j < ranking.getSize(); j++) {
+                               TunerRecord *t = ranking.get(j);
+                               int tscore = 0;
+                               if (scores.contains(t))
+                                       tscore = scores.get(t);
+                               if (score > tscore)
+                                       break;
+                       }
+                       ranking.insertAt(j, tuner);
+               }
+               for (uint i = tuners.getSize(); i < ranking.getSize(); i++) {
+                       TunerRecord *tuner = ranking.get(i);
+                       for (uint j = 0; j < tunerV->getSize(); j++) {
+                               if (tunerV->get(j) == tuner)
+                                       tunerV->removeAt(j);
+                       }
+               }
+       }
+}
+
 void MultiTuner::mapProblemsToTuners(Vector<TunerRecord *> *tunerV) {
        for (uint i = 0; i < problems.getSize(); i++) {
                Problem *problem = problems.get(i);
@@ -83,7 +175,12 @@ void MultiTuner::mapProblemsToTuners(Vector<TunerRecord *> *tunerV) {
                long long bestscore = 0;
                for (uint j = 0; j < tunerV->getSize(); j++) {
                        TunerRecord *tuner = tunerV->get(j);
-                       long long metric = evaluate(problem, tuner->getTuner());
+                       long long metric = tuner->getTime(problem);
+                       if (metric == -1) {
+                               metric = evaluate(problem, tuner);
+                               if (metric != -1)
+                                       tuner->setTime(problem, metric);
+                       }
                        if ((bestTuner == NULL && metric != -1) ||
                                        (metric < bestscore && metric != -1)) {
                                bestTuner = tuner;
@@ -104,30 +201,45 @@ void clearVector(Vector<TunerRecord *> *tunerV) {
 
 void MultiTuner::tuneK() {
        Vector<TunerRecord *> *tunerV = new Vector<TunerRecord *>(&tuners);
-       while (true) {
+       for (uint i = 0; i < rounds; i++) {
                clearVector(tunerV);
                mapProblemsToTuners(tunerV);
                improveTuners(tunerV);
        }
-
+       model_print("Best tuners\n");
+       for (uint j = 0; j < tunerV->getSize(); j++) {
+               TunerRecord *tuner = tunerV->get(j);
+               char buffer[256];
+               sprintf(buffer, "tuner%u.conf", j);
+               tuner->getTuner()->serialize(buffer);
+               tuner->getTuner()->print();
+       }
        delete tunerV;
 }
 
 void MultiTuner::improveTuners(Vector<TunerRecord *> *tunerV) {
        for (uint j = 0; j < tunerV->getSize(); j++) {
                TunerRecord *tuner = tunerV->get(j);
-               SearchTuner *newtuner = tune(tuner->getTuner(), &tuner->problems);
+               TunerRecord *newtuner = tune(tuner);
+               tunerV->set(j, newtuner);
        }
 }
 
-double MultiTuner::evaluateAll(SearchTuner *tuner, Vector<Problem *> *tProblems) {
+double MultiTuner::evaluateAll(TunerRecord *tuner) {
        double product = 1;
-       for (uint i = 0; i < tProblems->getSize(); i++) {
-               Problem *problem = tProblems->get(i);
-               double score = evaluate(problem, tuner);
+       for (uint i = 0; i < tuner->problems.getSize(); i++) {
+               Problem *problem = tuner->problems.get(i);
+               long long metric = tuner->getTime(problem);
+               if (metric == -1) {
+                       metric = evaluate(problem, tuner);
+                       if (metric != -1)
+                               tuner->setTime(problem, metric);
+               }
+
+               double score = metric;
                product *= score;
        }
-       return pow(product, 1 / ((double)tProblems->getSize()));
+       return pow(product, 1 / ((double)tuner->problems.getSize()));
 }
 
 SearchTuner *MultiTuner::mutateTuner(SearchTuner *oldTuner, uint k) {
@@ -143,25 +255,25 @@ SearchTuner *MultiTuner::mutateTuner(SearchTuner *oldTuner, uint k) {
        return newTuner;
 }
 
-SearchTuner *MultiTuner::tune(SearchTuner *tuner, Vector<Problem *> *tProblems) {
-       SearchTuner *bestTuner = NULL;
+TunerRecord *MultiTuner::tune(TunerRecord *tuner) {
+       TunerRecord *bestTuner = NULL;
        double bestScore = DBL_MAX;
 
-       SearchTuner *oldTuner = tuner->copyUsed();
-       double base_temperature = evaluateAll(oldTuner, tProblems);
+       TunerRecord *oldTuner = tuner;
+       double base_temperature = evaluateAll(oldTuner);
        double oldScore = base_temperature;
 
        for (uint i = 0; i < budget; i++) {
-               SearchTuner *newTuner = mutateTuner(oldTuner, i);
-               double newScore = evaluateAll(newTuner, tProblems);
-               newTuner->printUsed();
+               SearchTuner *tmpTuner = mutateTuner(oldTuner->getTuner(), i);
+               TunerRecord *newTuner = oldTuner->changeTuner(tmpTuner);
+               allTuners.push(newTuner);
+               double newScore = evaluateAll(newTuner);
+               newTuner->tuner->printUsed();
                model_print("Received score %f\n", newScore);
                double scoreDiff = newScore - oldScore; //smaller is better
                if (newScore < bestScore) {
-                       if (bestTuner != NULL)
-                               delete bestTuner;
                        bestScore = newScore;
-                       bestTuner = newTuner->copyUsed();
+                       bestTuner = newTuner;
                }
 
                double acceptanceP;
@@ -173,11 +285,8 @@ SearchTuner *MultiTuner::tune(SearchTuner *tuner, Vector<Problem *> *tProblems)
                }
                double ran = ((double)random()) / RAND_MAX;
                if (ran <= acceptanceP) {
-                       delete oldTuner;
                        oldScore = newScore;
                        oldTuner = newTuner;
-               } else {
-                       delete newTuner;
                }
        }