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;
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);
public:
AutoTuner(uint budget);
void addProblem(CSolver *solver);
- void tune();
+ virtual void tune();
CMEMALLOC;
-private:
+protected:
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;
SearchTuner::SearchTuner() {
-#ifdef STATICENCGEN
- graphEncoding =false;
- naiveEncoding = ELEM_UNASSIGNED;
-#endif
ifstream myfile;
myfile.open (TUNEFILE, ios::in);
if (myfile.is_open()) {
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;
}
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()) {
friend unsigned int tunableSettingHash(TunableSetting *setting);
friend bool tunableSettingEquals(TunableSetting *setting1, TunableSetting *setting2);
friend class SearchTuner;
+ friend class StaticSearchTuner;
};
unsigned int tunableSettingHash(TunableSetting *setting);
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;
-#ifdef STATICENCGEN
- bool graphEncoding;
- ElementEncodingType naiveEncoding;
-public:
- int nextStaticTuner();
-#endif
};
-
#endif
--- /dev/null
+#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;
+ }
+}
--- /dev/null
+#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
--- /dev/null
+#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;
+}
--- /dev/null
+#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
class SearchTuner;
class TunableSetting;
+class StaticAutoTuner;
+class StaticSearchTuner;
+
class TunableDesc;
class OrderResolver;
//#define CONFIG_DEBUG
#endif
-//#define STATICENCGEN
-
#ifndef CONFIG_ASSERT
#define CONFIG_ASSERT
#endif