Fix tuner issues
[satune.git] / src / Tuner / multituner.cc
index e20aa1f..03bfeec 100644 (file)
@@ -45,6 +45,8 @@ MultiTuner::MultiTuner(uint _budget, uint _rounds, uint _timeout) :
 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) {
@@ -57,18 +59,19 @@ void MultiTuner::addTuner(SearchTuner *tuner) {
        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%s%u > log%u", problem->getProblem(), timeout, execnum, problem->getProblem(), 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);
@@ -82,6 +85,12 @@ long long MultiTuner::evaluate(Problem *problem, SearchTuner *tuner) {
                        myfile >> sat;
                        myfile.close();
                }
+
+               snprintf(buffer, sizeof(buffer), "tuner%uused", execnum);
+               SearchTuner *usedtuner = new SearchTuner(buffer);
+               delete tuner->getTuner();
+               tuner->updateTuner(usedtuner);
+
        }
        //Increment execution count
        execnum++;
@@ -96,12 +105,70 @@ long long MultiTuner::evaluate(Problem *problem, SearchTuner *tuner) {
 
 void MultiTuner::tuneComp() {
        Vector<TunerRecord *> *tunerV = new Vector<TunerRecord *>(&tuners);
-       for (uint i = 0; i < problems.getSize(); i++) {
-               Problem *problem = problems.get(i);
-
+       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) {
@@ -113,7 +180,7 @@ void MultiTuner::mapProblemsToTuners(Vector<TunerRecord *> *tunerV) {
                        TunerRecord *tuner = tunerV->get(j);
                        long long metric = tuner->getTime(problem);
                        if (metric == -1) {
-                               metric = evaluate(problem, tuner->getTuner());
+                               metric = evaluate(problem, tuner);
                                if (metric != -1)
                                        tuner->setTime(problem, metric);
                        }
@@ -167,7 +234,7 @@ double MultiTuner::evaluateAll(TunerRecord *tuner) {
                Problem *problem = tuner->problems.get(i);
                long long metric = tuner->getTime(problem);
                if (metric == -1) {
-                       metric = evaluate(problem, tuner->getTuner());
+                       metric = evaluate(problem, tuner);
                        if (metric != -1)
                                tuner->setTime(problem, metric);
                }