BUGFIX: when ONE or TWO tuner cannot solve a problem
[satune.git] / src / Tuner / comptuner.cc
index 895e06bd2ef6c8dffb11ff989d13d2a2d896ef1b..d630f2d2280d77fc09b259275901710a46c614d2 100644 (file)
@@ -4,6 +4,7 @@
 #include "searchtuner.h"
 #include <iostream>
 #include <fstream>
+#include <limits>
 #include "solver_interface.h"
 
 CompTuner::CompTuner(uint _budget, uint _timeout) :
@@ -11,43 +12,47 @@ CompTuner::CompTuner(uint _budget, uint _timeout) :
 {
 }
 
-CompTuner::~CompTuner(){
-       
+CompTuner::~CompTuner() {
+
 }
 
-void CompTuner::findBestThreeTuners() {
-       if (allTuners.getSize() < 3) {
+void CompTuner::findBestTwoTuners() {
+       if (allTuners.getSize() < 2) {
                printData();
                return;
        }
-       TunerRecord *bestTuners[3];
+       TunerRecord *bestTuners[2];
        double score = DBL_MAX;
-       for (uint i = 0; i < allTuners.getSize() - 2; i++) {
-               for (uint j = i + 1; j < allTuners.getSize() - 1; j++) {
-                       for (uint k = j + 1; k < allTuners.getSize(); k++) {
-                               TunerRecord *tuner1 = allTuners.get(i);
-                               TunerRecord *tuner2 = allTuners.get(j);
-                               TunerRecord *tuner3 = allTuners.get(k);
-                               double mintimes[problems.getSize()];
-                               for (uint l = 0; l < problems.getSize(); l++) {
-                                       Problem *problem = problems.get(l);
-                                       mintimes[l] = pow(min(tuner1->getTime(problem), tuner2->getTime(problem), tuner3->getTime(problem)), (double)1 / problems.getSize());
-                               }
-                               double result = 1;
-                               for (uint l = 0; l < problems.getSize(); l++) {
-                                       result *= mintimes[l];
+       for (uint j = 0; j < allTuners.getSize() - 1; j++) {
+               for (uint k = j + 1; k < allTuners.getSize(); k++) {
+                       TunerRecord *tuner1 = allTuners.get(j);
+                       TunerRecord *tuner2 = allTuners.get(k);
+                       double mintimes[problems.getSize()];
+                       for (uint l = 0; l < problems.getSize(); l++) {
+                               Problem *problem = problems.get(l);
+                               long long time1 = tuner1->getTime(problem);
+                               if(time1 == -1){
+                                       time1=LLONG_MAX;
                                }
-                               if (result < score) {
-                                       score = result;
-                                       bestTuners[0] = tuner1;
-                                       bestTuners[1] = tuner2;
-                                       bestTuners[2] = tuner3;
+                               long long time2 = tuner2->getTime(problem);
+                               if(time2 == -1){
+                                       time2 = LLONG_MAX;
                                }
+                               mintimes[l] = pow(min(time1,time2), (double)1 / problems.getSize());
+                       }
+                       double result = 1;
+                       for (uint l = 0; l < problems.getSize(); l++) {
+                               result *= mintimes[l];
+                       }
+                       if (result < score) {
+                               score = result;
+                               bestTuners[0] = tuner1;
+                               bestTuners[1] = tuner2;
                        }
                }
        }
-       model_print("Best 3 tuners:\n");
-       for (uint i = 0; i < 3; i++) {
+       model_print("Best 2 tuners:\n");
+       for (uint i = 0; i < 2; i++) {
                TunerRecord *tuner = bestTuners[i];
                SearchTuner *stun = tuner->getTuner();
                char buffer[512];
@@ -60,6 +65,7 @@ void CompTuner::findBestThreeTuners() {
        }
 }
 
+
 void CompTuner::readData(uint numRuns) {
        for (uint i = 0; i < numRuns; i++) {
                ifstream myfile;
@@ -127,12 +133,12 @@ void CompTuner::tune() {
                model_print("Round %u of %u\n", b, budget);
                Hashtable<TunerRecord *, int, uint64_t> scores;
                Vector<Vector<TunerRecord *> *> allplaces;
-               for(uint ii=0; ii< problems.getSize(); ii++){
+               for (uint ii = 0; ii < problems.getSize(); ii++) {
                        allplaces.push(new Vector<TunerRecord *>());
                }
                for (uint j = 0; j < tunerV->getSize(); j++) {
                        TunerRecord *tuner = tunerV->get(j);
-                       
+
                        for (uint i = 0; i < problems.getSize(); i++) {
                                Vector<TunerRecord *> *places = allplaces.get(i);
                                Problem *problem = problems.get(i);
@@ -148,8 +154,8 @@ void CompTuner::tune() {
                                                tuner->setTime(problem, metric);
                                        else
                                                tuner->setTime(problem, -2);
-                                       
-                                       if(tunerExists(tuner)){
+
+                                       if (tunerExists(tuner)) {
                                                //Solving the problem and noticing the tuner
                                                //already exists
                                                tuner->setDuplicate(true);
@@ -167,13 +173,13 @@ void CompTuner::tune() {
                                }
                        }
                }
-               for(uint ii=0; ii < problems.getSize(); ii++){
+               for (uint ii = 0; ii < problems.getSize(); ii++) {
                        Problem *problem = problems.get(ii);
                        Vector<TunerRecord *> *places = allplaces.get(ii);
                        int points = 9;
                        for (uint k = 0; k < places->getSize() && points; k++) {
                                TunerRecord *tuner = places->get(k);
-                               if(tuner->isDuplicate()){
+                               if (tuner->isDuplicate()) {
                                        continue;
                                }
                                int currScore = 0;
@@ -186,7 +192,7 @@ void CompTuner::tune() {
                                points = points / 3;
                        }
                }
-               for(uint ii=0; ii< problems.getSize(); ii++){
+               for (uint ii = 0; ii < problems.getSize(); ii++) {
                        delete allplaces.get(ii);
                }
                Vector<TunerRecord *> ranking;
@@ -220,7 +226,7 @@ void CompTuner::tune() {
                uint tSize = tunerV->getSize();
                for (uint i = 0; i < tSize; i++) {
                        SearchTuner *tmpTuner = mutateTuner(tunerV->get(i)->getTuner(), b);
-                       while(subTunerIndex(tmpTuner) != -1){
+                       while (subTunerIndex(tmpTuner) != -1) {
                                model_print("******** New Tuner already explored...\n");
                                delete tmpTuner;
                                tmpTuner = mutateTuner(tunerV->get(i)->getTuner(), b);