string Entity::checkVicinity_all( map< string , Payoff > & d, map< string , Payoff > & d_two) /* this version, once complete, will consider both landscapes and make the move that provides the biggest average */ { Payoff Move; string tempConfig = itsPayoff.GetConfig(); string tempConfig_two; unsigned short int i; unsigned short int strLength; vector <Payoff> averages; double tempAverage; strLength = tempConfig.length(); for ( i = 0 ; i < strLength ; i++) { tempConfig = itsPayoff.GetConfig(); if ( tempConfig[i] == '0' ) { tempConfig[i] = '1'; } else { tempConfig[i] = '0'; } tempConfig_two = flipConfigString( tempConfig ); tempAverage = (d[tempConfig].GetFitness() + d_two[tempConfig_two].GetFitness()) / (double) 2; averages.push_back ( Payoff(tempConfig,tempAverage) ); } Move.SetFitness(0); vector <Payoff>::iterator IT; for ( IT = averages.begin() ; IT != averages.end() ; IT++) { if (Move.GetFitness() < (*IT).GetFitness() ) { Move.SetConfig( (*IT).GetConfig() ); Move.SetFitness( (*IT).GetFitness() ); } } if ( Move.GetFitness() <= itsPayoff.GetFitness() ) { return "EMPTY"; } return Move.GetConfig(); }
string Entity::checkVicinity_two( map< string , Payoff > & d, map< string , Payoff > & d_two, unsigned short int maxN) /* this version keeps landscape 2 choices constant */ { Payoff Move; string tempConfig = itsPayoff.GetConfig(), tempBinary, tempConfig_two; double currentAverage = (d[ itsPayoff.GetConfig() ].GetFitness() + d_two[ flipConfigString(itsPayoff.GetConfig()) ].GetFitness()) / (double) 2; unsigned short int i, j; unsigned short int strLength; vector <Payoff> averages; double tempAverage; strLength = tempConfig.length(); for ( i = maxN ; i < 2*maxN ; i++) { tempConfig = itsPayoff.GetConfig(); if ( tempConfig[i] == '0' ) { tempConfig[i] = '1'; } else { tempConfig[i] = '0'; }; tempConfig_two = flipConfigString( tempConfig ); tempAverage = (d[tempConfig].GetFitness() + d_two[tempConfig_two].GetFitness()) / (double) 2; averages.push_back ( Payoff(tempConfig,tempAverage) ); } Move.SetFitness(0); vector <Payoff>::iterator IT; for ( IT = averages.begin() ; IT != averages.end() ; IT++ ) { if ( Move.GetFitness() < (*IT).GetFitness() ) { Move.SetConfig( (*IT).GetConfig() ); Move.SetFitness( (*IT).GetFitness() ); } } if ( Move.GetFitness() <= itsPayoff.GetFitness() ) { return "EMPTY"; } return Move.GetConfig(); }
bool Entity::Move( string Move, map< string , Payoff > & d, map< string , Payoff > & d_two ) { if ( Move == "EMPTY" ) { return 0; } cout << "Move from " << itsPayoff.GetConfig() << ":" << itsPayoff.GetFitness() << " to "; itsPayoff.SetConfig( Move ); itsPayoff.SetFitness( (d[Move].GetFitness() + d_two[ flipConfigString(Move) ].GetFitness()) / (double) 2 ); cout << itsPayoff.GetConfig() << ":" << itsPayoff.GetFitness() << " is successful." << endl; return 1; }
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; }
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(); }
/* ********************************************* BLIND ************************ */ string Entity::checkVicinity_one_blind( map< string , Payoff > & d, map< string , Payoff > & d_two, unsigned short int maxN ) /* this version keeps landscape 2 choices constant */ { Payoff Move; string tempConfig = itsPayoff.GetConfig(); unsigned short int i, j; unsigned short int strLength; double tempFitness; strLength = tempConfig.length(); tempConfig = itsPayoff.GetConfig(); i = rand() % (maxN); if ( tempConfig[i] == '0' ) { tempConfig[i] = '1'; } else { tempConfig[i] = '0'; }; tempFitness = d[tempConfig].GetFitness(); Move.SetConfig( tempConfig ); Move.SetFitness( tempFitness ); if ( Move.GetFitness() <= d[ itsPayoff.GetConfig() ].GetFitness() ) { return "EMPTY"; } return Move.GetConfig(); }