Exemplo n.º 1
0
        private void optimisePath(int pathSegments, int obstaclePassDistance)
        {
            PathFinding finder = new PathFinding(agent.currentLocation, goalLocation, pathSegments + 1, obstaclePassDistance, 50000, world, curvedPath);

            Coordinate[] longerPathOne = null;
            try
            {
                longerPathOne = finder.findPath();
                paths.Add(longerPathOne);
            }
            catch (PathFinding.TestFailedException)
            { }

            finder = new PathFinding(agent.currentLocation, goalLocation, pathSegments + 2, obstaclePassDistance, 50000, world, curvedPath);
            Coordinate[] longerPathTwo = null;
            try
            {
                longerPathTwo = finder.findPath();
                paths.Add(longerPathTwo);
            }
            catch (PathFinding.TestFailedException)
            { }

            optimisePathLength(pathSegments, path);
            if (longerPathOne != null)
            {
                optimisePathLength(longerPathOne.Length - 1, longerPathOne.ToList());
            }
            if (longerPathTwo != null)
            {
                optimisePathLength(longerPathTwo.Length - 1, longerPathTwo.ToList());
            }
        }
Exemplo n.º 2
0
 private void executeButton_Click(object sender, EventArgs e)
 {
     if (world != null && obstacles != null && agent != null)
     {
         curvedPath = curvedCheckBox.Checked;
         path       = new List <Coordinate>();
         transformNonRectangularObstacles();
         progressLabel.Text = "Finding path...";
         progressLabel.Invalidate();
         int pathSegments = 0;
         for (int i = 1; i <= 20; i++)
         {
             PathFinding finder = new PathFinding(agent.currentLocation, goalLocation, i, distance, 50000, world, curvedPath);
             try
             {
                 path.AddRange(finder.findPath());
             }
             catch (PathFinding.TestFailedException)
             {
                 continue;
             }
             pathSegments = i;
             break;
         }
         if (path.Count == 0)
         {
             progressLabel.Text = "Path not found.";
             showPathError("There is no clear available path to goal location.");
         }
         else
         {
             paths.Add(path.ToArray());
             progressLabel.Text = "Optimising...";
             progressLabel.Invalidate();
             Thread.Sleep(10);
             optimisePath(pathSegments, distance);
             if (commandsCheckBox.Checked == true)
             {
                 PathCommandsGenerator generator = new PathCommandsGenerator(227, path);
                 generator.initialOrientation = 0;
                 generator.generateAndSaveCommands(1.35);
             }
             pathCalculated     = true;
             progressLabel.Text = "Path calculated.";
         }
     }
     else
     {
         showPathError("Some parameters were not set.");
     }
 }
Exemplo n.º 3
0
        private void optimisePathLength(int pathSegments, List <Coordinate> path)
        {
            int pathLength = 0;

            for (int i = 0; i < path.Count - 1; i++)
            {
                pathLength += Coordinate.getXDistanceBetweenCoordinates(path[i], path[i + 1]) +
                              Coordinate.getYDistanceBetweenCoordinates(path[i], path[i + 1]);
            }
            double bestFitness = evaluateFitnessValueOfPath(this.path.ToArray());

            int  decrementor = pathLength / 25;
            bool hasFailed   = false;

            while (decrementor > 0)
            {
                pathLength -= decrementor;
                PathFinding finder = new PathFinding(agent.currentLocation, goalLocation, pathSegments,
                                                     distance, pathLength, world, curvedPath);
                try
                {
                    Coordinate[] newPath   = null;
                    Exception    exception = null;
                    Thread       optimiser = new Thread(() => SafeExecute(() => newPath = finder.findPath(), out exception));
                    optimiser.Start();
                    bool success = optimiser.Join(750);
                    if (exception != null)
                    {
                        if (hasFailed)
                        {
                            break;
                        }
                        else
                        {
                            hasFailed = true;
                            continue;
                        }
                    }
                    if (success == true)
                    {
                        paths.Add(newPath);
                        double newPathFitness = evaluateFitnessValueOfPath(newPath);
                        if (bestFitness > newPathFitness)
                        {
                            this.path = new List <Coordinate>();
                            this.path.AddRange(newPath);
                            bestFitness = evaluateFitnessValueOfPath(newPath);
                        }
                    }
                    else
                    {
                        if (hasFailed)
                        {
                            break;
                        }
                        else
                        {
                            hasFailed = true;
                            continue;
                        }
                    }
                }
                catch (PathFinding.TestFailedException)
                {
                    break;
                }
            }
        }