1 #include "searchtuner.h"
3 TunableSetting::TunableSetting(VarType _type, TunableParam _param) :
9 TunableSetting::TunableSetting(TunableParam _param) :
15 TunableSetting::TunableSetting(TunableSetting * ts) :
19 lowValue(ts->lowValue),
20 highValue(ts->highValue),
21 defaultValue(ts->defaultValue),
22 selectedValue(ts->selectedValue)
26 void TunableSetting::setDecision(int _low, int _high, int _default, int _selection) {
29 defaultValue = _default;
30 selectedValue = _selection;
33 unsigned int tunableSettingHash(TunableSetting *setting) {
34 return setting->hasVar ^ setting->type ^ setting->param;
37 bool tunableSettingEquals(TunableSetting *setting1, TunableSetting *setting2) {
38 return setting1->hasVar == setting2->hasVar &&
39 setting1->type == setting2->type &&
40 setting1->param == setting2->param;
43 SearchTuner::SearchTuner() {
46 SearchTuner * SearchTuner::copyUsed() {
47 SearchTuner * tuner = new SearchTuner();
48 HSIteratorTunableSetting *iterator=usedSettings.iterator();
49 while(iterator->hasNext()) {
50 TunableSetting *setting=iterator->next();
51 TunableSetting *copy=new TunableSetting(setting);
52 tuner->settings.add(copy);
58 SearchTuner::~SearchTuner() {
59 HSIteratorTunableSetting *iterator=settings.iterator();
60 while(iterator->hasNext()) {
61 TunableSetting *setting=iterator->next();
67 int SearchTuner::getTunable(TunableParam param, TunableDesc *descriptor) {
68 TunableSetting setting(param);
69 TunableSetting * result = usedSettings.get(&setting);
71 result = settings.get(&setting);
72 if ( result == NULL) {
73 result=new TunableSetting(param);
74 uint value = descriptor->lowValue + (random() % (1+ descriptor->highValue - descriptor->lowValue));
75 result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
78 usedSettings.add(result);
80 return result->selectedValue;
83 int SearchTuner::getVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor) {
84 TunableSetting setting(vartype, param);
85 TunableSetting * result = usedSettings.get(&setting);
87 result = settings.get(&setting);
88 if ( result == NULL) {
89 result=new TunableSetting(vartype, param);
90 uint value = descriptor->lowValue + (random() % (1+ descriptor->highValue - descriptor->lowValue));
91 result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
94 usedSettings.add(result);
96 return result->selectedValue;
99 void SearchTuner::randomMutate() {
100 TunableSetting * randomSetting = settings.getRandomElement();
101 uint range=randomSetting->highValue-randomSetting->lowValue;
102 uint randomchoice=(random() % range) + randomSetting->lowValue;
103 if (randomchoice < randomSetting->selectedValue)
104 randomSetting->selectedValue = randomchoice;
106 randomSetting->selectedValue = randomchoice + 1;