void Strategy::doeInitRandom(const_device_ptr device) { while(getTableSize() < getDoeSize()){ Config c = getConfigRandom(device); memoizeConfig(c); } #ifdef DEBUG cout << "Random Initialized Table Size: " << getTableSize() << endl; #endif }
int hashName(char* ID) { int x=0, i = 0; while(ID[i] != '\0') { x += ((int)ID[i] % getTableSize()); } return x; }
int fetchIDIndex(char* ID) { //printf("Fetching ID Index - %s\n",ID); //THIS NEEDS TO BE FIXED ONCE WE CAN HAVE NESTED PROCEDURES!!! int hashval = hashName(ID); //int i = hashval; int x = getTableSize(); int lastseen = hashval; //printf("Fetched Hash Value -> %d\n",hashval); /* if(symbolTable[hashval].kind == 0) { printf("Hash Lookup Failed. ID -> %s\n", ID); return -1; } */ while(symbolTable[hashval].kind != 0) { if(strcmp(ID,symbolTable[hashval].name) == 0 ){ lastseen = hashval; } hashval = (hashval+1)%x; } return lastseen; }
int hashName(char* ID) { int x=0, i = 0; int a = getTableSize(); while(ID[i] != '\0') { x += ((int)ID[i] % a); i++; } return x; }
//returns the average length of linked list in the hash table double hashADT::getAvgNode() { int buckets = 0; for (int i = 0; i < getTableSize(); i++) { if (NumberOfPokemon(i) > 0) { buckets++; } //increment number of buckets that don't have dummy pokemon } return 1.0 * count / buckets; }
//returns the longest linked list in the hash table int hashADT::getLongestLink() { int num = 0; for (int i = 0; i < getTableSize(); i++) { if (NumberOfPokemon(i) > num) { num = NumberOfPokemon(i); } //save the longest chain } return num; }
Result DesignOfExperiments::search(const_target_ptr target, const_device_ptr device) { init(target,device); Config bestConfig; assert(findNearestConfig(target,bestConfig) == true); // must be initialized first Result r; r.setConfig(bestConfig); r.setDuration(getDuration()); r.setNumHits(numHits); r.setNumTries(1); r.setTableSize(getTableSize()); r.setTarget(target); r.setTotalEnergy(totalEnergy); r.setValue(target->getValue(&bestConfig)); r.setTablePercent((double)getTableSize()/device->getNumConfigs() * 100); r.setHitPercent(getHitPercent(target)); return r; }
int initSymbolTable() { int i = 0; symbolTable = NULL; symbolTable = (symbol*) malloc(sizeof(symbol)*(2*LL->numId+1)); if(symbolTable == NULL) { printf("Fatal Error: Malloc returned NULL!"); return 1; } for ( i = 0; i < getTableSize(); i++) { symbolTable[i].kind = 0; } return 0; }
int fetchIDIndex(char* ID) { //THIS NEEDS TO BE FIXED ONCE WE CAN HAVE NESTED PROCEDURES!!! int hashval = hashName(ID); int i = hashval; int x = getTableSize(); int lastseen = -1; if(symbolTable[hashval].kind == 0) return -1; while(symbolTable[hashval].kind != 0) { if(!strcmp(ID,symbolTable[hashval].name) ){ lastseen = hashval; } hashval++; } return lastseen; }
//Return 1 for fail, 0 for success int insertConst(char* ID, int val) { int hashval = hashName(ID); int i = hashval; int x = getTableSize(); do { if(symbolTable[hashval].kind == 0) { symbolTable[hashval].kind = 1; symbolTable[hashval].name = ID; symbolTable[hashval].val = val; return 0; } else if(!strcmp(ID, symbolTable[hashval].name)) { if(symbolTable[hashval].level == g_level){ return 1; } } hashval = (hashval+1)%x; } while(i != hashval); return 1; }
int insertProcedure(char* ID) { int hashval = hashName(ID); int i = hashval; int x = getTableSize(); do { if(symbolTable[hashval].kind == 0) { symbolTable[hashval].kind = 3; symbolTable[hashval].name = ID; symbolTable[hashval].level = g_level; symbolTable[hashval].addr = g_addr; return 0; } else if(!strcmp(ID, symbolTable[hashval].name)) { if(symbolTable[hashval].level == g_level){ return 1; } } hashval = (hashval+1)%x; } while(i != hashval); return 1; }
int insertVar(char* ID) { //printf("Inserted %s\n",ID); int hashval = hashName(ID); int i = hashval; int x = getTableSize(); do { if(symbolTable[hashval].kind == 0) { symbolTable[hashval].kind = 2; symbolTable[hashval].name = ID; symbolTable[hashval].level = g_level; symbolTable[hashval].addr = g_addr++; return 0; } else if(!strcmp(ID, symbolTable[hashval].name)) { if(symbolTable[hashval].level == g_level){ return 1; } } hashval = (hashval+1)%x; } while(i != hashval); return 1; }
//returns the load factor as a double double hashADT::getLoadFactor() { return 1.0 * count / getTableSize(); }
Result Genetic::search(const_target_ptr target, const_device_ptr device) { init(target,device); unsigned int i = 0, t = 0, gens; vector<Config> population; bool sortedPopulation = false; Config bestConfig; bool validBestConfig = findNearestConfig(target,bestConfig); srand((unsigned)time(0)); //cout << "Search for: " << target->getValue() << endl; // TODO: Use all DoE configs in initial population // Add bestConfig into population if(validBestConfig){ population.push_back(bestConfig); t++; } // Start with DoE population if available, otherwise random if(doeInitialized){ population = vector<Config>(doeConfigs.begin(),doeConfigs.end()); } // Create initial population while(population.size() < seedPopulation && !(target->getValue(&bestConfig) < target->getUpperBound() && target->getValue(&bestConfig) > target->getLowerBound())){ Config c = device->getConfigRandom(); memoizeConfig(c); // Make sure not already included bool addConfig = true; for(vector<Config>::iterator it = population.begin(); it != population.end(); it++){ if((*it).getConfigNum() == c.getConfigNum()) addConfig = false; } if(addConfig){ population.push_back(c); t++; } } // Determine how many generations we can run gens = (tries - population.size()) / (float) childrenCount; while (t < gens && !(target->getValue(&bestConfig) < target->getUpperBound() && target->getValue(&bestConfig) > target->getLowerBound()) && !overLimit){ sort(population.begin(), population.end(), compare(target)); sortedPopulation = true; vector<Config> children; for(i=0; i<childrenCount; i += 2){ Config parent1 = selectBreeder(population); Config parent2 = selectBreeder(population); list<Config> kids = mate(parent1, parent2, device); for(list<Config>::iterator it = kids.begin(); it != kids.end(); it++){ children.push_back(*it); memoizeConfig(*it); t++; } } // Add children to population sortedPopulation = false; for(vector<Config>::iterator it = children.begin(); it != children.end(); it++, i++){ population.push_back(*it); } sort(population.begin(), population.end(), compare(target)); sortedPopulation = true; vector<Config>::iterator iv; iv = unique(population.begin(), population.end(), same_config(target)); population.resize(iv - population.begin()); // Cull population down to populationLimit population.resize(populationLimit); bestConfig = population.front(); //cout << endl << "Target = " << target->getValue() << endl; //cout << endl << "Best = " << target->getValue(&bestConfig) << endl; } Result r; r.setTarget(target); r.setConfig(bestConfig); r.setDuration(duration); r.setNumHits(numHits); r.setNumTries(t); r.setTableSize(getTableSize()); r.setTargetNumber(target->getValue()); r.setTotalEnergy(totalEnergy); r.setValue(target->getValue(&bestConfig)); r.setTablePercent((double)getTableSize()/device->getNumConfigs() * 100); r.setHitPercent(getHitPercent(target)); return r; }