Ejemplo n.º 1
0
void natural_loop_dfs(jit::vector<Block*>& out, Visited& visited, Block* blk) {
  if (visited.test(blk->id())) return;
  visited.set(blk->id());
  blk->forEachPred([&] (Block* pred) {
    natural_loop_dfs(out, visited, pred);
  });
  out.push_back(blk);
}
Ejemplo n.º 2
0
list<TPoint> PathFindAStar::computePath(TPoint  startPoint, TPoint endPoint)
{
    TPoint current;
    myCloseList.clear();
    myOpenList.push_back(startPoint);
    computeHeuristics(endPoint);
    Visited myNavigated;
    myNavigated.clear();
    myMap->getGraph()[startPoint.x][startPoint.y].poids=0;
    myMap->getGraph()[startPoint.x][startPoint.y].heuristic=
	myMap->getGraph()[startPoint.x][startPoint.y].poids+computeHeuristic(startPoint,endPoint);
    while(myOpenList.size()!=0)
    {

	current=minHeuristic(myOpenList);
	if((current.x==endPoint.x) && (current.y==endPoint.y))
	{
	    return reconstruire(myNavigated,endPoint);
	}
	removeFromList(myOpenList,current);
	myCloseList.push_front(current);
	list<TPoint> neibs= myMap->getGraph()[current.x][current.y].points;
	list<TPoint>::iterator p;
        orderNeibs(neibs,endPoint);
	for(p=neibs.begin();p!=neibs.end();p++)
	{
		
	    if(belongs(myCloseList,*p))
	    {
	    }
	    else
	    {
		int tentative=myMap->getGraph()[current.x][current.y].poids+1;
				
		if((!belongs(myOpenList,*p))||(tentative<=myMap->getGraph()[(*p).x][(*p).y].poids))
		{


		    myNavigated[make_pair((*p).x,(*p).y)]=make_pair(current.x,current.y);
		    myMap->getGraph()[(*p).x][(*p).y].poids=tentative;
		    myMap->getGraph()[(*p).x][(*p).y].heuristic=tentative+computeHeuristic((*p),endPoint);
		    if(!belongs(myOpenList,*p))
		    {
			myOpenList.push_front(*p);	
		    }

		}
			
	    }
		
	}
    }
    list<TPoint> b;
    return b;

	
}
Ejemplo n.º 3
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);
	}
}
Ejemplo n.º 4
0
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;
    }
}
Ejemplo n.º 5
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]);
		}
	}
}
Ejemplo n.º 6
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);
	}
}