vector<vertex_t> ShortestPath::dijkstra(string filename, vertex_t source_out, vertex_t dest_out)
{
     // remember to insert edges both ways for an undirected graph
    adjacency_list_t adjacency_list(REFERENCE_POINT_NUMBER);

    readFile(filename.c_str(), adjacency_list);
    int SOURCE_REVERSE, SOURCE = source_out;
    int DEST_REVERSE, DEST = dest_out;
    SOURCE_REVERSE = DEST;
    DEST_REVERSE = SOURCE;
 
    vector<weight_t> min_distance;
    vector<vertex_t> previous;
    vector<weight_t> min_distance_reverse;
    vector<vertex_t> previous_reverse;

    int dist;
    vector<vertex_t> realpath;

    DijkstraComputePaths(SOURCE, adjacency_list, min_distance, previous);
    DijkstraComputePaths(SOURCE_REVERSE, adjacency_list, min_distance_reverse, previous_reverse);
    cout << "min :" << min_distance[DEST] << endl;
    cout << "min_reverse : " << min_distance_reverse[DEST_REVERSE] << endl;

    vector<vertex_t> path;
    if(min_distance[DEST] > min_distance_reverse[DEST_REVERSE])
    {
        dist = min_distance_reverse[DEST_REVERSE];
        path = DijkstraGetShortestPathTo(DEST_REVERSE, previous_reverse);       
    }
    else
    {
        dist = min_distance[DEST];
        path = DijkstraGetShortestPathTo(DEST, previous);   

        for (int i = 0; i < path.size()/2; ++i)
        {
            vertex_t temp = path[i];
            path[i] = path[path.size() - 1 - i];
            path[path.size() - 1 - i] = temp;
        }
    }

    cout << "Distance : " << dist << endl;
    //printPath(path);
    return path;

}
Пример #2
0
void test_dijkstra()
{
    // remember to insert edges both ways for an undirected graph
    Landscape landscape(5, 5);

    for (int yi = 0; yi < landscape.height; yi++) {
        for (int xi = 0; xi < landscape.width; xi++) {
            QPoint coords = QPoint(xi, yi);

            if (xi == 2) {
                landscape.setTile(coords, Tile::WATER_TILE);
            } else {
                landscape.setTile(coords, Tile::GRASS_TILE);
            }

            std::cout << "+" << landscape.getTile(coords).walkSpeed() << std::endl;
        }
    }
    adjacency_list_t adjacency_list = make_graph(landscape);

    std::vector<weight_t> min_distance;
    std::vector<vertex_t> previous;

    QPoint start(0, 0);
    QPoint end(4, 0);

    DijkstraComputePaths(landscape.coordsToIndex(start),
                         adjacency_list, min_distance, previous);

    std::cout << "Distance from 0,0 to 11,5: " << min_distance[landscape.coordsToIndex(end)] << std::endl;
    std::list<vertex_t> path = DijkstraGetShortestPathTo(landscape.coordsToIndex(end), previous);
    std::cout << "Path : ";
    std::copy(path.begin(), path.end(), std::ostream_iterator<vertex_t>(std::cout, " "));
    std::cout << std::endl;
}
Пример #3
0
void CityUpdater::createPath(Location start, Location end, std::vector<Location> &path){
    // VOIR DIJKSTRA EN HAUT !!!!
    int startIndex = std::find(roadMap.begin(), roadMap.end(), cityMap->getCase(start)) - roadMap.begin();
    int endIndex = std::find(roadMap.begin(), roadMap.end(), cityMap->getCase(end)) - roadMap.begin();
    std::vector<weight_t> min_distance;
    std::vector<vertex_t> previous;
    DijkstraComputePaths(startIndex, adjacencyList, min_distance, previous);
    std::vector<vertex_t> tmpPath = DijkstraGetShortestPathTo(endIndex, previous);
    for (unsigned int i=1; i<tmpPath.size(); i++){
    //std::cout<<roadMap[tmpPath[i]]->getLocation().getRow()<<","<<roadMap[tmpPath[i]]->getLocation().getCol()<<std::endl;
        path.push_back(roadMap[tmpPath[i]]->getLocation());
    }
}
Пример #4
0
void HostileNPC::pathFinding(unsigned int destination)
{
	Path_To_Destination.clear();
	std::vector<double> min_distance;
	std::vector<int> previous;
	DijkstraComputePaths(convertSFMLToDijkstras(Body_Sprite.getPosition()), *(Current_Grid->getAdjList()), min_distance, previous);
	std::list<int> Temp = DijkstraGetShortestPathTo(destination, previous);
	unsigned int temp_size = Temp.size();
	for(unsigned int i = 0; i < temp_size; i++)
	{
		Path_To_Destination.push_back(convertDijkstrasToSFML(Temp.front()));
		Temp.pop_front();
	}
}
Пример #5
0
QVector<QPoint> Landscape::getPath(QPoint start, QPoint end) const
{
    std::vector<weight_t> min_distance;
    std::vector<vertex_t> previous;

    adjacency_list_t adj = make_graph(*this);
    DijkstraComputePaths(this->coordsToIndex(start), adj, min_distance, previous);
    std::list<vertex_t> path = DijkstraGetShortestPathTo(this->coordsToIndex(end), previous);

    if (min_distance[this->coordsToIndex(end)] > 2000000) {
        return QVector<QPoint>();
    }

    QVector<QPoint> retPath;
    foreach(vertex_t vx, path) {
        retPath.append(this->indexToCoords(vx));
    }
Пример #6
0
int initializeAdjacencyLists(bool isInside, int sourceNodeId, int destNodeId)
{
	/*****************************************IC MEKAN************************************/

	// remember to insert edges both ways for an undirected graph

	// 0 = A Robotik
	informations::adjacencyListIndoor[0].push_back(neighbour(1, 5));  // Robotik-Z02 = 3
	informations::adjacencyListIndoor[0].push_back(neighbour(10, 5)); // Robotik-X4 = 2
	// 1 = B Z02
	informations::adjacencyListIndoor[1].push_back(neighbour(0, 5)); // Z02-Robotik = 3
	informations::adjacencyListIndoor[1].push_back(neighbour(2, 5)); // Z02-arkaKapý = 3
	// 2 = C arkaKapi
	informations::adjacencyListIndoor[2].push_back(neighbour(1, 5)); // arkaKapi-Z04 = 2
	informations::adjacencyListIndoor[2].push_back(neighbour(3, 5)); // arkaKapi-Z02 = 3
	// 2 = D Z04
	informations::adjacencyListIndoor[3].push_back(neighbour(1, 5)); // Z04-arkaKapi = 2
	informations::adjacencyListIndoor[3].push_back(neighbour(3, 5)); // Z04-Z05 = 4
	// 3 = E Z05
	informations::adjacencyListIndoor[4].push_back(neighbour(2, 5)); // Z05-Z04 = 4
	informations::adjacencyListIndoor[4].push_back(neighbour(4, 5)); // Z05-X1 = 1
	// 3 = F Z05-Z10 arasýndaki köþe X1
	informations::adjacencyListIndoor[5].push_back(neighbour(2, 5)); // X1-Z05 = 1
	informations::adjacencyListIndoor[5].push_back(neighbour(4, 5)); // X1-Z10 = 3
	// 4 = G Z10
	informations::adjacencyListIndoor[6].push_back(neighbour(3, 5)); // Z10-X1 = 3
	informations::adjacencyListIndoor[6].push_back(neighbour(5, 5)); // Z10-X2 = 1
	// 4 = H Z10-Mikro arasi duz köse X2
	informations::adjacencyListIndoor[7].push_back(neighbour(3, 5)); // X2-Z10 = 1
	informations::adjacencyListIndoor[7].push_back(neighbour(5, 5)); // X2-X3 = 2
	// 5 = G Z10-Mikro arasi yan kose X3
	informations::adjacencyListIndoor[8].push_back(neighbour(4, 5)); // X3-X2 = 2
	informations::adjacencyListIndoor[8].push_back(neighbour(6, 5)); // X3-MikroLab = 2
	// 5 = H MikroLab
	informations::adjacencyListIndoor[9].push_back(neighbour(4, 5));  // MikroLab-X3 = 2
	informations::adjacencyListIndoor[9].push_back(neighbour(6, 5)); // MikroLab-WcKiz = 2
	// 10 = I WcKiz
	informations::adjacencyListIndoor[10].push_back(neighbour(9, 5));  // WcKýz-MikroLab = 2
	informations::adjacencyListIndoor[10].push_back(neighbour(0, 5)); // WcKýz-DevreLab = 0

	/*****************************************DIS MEKAN************************************/
	// 0 = BilMuh OnKapi Sol Kose
	informations::adjacencyListOutdoor[0].push_back(neighbour(1, 5));
	informations::adjacencyListOutdoor[0].push_back(neighbour(7, 5));
	informations::adjacencyListOutdoor[0].push_back(neighbour(15, 20));
	// 1 = Bil Muh On kapı
	informations::adjacencyListOutdoor[1].push_back(neighbour(0, 5));
	informations::adjacencyListOutdoor[1].push_back(neighbour(2, 5));
	// 2 = Bil Muh On kapı Sag kose
	informations::adjacencyListOutdoor[2].push_back(neighbour(1, 5));
	informations::adjacencyListOutdoor[2].push_back(neighbour(8, 10));
	informations::adjacencyListOutdoor[2].push_back(neighbour(3, 5));
	// 3 = Bil muh Sag Taraf
	informations::adjacencyListOutdoor[3].push_back(neighbour(2, 5));
	informations::adjacencyListOutdoor[3].push_back(neighbour(4, 5));
	// 4 =
	informations::adjacencyListOutdoor[4].push_back(neighbour(3, 5));
	informations::adjacencyListOutdoor[4].push_back(neighbour(5, 5));
	// 5 = Bil Muh Arka Taraf
	informations::adjacencyListOutdoor[5].push_back(neighbour(4, 5));
	informations::adjacencyListOutdoor[5].push_back(neighbour(6, 5));
	// 6 =
	informations::adjacencyListOutdoor[6].push_back(neighbour(5, 5));
	informations::adjacencyListOutdoor[6].push_back(neighbour(7, 5));
	// 7 = Bil muh sol taraf
	informations::adjacencyListOutdoor[7].push_back(neighbour(0, 5));
	informations::adjacencyListOutdoor[7].push_back(neighbour(6, 5));
	// 8 = Bil muh soldaki yol
	informations::adjacencyListOutdoor[8].push_back(neighbour(2, 10));
	informations::adjacencyListOutdoor[8].push_back(neighbour(9, 10));
	// 9 =
	informations::adjacencyListOutdoor[9].push_back(neighbour(8, 10));
	informations::adjacencyListOutdoor[9].push_back(neighbour(10, 20));
	// 10 = kütüphaneye giden uzun yol
	informations::adjacencyListOutdoor[10].push_back(neighbour(9, 20));
	informations::adjacencyListOutdoor[10].push_back(neighbour(11, 20));
	// 11 = kütüphaneden önceki köşe
	informations::adjacencyListOutdoor[11].push_back(neighbour(12, 10));
	informations::adjacencyListOutdoor[11].push_back(neighbour(17, 20));
	// 12 = kütüphane
	informations::adjacencyListOutdoor[12].push_back(neighbour(11, 10));
	informations::adjacencyListOutdoor[12].push_back(neighbour(13, 10));
	// 13 = biyoloji yolu
	informations::adjacencyListOutdoor[13].push_back(neighbour(12, 10));
	informations::adjacencyListOutdoor[13].push_back(neighbour(14, 10));
	// 14 = biyoloji - kimya
	informations::adjacencyListOutdoor[14].push_back(neighbour(13, 10));
	// 15 = Bil Müh sağdaki uzun yol
	informations::adjacencyListOutdoor[15].push_back(neighbour(0, 20));
	informations::adjacencyListOutdoor[15].push_back(neighbour(16, 20));
	// 16 =
	informations::adjacencyListOutdoor[16].push_back(neighbour(15, 20));
	informations::adjacencyListOutdoor[16].push_back(neighbour(17, 10));
	informations::adjacencyListOutdoor[16].push_back(neighbour(18, 10));
	// 17 =
	informations::adjacencyListOutdoor[17].push_back(neighbour(16, 20));
	informations::adjacencyListOutdoor[17].push_back(neighbour(11, 20));
	// 18 =
	informations::adjacencyListOutdoor[18].push_back(neighbour(16, 10));
	informations::adjacencyListOutdoor[18].push_back(neighbour(19, 10));
	// 19 =
	informations::adjacencyListOutdoor[19].push_back(neighbour(18, 10));
	/**************************************************************************************/
	vector<weight_t> min_distance;
	vector<vertex_t> previous;

	// Inside
	if (isInside) {
		cout << "Su anda bulunulan node : " << sourceNodeId << endl;
		cout << "Gidilecek node : " << destNodeId << endl;

		DijkstraComputePaths(sourceNodeId, informations::adjacencyListIndoor, min_distance, previous);
		cout << "Distance from " << sourceNodeId << " to " << destNodeId << "  ->  " << min_distance[destNodeId] << endl;
		list<vertex_t> path = DijkstraGetShortestPathTo(destNodeId, previous);
		cout << "Path : ";
		copy(path.begin(), path.end(), ostream_iterator<vertex_t>(cout, " "));
		cout << endl;
		list<vertex_t>::iterator it2 = path.begin();
		it2++;
		for (list<vertex_t>::iterator it = path.begin(); it != path.end(); ++it) {
			if (it == --path.end())
				break;
			else{
				cout << informations::directionMatrixIndoor[*it][*it2] << " - ";
				it2++;
			}
		}
	}
	// Outside
	else{
		cout << "Su anda bulunulan node : " << sourceNodeId << endl;
		cout << "Gidilecek node : " << destNodeId << endl;

		DijkstraComputePaths(sourceNodeId, informations::adjacencyListOutdoor, min_distance, previous);
		cout << "Distance from " << sourceNodeId << " to " << destNodeId << "  ->  " << min_distance[destNodeId] << endl;
		list<vertex_t> path = DijkstraGetShortestPathTo(destNodeId, previous);
		cout << "Path : ";
		copy(path.begin(), path.end(), ostream_iterator<vertex_t>(cout, " "));
		cout << endl;
		list<vertex_t>::iterator it2 = path.begin();
		it2++;
		for (list<vertex_t>::iterator it = path.begin(); it != path.end(); ++it) {
			if (it == --path.end())
				break;
			else{
				cout << informations::directionMatrixOutdoor[*it][*it2] << " - ";
				it2++;
			}
		}
	}
	return 0;
}