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); } }
list<TPoint> reconstruire(Visited aMap,TPoint a) { if (aMap.find(make_pair(a.x,a.y))!=aMap.end()) { TPoint z(aMap[make_pair(a.x,a.y)].first,aMap[make_pair(a.x,a.y)].second); list<TPoint> p = reconstruire(aMap, z); p.push_back(a); return p; } else { list<TPoint> z; z.push_back(a); return z; } }
void Dijkstra::pushChild(char id, const Element& parentNode, Visited& visited, Priority& priority, Graph &network,double startTime) { double cost= findCost(id,parentNode,network,startTime); // get the cost of the node...from the unique function if (visited.find(id) == visited.end()) { // Element is not in visited map -> add it //visited.insert(std::pair<char,Element>(id,Element(id,cost,parentNode.id))); - we are going to check if visited later Element newnode; newnode.id=id;newnode.cost=cost;newnode.parent=parentNode.id; newnode.parents.push_back(parentNode.id); priority.push(newnode); } else { // if the current cost is less than or equal to the elements cost then we may need to update the map if (cost == visited.at(id).cost){ // we have found the node at the same cost... we need to put it as a parent as well visited[id].parents.push_back(parentNode.id); } else if (cost < visited.at(id).cost){ // need to look at node again visited[id].cost=cost; priority.push(visited[id]); } } }