double speed_test_asign() { timeval start, end; unsigned int testTime = 0; unsigned int compareTime = 0; SecurePrim<unsigned int> testValue(0); unsigned int compareValue = 0; for(int i=0; i < 100000; i++) { gettimeofday(&start, NULL); testValue = 5; gettimeofday(&end, NULL); testTime += (end.tv_usec - start.tv_usec); } for(int i=0; i < 100000; i++) { gettimeofday(&start, NULL); compareValue = 5; gettimeofday(&end, NULL); compareTime += (end.tv_usec - start.tv_usec); } //~ std::cout << "\n" << testValue; //~ std::cout << "\n" << compareValue; //~ std::cout << "\n" << testTime; //~ std::cout << "\n" << compareTime; return double(testTime) / double(compareTime); }
int main() { { //test with ptr false testRef(NULL); testConstRef(NULL); testValue(NULL); } A* x = new A(); { //test with ptr true x->x=2; testRef(x); testConstRef(x); testValue(x); } delete x; return 0; }
/* ======================================================================================================================= test the answer state for validity, returns string "True" or "False" ======================================================================================================================= */ char *testAnswer(State *resultState, long numberCompanies, long numberBlocks) { if(isConsistent(resultState, numberCompanies, numberBlocks) && testValue(resultState)) { return "True"; } else { return "False"; } }
int main() { int score = 0; printf("\nIniciando o teste do módulo message\n"); score += testResult(); score += testKey(); score += testValue(); score += testEntry(); score += testKeys(); score += testInvalida(); printf("Resultados do teste do módulo message: %d em 6\n\n",score); return score; }
int ImmediateBestValueStrategy1::claim(GameState state) { if (beVerbose) std::cout << "________________________________" << std::endl; if (beVerbose) std::cout << "ImmediateBestValueStrategy1 "<<myPlayerNumber<<" is claiming his land" << std::endl; if (beVerbose) state.display(); std::vector<std::pair<int,double>> choices; for (int i=0; i<state.getNumRegions(); i++) { if (state.getRegionInfo(i).first < 0) choices.push_back(std::pair<int,double>(i,testValue(state, i))); } std::sort(choices.begin(), choices.end(), [](const std::pair<int, double> &lhs, const std::pair<int, double> &rhs) { return lhs.second < rhs.second; }); int pickedRegion = choices[choices.size()-1].first; if (beVerbose) std::cout << "Chose: " << pickedRegion << std::endl; if (beVerbose) std::cout << "________________________________" << std::endl; // return the available country with the lowest region count return pickedRegion; }
double speed_test_postincrement() { timeval start, end; unsigned int testTime = 0; unsigned int compareTime = 0; SecurePrim<unsigned int> testValue(0); unsigned int compareValue = 0; for(int i=0; i < 100000; i++) { gettimeofday(&start, NULL); testValue++; gettimeofday(&end, NULL); testTime += (end.tv_usec - start.tv_usec); } for(int i=0; i < 100000; i++) { gettimeofday(&start, NULL); compareValue++; gettimeofday(&end, NULL); compareTime += (end.tv_usec - start.tv_usec); } return double(testTime) / double(compareTime); }
std::vector<std::pair<int, int>> ImmediateBestValueStrategy1::place(GameState state, int numTroops) { if (beVerbose) std::cout << "________________________________" << std::endl; if (beVerbose) std::cout << "ImmediateBestValueStrategy1 " << myPlayerNumber << " is placing " << numTroops << " troops." << std::endl; if (beVerbose) state.display(); std::vector<std::pair<int, int>> actions; //First, make sure there are no regions with only one troop std::vector<int> myRegions = state.getRegionsOwnedByPlayer(myPlayerNumber); for (int i=0; i<myRegions.size() && numTroops>0; i++) { if (state.getRegionInfo(myRegions[i]).second == 1) { if (beVerbose) std::cout<<"Placing a troop at "<<myRegions[i]<<" to meet the minimum."<<std::endl; actions.push_back(std::pair<int,int>(myRegions[i],1)); numTroops--; } } //Now, get a list of all exposed borders, sorted by danger, and give them troops, if needed if (numTroops>0) { std::vector<std::pair<int,int>> borders = state.getAllExposedBorders(myPlayerNumber, map); //border's second index is the number of enemy troops; change it to the amount of danger for (int i=0; i<borders.size(); i++) { int danger = borders[i].second - state.getRegionInfo(borders[i].first).second; borders[i].second = danger; } std::sort(borders.begin(), borders.end(), [](const std::pair<int, int> &lhs, const std::pair<int, int> &rhs) { return lhs.second < rhs.second; }); for (int i=borders.size()-1; i>=0 && numTroops>0; i--) { if (borders[i].second > dangerThreshold) { int troopsToSend = borders[i].second - dangerThreshold; if (troopsToSend > numTroops) troopsToSend = numTroops; if (beVerbose) std::cout<<"Placing "<<troopsToSend<<" troops at "<<borders[i].first<<" to get down to the danger threshold."<<std::endl; actions.push_back(std::pair<int,int>(borders[i].first,troopsToSend)); numTroops -= troopsToSend; } } } //if we still have troops, put them all in the border region that would score the highest points if (numTroops > 0) { //get a list of ideal targets std::vector<std::pair<int,double>> targets; std::vector<int> options = state.getAllNeighborsOfPlayer(myPlayerNumber, map); for (int i=0; i<options.size(); i++) targets.push_back(std::pair<int,double>(options[i], testValue(state, options[i]))); std::sort(targets.begin(), targets.end(), [](const std::pair<int, double> &lhs, const std::pair<int, double> &rhs) { return lhs.second < rhs.second; }); //and pick a neighboring spot to strengthen std::vector<int> theirNeighbors = map->getNeighborsOfRegion(targets[targets.size()-1].first); for (int i=0; i<theirNeighbors.size(); i++) { if (state.getRegionInfo(theirNeighbors[i]).first == myPlayerNumber) { if (beVerbose) std::cout<<"Placing "<<numTroops<<" troop at "<<theirNeighbors[i]<<" to prepare to attack "<<targets[targets.size()-1].first<<std::endl; actions.push_back(std::pair<int,int>(theirNeighbors[i],numTroops)); break; } } } if (beVerbose) std::cout << "________________________________" << std::endl; return actions; }
std::pair<int, int> ImmediateBestValueStrategy1::attack(GameState state) { if (beVerbose) std::cout << "________________________________" << std::endl; if (beVerbose) std::cout << "ImmediateBestValueStrategy1 "<<myPlayerNumber<<" is attacking" << std::endl; if (beVerbose) state.display(); //sort neighbors by weight=valueOfVictory*probabilyOfVictory std::vector<std::tuple<int,int,double>> weightedNeighborList; //first int is whereTo, second is whereFrom; double is the weight std::vector<int> myNeighbors = state.getAllNeighborsOfPlayer(myPlayerNumber, map); for (int i=0; i<myNeighbors.size(); i++) { double value = testValue(state,myNeighbors[i]); //we have the value, now we need the probability at the best attack-from location double probability = 0; int bestAttackFromSpot = -1; std::vector<int> theirNeighbors = map->getNeighborsOfRegion(myNeighbors[i]); for (int j=0; j<theirNeighbors.size(); j++) { if (state.getRegionInfo(theirNeighbors[j]).first == myPlayerNumber) { double currentProbability = Game::getProbabilityOfVictory(state.getRegionInfo(theirNeighbors[j]).second - 1, state.getRegionInfo(myNeighbors[i]).second); if (currentProbability>probability) { probability = currentProbability; bestAttackFromSpot = theirNeighbors[j]; } } } if (bestAttackFromSpot >= 0) { if (beVerbose) std::cout<<"Region "<<myNeighbors[i]<<" has weight "<<probability*value<<" when attacked from "<<bestAttackFromSpot<<std::endl; weightedNeighborList.push_back(std::tuple<int,int,double>(myNeighbors[i],bestAttackFromSpot,probability*value)); } } std::sort(weightedNeighborList.begin(), weightedNeighborList.end(), [](const std::tuple<int,int,double> &lhs, const std::tuple<int,int,double> &rhs) { return std::get<2>(lhs) < std::get<2>(rhs); }); //Now, we pick one to attack int chosen = weightedNeighborList.size() - 1; while (chosen >= 0) { if (beVerbose) std::cout<<"Considering attacking "<<std::get<0>(weightedNeighborList[chosen])<<std::endl; int attackFrom = std::get<1>(weightedNeighborList[chosen]); int armiesAtAttackFrom = state.getRegionInfo(attackFrom).second; if (armiesAtAttackFrom>10) //if we have at least 10 and are still in danger, the game is stagnating. Just let it happen, danger or no break; if (armiesAtAttackFrom>2) { int danger = 0 - armiesAtAttackFrom; std::vector<int> localNeighbors = map->getNeighborsOfRegion(attackFrom); for (int k=0; k<localNeighbors.size(); k++) { if (state.getRegionInfo(localNeighbors[k]).first != myPlayerNumber) danger += state.getRegionInfo(localNeighbors[k]).second; } if (danger < dangerThreshold) break; } chosen--; } //And attack it if (chosen >= 0) { int attackTo = std::get<0>(weightedNeighborList[chosen]); int attackFrom = std::get<1>(weightedNeighborList[chosen]); if (beVerbose) std::cout << "Attacking " << attackTo << " with " << attackFrom << std::endl; if (beVerbose) std::cout << "________________________________" << std::endl; return std::pair<int,int>(attackFrom,attackTo); } else { if (beVerbose) std::cout<<"Not attacking."<<std::endl; if (beVerbose) std::cout << "________________________________" << std::endl; return std::pair<int,int>(-1,-1); } }