Java与路径搜索算法中的C#速度比较

时间:2016-12-21 15:02:14

标签: java c# algorithm performance

首先,我不确定这是否是发布此问题的正确位置,所以如果我错了,请移动它。感谢。

我有一个分配来比较Java和C#中的相同算法性能。该算法应该是A *搜索,但我认为我更像是洪水,但它运作良好,我不在这里修复它。首先,我将发布我在Java和C#中使用的代码,然后解释我得到的内容。

由于问题的主体限制为30000个字符并且我输入了更多,我不得不从Java和C#中删除函数readFile()以使其适合。

已更新

Jim Mischel指出我在C#版本中更新了哈希函数,使其与Java相同,从而提高了性能。

还要感谢Matt Timmermans,我意识到我一直在调试中运行C#(不考虑它的结果)并且更改为更多地释放性能。

C#版本:

文件:Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;

namespace A_Star_Compare
{
    class Program
    {
        static int StartX = 0;
        static int StartY = 0;
        static int TargetX = 0;
        static int TargetY = 0;
        static int Width = 0;
        static int Height = 0;
        static TimeSpan TotalTime = TimeSpan.Zero;
        static double[] TrialTimes = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        static Dictionary<int, List<int>> Obstacles = new Dictionary<int, List<int>>();

        static void Main(string[] args)
        {
            for (int z = 0; z < 10; z++)
            {
                int Index = 0;
                Console.WriteLine("z: " + z);
                for (int x = 0; x < 14; x++)
                {
                    if (x < 10)
                        Index += 10;
                    else
                        Index += 100;

                    string Line = string.Empty;
                    string FileName = "Maps-" + Index + ".txt";
                    TotalTime = TimeSpan.Zero;
                    readFile(FileName);
                    TrialTimes[x] += (double)TotalTime.TotalSeconds / 100;
                }
            }

            int Index0 = 0;

            for (int i = 0; i < 14; i++)
            {
                if (i < 10)
                    Index0 += 10;
                else
                    Index0 += 100;

                string FileName = "Maps-" + Index0 + ".txt";

                Console.WriteLine("{0} Map size: {1}*{2}. On average map solved in: {3}", FileName, Index0, Index0, (double)TrialTimes[i] / 10);
            }
        }

        static void measureTime()
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            Algorithm Solve = new Algorithm(StartX, StartY, TargetX, TargetY, Width, Height, Obstacles);
            Solve.FullSolve();

            stopwatch.Stop();

            TotalTime += stopwatch.Elapsed;
        } 
    }
}

文件:Algorithm.cs

using System.Collections.Generic;

namespace A_Star_Compare
{
    public class Node
    {
        public int X { get; set; }
        public int Y { get; set; }
        public int G { get; set; }
        public int F { get; set; }
        public int H { get; set; }
        public Node PointsTo { get; set; }

        public Node(int x, int y, int g, int f, int h, Node point)
        {
            this.X = x;
            this.Y = y;
            this.G = g;
            this.F = f;
            this.H = h;
            this.PointsTo = point;
        }

        public override bool Equals(object obj)
        {
            Node rhs = obj as Node;

            return rhs.X == this.X && rhs.Y == this.Y;
        }

        public override int GetHashCode()
        {
            int hash = 7;
            hash = 83 * hash + this.X;
            hash = 83 * hash + this.Y;
            return hash;
        }
    }

    class Algorithm
    {
        private Dictionary<int, List<int>> Obstacles { get; set; }
        public HashSet<Node> OpenList { get; set; }
        public HashSet<Node> ClosedList { get; set; }
        private Node Parent { get; set; }
        private Node LowestCost { get; set; }
        private int StartX { get; set; }
        private int StartY { get; set; }
        private int TargetX { get; set; }
        private int TargetY { get; set; }
        private int Width { get; set; }
        private int Height { get; set; }
        private bool FirstIter = true;

        public Algorithm(int stX, int stY, int tgX, int tgY, int wid, int hei, Dictionary<int, List<int>> obs)
        {
            this.StartX = stX;
            this.StartY = stY;
            this.TargetX = tgX;
            this.TargetY = tgY;
            this.Width = wid - 1;
            this.Height = hei - 1;
            this.Obstacles = new Dictionary<int, List<int>>(obs);
            this.Parent = new Node(StartX, StartY, 0, 0, 0, null);
            this.LowestCost = new Node(int.MaxValue, int.MaxValue, 0, int.MaxValue, 0, null);
            this.ClosedList = new HashSet<Node>();
            this.OpenList = new HashSet<Node>();
        }

        private bool IsBlockObstacle(int X, int Y)
        {
            if (Obstacles.ContainsKey(X) == false || (Obstacles.ContainsKey(X) == true && Obstacles[X].Contains(Y) == false))
                return false;

            return true;
        }

        private void Calculate(ref int H, int G, ref int F, int MovedX, int MovedY, Node AddToList)
        {
            int H1 = 0;

            H = (TargetX - MovedX) * 10;

            if (H < 0)
                H *= -1;

            H1 = (TargetY - MovedY) * 10;

            if (H1 < 0)
                H1 *= -1;

            H += H1;
            F = G + H;

            AddToList.F = F;
            AddToList.H = H;
            AddToList.PointsTo = Parent;
        }

        private Node GetNodeFromOpen(Node Find)
        {
            Node Ret = null;

            foreach (Node Nfo in OpenList)
            {
                if (Nfo.Equals(Find))
                {
                    Ret = Nfo;

                    break;
                }
            }

            return Ret;
        }

        private bool CheckNode(Node AddToList, int G)
        {
            if (!OpenList.Contains(AddToList))
            {
                OpenList.Add(AddToList);

                return true;
            }
            else
            {
                Node Check = GetNodeFromOpen(AddToList);

                if (Parent.G + G < Check.G)
                {
                    int Offset = Check.G - Parent.G - G;

                    Check.G -= Offset;
                    Check.F -= Offset;
                    Check.PointsTo = Parent;
                }
            }

            return false;
        }

        private void ChooseNode()
        {
            foreach (Node Nfo in OpenList)
            {
                if (Nfo.X == TargetX && Nfo.Y == TargetY)
                {
                    LowestCost = Nfo;

                    break;
                }

                if (Nfo.F < LowestCost.F)
                    LowestCost = Nfo;
            }
        }

        private void CountCost()
        {
            int[] Directions = { 1, -1 };
            int[] Diagnoly = { 1, 1, -1, 1, 1, -1, -1, -1 };
            int ParentX = Parent.X;
            int ParentY = Parent.Y;
            int MovedX = 0;
            int MovedY = 0;
            int H = 0;
            int F = 0;
            Node AddToList = null;

            //Left and right
            for (int i = 0; i < 2; i++)
            {
                //Check if it is possible to move right or left
                if (ParentX + Directions[i] <= Width && ParentX + Directions[i] >= 0)
                {
                    //Check if blocks to the right and left of parent aren't obstacles                   
                    if (!IsBlockObstacle(ParentX + Directions[i], ParentY))
                    {
                        AddToList = new Node(ParentX + Directions[i], ParentY, Parent.G + 10, 0, 0, null);

                        //Check if it is not on closed list
                        if (!ClosedList.Contains(AddToList))
                        {
                            MovedX = AddToList.X;
                            MovedY = AddToList.Y;

                            Calculate(ref H, AddToList.G, ref F, MovedX, MovedY, AddToList);
                            CheckNode(AddToList, 10);
                        }
                    }
                }
            }

            //Up and down
            for (int i = 0; i < 2; i++)
            {
                //Check if possible to move up or down
                if (ParentY + Directions[i] <= Height && ParentY + Directions[i] >= 0)
                {
                    //Check if higher and lower block of parent aren't obstacles 
                    if (!IsBlockObstacle(ParentX, ParentY + Directions[i]))
                    {
                        AddToList = new Node(ParentX, ParentY + Directions[i], Parent.G + 10, 0, 0, null);

                        if (!ClosedList.Contains(AddToList))
                        {
                            MovedX = ParentX;
                            MovedY = ParentY + Directions[i];

                            Calculate(ref H, AddToList.G, ref F, MovedX, MovedY, AddToList);
                            CheckNode(AddToList, 10);
                        }
                    }
                }
            }

            //Diagnoly
            for (int i = 0; i < 8; i += 2)
            {
                if (ParentX + Diagnoly[i] <= Width && ParentX + Diagnoly[i] >= 0 && ParentY + Diagnoly[i + 1] <= Height && ParentY + Diagnoly[i + 1] >= 0)
                {
                    if (!IsBlockObstacle(ParentX + Diagnoly[i], ParentY + Diagnoly[i + 1]))
                    {
                        AddToList = new Node(ParentX + Diagnoly[i], ParentY + Diagnoly[i + 1], Parent.G + 14, 0, 0, null);

                        if (!ClosedList.Contains(AddToList))
                        {
                            MovedX = ParentX + Diagnoly[i];
                            MovedY = ParentY + Diagnoly[i + 1];

                            Calculate(ref H, AddToList.G, ref F, MovedX, MovedY, AddToList);
                            CheckNode(AddToList, 14);
                        }
                    }
                }
            }
        }
        public void FullSolve()
        {
            Node Final = null;

            if (FirstIter)
            {
                CountCost();
                ChooseNode();
                OpenList.Remove(Parent);
                ClosedList.Add(Parent);
                Parent = LowestCost;
                OpenList.Remove(Parent);
                ClosedList.Add(Parent);
                FirstIter = false;

                FullSolve();
            }
            else
            {
                while (true)
                {
                    if (OpenList.Count == 0)
                        break;

                    CountCost();

                    HashSet<Node> Copy = new HashSet<Node>(OpenList);

                    foreach (Node Nfo in Copy)
                    {
                        Parent = Nfo;
                        CountCost();
                        ClosedList.Add(Parent);
                        OpenList.Remove(Parent);

                        if (Parent.X == TargetX && Parent.Y == TargetY)
                        {
                            Final = Parent;
                            break;
                        }
                    }

                    ChooseNode();
                    OpenList.Remove(Parent);
                    ClosedList.Add(Parent);
                    Parent = LowestCost;

                    LowestCost.F = int.MaxValue;

                    if (Parent.X == TargetX && Parent.Y == TargetY)
                    {
                        Final = Parent;
                        break;
                    }
                }
            }
        }
    }
}

Java版本:

文件:AStar_Compare.java

package a.star_compare;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.lang3.time.StopWatch;

public class AStar_Compare {

    static double totalTime;
    static double[] trialTimes = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    static int startX;
    static int startY;
    static int targetX;
    static int targetY;
    static int width;
    static int heigth;
    static HashMap<Integer, List<Integer>> obstacles = new HashMap<>();
    static NumberFormat formatter = new DecimalFormat("#0.000000000");

    public static void main(String[] args) throws FileNotFoundException, IOException {

        for (int z = 0; z < 10; z++) {
            int Index = 0;
            System.out.println("z: " + z);
            for (int x = 0; x < 5; x++) {
                if (x < 10) {
                    Index += 10;
                } else {
                    Index += 100;
                }

                String fileName = "Maps-" + Index + ".txt";

                totalTime = 0;
                readFile(fileName);
                trialTimes[x] += totalTime / 1E9 / 100;
            }
        }

        int index0 = 0;

        for (int i = 0; i < 14; i++) {
            if (i < 10) {
                index0 += 10;
            } else {
                index0 += 100;
            }
            trialTimes[i] /= 10;
            String fileName = "Maps-" + index0 + ".txt";

            System.out.println(fileName + " Map size: " + index0 + "*" + index0 + ". On average map solved in: " + formatter.format(trialTimes[i]));
        }
    }

    static void measureTime() {
        StopWatch time = new StopWatch();

        time.start();

        Algorithm solve = new Algorithm(obstacles, startX, startY, targetX, targetY, width, heigth);
        solve.FullSolve();

        time.stop();

        totalTime += time.getNanoTime();
    }

}

文件:Node.java

package a.star_compare;

public class Node {
    public int x;
    public int y;
    public int g;
    public int h;
    public int f;

    public Node pointsTo;

    public Node(int gx, int gy, int gg, int gh, int gf, Node point){
        this.x = gx;
        this.y = gy;
        this.g = gg;
        this.h = gh;
        this.f = gf;
        this.pointsTo = point;
    }

    @Override
    public boolean equals(Object other){
        if(other == null) return false;
        if(other == this) return true;
        if(!(other instanceof Node)) return false;

        Node rhs = (Node)other;

        return this.x == rhs.x && this.y == rhs.y;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 83 * hash + this.x;
        hash = 83 * hash + this.y;
        return hash;
    }
}

文件:Algorithm.java

package a.star_compare;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

public class Algorithm {

    private final HashMap<Integer, List<Integer>> obstacles;
    private final HashSet<Node> closedList;
    private final HashSet<Node> openList;
    private Node parent;
    private Node lowestCost;
    private final int startX;
    private final int startY;
    private final int targetX;
    private final int targetY;
    private final int width;
    private final int height;
    private boolean firstIter = true;

    public Algorithm(HashMap<Integer, List<Integer>> obs, int stX, int stY, int tgX, int tgY, int wid, int hei) {
        this.obstacles = new HashMap(obs);
        this.startX = stX;
        this.startY = stY;
        this.targetX = tgX;
        this.targetY = tgY;
        this.width = wid - 1;
        this.height = hei - 1;
        this.parent = new Node(startX, startY, 0, 0, 0, null);
        this.lowestCost = new Node(Integer.MAX_VALUE, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, 0, null);
        this.closedList = new HashSet<>();
        this.openList = new HashSet<>();
    }

    private boolean isBlockObstacle(Integer x, Integer y) {
        if (obstacles.containsKey(x) == false || (obstacles.containsKey(x) == true && obstacles.get(x).contains(y) == false)) {
            return false;
        }

        return true;
    }

    private void calculate(int h, int g, int f, int movedX, int movedY, Node addToList) {
        int h1 = 0;

        h = (targetX - movedX) * 10;

        if (h < 0) {
            h *= -1;
        }

        h1 = (targetY - movedY) * 10;

        if (h1 < 0) {
            h1 *= -1;
        }

        h += h1;
        f = g + h;

        addToList.f = f;
        addToList.h = h;
        addToList.pointsTo = parent;
    }

    private Node getNodeFromOpen(Node find) {
        Node ret = null;

        for (Node nfo : openList) {

            if (nfo.equals(find)) {
                ret = nfo;
                break;
            }

        }

        return ret;
    }

    private boolean checkNode(Node addToList, int g) {
        if (!openList.contains(addToList)) {
            openList.add(addToList);
            return true;
        } else {
            Node check = getNodeFromOpen(addToList);

            if (parent.g + g < check.g) {
                int offset = check.g - parent.g - g;

                check.g -= offset;
                check.f -= offset;
                check.pointsTo = parent;
            }
        }

        return false;
    }

    private void chooseNode() {
        for (Node nfo : openList) {
            if (nfo.x == targetX && nfo.y == targetY) {
                lowestCost = nfo;
                break;
            }

            if (nfo.f < lowestCost.f) {
                lowestCost = nfo;
            }
        }
    }

    private void countCost() {
        int[] directions = {1, -1};
        int[] diagnoly = {1, 1, -1, 1, 1, -1, -1, -1};
        int parentX = parent.x;
        int parentY = parent.y;
        int movedX = 0;
        int movedY = 0;
        int h = 0;
        int f = 0;
        Node addToList = null;

        //Left and right
        for (int i = 0; i < 2; i++) {
            //Check if it is possible to move right or left
            if (parentX + directions[i] <= width && parentX + directions[i] >= 0) {
                //Check if blocks to the right and left of parent aren't obstacles                   
                if (!isBlockObstacle(parentX + directions[i], parentY)) {
                    addToList = new Node(parentX + directions[i], parentY, parent.g + 10, 0, 0, null);

                    //Check if it is not on closed list
                    if (!closedList.contains(addToList)) {
                        movedX = addToList.x;
                        movedY = addToList.y;

                        calculate(h, addToList.g, f, movedX, movedY, addToList);
                        checkNode(addToList, 10);
                    }
                }
            }
        }

        //Up and down
        for (int i = 0; i < 2; i++) {
            //Check if possible to move up or down
            if (parentY + directions[i] <= height && parentY + directions[i] >= 0) {
                //Check if higher and lower block of parent aren't obstacles 
                if (!isBlockObstacle(parentX, parentY + directions[i])) {
                    addToList = new Node(parentX, parentY + directions[i], parent.g + 10, 0, 0, null);

                    if (!closedList.contains(addToList)) {
                        movedX = parentX;
                        movedY = parentY + directions[i];

                        calculate(h, addToList.g, f, movedX, movedY, addToList);
                        checkNode(addToList, 10);
                    }
                }
            }
        }

        //diagnoly
        for (int i = 0; i < 8; i += 2) {
            if (parentX + diagnoly[i] <= width && parentX + diagnoly[i] >= 0 && parentY + diagnoly[i + 1] <= height && parentY + diagnoly[i + 1] >= 0) {
                if (!isBlockObstacle(parentX + diagnoly[i], parentY + diagnoly[i + 1])) {
                    addToList = new Node(parentX + diagnoly[i], parentY + diagnoly[i + 1], parent.g + 14, 0, 0, null);

                    if (!closedList.contains(addToList)) {
                        movedX = parentX + diagnoly[i];
                        movedY = parentY + diagnoly[i + 1];

                        calculate(h, addToList.g, f, movedX, movedY, addToList);
                        checkNode(addToList, 14);
                    }
                }
            }
        }
    }

    public void FullSolve() {
        Node finalPath = null;

        if (firstIter) {
            countCost();
            chooseNode();
            openList.remove(parent);
            closedList.add(parent);
            parent = lowestCost;
            openList.remove(parent);
            closedList.add(parent);
            firstIter = false;

            FullSolve();
        } else {
            while (true) {
                if (openList.isEmpty()) {
                    break;
                }

                countCost();

                HashSet<Node> copy = new HashSet<>(openList);

                for (Node nfo : copy) {
                    parent = nfo;
                    countCost();
                    closedList.add(parent);
                    openList.remove(parent);

                    if (parent.x == targetX && parent.y == targetY) {
                        finalPath = parent;
                        break;
                    }
                }

                chooseNode();
                openList.remove(parent);
                closedList.add(parent);
                parent = lowestCost;

                lowestCost.f = Integer.MAX_VALUE;

                if (parent.x == targetX && parent.y == targetY) {
                    finalPath = parent;
                    break;
                }
            }        
        }
    }
}

测试是使用预生成的地图文件完成的。我有14个地图文件,每个都包含100个特定大小的地图。最低的一个是10 * 10,最高的是500 * 500。

另请注意,如果每个地图都有100个示例,则意味着算法经过100次测试才能使用一个特定大小,而且我想提高准确度,因此我重复整个过程10次。这让我用一张地图进行1000次测试。我当然平均那些时间。

我并不熟悉高精度时间测量方法,因此我在Java和C#中使用StopWatch()(在Java中使用它我从apache commons下载它)。我做的是在阅读了一个我称为函数measureTime()的地图信息并启动StopWatch()然后调用算法类并使其解决拼图后我停止StopWatch()并花费时间。

以下是我得到的结果:
我发布图片是因为我不确定如何在这里制作表格。时间排在第二位,平均解决一张地图需要多少时间。

注意&#34; - &#34;符号有地图大小。 (Maps-20.txt表示按20 * 20映射,依此类推)

table

也是图表:

graph

这些结果让我感到惊讶,我期待一种语言有一点优势,但不是这样的。更新后,C#graph看起来与Java图类似,但增长率更高。首先我认为在将算法复制到Java时我犯了一些错误(首先我用C#编写),但我找不到任何错误。所以假设我没有犯一些愚蠢的错误。

如何进一步提高C#性能?

我还考虑过在Dictionary<int, List<int>>而不是List<int>中获取这些结果的一件事我可以使用HashSet<int>因为我只需要确认元素是否存在。但由于我没有处理成千上万的元素,所以我不认为它可能是主要因素。

感谢。

0 个答案:

没有答案