Move static tuner into its own class
authorbdemsky <bdemsky@uci.edu>
Thu, 11 Oct 2018 18:33:20 +0000 (11:33 -0700)
committerbdemsky <bdemsky@uci.edu>
Thu, 11 Oct 2018 18:33:49 +0000 (11:33 -0700)
src/Tuner/autotuner.cc
src/Tuner/autotuner.h
src/Tuner/searchtuner.cc
src/Tuner/searchtuner.h
src/Tuner/staticautotuner.cc [new file with mode: 0644]
src/Tuner/staticautotuner.h [new file with mode: 0644]
src/Tuner/staticsearchtuner.cc [new file with mode: 0644]
src/Tuner/staticsearchtuner.h [new file with mode: 0644]
src/classlist.h
src/config.h

index 857b21cf6a5ad3c935d26f63a2d0d2b8e3db5191..e448834cfc7c154b2e22422efe4de6843c3bb5b5 100644 (file)
@@ -55,14 +55,6 @@ SearchTuner *AutoTuner::mutateTuner(SearchTuner *oldTuner, uint k) {
        return newTuner;
 }
 
        return newTuner;
 }
 
-#ifdef STATICENCGEN
-SearchTuner *AutoTuner::mutateTuner(SearchTuner *oldTuner) {
-       SearchTuner *newTuner = oldTuner->copyUsed();
-       result = newTuner->nextStaticTuner();
-       return result==EXIT_FAILURE? newTuner: NULL;
-}
-#endif
-
 void AutoTuner::tune() {
        SearchTuner *bestTuner = NULL;
        double bestScore = DBL_MAX;
 void AutoTuner::tune() {
        SearchTuner *bestTuner = NULL;
        double bestScore = DBL_MAX;
@@ -71,20 +63,6 @@ void AutoTuner::tune() {
        double base_temperature = evaluateAll(oldTuner);
        double oldScore = base_temperature;
 
        double base_temperature = evaluateAll(oldTuner);
        double oldScore = base_temperature;
 
-#ifdef STATICENCGEN
-       while(true){
-               SearchTuner *newTuner = mutateTuner(oldTuner);
-               if(newTuner == NULL)
-                       return;
-               double newScore = evaluateAll(newTuner);
-               newTuner->printUsed();
-               model_print("Received score %f\n", newScore);
-               delete oldTuner;
-               oldScore = newScore;
-               oldTuner = newTuner;
-       }
-#endif
-       
        for (uint i = 0; i < budget; i++) {
                SearchTuner *newTuner = mutateTuner(oldTuner, i);
                double newScore = evaluateAll(newTuner);
        for (uint i = 0; i < budget; i++) {
                SearchTuner *newTuner = mutateTuner(oldTuner, i);
                double newScore = evaluateAll(newTuner);
index ed276ee253963cfc95cd28470cb411dec85c756c..8271008cb74ad4c2ceb560a56a56c49ad564c142 100644 (file)
@@ -10,15 +10,12 @@ class AutoTuner {
 public:
        AutoTuner(uint budget);
        void addProblem(CSolver *solver);
 public:
        AutoTuner(uint budget);
        void addProblem(CSolver *solver);
-       void tune();
+       virtual void tune();
        CMEMALLOC;
        CMEMALLOC;
-private:
+protected:
        long long evaluate(CSolver *problem, SearchTuner *tuner);
        double evaluateAll(SearchTuner *tuner);
        SearchTuner *mutateTuner(SearchTuner *oldTuner, uint k);
        long long evaluate(CSolver *problem, SearchTuner *tuner);
        double evaluateAll(SearchTuner *tuner);
        SearchTuner *mutateTuner(SearchTuner *oldTuner, uint k);
-#ifdef STATICENCGEN
-       SearchTuner *mutateTuner(SearchTuner *oldTuner);
-#endif
        Vector<CSolver *> solvers;
        uint budget;
        int result;
        Vector<CSolver *> solvers;
        uint budget;
        int result;
index 1aef3fcd566a4adf28e201aac1fe1ec91d19e7af..bfe66bb4dd77cdcb21e15230a09cb9e5a6da5882 100644 (file)
@@ -72,10 +72,6 @@ ostream &operator<<(ostream &os, const TunableSetting &ts)
 
 
 SearchTuner::SearchTuner() {
 
 
 SearchTuner::SearchTuner() {
-#ifdef STATICENCGEN
-        graphEncoding =false;
-        naiveEncoding = ELEM_UNASSIGNED;
-#endif
        ifstream myfile;
        myfile.open (TUNEFILE, ios::in);
        if (myfile.is_open()) {
        ifstream myfile;
        myfile.open (TUNEFILE, ios::in);
        if (myfile.is_open()) {
@@ -110,12 +106,6 @@ SearchTuner *SearchTuner::copyUsed() {
                TunableSetting *copy = new TunableSetting(setting);
                tuner->settings.add(copy);
        }
                TunableSetting *copy = new TunableSetting(setting);
                tuner->settings.add(copy);
        }
-#ifdef STATICENCGEN
-       if(naiveEncoding != ELEM_UNASSIGNED){
-               tuner->graphEncoding = graphEncoding;
-               tuner->naiveEncoding = naiveEncoding;
-       }
-#endif
        delete iterator;
        return tuner;
 }
        delete iterator;
        return tuner;
 }
@@ -179,50 +169,6 @@ void SearchTuner::randomMutate() {
        model_print("&&&&&&&&&&&&&&&&&&&&&&&\n");
 }
 
        model_print("&&&&&&&&&&&&&&&&&&&&&&&\n");
 }
 
-#ifdef STATICENCGEN
-int SearchTuner::nextStaticTuner() {
-       if(naiveEncoding == ELEM_UNASSIGNED){
-               naiveEncoding = ONEHOT;
-               SetIteratorTunableSetting *iter = settings.iterator();
-               while(iter->hasNext()){
-                       TunableSetting *setting = iter->next();
-                       if (setting->param == NAIVEENCODER){
-                               setting->selectedValue = ONEHOT;
-                       } else if(setting->param == ENCODINGGRAPHOPT){
-                               setting->selectedValue = false;
-                       }
-               }
-               delete iter;
-               return EXIT_FAILURE;
-       }
-       int result=EXIT_FAILURE;
-       if(naiveEncoding == BINARYINDEX && graphEncoding){
-               model_print("Best tuner\n");
-               return EXIT_SUCCESS;
-       }else if (naiveEncoding == BINARYINDEX && !graphEncoding){
-               naiveEncoding = ONEHOT;
-               graphEncoding = true;
-       }else {
-               naiveEncoding = (ElementEncodingType)((int)naiveEncoding + 1);
-       }
-       SetIteratorTunableSetting *iter = settings.iterator();
-       uint count = 0;
-       while(iter->hasNext()){
-               TunableSetting * setting = iter->next();
-               if (setting->param == NAIVEENCODER){
-                       setting->selectedValue = naiveEncoding;
-                       count++;
-               } else if(setting->param == ENCODINGGRAPHOPT){
-                       setting->selectedValue = graphEncoding;
-                       count++;
-               }
-       }
-       model_print("Mutating %u settings\n", count);
-       delete iter;
-       return result;
-}
-#endif
-
 void SearchTuner::print() {
        SetIteratorTunableSetting *iterator = settings.iterator();
        while (iterator->hasNext()) {
 void SearchTuner::print() {
        SetIteratorTunableSetting *iterator = settings.iterator();
        while (iterator->hasNext()) {
index 612f60e174f826b239911d817428d458fb92a223..107926d1c02fa2f86933aad93a0fecbfd090b9e1 100644 (file)
@@ -29,6 +29,7 @@ private:
        friend unsigned int tunableSettingHash(TunableSetting *setting);
        friend bool tunableSettingEquals(TunableSetting *setting1, TunableSetting *setting2);
        friend class SearchTuner;
        friend unsigned int tunableSettingHash(TunableSetting *setting);
        friend bool tunableSettingEquals(TunableSetting *setting1, TunableSetting *setting2);
        friend class SearchTuner;
+       friend class StaticSearchTuner;
 };
 
 unsigned int tunableSettingHash(TunableSetting *setting);
 };
 
 unsigned int tunableSettingHash(TunableSetting *setting);
@@ -52,20 +53,13 @@ public:
        void serialize();
 
        CMEMALLOC;
        void serialize();
 
        CMEMALLOC;
-private:
+protected:
        /** Used Settings keeps track of settings that were actually used by
           the example. Mutating settings may cause the Constraint Compiler
           not to query other settings.*/
        HashsetTunableSetting usedSettings;
        /** Settings contains all settings. */
        HashsetTunableSetting settings;
        /** Used Settings keeps track of settings that were actually used by
           the example. Mutating settings may cause the Constraint Compiler
           not to query other settings.*/
        HashsetTunableSetting usedSettings;
        /** Settings contains all settings. */
        HashsetTunableSetting settings;
-#ifdef STATICENCGEN
-        bool graphEncoding;
-        ElementEncodingType naiveEncoding;
-public:
-        int nextStaticTuner();
-#endif
 };
 
 };
 
-
 #endif
 #endif
diff --git a/src/Tuner/staticautotuner.cc b/src/Tuner/staticautotuner.cc
new file mode 100644 (file)
index 0000000..71e8c68
--- /dev/null
@@ -0,0 +1,38 @@
+#include "staticautotuner.h"
+#include "csolver.h"
+#include "staticsearchtuner.h"
+#include <math.h>
+#include <stdlib.h>
+#include <float.h>
+
+#define UNSETVALUE -1
+#define TIMEOUTSEC 5000
+StaticAutoTuner::StaticAutoTuner(uint _budget) : AutoTuner(_budget) {
+}
+
+StaticSearchTuner *StaticAutoTuner::mutateTuner(StaticSearchTuner *oldTuner) {
+       StaticSearchTuner *newTuner = oldTuner->copyUsed();
+       result = newTuner->nextStaticTuner();
+       return result==EXIT_FAILURE? newTuner: NULL;
+}
+
+void StaticAutoTuner::tune() {
+       StaticSearchTuner *bestTuner = NULL;
+       double bestScore = DBL_MAX;
+
+       StaticSearchTuner *oldTuner = new StaticSearchTuner();
+       double base_temperature = evaluateAll(oldTuner);
+       double oldScore = base_temperature;
+
+       while(true){
+               StaticSearchTuner *newTuner = mutateTuner(oldTuner);
+               if(newTuner == NULL)
+                       return;
+               double newScore = evaluateAll(newTuner);
+               newTuner->printUsed();
+               model_print("Received score %f\n", newScore);
+               delete oldTuner;
+               oldScore = newScore;
+               oldTuner = newTuner;
+       }
+}
diff --git a/src/Tuner/staticautotuner.h b/src/Tuner/staticautotuner.h
new file mode 100644 (file)
index 0000000..cc0d1e2
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef STATICAUTOTUNER_H
+#define STATICAUTOTUNER_H
+#include "autotuner.h"
+
+
+class StaticAutoTuner : public AutoTuner {
+public:
+       StaticAutoTuner(uint budget);
+  virtual void tune();
+       CMEMALLOC;
+private:
+       StaticSearchTuner *mutateTuner(StaticSearchTuner *oldTuner);
+};
+#endif
diff --git a/src/Tuner/staticsearchtuner.cc b/src/Tuner/staticsearchtuner.cc
new file mode 100644 (file)
index 0000000..e3cc3fa
--- /dev/null
@@ -0,0 +1,100 @@
+#include "staticsearchtuner.h"
+#include <iostream>
+#include <fstream>
+using namespace std;
+
+StaticSearchTuner::StaticSearchTuner() {
+        graphEncoding =false;
+        naiveEncoding = ELEM_UNASSIGNED;
+       ifstream myfile;
+       myfile.open (TUNEFILE, ios::in);
+       if (myfile.is_open()) {
+               bool hasVar;
+               VarType type1;
+               VarType type2;
+               TunableParam param;
+               int lowValue;
+               int highValue;
+               int defaultValue;
+               int selectedValue;
+               while (myfile >> hasVar >> type1 >> type2 >> param >> lowValue >> highValue >> defaultValue >> selectedValue) {
+                       TunableSetting *setting;
+
+                       if (hasVar) {
+                               setting = new TunableSetting(type1, type2, param);
+                       } else {
+                               setting = new TunableSetting(param);
+                       }
+                       setting->setDecision(lowValue, highValue, defaultValue, selectedValue);
+                       usedSettings.add(setting);
+               }
+               myfile.close();
+       }
+}
+
+StaticSearchTuner *StaticSearchTuner::copyUsed() {
+       StaticSearchTuner *tuner = new StaticSearchTuner();
+       SetIteratorTunableSetting *iterator = usedSettings.iterator();
+       while (iterator->hasNext()) {
+               TunableSetting *setting = iterator->next();
+               TunableSetting *copy = new TunableSetting(setting);
+               tuner->settings.add(copy);
+       }
+       if(naiveEncoding != ELEM_UNASSIGNED){
+               tuner->graphEncoding = graphEncoding;
+               tuner->naiveEncoding = naiveEncoding;
+       }
+       delete iterator;
+       return tuner;
+}
+
+StaticSearchTuner::~StaticSearchTuner() {
+       SetIteratorTunableSetting *iterator = settings.iterator();
+       while (iterator->hasNext()) {
+               TunableSetting *setting = iterator->next();
+               delete setting;
+       }
+       delete iterator;
+}
+
+int StaticSearchTuner::nextStaticTuner() {
+       if(naiveEncoding == ELEM_UNASSIGNED){
+               naiveEncoding = ONEHOT;
+               SetIteratorTunableSetting *iter = settings.iterator();
+               while(iter->hasNext()){
+                       TunableSetting *setting = iter->next();
+                       if (setting->param == NAIVEENCODER){
+                               setting->selectedValue = ONEHOT;
+                       } else if(setting->param == ENCODINGGRAPHOPT){
+                               setting->selectedValue = false;
+                       }
+               }
+               delete iter;
+               return EXIT_FAILURE;
+       }
+       int result=EXIT_FAILURE;
+       if(naiveEncoding == BINARYINDEX && graphEncoding){
+               model_print("Best tuner\n");
+               return EXIT_SUCCESS;
+       }else if (naiveEncoding == BINARYINDEX && !graphEncoding){
+               naiveEncoding = ONEHOT;
+               graphEncoding = true;
+       }else {
+               naiveEncoding = (ElementEncodingType)((int)naiveEncoding + 1);
+       }
+       SetIteratorTunableSetting *iter = settings.iterator();
+       uint count = 0;
+       while(iter->hasNext()){
+               TunableSetting * setting = iter->next();
+               if (setting->param == NAIVEENCODER){
+                       setting->selectedValue = naiveEncoding;
+                       count++;
+               } else if(setting->param == ENCODINGGRAPHOPT){
+                       setting->selectedValue = graphEncoding;
+                       count++;
+               }
+       }
+       model_print("Mutating %u settings\n", count);
+       delete iter;
+       return result;
+}
diff --git a/src/Tuner/staticsearchtuner.h b/src/Tuner/staticsearchtuner.h
new file mode 100644 (file)
index 0000000..407037b
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef STATICSEARCHTUNER_H
+#define STATICSEARCHTUNER_H
+#include "searchtuner.h"
+
+class StaticSearchTuner : public SearchTuner {
+public:
+       StaticSearchTuner();
+       ~StaticSearchTuner();
+        int nextStaticTuner();
+       StaticSearchTuner *copyUsed();
+  
+       CMEMALLOC;
+private:
+        bool graphEncoding;
+        ElementEncodingType naiveEncoding;
+};
+
+#endif
index 3269985504a2da78a90393ecb58a681bec7a19ca..cd7757d4035534707a9f98f9a57edc78d575745c 100644 (file)
@@ -60,6 +60,9 @@ class AutoTuner;
 class SearchTuner;
 class TunableSetting;
 
 class SearchTuner;
 class TunableSetting;
 
+class StaticAutoTuner;
+class StaticSearchTuner;
+
 class TunableDesc;
 
 class OrderResolver;
 class TunableDesc;
 
 class OrderResolver;
index ef0b9b1929bc5f046f996ace37daf3aa47a81db1..ce13d3c8ef6873db22e29ce7dc5d7e25ef0b1630 100644 (file)
@@ -19,8 +19,6 @@
 //#define CONFIG_DEBUG
 #endif
 
 //#define CONFIG_DEBUG
 #endif
 
-//#define STATICENCGEN
-
 #ifndef CONFIG_ASSERT
 #define CONFIG_ASSERT
 #endif
 #ifndef CONFIG_ASSERT
 #define CONFIG_ASSERT
 #endif