void updateBackTrackKey(int index, vecPair &keyProb) { if (keyProb.size() != 0) { std::sort(keyProb.begin(), keyProb.end(), sort_pred()); vecPair::iterator it = keyProb.begin(); K[index + 3] = (int) (it->first); } }
void ShortestDistance::writeXML(map<int, Vertex *> &dataMap, map<int, string> &carrierNames) { out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl; map<int,Vertex*>::const_iterator mt = dataMap.begin(); for(; mt != dataMap.end(); ++mt) { out << "<Vertex>" << endl; out << "\t<Source Airport ID>" << mt->second->airport_id << "</Source Airport ID>" << endl; out << "\t<Source Airport Name>" << mt->second->airport_name << "</<Source Airport Name>" << endl; out << "\t<Source Airport IATA>" << mt->second->airport_iata << "</Source Airport IATA>" << endl; out << "\t<Source Airport City>" << mt->second->airport_city << "</Source Airport City>" << endl; out << "\t<Source Airport Country>" << mt->second->airport_country << "</Source Airport Country>" << endl; out << "\t<Source Airport Latitude>" << mt->second->lat << "</Source Airport Latitude>" << endl; out << "\t<Source Airport Longitude>" << mt->second->longi << "</Source Airport Longitude>" << endl; out << "\t<Edges>" << endl; sort(mt->second->routes.begin(),mt->second->routes.end(), sort_pred()); vector<Routes*>::const_iterator it = mt->second->routes.begin(); for(; it != mt->second->routes.end(); ++it) { out << "\t\t<Edge>" << endl; out << "\t\t\t<Destination Airport ID>" << (*it)->nextAirport << "</Destination Airport ID>" << endl; out << "\t\t\t<Destination Airport Name>" << (*it)->destination_airport_name << "</Destination Airport Name>" << endl; out << "\t\t\t<Destination Airport IATA>" << (*it)->destination_airport_iata << "</Destination Airport IATA>" << endl; out << "\t\t\t<Destination Airport City>" << (*it)->destination_airport_city << "</Destination Airport City>" << endl; out << "\t\t\t<Destination Airport Country>" << (*it)->destination_airport_country << "</Destination Airport Country>" << endl; out << "\t\t\t<Carrier>" << (*it)->airline_iata << "</Carrier>" << endl; out << "\t\t\t<Carrier Name>" << carrierNames[(*it)->airline_id] << "</Carrier Name>" << endl; out << "\t\t\t<Distance>" << (*it)->distance << "</Distance>" << endl; out << "\t\t\t<Carrier ID>" << (*it)->airline_id << "</Carrier ID>" << endl; out << "\t\t</Edge>" << endl; } out << "\t</Edges>" << endl; out << "</Vertex>" << endl; } out.close(); }
//parse void data::parse(){ cout << "parse" << endl; //prep the particiapnt xml file participantXML.pushTag("Document"); //iterate through all keywords in the XML file and generate a list of all the keywords used and their frequencies //add keyword to keyword list if it is not already in the list //go to first row with data in it XML.pushTag("Document"); // XML.pushTag("Worksheet"); // XML.pushTag("Table"); int numRows = XML.getNumTags("Row"); for (int rCnt = 1;rCnt < numRows ;rCnt++){ XML.pushTag("Row",rCnt); //now i need to iterate through all the cells of the row and look for the one with the correct index of 86 // int numCells = XML.getNumTags("Cell"); // bool collectKeywords = false; // for (int cCnt = 0;cCnt< numCells;cCnt++){ // if(XML.getAttribute("Cell", "ss:Index", 0, cCnt) == 86){ // collectKeywords = true; //} //get all keywords in cells starting at cell 86 // if(collectKeywords){ // XML.pushTag("Cell",cCnt);//col with keywords // string kws = XML.getValue("Field_85","");//this should return the keywords that need to be split at the return addKeywords(XML.getValue("Field_85","")); addKeywords(XML.getValue("Field_86","")); addKeywords(XML.getValue("Field_87","")); addKeywords(XML.getValue("Field_88","")); XML.popTag(); // } // } // XML.popTag(); } //keyword list has been generated, let's see it! /*cout << endl << endl << "KEYWORD LIST: " << endl; for (int kCnt = 0; kCnt<keywords.size(); kCnt++){ //cout << keywords[kCnt].first << ", "<< keywords[kCnt].second << endl; }*/ //sort the list struct sort_pred { bool operator()(const std::pair<string,int> &left, const std::pair<string,int> &right) { return left.second > right.second; } }; std::sort(keywords.begin(), keywords.end(), sort_pred()); //std::sort(keywords.begin(), keywords.end(), boost::bind(&std::pair<string, int>::second, _1) < boost::bind(&std::pair<string, int>::second, _2)); //now return the sorted list... cout << endl << endl << "KEYWORD LIST: " << endl; for (int kCnt = 0; kCnt<keywords.size(); kCnt++){ cout << keywords[kCnt].first << ", "<< keywords[kCnt].second << endl; } }
/** * Main function **/ int main() { srand (time(NULL)); /* Tree x; x.set(0,4); x.set(1,1); x.set(2,9); x.set(3,2); x.set(4,14); x.set(5,8); x.set(6,13); x.set(7,0); x.set(8,3); x.set(9,12); x.set(10,10); x.set(11,5); x.set(12,7); x.set(13,6); x.set(14,11); //getMax at 1 -> 14 std::cout << x.getMax(0) << std::endl; std::cout << x.getMin(0) << std::endl; std::cout << x.fitness(0) << std::endl; std::cout << x.fitness(3) << std::endl; */ mc=0; std::array<Tree, 200> population; //init population for(int i=0; i<100; i++) { Tree x; x.init(); population[i] = x; } int count = 0; MUTATION_RATE=5; //alter population double start = omp_get_wtime(); while(population[0].fitness(0) > 0.000) { count++; std::list<std::pair<int,double>> fitnesses; //create offspring for(int j=0; j<100; j++) { int p1 = rand() % 100; int p2 = rand() % 100; Tree kid = population[p1].combine(population[p2]); kid.mutate(); population[100+j] = kid; } //calc fitness for(int k=0; k<200; k++) { auto x = std::make_pair(k, population[k].fitness(0)); fitnesses.push_back(x); } //sort by value fitnesses.sort(sort_pred()); //remove old population std::array<Tree, 100> newpop; for(int j=0; j<100; j++) { int newPos = fitnesses.back().first; fitnesses.pop_back(); newpop[j] = population[newPos]; } //clear old population and take the new ones if(count%10000==0) { double end = omp_get_wtime(); std::cout << "Iteration count: " << count << " in " << (end-start) << std::endl; std::cout << "Best fitness: " << population[0].fitness(0) << std::endl; std::cout << "Weakest fitness: " << population[199].fitness(0) << std::endl; std::cout << "Mutation rate: " << MUTATION_RATE << std::endl; std::cout << "Mutation count: " << mc << std::endl; population[0].print(); std::cout << std::endl; start = omp_get_wtime(); } for(int i=0; i<100; i++) { population[i] = newpop[i]; } } std::cout << "found solution with " << count << " iterations.n"; population[0].print(); return 0; }