예제 #1
0
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);
	}
}