Example #1
0
        static void Main(string[] args)
        {
            var start = new APoint {
                X = 0, Y = 0
            };
            var target = new APoint {
                X = 5, Y = 4
            };

            //var DFSmatrix = Common.GetRandomMatrix(7, 7);
            //var dfsMatrix = Algorithms.DFS(DFSmatrix, start, target);
            //Console.WriteLine("DFS path: ");
            //Common.PrettyPrint(dfsMatrix, 7, 7);

            //Console.WriteLine("---------------------------");

            //var BFSmatrix = Common.GetRandomMatrix(7, 7);
            //var bfsMatrix = Algorithms.BFS(BFSmatrix, start, target);
            //Console.WriteLine("BFS path: ");
            //Common.PrettyPrint(bfsMatrix, 7, 7);

            //var idsMatrix = Common.GetRandomMatrix(7, 7);
            //IDS.Matrix = idsMatrix;
            //IDS.Run(start, target);
            //Console.WriteLine("IDS path: ");
            //Common.PrettyPrint(idsMatrix, 7, 7);

            //var aStarMatrix = Common.GetRandomMatrix(7, 7);
            //InformedAlgs.Matrix = aStarMatrix;
            //InformedAlgs.AStar(start, target);
            //Console.WriteLine("A-Star path: ");
            //Console.WriteLine(InformedAlgs.PathLength);
            //Common.PrettyPrint(aStarMatrix, 7, 7);

            //int n = 1000;
            //QueensProblem queensProblem = new QueensProblem(n);
            //queensProblem.Solve();
            //queensProblem.GetSolution(n);

            //var knapsack = new Knapsack();
            //knapsack.InitializeItems();
            //Generator generator = new Generator(knapsack);
            //generator.Solve();
            //Console.Read();

            var knn = new Knn();

            knn.InitializeSets();
            int k = 12;

            knn.Classify(k);
            Console.WriteLine("Accuracy: " + knn.predictedAnswers * 100 / 20 + "%");
        }
Example #2
0
 public static int CalculateManhattanDistance(APoint from, APoint to)
 {
     return(Math.Abs(to.X - from.X) + Math.Abs(to.Y - from.Y));
 }
Example #3
0
        public static void AStar(APoint start, APoint target)
        {
            APoint current      = null;
            var    currentPaths = new List <APoint>();
            var    visited      = new List <APoint>();

            PathLength = 0;

            //G-score - distance from the starting location to the current
            int g = 0;

            // start by adding the original position to the open list
            currentPaths.Add(start);

            while (currentPaths.Count > 0)
            {
                // get the square with the lowest F score
                var lowest = currentPaths.Min(point => point.F);
                current = currentPaths.First(point => point.F == lowest);

                Matrix[current.X][current.Y] = '*';
                Console.WriteLine(current.Direction);
                PathLength++;

                // add the current square to the closed list
                visited.Add(current);

                // remove it from the open list
                currentPaths.Remove(current);

                // if we added the destination to the closed list, we've found a path
                if (visited.FirstOrDefault(point => point.X == target.X && point.Y == target.Y) != null)
                {
                    break;
                }

                var adjacentSquares = Common.GetNeithbours(Matrix, current);
                g++;

                foreach (var adjacentSquare in adjacentSquares)
                {
                    if (visited.FirstOrDefault(point =>
                                               point.X == adjacentSquare.X &&
                                               point.Y == adjacentSquare.Y) != null)
                    {
                        continue;
                    }

                    if (currentPaths.FirstOrDefault(point =>
                                                    point.X == adjacentSquare.X &&
                                                    point.Y == adjacentSquare.Y) == null)
                    {
                        adjacentSquare.G      = g;
                        adjacentSquare.H      = Common.CalculateManhattanDistance(adjacentSquare, target);
                        adjacentSquare.F      = adjacentSquare.G + adjacentSquare.H;
                        adjacentSquare.Parent = current;

                        currentPaths.Insert(0, adjacentSquare);
                    }
                    else
                    {
                        if (g + adjacentSquare.H < adjacentSquare.F)
                        {
                            adjacentSquare.G      = g;
                            adjacentSquare.F      = adjacentSquare.G + adjacentSquare.H;
                            adjacentSquare.Parent = current;
                        }
                    }
                }
            }
        }