Esempio n. 1
0
        private void ButtonSolveClick(object sender, RoutedEventArgs e)
        {
            if (_currentPolyLine.Points.Count >= 2)
            {
                _currentPolyLine.Points.Add(_currentPolyLine.Points[0]);
            }

            var initialState = new PathFindingState(new Search.Types.Point(100, 100));
            var goalState    = new PathFindingState(new Search.Types.Point(800, 600));

            var environment = new PathFindingEnvironment();

            foreach (var polyLine in _polyLines)
            {
                var points = polyLine.Points.Select(p => p).Distinct().ToList();
                for (int i = 0; i < points.Count; i++)
                {
                    for (int j = i + 1; j < points.Count; j++)
                    {
                        if (i == j)
                        {
                            continue;
                        }

                        var pointLine = new PointLine(
                            new Point(points[i].X, points[i].Y),
                            new Point(points[j].X, points[j].Y));

                        environment.PointLines.Add(pointLine);
                    }
                }
            }

            var actionFunction  = new PathFindingActionFunction(environment, goalState);
            var resultFunction  = new PathFindingResultFunction();
            var goalTest        = new PathFindingGoalTest(goalState);
            var stepCost        = new PathFindingStepCost();
            var searchAlgorithm = new GraphSearch <PathFindingState, PathFindingAction>();

            var problem = new Problem <PathFindingState, PathFindingAction>(initialState, actionFunction, resultFunction, goalTest, stepCost);

            var solution = searchAlgorithm.Search(problem);

            for (int i = 1; i < solution.Count; i++)
            {
                AddLine(
                    solution[i - 1].State.Point.X,
                    solution[i - 1].State.Point.Y,
                    solution[i].State.Point.X,
                    solution[i].State.Point.Y);
            }
        }
Esempio n. 2
0
        private static void SolvePathFindingProblem()
        {
            var initialState = new PathFindingState(new Point(1, 1));
            var goalState    = new PathFindingState(new Point(5, 5));

            var environment = new PathFindingEnvironment();

            environment.PointLines.Add(new PointLine(new Point(1, 3), new Point(3, 1)));
            environment.PointLines.Add(new PointLine(new Point(3, 1), new Point(5, 3)));
            environment.PointLines.Add(new PointLine(new Point(5, 3), new Point(3, 5)));
            environment.PointLines.Add(new PointLine(new Point(3, 5), new Point(1, 3)));
            environment.PointLines.Add(new PointLine(new Point(3, 1), new Point(3, 5)));
            environment.PointLines.Add(new PointLine(new Point(1, 3), new Point(5, 3)));

            var actionFunction  = new PathFindingActionFunction(environment, goalState);
            var resultFunction  = new PathFindingResultFunction();
            var goalTest        = new PathFindingGoalTest(goalState);
            var stepCost        = new PathFindingStepCost();
            var searchAlgorithm = new GraphSearch <PathFindingState, PathFindingAction>();

            SolveProblem(actionFunction, resultFunction, goalTest, stepCost, initialState, searchAlgorithm);
        }
Esempio n. 3
0
        private void ButtonSolveClick(object sender, RoutedEventArgs e)
        {
            if (_currentPolyLine.Points.Count >= 2)
            {
                _currentPolyLine.Points.Add(_currentPolyLine.Points[0]);
            }

            var initialState = new PathFindingState(new Search.Types.Point(100, 100));
            var goalState = new PathFindingState(new Search.Types.Point(800, 600));

            var environment = new PathFindingEnvironment();

            foreach (var polyLine in _polyLines)
            {
                var points = polyLine.Points.Select(p => p).Distinct().ToList();
                for (int i = 0; i < points.Count; i++)
                {
                    for (int j = i + 1; j < points.Count; j++)
                    {
                        if (i == j) continue;

                        var pointLine = new PointLine(
                            new Point(points[i].X, points[i].Y),
                            new Point(points[j].X, points[j].Y));

                        environment.PointLines.Add(pointLine);
                    }
                }
            }

            var actionFunction = new PathFindingActionFunction(environment, goalState);
            var resultFunction = new PathFindingResultFunction();
            var goalTest = new PathFindingGoalTest(goalState);
            var stepCost = new PathFindingStepCost();
            var searchAlgorithm = new GraphSearch<PathFindingState, PathFindingAction>();

            var problem = new Problem<PathFindingState, PathFindingAction>(initialState, actionFunction, resultFunction, goalTest, stepCost);

            var solution = searchAlgorithm.Search(problem);

            for (int i = 1; i < solution.Count; i++)
            {
                AddLine(
                    solution[i - 1].State.Point.X,
                    solution[i - 1].State.Point.Y,
                    solution[i].State.Point.X,
                    solution[i].State.Point.Y);
            }
        }