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