//////////////////////////////////////////////////////////////// // FINDING MINIMUM UNKNOWN DISTANCE // //////////////////////////////////////////////////////////////// HeapObj Router::FindMinUnknown(BinaryHeap<HeapObj> &heap){ while(vArray[(heap.findMin()).index]->known) { //while the min vertex is unknown, we ignore it and keep popping heap.deleteMin(); } return heap.findMin(); }
int main(int argc, char **argv) { BinaryHeap<int> testHeap; std::string input; std::string numbers = "0123456789"; std::cout << "Input vals to add to BinaryHeap (val1 val2 val3 val4 ... end)\n"; while (std::cin >> input) { // if 'std::String::find_first_not_of' doesnt find anything, then it outputs max_int (long int) // since any num higher than 1 will be set to true, add 1 to max int to start at 0(false) if (input.find_first_not_of(numbers)+1 ) { if (input == "end") { break; } else { std::cerr << input << " not recognized\n"; continue; } } // end if int num = std::atoi(input.c_str()); // transform string into number, would have normally use boost::lexical_cast though testHeap.insert(num); } std::cout << "Input vals to remove from BinaryHeap (val1 val2 val3 val4 ... end)\n"; while (std::cin >> input) { // if 'std::String::find_first_not_of' doesnt find anything, then it outputs max_int (long int) // since any num higher than 1 will be set to true, add 1 to max int to start at 0(false) if (input.find_first_not_of(numbers)+1 ) { if (input == "end") break; else { std::cerr << input << " not recognized\n"; continue; } } // end if int num = std::atoi(input.c_str()); testHeap.removeKey(num); } std::cerr << "\n"; while (!testHeap.isEmpty()) { std::cerr << testHeap.findMin() << " "; testHeap.deleteMin(); } std::cerr << "\n"; return 0; }
//Will pick a soldier from our remaining soldiers alive. // void processTurn(BinaryHeap<soldier>& heap, vector<int>& soldiers, int& remaining_Soldiers) { int position_of_Soldier; if(remaining_Soldiers == 1) position_of_Soldier = 0;//A condition to ensure kill a soldier is not called when only one soldier is left else position_of_Soldier = rand() % remaining_Soldiers; bool target_Condition; soldier nextUp = heap.findMin(); //Return the smallest value; our next up. target_Condition = heap.interact_with_Soldier(nextUp.return_Faction(), soldiers[position_of_Soldier]);//False if alive, true if killed if(target_Condition && nextUp.return_Faction() ) kill_a_Soldier(soldiers, remaining_Soldiers, position_of_Soldier);//Will removed the soldier if dead. else if(target_Condition && !(nextUp.return_Faction()) ) {//Will invigorate if a soldier is killed, AND the soldier target was a Persian kill_a_Soldier(soldiers, remaining_Soldiers, position_of_Soldier);//Remove the soldier as per usual invigorate(heap, soldiers, remaining_Soldiers);//function invigorate will go through the entire array to invigorate soldiers. } }
int main(int argc, char *argv[] ) { srand (time(NULL)); clock_t start; double seconds_since_start; BinaryHeap<soldier> heap; soldier nextUp; vector<int> alive_Spartans, alive_Persians; int total_Spartans, total_Persians, increase_Amount, time_Elapsed = 0, spartan_Count = 0, persian_Count = 0; int numberOfSpartanVictories = 0, numberOfPersianVictories = 0; double sum_Spartans = 0, sum_Persians = 0, sum_Time = 0, spartan_Squared= 0, persian_Squared = 0, time_Squared = 0; double spartan_Final_Average = 0, persian_Final_Average = 0, time_Final_Average = 0, omega_Spartan = 0, omega_Time = 0; long double omega_Persian = 0; vector<double> soldier_count_Spartans, soldier_count_Persians, time_Count; total_Spartans = atoi(argv[2]); total_Persians = atoi(argv[3]); spartan_Count = total_Spartans; persian_Count = total_Persians; int numberOfTimes; numberOfTimes = atoi(argv[1]); cout << "You have set the simulations to run for: " << numberOfTimes << " times." << endl; cout << "You have selected the number of spartans to be: " << spartan_Count << endl; cout << "You have selected the number of persians to be: " << persian_Count << endl; if(total_Spartans == 0 || total_Persians == 0){ cout << "what are you doing" << endl; exit(1); } string outputFile; cout << "Enter the name of the file to output results to: " << endl; cin >> outputFile; ofstream file(outputFile); //cout << "How many times would you like to simulate the battle?" << endl; //char begin; cout << "You have set the simulations to run for: " << numberOfTimes << " times." << endl; cout << "You have selected the number of spartans to be: " << spartan_Count << endl; cout << "You have selected the number of persians to be: " << persian_Count << endl; //cout << "Enter any character to proceed..." << endl; //cin >> numberOfTimes; for(int i = 0; i < numberOfTimes; i++) { start = clock(); generate_Soldiers(heap, total_Spartans, total_Persians, alive_Spartans, alive_Persians); //invigorate(heap, alive_Spartans, total_Spartans); //cout << "The ID's of all our Spartans: " << endl; //printOutIDs(alive_Spartans, total_Spartans); //cout << "The ID's of all our Persians: " << endl; //printOutIDs(alive_Persians, total_Persians); cout << "Our heap reports these soldiers: " << endl; //heap.printAllSoldiers(); cout << "------------------------Let the battle begin!-------------------------" << endl; while( spartan_Count > 0 && persian_Count > 0) { //cout << "Spartans left: " << spartan_Count << endl; //cout << "Persians left: " << persian_Count << endl; nextUp = heap.findMin();//Peek at the first soldier. This is the soldier whose turn is next time_Elapsed = nextUp.return_actionTime(); //cout << "Next up: " << "soldier number: " << nextUp.return_iD() << endl; //cout << "This soldier's turn is at: " << nextUp.return_actionTime() << endl; //if(nextUp.return_Faction() == true) //cout << "A Spartan! " << endl; //else //cout << "A Persian!" << endl; if(nextUp.return_Faction() == true) processTurn(heap, alive_Persians, persian_Count); else processTurn(heap, alive_Spartans, spartan_Count); //The soldier's key is edited; his next turn is determined by adding a random value to his turn time atm. if(nextUp.return_Faction() == true) heap.increase_by_iD(nextUp.return_iD(), rand() % 7 + 1); else if(nextUp.return_Faction() == false) heap.increase_by_iD(nextUp.return_iD(), rand() % 51 + 10); //cout << "After the event, our heap looks like: " << endl; //cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" << endl; //heap.printAllSoldiers(); //cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" << endl; } //cout << "our heap reports these soldiers: " << endl; //heap.printAllSoldiers(); cout << "Spartans remaining: " << spartan_Count << endl; sum_Spartans = sum_Spartans + spartan_Count; soldier_count_Spartans.push_back(spartan_Count); cout << "Persians remaining: " << persian_Count << endl; sum_Persians = sum_Persians + persian_Count; soldier_count_Persians.push_back(persian_Count); cout << "The ID's of all our Spartans: " << endl; file << "Output for simulation: " << i << endl; file << "-----------------------" << endl; file << "Remaining soldiers: " << endl; file << "Spartans: " << spartan_Count << " Persians: " << persian_Count << endl; //printOutIDs(alive_Spartans, total_Spartans); cout << "The ID's of all our Persians: " << endl; //printOutIDs(alive_Persians, total_Persians); cout << "*****************************************************" << endl; cout << "******************THE WINNER IS..********************" << endl; if(spartan_Count == 0){ cout << "The Persians are victorious!" << endl; numberOfPersianVictories++; file << "The Persians have won simulation: " << i << endl; } else{ cout << "The Spartans are victorious!" << endl; numberOfSpartanVictories++; file << "The Spartans have won simulation: " << i << endl; } heap.makeEmpty(); spartan_Count = total_Spartans; persian_Count = total_Persians; alive_Spartans.clear(); alive_Persians.clear(); seconds_since_start = (double)(clock() - start)/CLOCKS_PER_SEC; cout << "This battle took: " << seconds_since_start << " seconds." << endl; file << "Battle " << i << " took a total of: " << seconds_since_start << " seconds." << endl; time_Count.push_back(seconds_since_start); sum_Time += seconds_since_start; file << "------------------" << endl; } file << "~~~~~~~~~~~~~~~~~~~~~SIMULATIONS HAVE ENDED~~~~~~~~~~~~~~~~~~" << endl; cout << "The Spartans have won: " << numberOfSpartanVictories << " time(s)" << endl; cout << "The Persians have won: " << numberOfPersianVictories << " time(s)" << endl; spartan_Final_Average = arithMean(sum_Spartans, numberOfTimes); persian_Final_Average = arithMean(sum_Persians, numberOfTimes); time_Final_Average = arithMean(sum_Time, numberOfTimes); cout << "Average number of living Spartans: " << spartan_Final_Average << endl; cout << "Average number of living Persians: " << persian_Final_Average << endl; cout << "Average battle length: " << time_Final_Average << endl; omega_Spartan = standard_deviation(soldier_count_Spartans, numberOfTimes, spartan_Final_Average); omega_Persian = standard_deviation(soldier_count_Persians, numberOfTimes, persian_Final_Average); omega_Time = standard_deviation(time_Count, numberOfTimes, time_Final_Average); cout << "Std. dev of living Spartans: " << omega_Spartan << endl; cout << "Std. dev of living Persians: " << omega_Persian << endl; cout << "Std. dev of battle length: " << omega_Time << endl; file << "TOTAL BATTLES WON BY EACH TEAM: " << endl; file << "Spartan victories: " << numberOfSpartanVictories << endl; file << "Persian victories: " << numberOfPersianVictories << endl; file << "STATISTICS:" << endl; file << "Average number of living Spartans: " << spartan_Final_Average << endl; file << "Average number of living Persians: " << persian_Final_Average << endl; file << "Average battle length: " << time_Final_Average << endl; file << "Std. Dev of living Spartans: " << omega_Spartan << endl; file << "Std. Dev of living Persians: " << omega_Persian << endl; file << "Std. Dev of battle length: " << omega_Time << endl; //cout << "I will show you where each soldier is in the heap: " << endl; //heap.determine_hash_positions(10); //heap.remove(5, removed); //cout << removed.return_iD() << endl; //cout << removed.return_actionTime() << endl; //Test min remove snippet /*soldier min; while(!heap.isEmpty()){ cout << "1 if not empty, 0 if empty: " << !heap.isEmpty() << endl; min = heap.findMin(); cout << "This soldier's ID is: " << new_Mook.return_iD() << endl; if(new_Mook.return_Faction() == true) cout << "Tis a Spartan!" << endl; else cout << "tis a persian" << endl; heap.deleteMin(min); }*/ cout << "Done!" << endl; return 0; }