public virtual IPath <T> Solve()
        {
            //Path result = createRandom();
            IPath <T> result = CreateNearest();

            for (int i = 0; i < 10; i++)
            {
                population.Add(result);
            }
            for (int i = 0; i < populationCount - 10; i++)
            {
                population.Add(CreateRandom());
            }
            for (int generation = 0; generation < generationCount; generation++)
            {
                List <IPath <T> > descendants = new List <IPath <T> >();
                population.Sort();
                for (int i = 0; i < eliteCount; i++)
                {
                    IPath <T> elite = population[i];
                    for (int j = 0; j < eliteDescendendCount; j++)
                    {
                        IPath <T> descendant = Evolve(elite);
                        if (random.Next(100) < mutatePercent)
                        {
                            descendant = Mutate(descendant);
                        }
                        descendants.Add(descendant);
                    }
                }
                problem.CalculateLengths(descendants);
                population.AddRange(descendants);
                population.Sort();
                while (population.Count > populationCount)
                {
                    population.RemoveAt(population.Count - 1);
                }
                IPath <T> best = population[0];
                if (best.IsBetter(result))
                {
                    result = best;
                    problem.SetBestPath(best);
                }
                else
                {
                    noImprovement++;
                    if (noImprovement >= maxNoImprovement)
                    {
                        System.Console.Out.WriteLine("kill no imporvment");
                        break;
                    }
                }
            }
            return(result);
        }
Пример #2
0
        protected internal void CalculateShortestPath(IProblem <T> problem, IPath <T> startPath, IList <int> leftToVisit)
        {
            if (leftToVisit.Count == 1)
            {
                IPath <T> path = startPath.To(leftToVisit[0]);
                SetBestPath(path);
            }
            else
            {
                List <IPath <T> > nextPaths      = new List <IPath <T> >();
                IPath <T>         globalBestPath = problem.GetBestPath();
                foreach (int?nextLocation in leftToVisit)
                {
                    IPath <T> nextPath = startPath.To(nextLocation.Value);
                    //if (globalBestPath == null || nextPath.getLength() < globalBestPath.getLength()) {
                    if (nextPath.IsBetter(globalBestPath))
                    {
                        nextPaths.Add(nextPath);
                    }
                }
                problem.CalculateLengths(nextPaths);
                nextPaths.Sort();

                if (problem.GetLocationsCount() - startPath.GetLocationsCount() > 8 && Volatile.Read(ref taskCount) <= threadCount)
                {
                    foreach (IPath <T> nextPath in nextPaths)
                    {
                        IList <int> newLeftToVisit = new List <int>(leftToVisit);
                        int         nextLocation   = nextPath.GetLast();
                        newLeftToVisit.Remove(nextLocation);
                        Interlocked.Increment(ref taskCount);
                        ThreadPool.QueueUserWorkItem(state =>
                        {
                            CalculateShortestPath(problem, nextPath, newLeftToVisit);
                            Interlocked.Decrement(ref taskCount);
                        });
                    }
                }
                else
                {
                    foreach (IPath <T> nextPath in nextPaths)
                    {
                        IList <int> newLeftToVisit = new List <int>(leftToVisit);
                        int         nextLocation   = nextPath.GetLast();
                        newLeftToVisit.Remove(nextLocation);
                        CalculateShortestPath(problem, nextPath, newLeftToVisit);
                    }
                }
            }
        }
        public virtual IPath <T> Solve()
        {
            IPath <T> start = problem.CreatePath();

            for (int j = 0; j < problem.GetLocationsCount(); j++)
            {
                List <IPath <T> > neighbors = new List <IPath <T> >();
                for (int i = 0; i < problem.GetLocationsCount(); i++)
                {
                    if (!start.Contains(i))
                    {
                        neighbors.Add(start.To(i));
                    }
                }
                problem.CalculateLengths(neighbors);
                neighbors.Sort();
                start = neighbors[0];
            }
            return(start);
        }