string Search::checkVicinityBlind( JoinedLandscape & thisLandscape, Entity & thisEntity, bool Sequential, int WhichLandscape ) /* randomly selects one move */ { tempPayoff.SetConfig ( thisEntity.GetConfig() ); if ( !thisEntity.IsAlive() ) { return "EMPTY"; } string tempConfig = tempPayoff.GetConfig(); unsigned short int i, j; unsigned short int strLength; double tempFitness; strLength = tempConfig.length(); i = rand() % (strLength); if ( tempConfig[i] == '0' ) { tempConfig[i] = '1'; } else { tempConfig[i] = '0'; }; switch (WhichLandscape) { case 0: pLandscape = &thisLandscape; break; case 1: pLandscape = &thisLandscape.LOne; break; case 2: pLandscape = &thisLandscape.LTwo; break; } tempFitness = thisLandscape.d[tempConfig].GetFitness(); if ( tempPayoff.GetFitness() <= thisLandscape.d[ thisEntity.GetConfig() ].GetFitness() ) { return "EMPTY"; } return tempPayoff.GetConfig(); }
string Search::checkVicinityDist( JoinedLandscape & thisLandscape, Entity & thisEntity, bool Sequential, int WhichLandscape ) /* randomly selects one move weighted by distribution */ { tempPayoff.SetConfig ( thisEntity.GetConfig() ); if ( !thisEntity.IsAlive() ) { return "EMPTY"; } string tempConfig = tempPayoff.GetConfig(); int i, j; unsigned short int strLength; double tempFitness; map <string,int> chance; vector <string> Configs; strLength = tempConfig.length(); Configs.push_back( tempConfig ); for ( i = 0 ; i < strLength; i++ ) { tempConfig = tempPayoff.GetConfig(); if ( tempConfig[i] == '0' ) { tempConfig[i] = '1'; } else { tempConfig[i] = '0'; } Configs.push_back( tempConfig ); } unsigned int total = 0; switch (WhichLandscape) { case 0: pLandscape = &thisLandscape; break; case 1: pLandscape = &thisLandscape.LOne; break; case 2: pLandscape = &thisLandscape.LTwo; break; } vector <string>::iterator IT; for ( IT = Configs.begin() ; IT != Configs.end() ; IT++ ) { chance.insert( pair<string,int> ( (*IT), thisLandscape.d[(*IT)].GiveDistribution() ) ); total += thisLandscape.d[(*IT)].GiveDistribution(); } i = rand() % total; map <string,int>::reverse_iterator IT_TWO; for ( IT_TWO = chance.rbegin() ; IT_TWO != chance.rend() ; IT_TWO++ ) // this random number might be 1 off { i = i-(*IT_TWO).second; tempConfig = (*IT_TWO).first; if (i < 0) { break; } } tempFitness = thisLandscape.d[tempConfig].GetFitness(); tempPayoff.SetConfig( tempConfig ); tempPayoff.SetFitness( tempFitness ); // if ( Move.GetFitness() <= d[ itsPayoff.GetConfig() ].GetFitness() ) { return "EMPTY"; } return tempPayoff.GetConfig(); }
string Search::checkVicinityNonrandom( JoinedLandscape & thisLandscape, Entity & thisEntity, bool Sequential, int WhichLandscape ) /* this version finds the move with the greatest improvement choices constant */ { tempPayoff.SetConfig ( thisEntity.GetConfig() ); // tempPayoff is current value for thisEntity if ( !thisEntity.IsAlive() ) { return "EMPTY"; } string configuration = tempPayoff.GetConfig(); // configuration is a temporary string to be returned as the potential move unsigned short int i, j, strLength, searchLength; double tempAverage = 0; vector <Payoff> averages; strLength = configuration.length(); switch (WhichLandscape) { case 0: pLandscape = &thisLandscape; searchLength = strLength; break; case 1: pLandscape = &thisLandscape.LOne; searchLength = strLength / 2; break; case 2: pLandscape = &thisLandscape.LTwo; configuration = flipConfigString( configuration ); tempPayoff.SetConfig ( flipConfigString ( tempPayoff.GetConfig() ) ); searchLength = strLength / 2; break; } tempPayoff.SetFitness ( (*pLandscape).d[configuration].GetFitness() ); for ( i = 0 ; i < searchLength ; i++) { configuration = tempPayoff.GetConfig(); // reset configuration if ( configuration[i] == '0' ) { configuration[i] = '1'; } // flip one decision else { configuration[i] = '0'; }; tempAverage = (*pLandscape).d[configuration].GetFitness(); // get the new value for the flipped string averages.push_back ( Payoff(configuration, tempAverage) ); // add to the list of possibilties } configuration = tempPayoff.GetConfig(); // resetting configuration to the Entity's current value if (WhichLandscape == 2) { configuration = flipConfigString (configuration); } // ugly hack to make the second landscape work tempAverage = tempPayoff.GetFitness(); // resetting fitness to the Entity's current value vector <Payoff>::iterator IT; for ( IT = averages.begin() ; IT != averages.end() ; IT++ ) { if ( tempAverage < (*IT).GetFitness() ) // if one of the averages is greater { configuration = (*IT).GetConfig(); // set the move information tempAverage = (*IT).GetFitness(); // similar } } // the move information should be the greatest possible if ( tempAverage <= tempPayoff.GetFitness() ) { return "EMPTY"; } // if less, don't recommend a move if ( isEqual (tempAverage, tempPayoff.GetFitness() ) ) { return "EMPTY"; } // if equal, no reason to move if ( WhichLandscape == 2 ) { return flipConfigString ( configuration ); } // flip the string back for the potential move return configuration; }
int main() { unsigned short int n=0, k=0, totalruns=0, i=0, k_two=0, walks=0, j=0; string search_strategy; Multicounter CounterA("A"), CounterB("B"), CounterC("C"), CounterD("D"); cout << "How many runs? "; cin >> totalruns; cout << "Select a size: "; cin >> n; srand ( time(NULL) ); cout << "Select k_1 (" << n-1 << " or less): "; cin >> k; cout << "Select k_2 (" << n-1 << " or less): "; cin >> k_two; cout << "Select number of steps: "; cin >> walks; cout << endl << "a) Keeps landscape constant (non-blind)" << endl << "b) Optimizes all choices at the same time (non-blind)" << endl << "all) Use all methods" << endl; cout << "Select a search strategy[default='all']: " ; cin >> search_strategy; if ( search_strategy != "a" && search_strategy != "b" ) { search_strategy = "all"; } Landscape * pLandscape = 0; Landscape * pLandscape_two = 0; JoinedLandscape * pLandscape_join = 0; Entity * pEntity = 0; string Move; bool success = 0; while( i < totalruns) { pLandscape = new Landscape(n,k,1); pLandscape_two = new Landscape(n,k,1); pLandscape_join = new JoinedLandscape(*pLandscape, *pLandscape_two, k_two); pEntity = new Entity( pLandscape_join->d, pLandscape_join->d_two, 2*n); if ( search_strategy == "a" || search_strategy == "all") { cout << "--------Search A--------" << endl; for ( j = 0 ; j < walks ; j++) { Move = pEntity->checkVicinity_one( pLandscape_join->d , pLandscape_join->d_two , n ); success = pEntity->Move( Move, pLandscape_join->d , pLandscape_join->d_two ); if ( !success ) { cout << "No move (1)" << endl; } } for ( j = 0 ; j < walks ; j++) { Move = pEntity->checkVicinity_two( pLandscape_join->d , pLandscape_join->d_two , n ); success = pEntity->Move( Move, pLandscape_join->d , pLandscape_join->d_two ); if ( !success ) { cout << "No move (2)" << endl; } } cout << "Final: " << pEntity->GetConfig() << ":" << pEntity->GetFitness() << endl; CounterA.Add ( pEntity->GetFitness() ); cout << "------------------------" << endl; } pEntity->Reset( pLandscape_join->d, pLandscape_join->d_two ); if ( search_strategy == "b" || search_strategy == "all") { cout << "--------Search B--------" << endl; for ( j = 0 ; j < walks ; j++) { Move = pEntity->checkVicinity_all( pLandscape_join->d , pLandscape_join->d_two ); success = pEntity->Move( Move, pLandscape_join->d , pLandscape_join->d_two ); if ( !success ) { cout << "No move" << endl; } } cout << "Final: " << pEntity->GetConfig() << ":" << pEntity->GetFitness() << endl; CounterB.Add ( pEntity->GetFitness() ); cout << "------------------------" << endl; } pEntity->Reset( pLandscape_join->d, pLandscape_join->d_two ); if ( 1 ) { cout << "--------Search C--------" << endl; for ( j = 0 ; j < walks ; j++) { Move = pEntity->checkVicinity_one_blind( pLandscape_join->d , pLandscape_join->d_two , n ); success = pEntity->Move( Move, pLandscape_join->d , pLandscape_join->d_two ); if ( !success ) { cout << "No move (1)" << endl; } } for ( j = 0 ; j < walks ; j++) { Move = pEntity->checkVicinity_two_blind( pLandscape_join->d , pLandscape_join->d_two , n ); success = pEntity->Move( Move, pLandscape_join->d , pLandscape_join->d_two ); if ( !success ) { cout << "No move (2)" << endl; } } cout << "Final: " << pEntity->GetConfig() << ":" << pEntity->GetFitness() << endl; CounterC.Add ( pEntity->GetFitness() ); cout << "------------------------" << endl; } pEntity->Reset( pLandscape_join->d, pLandscape_join->d_two ); if ( 1 ) { cout << "--------Search D--------" << endl; for ( j = 0 ; j < walks ; j++) { Move = pEntity->checkVicinity_one_blind( pLandscape_join->d , pLandscape_join->d_two , n ); success = pEntity->Move( Move, pLandscape_join->d , pLandscape_join->d_two ); if ( !success ) { cout << "No move (1)" << endl; } Move = pEntity->checkVicinity_two_blind( pLandscape_join->d , pLandscape_join->d_two , n ); success = pEntity->Move( Move, pLandscape_join->d , pLandscape_join->d_two ); if ( !success ) { cout << "No move (2)" << endl; } } cout << "Final: " << pEntity->GetConfig() << ":" << pEntity->GetFitness() << endl; CounterD.Add ( pEntity->GetFitness() ); cout << "------------------------" << endl; } // pLandscape_join->Writeout(); delete pLandscape, pLandscape_two, pLandscape_join, pEntity; i++; } *pLandscape, pLandscape_two, pLandscape_join = 0; CounterA.Show(); CounterB.Show(); CounterC.Show(); CounterD.Show(); return 0; }