The practice of Dijkstra algorithm in global path planning of intelligent network-connected autonomous driving sandbox

In the field of intelligent connected smart transportation, global path planning is a crucial part of the autonomous driving system. This article will delve into the global path planning using the Dijkstra algorithm in the autonomous driving sandbox, and demonstrate its application in smart transportation scenarios through actual code.

Part One: Analysis of the Principle of Dijkstra’s Algorithm 

Dijkstra's algorithm is a greedy algorithm used to solve the single-source shortest path problem in graphs. Its main goal is to find the shortest path from the starting point to all other nodes in the graph.

  1. initialization

    First, we initialize two key data structures: an array ( ) that stores distances between nodes dist, and a priority queue ( ) that records the shortest path to visited nodes pq.

vector<int> dist(rows, INT_MAX); // 距离数组,初始值为无穷大
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; // 优先队列,用于按距离排序

     2. Set the starting point

        Set the distance from the starting point to 0 and add it to the priority queue.

dist[start] = 0;
pq.push({0, start});

      3. Iterative update

        During the iteration process, the node with the smallest current distance is taken out from the priority queue. For all neighbors of this node, if the path to reach them through the current node is shorter, their distances are updated and added to the priority queue.

while (!pq.empty()) {
    int u = pq.top().second;
    pq.pop();

    for (int v = 0; v < cols; ++v) {
        if (map[u][v] && dist[v] > dist[u] + map[u][v]) {
            dist[v] = dist[u] + map[u][v];
            pq.push({dist[v], v});
        }
    }
}

        In each iteration, the algorithm always selects the node with the smallest current distance, ensuring that distthe shortest distance from the known starting point to each node is stored in the array after each iteration.

      4. Final result

        Finally, distwhat is stored in the array is the shortest distance from the starting point to each node in the graph.

        The idea of ​​this greedy algorithm is to always choose the path that seems to be the best at the moment and update it iteratively until the shortest path is found. This iterative process ensures that the local optimal solution is selected at each step, and finally the global optimal solution is obtained.

// Dijkstra算法C++示例代码
#include <iostream>
#include <vector>
#include <queue>
#include <climits>

using namespace std;

typedef pair<int, int> iPair;

class Graph {
    int vertices;
    vector<vector<iPair>> adjList;

public:
    Graph(int v);
    void addEdge(int u, int v, int w);
    void dijkstra(int src);
};

Graph::Graph(int v) : vertices(v), adjList(v) {}

void Graph::addEdge(int u, int v, int w) {
    adjList[u].emplace_back(v, w);
    adjList[v].emplace_back(u, w);
}

void Graph::dijkstra(int src) {
    priority_queue<iPair, vector<iPair>, greater<iPair>> pq;
    vector<int> dist(vertices, INT_MAX);

    pq.push(make_pair(0, src));
    dist[src] = 0;

    while (!pq.empty()) {
        int u = pq.top().second;
        pq.pop();

        for (const auto& neighbor : adjList[u]) {
            int v = neighbor.first;
            int weight = neighbor.second;

            if (dist[v] > dist[u] + weight) {
                dist[v] = dist[u] + weight;
                pq.push(make_pair(dist[v], v));
            }
        }
    }

    // 输出最短路径
    cout << "Vertex \t Distance from Source\n";
    for (int i = 0; i < vertices; ++i)
        cout << i << "\t\t" << dist[i] << endl;
}

int main() {
    Graph g(6);

    g.addEdge(0, 1, 5);
    g.addEdge(0, 2, 1);
    g.addEdge(1, 2, 2);
    g.addEdge(1, 3, 6);
    g.addEdge(2, 3, 1);
    g.addEdge(2, 4, 4);
    g.addEdge(3, 4, 3);
    g.addEdge(4, 5, 2);

    g.dijkstra(0);

    return 0;
}

 

Part 2: Application of Dijkstra algorithm in autonomous driving sandbox

In the autonomous driving sandbox, the Dijkstra algorithm plays a key role and is responsible for global path planning. We will deeply explore the application of Dijkstra's algorithm in the sandbox and demonstrate its process of finding the optimal path in the simulation environment through actual code.

First, we need to build the data structure of the sandbox map. Here, we use a two-dimensional array to represent the map, with each element representing a node. The connection relationship between nodes is represented by edge weights, which reflect the distance or cost between nodes.

#include <iostream>
#include <vector>
#include <climits>
#include <queue>

using namespace std;

class AutopilotSandbox {
public:
    vector<vector<int>> map; // 二维数组表示地图
    int rows, cols;

    AutopilotSandbox(int r, int c) : rows(r), cols(c), map(r, vector<int>(c)) {}

    // 添加道路,设定权重
    void addRoad(int from, int to, int weight) {
        map[from][to] = weight;
        map[to][from] = weight; // 无向图
    }

    // Dijkstra算法实现全局路径规划
    vector<int> dijkstra(int start) {
        vector<int> dist(rows, INT_MAX);
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;

        dist[start] = 0;
        pq.push({0, start});

        while (!pq.empty()) {
            int u = pq.top().second;
            pq.pop();

            for (int v = 0; v < cols; ++v) {
                if (map[u][v] && dist[v] > dist[u] + map[u][v]) {
                    dist[v] = dist[u] + map[u][v];
                    pq.push({dist[v], v});
                }
            }
        }

        return dist;
    }
};

In the above code, we created an AutopilotSandbox class, which contains the storage of map data and the implementation of Dijkstra's algorithm.

Next, we apply Dijkstra's algorithm to the scene in the sandbox. To be more intuitive, we mark some key points on the map, such as the starting point, the ending point, and the nodes passed through. In practical applications, these nodes may correspond to road intersections, parking lots, charging stations, etc.

int main() {
    // 创建沙盘实例
    AutopilotSandbox sandbox(6, 6);

    // 添加道路和权重
    sandbox.addRoad(0, 1, 5);
    sandbox.addRoad(0, 2, 2);
    sandbox.addRoad(1, 3, 4);
    sandbox.addRoad(1, 4, 2);
    sandbox.addRoad(2, 4, 3);
    sandbox.addRoad(3, 5, 6);
    sandbox.addRoad(4, 5, 1);

    // 执行Dijkstra算法,获取最短路径
    vector<int> shortestPath = sandbox.dijkstra(0);

    // 输出最短路径
    cout << "Shortest Path from 0 to other vertices:\n";
    for (int i = 1; i < shortestPath.size(); ++i)
        cout << "To " << i << ": " << shortestPath[i] << " units\n";

    return 0;
}

In this example, we demonstrate the application of Dijkstra's algorithm in an autonomous driving sandbox. Readers can modify the structure and weight of the map to simulate different road conditions and gain an in-depth understanding of the principles of global path planning. Such practical examples help students better understand the application of algorithms in autonomous driving systems.

Part 3: Application of Dijkstra algorithm in practical cases

In actual cases, the application of Dijkstra's algorithm in the autonomous driving sandbox will be more specific. We will use a larger scenario to simulate an urban traffic network and demonstrate the application of Dijkstra's algorithm through a practical case.

 

First, we need a larger sandbox and a more complex map. To simplify the example, we only show a few key intersections in the city, each intersection represents a node. Road weights will reflect congestion in real urban traffic.

// 省略AutopilotSandbox类的定义,保留上述代码

int main() {
    // 创建城市交通沙盘实例
    AutopilotSandbox citySandbox(10, 10);

    // 添加城市道路和权重,模拟真实交通情况
    citySandbox.addRoad(0, 1, 5);
    citySandbox.addRoad(0, 2, 8);
    citySandbox.addRoad(1, 3, 4);
    citySandbox.addRoad(1, 4, 6);
    citySandbox.addRoad(2, 5, 7);
    citySandbox.addRoad(3, 6, 5);
    citySandbox.addRoad(4, 7, 3);
    citySandbox.addRoad(5, 8, 4);
    citySandbox.addRoad(6, 9, 6);
    citySandbox.addRoad(7, 9, 2);
    citySandbox.addRoad(8, 9, 7);

    // 执行Dijkstra算法,获取最短路径
    vector<int> shortestPath = citySandbox.dijkstra(0);

    // 输出最短路径
    cout << "Shortest Path from City Center (0) to other vertices:\n";
    for (int i = 1; i < shortestPath.size(); ++i)
        cout << "To " << i << ": " << shortestPath[i] << " units\n";

    // 模拟实际案例:选择最短路径经过的道路
    cout << "\nSimulating the actual case...\n";
    vector<int> actualPath = {0, 2, 5, 8, 9}; // 假设选择经过的节点

    // 输出实际案例的道路
    cout << "Actual Path: ";
    for (int node : actualPath)
        cout << node << " -> ";
    cout << "Destination\n";

    return 0;
}

        In this practical case, we simulate an urban transportation network and use Dijkstra's algorithm to find the shortest path from the city center to a target location. Subsequently, we simulated the roads that users choose the shortest path through through actual cases. The actual demonstration of this scenario helps students understand the application of algorithms in real environments, while providing more space for discussion and thinking.

        Through these three parts, readers will have a comprehensive understanding of the practice of Dijkstra algorithm in the intelligent network-connected smart transportation autonomous driving sandbox, laying a solid foundation for more in-depth learning and research.

Guess you like

Origin blog.csdn.net/yang8767/article/details/134956172