void Dijkstra::calcPath(Workload& request, Graph& network, Stats& stats) { bool pathFound=false; bool exitLoop=false; Element current; // current Element Visited visited; // Visited list Priority items; // priority queue double previousCost=-1; // 1. Get initial Start node current.id=request.origin; current.cost=0; current.parent=request.origin; // TODO - possible problem... items.push(current); // 2. Loop until we have found a valid path //while (!exitLoop && !items.empty()){ while (!items.empty()){ // Pop from the priority queue current=items.top(); //std::cout<<"Current: "<<current.id<<" | Parent: "<<current.parent<< " | Goal:"<<request.destination<<std::endl; items.pop(); // remove the element from the list if (pathFound){ if(current.cost>previousCost){ exitLoop=true; // we want to exit the main loop break; } } previousCost=current.cost; if (visited.find(current.id)==visited.end()) { //visited.find(current.id)==visited.end() // Check if desired node if (current.id==request.destination){ // Found the node! pathFound=true; //std::cout<<std::endl<<request.origin<<request.destination<<std::endl; exitLoop=true; // TODO - remove me in future but currently bug when this is not here ????? map appears to be losing the parent of N for some weird reason.. } else { // push the valid children which have bandwidth and are not allready visited onto the priority queue network.begin(current.id,request.time); while (!network.end()) { pushChild(network.nextChild(),current,visited,items,network,request.time); } } // Append the current node to the visited list visited.insert(std::pair<char,Element>(current.id,current)); // be careful that the node is not allready in the visited list..possibly } } // 3. Choose one valid path at random and backtrack -> calculate and append statistics... if (pathFound){ // we have a valid path... -> backtrack to allocate resources backTrack(request.origin,request.destination,request.time ,request.timeDuration ,visited,network,stats); } else { //Path was impossible -> add blocked circuit stats.addCircuit(false); } }
/* * Function: allocates resources in the Graph moving along the path from start to finish in the visited map */ void Dijkstra::backTrack(char start, char finish, double startTime,double duration, Visited& visited,Graph &network, Stats &stats) { bool atEnd=false; char parentNode; bool blocked=false; char currentNode; int hops=0; double propDelay=0; //int access=0; std::vector<std::pair<char,char> > links; // stores the links we have visited.. std::vector <char> visit; // 1. While we have not reached the finish node, allocate resources currentNode=finish; srand(time(NULL)); std::vector<char >::iterator it2; //std::cout<<finish; while (!atEnd){ // 1. Get the current Node // access=rand()%(visited.at(currentNode).parents.size()); // it2=std::find(visit.begin(),visit.end(),visited.at(currentNode).parents.at(access)); // int count=0; // while (it2!=visit.end()){ // access=rand()%(visited.at(currentNode).parents.size()); // it2=std::find(visit.begin(),visit.end(),visited.at(currentNode).parents.at(access)); // if (count > 50){ // break; // } // count++; // } //parentNode=visited.at(currentNode).parents.front(); // TODO - modify this to be random access //parentNode=visited.at(currentNode).parents.at(access); // Completely random access parentNode=visited.at(currentNode).parent; // TODO - modify this to be random access from the vector as shown above... //std::cout<<parentNode; if (parentNode == start){ // we have reached the last link -> add last connection and exit loop atEnd=true; } if (network.getUsage(currentNode,parentNode,startTime) == 1.0){ // we have a blocked node //std::cout<<std::endl<<"Blocked Path! | Current: "<<currentNode<<" Parent: "<<parentNode<< std::endl; blocked=true; } links.push_back(std::pair<char,char>(currentNode,parentNode)); visit.push_back(currentNode); //3. Increment number hops and propagation delay of link hops++; // increment number of hops propDelay += network.getDelay(currentNode,parentNode); currentNode=parentNode;// make our current Node now the chosen parentNode } //std::cout<<std::endl; if (blocked){ // Assign usage to respective units and update states stats.addCircuit(false); } else { for (__gnu_cxx::__normal_iterator<std::pair<char, char>*, std::vector<std::pair<char, char>, std::allocator<std::pair<char, char> > > > it=links.begin();it!=links.end();++it){ network.setUsage(it->first,it->second,startTime,duration); } stats.addCircuit(true); stats.updateHopAverage(hops); stats.updatePropAverage(propDelay); } }