Example #1
0
  std::pair< std::list< BWTA::BaseLocation* >, double > ScoutManager::getShortestPath( BWTA::BaseLocation* currentBaseLocation, std::set< BWTA::BaseLocation* > &baseLocations )
  {
    std::pair< std::list< BWTA::BaseLocation* >, double > shortestPath;
    shortestPath.second = 0;
    // base case
    if ( baseLocations.size() == 1 )
    {
      shortestPath.first.push_back( *baseLocations.begin() );
      shortestPath.second = currentBaseLocation->getGroundDistance( *baseLocations.begin() );
      return shortestPath;
    }
    // Try traveling to each base location first
    for( BWTA::BaseLocation* bl : baseLocations )
    {
      // Travel to bl first
      baseLocations.erase( bl );

      // Get the best path of the remaining base locations
      std::pair< std::list< BWTA::BaseLocation* >, double > pathResult = getShortestPath( bl, baseLocations );
      baseLocations.insert( bl );

      // Prepend the first base location of the path and add the distance to the first base location to the length
      pathResult.first.push_front( bl );
      pathResult.second += currentBaseLocation->getGroundDistance( bl );

      // Update shortest path if we've found a shorter path
      if ( pathResult.second < shortestPath.second || shortestPath.first.empty() )
      {
        shortestPath = pathResult;
      }
    }
    return shortestPath;
  }
Example #2
0
Data GarbageCentral::getRoute(vector<GarbageDeposit*> to_pick) {

	if (to_pick.size() <= 1)
		throw RouteMissingData();

	auto map = graph.dfs();
	auto map_it = begin(map);

	Route route;
	vector<GarbageDeposit*> missing_deposits;
	GarbageDeposit* gd = new GarbageDeposit();
	vector<GarbageDeposit*>::iterator gd1_it;

	while ( (gd1_it = find(begin(to_pick), end(to_pick), map_it->getPointer())) == end(to_pick) ) {
		map_it++;
	}

	*gd = **gd1_it;
	map_it++;



	while (to_pick.size() > 1) {

		if ( find(to_pick.begin(), to_pick.end(), map_it->getPointer()) != to_pick.end() ) {

			try {
				Section section = getShortestPath(gd, map_it->getPointer());
				updatePicks(to_pick, section);
				route.push_back(section);
				*gd = *section.first[section.first.size() - 1];
			} catch (NoOptimalPath &e) {
				missing_deposits.push_back(map_it->getPointer());

				for (unsigned i = 0; i < to_pick.size(); i++) {
					if ( *to_pick[i] == *(map_it->getPointer()) ) {
						to_pick.erase(begin(to_pick) + i);
						break;
					}
				}
			}
		}

		map_it++;
	}

	delete gd;
	return make_pair(route, missing_deposits);
}
Example #3
0
  std::pair< std::list< BWTA::BaseLocation* >, double > ScoutManager::getScoutPath( std::set< BWTA::BaseLocation* > baseLocations )
  {
    std::pair< std::list< BWTA::BaseLocation* >, double > shortestPath;
    shortestPath.second = 0;

    BWTA::BaseLocation* start = BWTA::getStartLocation( BWAPI::Broodwar->self() );
    baseLocations.erase( start );

    if ( baseLocations.empty() )
    {
      // No base locations, the shortest path is empty
      return shortestPath;
    }

    return getShortestPath( start, baseLocations );
  }
Example #4
0
void myGraph::subPathes(set<int> &preNodeSet, vector<vector<int>> &Edge_t, set<int>& nodeSet, vector<int> &nodeRanks)
{
	float maxlength=-100000;
	int maxid=-1;
	vector<vector<vector<int>>> maxPath;	
	bool haveHead=false;
	set<int> headSet, endSet;
		
	for(int i=0; i<Edge_t.size(); i++)
	{
		headSet.insert(Edge_t[i][0]); endSet.insert(Edge_t[i][1]);
	}		
	for(set<int>::iterator it=nodeSet.begin(); it!=nodeSet.end(); it++)			
	{    
		int start=*it;
		if(headSet.find(start)!=headSet.end() && endSet.find(start)==endSet.end()) //a top node
		{
			int length;
			vector<vector<vector<int>>> path = getShortestPath(start, length, Edge_t); //edgeSet, nodeSet); 					
			if(length > maxlength)
			{
				maxlength = length;
				maxid = start;
				maxPath=path;
			}
			haveHead=true;
		}
	}	
	if(!haveHead)
	for(set<int>::iterator it=nodeSet.begin(); it!=nodeSet.end(); it++)			
	{    
		int start=*it;
		{
			int length;
			vector<vector<vector<int>>> path = getShortestPath(start, length, Edge_t); //edgeSet[sid], nodeSet); 					
			if(length > maxlength)
			{
				maxlength = length;
				maxid = start;
				maxPath=path;
			}
			haveHead=true;
		}
	}	
	//initial rank
	//get the rank by redo the shortest path search for the minimal span tree
	//Graph initG;
	vector<vector<int>> Edge_i=maxPath[0];
	for(int k=0; k<Edge_i.size(); k++)
	{
		//if(Edge[k][0]==etemp[0] || Edge[k][1]==etemp[1])
		//add_edge(maxPath[k][0], maxPath[k][1], 1, initG);
		Edge_i[k].push_back(1);
	}
	int length;
	vector<vector<vector<int>>> initPath = getShortestPath(maxid, length, Edge_i);//, edgeSet[sid], nodeSet); 
	//iniPath should have rank info
	vector<int> nodeRanks_t(nodeRanks.size(),20000);

	nodeRanks_t[maxid]=0;
	for(int k=0; k<initPath[0].size(); k++)
	{
		int v=initPath[0][k][1], d=initPath[0][k][initPath[0][k].size()-1];
		nodeRanks_t[v]=d;
	}						
	if(initPath[0].size()!=Edge_i.size())
	{
		//error
	}

	//reduce the searched graph
	Edge_t=maxPath[1];
	for(int i=0; i<maxPath[0].size(); i++)
	{
		preNodeSet.insert(maxPath[0][i][0]);  preNodeSet.insert(maxPath[0][i][1]);
	}
	if(preNodeSet.size()==nodeSet.size())
	{
		nodeSet.clear();
	}
	else
	{
		for(int i=0; i<Edge_t.size(); i++)
	    {
		  nodeSet.insert(Edge_t[i][0]);  nodeSet.insert(Edge_t[i][1]);
	    }
	}
	//reassign rank level to nodeRanks_t
	int markid=-1, markLevel=20000, dL=20000;
	for(int i=0; i<nodeRanks_t.size(); i++)
	{
	   if(nodeRanks_t[i]!=20000 && nodeRanks[i]!=20000 && nodeRanks_t[i]!=nodeRanks[i])
	   {
	       markid=i;  
		   dL = nodeRanks[i]-nodeRanks_t[i];
		   markLevel=nodeRanks_t[i];
	   }
	}
	if(markid>=0)
	for(int i=0; i<nodeRanks_t.size(); i++)
	{
	   if(nodeRanks_t[i]!=20000)
	   {
	       nodeRanks_t[i]=nodeRanks_t[i] + dL;
	   }
	}
	int negFlag=0;
	for(int i=0; i<nodeRanks.size(); i++)
	{
	   if(nodeRanks[i]==20000  && nodeRanks_t[i]!=20000)
	   {
	       nodeRanks[i]=nodeRanks_t[i];
		   if(nodeRanks_t[i]<negFlag)
			{
				negFlag=nodeRanks_t[i];
		   }
	   }
	}	
	if( negFlag<0)
	{
	    negFlag = -negFlag;
		for(int i=0; i<nodeRanks.size(); i++)
		{
			if(nodeRanks[i]!=20000)
			   nodeRanks[i]= nodeRanks[i]+negFlag;
		}
	}
}
Example #5
0
int main(int argv, char * argc[]) {
    int routeNum = 0, r;
    r = scanf("%d", &routeNum);
    int i, j, size;
    char routeChar;
    char route[256];
    memset(ICColSize, 0, 26 * 26 * sizeof(int));
    memset(SD, -1, 26 * 26 * 26 * 26 * sizeof(int));
    memset(SP, -1, 26 * 26 * 26 * 26 * sizeof(int));

    for (i = 0; i < routeNum; i++) {
        r = scanf("%s", route);
        size = strlen(route);
        struct Station * tempRoute[26 * 26];
        int tempRouteSize = 0;
        if (DEBUG) {
            printf("\nget route:%s\n", route);
        }
        routeChar = route[0];
        for (j = 2; j < size;) {
            if (route[j] == '=') {
                if (ICColSize[ICRowSize] == 0) {
                    IC[ICRowSize][ICColSize[ICRowSize]++] = alloc(routeChar, route[j - 1]);
                }
                IC[ICRowSize][ICColSize[ICRowSize]++] = alloc(route[j + 1], route[j + 2]);
                j += 3;
                if (j >= size) {
                    ICRowSize++;
                }

            } else {
                if (ICColSize[ICRowSize] > 0) {
                    ICRowSize++;
                }
                if (j == size - 1 && route[2] == route[j]) {
                    tempRoute[0]->left = tempRouteSize - 1;
                    tempRoute[tempRouteSize - 1]->right = tempRouteSize - 1;
                } else {
                    tempRoute[tempRouteSize++] = alloc(routeChar, route[j]);
                }
                j++;
            }
        }
        for (j = 0; j < tempRouteSize; j++) {
            S[sSize++] = tempRoute[j];
        }
    }

    setDistanceMatrix();
    findAllLeastDistance();

    if (DEBUG) {
        printInfo();
    }

    char pair[4];
    for (r = scanf("%s", pair); strcmp(pair, "#") != 0; r = scanf("%s", pair)) {
        if (DEBUG) {
            printf("pair:%s\n", pair);
        }
        int s1, s2;
        for (i = 0; i < sSize; i++) {
            if (S[i]->route == pair[0] && S[i]->name == pair[1]) {
                s1 = i;
            } else if (S[i]->route == pair[2] && S[i]->name == pair[3]) {
                s2 = i;
            }
        }
        char sPath[1024];
        getShortestPath(sPath, s1, s2);
        printf("%3d: %s\n", SD[s1][s2], sPath);
    }
    return 0;
}
Example #6
0
void PathObj::calculatePath() 
{
	path = getShortestPath(start, end);
	finished = true;
}
Example #7
0
bool AStar::getShortestPath(int _start, int _end){

  Node goal = G.allNodes[_end]; 
  Node start =G.allNodes[_start];
  getShortestPath(start,goal);
}