C#,人工智能,机器人,路径规划,A*(AStar Algorithm)算法、源代码及计算数据可视化

C#,人工智能,机器人,路径规划,A*(AStar Algorithm)算法、源代码及计算数据可视化_第1张图片

Peter Hart 

C#,人工智能,机器人,路径规划,A*(AStar Algorithm)算法、源代码及计算数据可视化_第2张图片

Nils Nilsson 

Bertram Raphael 

参考:

C#,人工智能(AI)机器人路径规划(Path Planning)的ARA*(Anytime Replanning A* Algorithm)算法与源程序https://blog.csdn.net/beijinghorn/article/details/125464754

一、A*算法概述

A*算法最初由斯坦福研究院(Stanford Institute)的 Peter Hart,Nils Nilsson,Bertram Raphael 发表于1968年,属于Dijkstra算法的拓展之一。

论文原文https://www.cs.auckland.ac.nz/courses/compsci709s2c/resources/Mike.d/astarNilsson.pdf

A*算法是常用的路径规划、最短路径搜索、图遍历算法。

借助於启发函数,A*同时拥有较好的性能与准确度,因而备受人工智能、机器人研究者们的欢迎。

除了 A* ,现在发展了 D*,D*Lite。。。诸多改进的算法,后续都会给出 C# 源代码。

A* 的算法不复杂,属于入门级别的。

二、计算结果的动画演示

三、A* 源代码

1、节点信息 NodeInfo

#define ANIMATE

using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Legalsoft.Truffer.Algorithm
{
    /// 
    /// 节点信息
    /// 
    public class NodeInfo
    {
        /// 
        /// 前一节点
        /// 
        public NodeInfo Last { get; set; } = null;
        /// 
        /// 邻接距离(权值)
        /// 
        public int ManhattanWeight { get; set; } = 0;
        /// 
        /// 曼哈顿距离(权值)
        /// 
        public int RemoteWeight { get; set; } = 0;
        /// 
        /// 综合距离(权值)
        /// 
        public int TotalWeight { get; set; } = 0;
        /// 
        /// 行号
        /// 
        public int Column { get; set; } = 0;
        /// 
        /// 列号
        /// 
        public int Row { get; set; } = 0;
        /// 
        /// 是否为墙(节点)?
        /// 
        public bool IsWall { get; set; } = false;
        /// 
        /// 是否被加入到了close中
        /// 
        public bool InClose { get; set; } = false;
        /// 
        /// 开口节点?
        /// 
        public bool InOpen { get; set; } = false;
        /// 
        /// 路过?
        /// 
        public bool Visited { get; set; } = false;
        /// 
        /// 构造函数
        /// 
        /// 行号
        /// 列号
        /// 
        public NodeInfo(int y, int x, bool iswall)
        {
            Row = y;
            Column = x;
            IsWall = iswall;
        }
    }
}

2、地图信息MapInfo

#define ANIMATE

using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Legalsoft.Truffer.Algorithm
{
    /// 
    /// 地图信息(墙1与空0)
    /// 
    public class MapInfo
    {
        /// 
        /// 行数
        /// 
        public int Row { get; set; } = 0;
        /// 
        /// 列数
        /// 
        public int Column { get; set; } = 0;
        /// 
        /// 所有节点
        /// 
        public NodeInfo[,] Nodes { get; set; } = null;

        /// 
        /// 从文本创建地图
        /// 
        /// 
        public void FromBuffer(string buf)
        {
            buf = buf.Replace("\r", "").Trim();
            string[] xlines = buf.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            Nodes = new NodeInfo[xlines.Length, xlines[0].Trim().Length];
            Row = Nodes.GetLength(0);
            Column = Nodes.GetLength(1);
            int y = 0;
            foreach (string xu in xlines)
            {
                for (int x = 0; x < xu.Trim().Length; x++)
                {
                    NodeInfo node = new NodeInfo(y, x, Int32.Parse(xu.Trim().Substring(x, 1)) > 0);
                    Nodes[y, x] = node;
                }
                y++;
            }
        }

        /// 
        /// 从文件创建地图
        /// 
        /// 
        /// 
        public void FromFile(string filename)
        {
            try
            {
                string buf = File.ReadAllText(filename);
                FromBuffer(buf);
            }
            catch (Exception ex)
            {
                throw new Exception("");
            }
        }
        /// 
        /// this重载
        /// 
        /// 
        /// 
        /// 
        public NodeInfo this[int y, int x]
        {
            set { Nodes[y, x] = value; }
            get { return Nodes[y, x]; }
        }
        /// 
        /// 第一个节点
        /// 
        public NodeInfo First
        {
            get { return Nodes[0, 0]; }
        }
        /// 
        /// 最后一个节点
        /// 
        public NodeInfo Last
        {
            get { return Nodes[Row - 1, Column - 1]; }
        }

        /// 
        /// 清除路径
        /// 
        public void ClearPath()
        {
            for (int i = 0; i < Row; i++)
            {
                for (int j = 0; j < Column; j++)
                {
                    this[i, j].Visited = false;
                }
            }
        }
    }
}

3、核心代码 AStar Kernal

#define ANIMATE

using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Legalsoft.Truffer.Algorithm
{
    public class AStar
    {
        /// 
        /// 开放列表
        /// 
        private List openList { get; set; } = new List();
        /// 
        /// 当前访问的节点
        /// 
        private NodeInfo curNode { get; set; } = null;
        /// 
        /// 起始节点
        /// 
        private NodeInfo startNode { get; set; } = null;
        /// 
        /// 终止节点
        /// 
        private NodeInfo endNode { get; set; } = null;
        /// 
        /// 地图
        /// 
        public MapInfo map { get; set; } = new MapInfo();

        /// 
        /// 构造函数
        /// 
        public AStar()
        {
        }

        /// 
        /// 节点加入列表,并计算相关权值
        /// 
        /// 
        private void AddToList(NodeInfo node)
        {
            if (node.IsWall) return;
            if (node.InClose) return;
            if (node.InOpen) return;
            openList.Add(node);
            node.InOpen = true;
            node.ManhattanWeight = curNode.ManhattanWeight + WeightOf(node, curNode);
            node.RemoteWeight = WeightOf(node, endNode);
            node.TotalWeight = node.ManhattanWeight + node.RemoteWeight;
            node.Last = curNode;
        }

        /// 
        /// 路径规划A*算法(最短路径A*算法)
        /// 默认左上角(0,0)为起点;右下角为终点;
        /// 
        /// 起点
        /// 终点
        public void Tracing(NodeInfo start = null, NodeInfo end = null)
        {
            List steps = new List() {
                new int[2] {  1, 1 },
                new int[2] {  0, 1 },
                new int[2] {  1, 0 },
                new int[2] {  0,-1 },
                new int[2] { -1, 0 },
                new int[2] {  1,-1 },
                new int[2] { -1, 1 },
                new int[2] { -1,-1 },
            };

            startNode = (start == null) ? map.First : start;
            endNode = (end == null) ? map.Last : end;

            // 将起点加入到开放列表中
            openList.Add(startNode);
            while (true)
            {
                openList.Sort(SortByCost);

                curNode = openList[0];
                openList.Remove(curNode);
                curNode.InOpen = false;
                curNode.InClose = true;

                // 终点?
                if (curNode == endNode)
                {
                    return;
                }

                MarkPath();
                map.ClearPath();
            }
        }

        /// 
        /// 节点1到节点2的Weight
        /// 
        /// 节点1
        /// 节点2
        /// 
        private int WeightOf(NodeInfo a, NodeInfo b)
        {
            // 直行步长
            double straightDistance = 1.0;
            // 斜行步长
            double diagonalDistance = 1.414;

            int deltaX = Math.Abs(a.Column - b.Column);
            int deltaY = Math.Abs(a.Row - b.Row);
            if (deltaX == deltaY)
            {
                return (int)(deltaX * diagonalDistance);
            }
            else if (deltaX < deltaY)
            {
                return (int)(deltaX * diagonalDistance + (deltaY - deltaX) * straightDistance);
            }
            else
            {
                return (int)(deltaY * diagonalDistance + (deltaX - deltaY) * straightDistance);
            }
        }

        /// 
        /// 排序的委托函数
        /// 
        /// 节点1
        /// 节点2
        /// 
        private int SortByCost(NodeInfo a, NodeInfo b)
        {
            if (a.TotalWeight.CompareTo(b.TotalWeight) != 0)
            {
                return (a.TotalWeight.CompareTo(b.TotalWeight));
            }
            else if (a.RemoteWeight.CompareTo(b.RemoteWeight) != 0)
            {
                return (a.RemoteWeight.CompareTo(b.RemoteWeight));
            }
            else
            {
                return 1;
            }
        }

        /// 
        /// 标记当前获取的路径
        /// 
        public void MarkPath()
        {
            NodeInfo cd = curNode;
            while (cd != null)
            {
                if (cd == startNode)
                {
                    break;
                }
                cd.Visited = true;
                cd = cd.Last;
            }
        }
    }
}

--------------------------------------------------------------------------------

POWER BY TRUFFER.CN

你可能感兴趣的:(C#算法演义,Algorithm,Recipes,C#,算法,路径规划,人工智能,图遍历)