基于谷歌地图的Dijkstra算法水路路径规划

  最终效果图如下:

还是图、邻接表,可以模拟出几个对象=》节点、边、路径。三个类分别如下:

Node 节点:

using System;
using System.Collections.Generic;

namespace Road.Plan
{
    public class Node
    {
        private string id;
        private IList<Edge> edgeList;

        public double Lat
        {
            get;
            set;
        }

        public double Lng
        {
            get;
            set;
        }
        
        public Node(string nid)
        {
            id = nid;
            edgeList = new List<Edge>();
        }

        public string Id
        {
            get
            {
                return id;
            }
        }

        public IList<Edge> EdgeList
        {
            get
            {
                return edgeList;
            }
        }
    }
}

  Edge 边:

using System;
using System.Web.Script.Serialization;

namespace Road.Plan
{
    public class Edge
    {
        [ScriptIgnore]
        public Node StartNode
        {
            get;
            set;
        }
        [ScriptIgnore]
        public Node EndNode
        {
            get;
            set;
        }
        public int Weight
        {
            get;
            set;
        }
    }
}

  Graph 图:

using System;
using System.Collections.Generic;

namespace Road.Plan
{
    /// <summary>
    /// 由于边存在节点里了,邻接表的方式的图就这么简单
    /// </summary>
    public class Graph
    {
        public List<Node> NodeList = new List<Node>();
    }
}

  路径Path:

using System;
using System.Collections.Generic;

namespace Road.Plan
{
    public class Path
    {
        //起始点 到 这个点
        public string CurrentNodeId;
        //该点是否已经计算
        public bool IsProcessed = false;
        //路径 权重合计
        public int Weight = 99999999;
        //路径表示
        public List<Node> PathNodeList = new List<Node>();
    }
}

  路径规划辅助类:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Road.Plan
{
    /// <summary>
    /// 计算最短路径帮助类
    /// </summary>
    public class CaculateHelper
    {
        private Dictionary<string, Path>  dicPath = new Dictionary<string, Path>();
        public Dictionary<string, Path> DicPath
        {
            get
            {
                return dicPath;
            }
        }

        public void IniFirstNode(Graph graph, string StartNodeId)
        {
            Node OriginNode = null;
            foreach (Node node in graph.NodeList)
            {
                if (node.Id == StartNodeId)
                {
                    OriginNode = node;
                }
                else
                {
                    Path path = new Path();
                    path.CurrentNodeId = node.Id;
                    dicPath.Add(path.CurrentNodeId, path);  //初始化A->到所有边都是默认的Path 99999999
                }
            }

            //如果有A直接进入的边,则设置为相应权重值,和记录下路径
            foreach (Edge edge in OriginNode.EdgeList)
            {
                Path path = new Path();
                path.CurrentNodeId = edge.EndNode.Id;
                path.Weight = edge.Weight;
                path.PathNodeList.Add(edge.StartNode);
                dicPath[path.CurrentNodeId] = path;
            }
        }

        public Node GetFromNodeMinWeightNode(Graph graph)
        {
            Node CNode = null;
            KeyValuePair<string, Path> KVPPath = dicPath.Where(m => !m.Value.IsProcessed).OrderBy(m => m.Value.Weight).FirstOrDefault();
            if (KVPPath.Key != null)
            {
                CNode = graph.NodeList.FirstOrDefault(m => m.Id == KVPPath.Value.CurrentNodeId);
            }
            return CNode;
        }

        /// <summary>
        /// 计算最短权值路径
        /// </summary>
        public void CatelateMinWeightRoad(Graph graph, string StartNodeId)
        {
            //取从第一个点出发,最小权值且未被访问果的节点的点
            Node CNode = GetFromNodeMinWeightNode(graph);
            //这段代码是核心 循环每个顶点,看看经过该顶点是否会让权值变小,如果会则存起此路径。直到再未访问过的点
            while (CNode != null)
            {
                Path CurrentPath = dicPath[CNode.Id];
                foreach (Edge edge in CNode.EdgeList)
                {
                    if (edge.EndNode.Id == StartNodeId)
                    {
                        continue;
                    }
                    Path TargetPath = dicPath[edge.EndNode.Id];

                    int tempWeight = CurrentPath.Weight + edge.Weight;
                    if (tempWeight < TargetPath.Weight)
                    {
                        TargetPath.Weight = tempWeight;
                        TargetPath.PathNodeList.Clear();

                        for (int i = 0; i < CurrentPath.PathNodeList.Count; i++)
                        {
                            TargetPath.PathNodeList.Add(CurrentPath.PathNodeList[i]);
                        }

                        TargetPath.PathNodeList.Add(CNode);
                    }
                }

                //标志为已处理
                dicPath[CNode.Id].IsProcessed = true;
                //再次获取权值最小的点
                CNode = GetFromNodeMinWeightNode(graph);
            }
        }
    }
}

  此处需要1个Controller、3个Action、1个页面。

  第一步,打开地图、并初始化好“运算-图”。

  第二步,获取所有节点,并将节点在地图上显示出来。

  第三步,获取运算结果,并在地图上根据计算结果将线划出来。

  Controller代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Road.Plan;

namespace Road.Controllers
{
    public class HomeController : Controller
    {
        static Graph graph = new Graph();

        public ActionResult GetAllNodes()
        {
            return Json(graph.NodeList, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Index()
        {
            graph.NodeList.Clear();

            #region 初始化航路-图

            Node Node1 = new Node("1");
            Node1.Lat = 23.1589850342836;
            Node1.Lng = 112.7859878540039;
            graph.NodeList.Add(Node1);

            Node Node2 = new Node("2");
            Node2.Lat = 23.136255816129122;
            Node2.Lng = 112.79937744140625;
            graph.NodeList.Add(Node2);

            Node Node3 = new Node("3");
            Node3.Lat = 23.11447003284563;
            Node3.Lng = 112.79869079589844;
            graph.NodeList.Add(Node3);

            Node Node4 = new Node("4");
            Node4.Lat = 23.142885569598484;
            Node4.Lng = 112.80890464782715;
            graph.NodeList.Add(Node4);

            Node Node5 = new Node("5");
            Node5.Lat = 23.144621879424374;
            Node5.Lng = 112.81577110290527;
            graph.NodeList.Add(Node5);

            Node Node6 = new Node("6");
            Node6.Lat = 23.151566893799817;
            Node6.Lng = 112.82074928283691;
            graph.NodeList.Add(Node6);

            Node Node7 = new Node("7");
            Node7.Lat = 23.15551276434145;
            Node7.Lng = 112.82984733581543;
            graph.NodeList.Add(Node7);

            Node Node8 = new Node("8");
            Node8.Lat = 23.1545657660099;
            Node8.Lng = 112.84452438354492;
            graph.NodeList.Add(Node8);

            Node Node9 = new Node("9");
            Node9.Lat = 23.167507497056675;
            Node9.Lng = 112.81705856323242;
            graph.NodeList.Add(Node9);

            //***************** 1 Node *******************
            //1 -> 2
            Edge aEdge1 = new Edge();
            aEdge1.StartNode = Node1;
            aEdge1.EndNode = Node2;
            aEdge1.Weight = 1;
            Node1.EdgeList.Add(aEdge1);

            //***************** 2 Node *******************
            //2 -> 3
            Edge bEdge3 = new Edge();
            bEdge3.StartNode = Node2;
            bEdge3.EndNode = Node3;
            bEdge3.Weight = 1;
            Node2.EdgeList.Add(bEdge3);

            //2 -> 1
            Edge bEdge1 = new Edge();
            bEdge1.StartNode = Node2;
            bEdge1.EndNode = Node1;
            bEdge1.Weight = 1;
            Node2.EdgeList.Add(bEdge1);

            //2 -> 4
            Edge bEdge4 = new Edge();
            bEdge4.StartNode = Node2;
            bEdge4.EndNode = Node4;
            bEdge4.Weight = 1;
            Node2.EdgeList.Add(bEdge4);


            //***************** 3 Node *******************
            //3 -> 2
            Edge cEdge2 = new Edge();
            cEdge2.StartNode = Node3;
            cEdge2.EndNode = Node2;
            cEdge2.Weight = 1;
            Node3.EdgeList.Add(cEdge2);

            //***************** 4 Node *******************
            //4 -> 2
            Edge dEdge2 = new Edge();
            dEdge2.StartNode = Node4;
            dEdge2.EndNode = Node2;
            dEdge2.Weight = 1;
            Node4.EdgeList.Add(dEdge2);

            //4 -> 5
            Edge dEdge5 = new Edge();
            dEdge5.StartNode = Node4;
            dEdge5.EndNode = Node5;
            dEdge5.Weight = 1;
            Node4.EdgeList.Add(dEdge5);

            //***************** 5 Node *******************
            //5 -> 6
            Edge eEdge6 = new Edge();
            eEdge6.StartNode = Node5;
            eEdge6.EndNode = Node6;
            eEdge6.Weight = 1;
            Node5.EdgeList.Add(eEdge6);

            //5 -> 4
            Edge eEdge4 = new Edge();
            eEdge4.StartNode = Node5;
            eEdge4.EndNode = Node4;
            eEdge4.Weight = 1;
            Node5.EdgeList.Add(eEdge4);

            //***************** 6 Node *******************
            //6 -> 5
            Edge fEdge5 = new Edge();
            fEdge5.StartNode = Node6;
            fEdge5.EndNode = Node5;
            fEdge5.Weight = 1;
            Node6.EdgeList.Add(fEdge5);

            //6 -> 7
            Edge fEdge7 = new Edge();
            fEdge7.StartNode = Node6;
            fEdge7.EndNode = Node7;
            fEdge7.Weight = 1;
            Node6.EdgeList.Add(fEdge7);

            //***************** 7 Node *******************
            //7 -> 6
            Edge gEdge6 = new Edge();
            gEdge6.StartNode = Node7;
            gEdge6.EndNode = Node6;
            gEdge6.Weight = 1;
            Node7.EdgeList.Add(gEdge6);

            //7 -> 8
            Edge gEdge8 = new Edge();
            gEdge8.StartNode = Node7;
            gEdge8.EndNode = Node8;
            gEdge8.Weight = 1;
            Node7.EdgeList.Add(gEdge8);

            //7 -> 9
            Edge gEdge9 = new Edge();
            gEdge9.StartNode = Node7;
            gEdge9.EndNode = Node9;
            gEdge9.Weight = 1;
            Node7.EdgeList.Add(gEdge9);

            //***************** 8 Node *******************
            //8 -> 7
            Edge hEdge7 = new Edge();
            hEdge7.StartNode = Node8;
            hEdge7.EndNode = Node7;
            hEdge7.Weight = 1;
            Node8.EdgeList.Add(hEdge7);

            //***************** 9 Node *******************
            //9 -> 7
            Edge iEdge7 = new Edge();
            iEdge7.StartNode = Node9;
            iEdge7.EndNode = Node7;
            iEdge7.Weight = 1;
            Node9.EdgeList.Add(iEdge7);

            #endregion

            return View();
        }

        /// <summary>
        /// 计算起始点,结束点的最短路径
        /// </summary>
        /// <param name="StartNodeId"></param>
        /// <param name="EndNodeId"></param>
        /// <returns></returns>
        public ActionResult GetWaterWay(string StartNodeId, string EndNodeId)
        {
            CaculateHelper CH = new CaculateHelper();
            //第一步,初始化初始化源点 A 到 其他各点的 权重以及 路径(完成 A->B A->C A->E A->D 边权重,与A无直接边的则默认99999999)
            CH.IniFirstNode(graph, StartNodeId);
            //第二步,从权重最小的点开始,一直到权值最大的点
            CH.CatelateMinWeightRoad(graph, StartNodeId);
            
            //组合返回值
            Path ShowPath = CH.DicPath[EndNodeId];
            ShowPath.PathNodeList.Add(graph.NodeList.First(m => m.Id == EndNodeId));    //补上结束点
            return Json(ShowPath.PathNodeList);
        }
    }
}

  页面HTML代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Complex icons</title>
    <style>
        html, body, #map-canvas {
            height: 100%;
            margin: 0px;
            padding: 0px;
        }
    </style>
    <script src="/jquery-1.10.2.min.js"></script>
    <script src="http://ditu.google.cn/maps/api/js?sensor=false" type="text/javascript"></script>

    <script>
        var map;
        var lat = 23.144621879424374;
        var lng = 112.81577110290527;

        var myLatlng = new google.maps.LatLng(lat, lng);


        //添加一个标记
        function addMarker(lat, lng, title) {
            var LatLng = new google.maps.LatLng(lat, lng);
            var marker = new google.maps.Marker({
                map: map,
                position: LatLng,
                title: title
            });
            return marker;
        }

        function LatLng(lat, lng) {
            return {
                lat: lat,
                lng: lng
            };
        }

        //线条选项
        function lineOption() {
            var lineOption = {
                strokeColor: "#FF0000",
                strokeOpacity: 1.0,
                strokeWeight: 2,
                coordinates: []
            };
            return lineOption;
        }

        //画线
        function addLine(lineOption) {
            var linecoordinates = [];
            for (var i = 0; i < lineOption.coordinates.length; i++) {
                linecoordinates.push(new google.maps.LatLng(lineOption.coordinates[i].lat, lineOption.coordinates[i].lng));
            }

            //显示线
            var line = new google.maps.Polyline({
                path: linecoordinates,
                strokeColor: lineOption.strokeColor,
                strokeOpacity: lineOption.strokeOpacity,
                strokeWeight: lineOption.strokeWeight,
                map: map
            });
            return line;
        }

        var MarkerId = 1;
        //初始化谷歌地图
        function initialize() {
            //设置地图中心
            var mapOptions = {
                zoom: 12,
                center: myLatlng
            }
            map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

            //google.maps.event.addListener(map, 'click', function (e) {
            //    var str = $("#position").html();
            //    var MID = MarkerId++;
            //    addMarker(e.latLng.lat(), e.latLng.lng(), MID);
            //    $("#position").html(str + " " + MID + " " + e.latLng.lat() + " " + e.latLng.lng() + "<br/>");
            //});

            $.ajax({
                url: "/Home/GetAllNodes",
                dataType: "json",
                type: "post",
                success: function (data) {
                    for (var i = 0; i < data.length; i++)
                    {
                        addMarker(data[i].Lat, data[i].Lng, data[i].Id);
                    }
                }
            })

            $.ajax({
                url: "/Home/GetWaterWay",
                dataType: "json",
                type: "post",
                data:{
                    StartNodeId: "1",
                    EndNodeId: "9"
                },
                success: function (data) {
                    var lo = lineOption();
                    lo.strokeWeight = 4;
                    lo.strokeColor = "Green";
                    lo.strokeOpacity = 0.8;
                    //用返回的路线画线
                    for (var i = 0; i < data.length; i++)
                    {
                        lo.coordinates.push(LatLng(data[i].Lat, data[i].Lng));
                    }
                    addLine(lo);
                }
            })
        }

        //监听页面事件,当页面加载完毕,加载谷歌地图
        google.maps.event.addDomListener(window, 'load', initialize);

    </script>
</head>
<body>
    <div id="map-canvas" style="height:600px;"></div>
    <div id="position"></div>
</body>
</html>
posted on 2016-04-27 15:57  逆心 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/kissdodog/p/5439217.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 基于Dijkstra算法的自动驾驶汽车路径规划的参考文献相对较多,以下列举了其中几篇: 1. 贺道辉, 周国亮, 于树青, & 纪其伟. (2018). 基于Dijkstra 算法的最佳路径规划及仿真研究. 计算机技术与发展, 28(2), 185-188. 这篇论文介绍了基于Dijkstra算法的最佳路径规划的基本原理,重点讨论了在自动驾驶汽车中应用该算法进行路径规划的可行性,并通过仿真研究验证了算法的有效性。 2. 郭宇明, 唐炎辉, & 雷林. (2019). 基于Dijkstra算法的自动驾驶汽车路径规划研究. 智能计算机与应用, (9), 237-239. 这篇论文探讨了基于Dijkstra算法的自动驾驶汽车路径规划,在考虑到实时交通情况和车辆行驶特性的基础上,提出了一种改进的Dijkstra算法,以提高路径规划的效率。 3. 王伟峰, 龙腾飞, & 黄翔. (2019). 基于改进Dijkstra算法的自动驾驶路径规划. 机械与电子, (24), 66. 这篇论文在基于Dijkstra算法路径规划的基础上,针对自动驾驶汽车路径规划中存在的问题,提出了一种改进的Dijkstra算法。通过引入权重和约束条件,优化路径规划结果,并提高了规划速度。 4. 张敏, 张长宁, & 彭云. (2017). 基于Dijkstra算法的自动驾驶路径规划研究. 机械设计与制造, (10), 27-28. 这篇论文研究了基于Dijkstra算法的自动驾驶路径规划,通过对路网图进行建模,并利用Dijkstra算法寻找最短路径,实现了自动驾驶汽车的高效路径规划。 以上是其中几篇关于基于Dijkstra算法的自动驾驶汽车路径规划的参考文献。这些研究为自动驾驶汽车的路径规划提供了理论支持和实践指导,为实现安全、高效的自动驾驶出行做出了贡献。 ### 回答2: Dijkstra算法是一种用于在加权图中寻找最短路径的经典算法。它的应用非常广泛,其中之一就是自动驾驶汽车路径规划。 在自动驾驶汽车路径规划中,Dijkstra算法可以用于确定汽车从起点到终点的最短路径。该算法基于图的搜索和权重计算,通过不断更新节点之间的最短距离和路径来找到最佳路径。 有许多文献可以作为基于Dijkstra算法的自动驾驶汽车路径规划的参考。以下是其中一些重要的文献: 1. Dijkstra, E.W. (1959). A note on two problems in connection with graphs. In Numerische Mathematik (pp. 269–271). Springer Berlin Heidelberg. 这是Dijkstra算法最初提出的经典文献之一,介绍了该算法的基本原理和应用。 2. Celebi, M.E., Alhajj, R. (2008). An efficient algorithm for finding the shortest path in transportation networks. Journal of Advanced Transportation, 42(4), 411-430. 这篇文章介绍了一种高效的改进Dijkstra算法,特别适用于自动驾驶汽车路径规划中的大规模交通网络。 3. Han, Z., Tang, T., Bai, X., Chen, Y., Huang, H., & Deng, Z. (2017). A Modified Dijkstra Algorithm for Shortest Path Computation in Large-Scale Networks. IEEE Transactions on Intelligent Transportation Systems, 18(5), 1124-1134. 该文献提出了一种改进的Dijkstra算法,以应对自动驾驶汽车路径规划中的大规模网络。 4. Bhatia, M., & Jain, R. (2018). Improved Dijkstra Algorithm for Vehicle Navigation Systems. International Journal of Scientific Research in Computer Science, Engineering, and Information Technology, 4(1), 115-120. 这篇文章提出了一种改进的Dijkstra算法,以加速自动驾驶汽车的导航系统。 以上是一些基于Dijkstra算法的自动驾驶汽车路径规划的参考文献,它们对于理解和应用该算法于车辆路径规划具有重要意义。 ### 回答3: 基于Dijkstra算法的自动驾驶汽车路径规划在近年来得到了广泛的研究和应用。下面是几篇相关的参考文献。 1. “A Dijkstra-based Approach for Path Planning of Self-driving Cars” - 这篇文献提出了一种基于Dijkstra算法的自动驾驶汽车路径规划方法。作者通过改进Dijkstra算法,将交通状态和车辆动态考虑进去,并利用实时的交通数据来更新路径规划,以确保行驶的安全和效率。 2. “Improved Dijkstra Algorithm for Autonomous Vehicle Path Planning” - 这篇文献在Dijkstra算法的基础上进行了改进,以适应自动驾驶汽车路径规划的需要。作者提出了一种优化的数据结构和算法,通过减少计算时间和空间复杂度,提高了路径规划的效率和准确性。 3. “Dijkstra-based Path Planning for Autonomous Vehicles in Dynamic Environments” - 这篇文献针对自动驾驶汽车在动态环境中的路径规划问题,提出了一种基于Dijkstra算法的解决方案。作者通过引入时空扩展图和权重函数,使得路径规划可以考虑交通状况和车辆运动的变化,从而实现安全和高效的驾驶。 4. “Traffic-aware Dijkstra Algorithm for Real-time Path Planning of Autonomous Vehicles” - 这篇文献中,作者提出了一种针对实时的自动驾驶汽车路径规划问题的Dijkstra算法版本。通过分析交通流量和路况信息,设计了一种基于交通感知的加权函数,使路径规划更加适应实际道路情况,并提高了系统的响应速度和准确性。 这些参考文献提供了关于基于Dijkstra算法的自动驾驶汽车路径规划的理论基础和实际应用方法。研究者们通过改进算法和引入新的因素,不断提高路径规划的效率和安全性,使得自动驾驶技术在道路上更有吸引力和可行性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值