Example #1
0
        private IList <KeyValuePair <int, int[]> > GenerateRoutes(TimeSpan durationLimit)
        {
            var result    = new List <KeyValuePair <int, int[]> >();
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            _stopwatchRoutes[0] = new Stopwatch();
            _stopwatchRoutes[0].Start();

            var initialRoute   = CalculateRoutesFromCity(_random.Next(Distances.Length));
            var optimizedRoute = _algorithmLocalSearch.OptimizeRouteFromCity(initialRoute.First(),
                                                                             initialRoute);

            do
            {
                var modifiedRoute = Perturbate(optimizedRoute);

                modifiedRoute = _algorithmLocalSearch.OptimizeRouteFromCity(modifiedRoute.First(),
                                                                            modifiedRoute);

                if (CalculateRouteLength(modifiedRoute) < CalculateRouteLength(optimizedRoute))
                {
                    optimizedRoute = modifiedRoute;
                }
            } while (stopwatch.Elapsed <= durationLimit);

            _stopwatchRoutes[0].Stop();

            result.Add(new KeyValuePair <int, int[]>(optimizedRoute.First(), optimizedRoute));


            return(result);
        }
Example #2
0
        public override IList <KeyValuePair <int, int[]> > CalculateRoutes(IDictionary <int, int>[] distances)
        {
            Distances = distances;
            var result = new List <KeyValuePair <int, int[]> >();

            _random = new Random();
            var algorithm = new RandomRoutes {
                RouteLengthLimit = 50
            };

            algorithm.Distances = distances;

            var algorithmLocalSearch = new LocalSearch(algorithm);

            algorithmLocalSearch.Distances = distances;

            for (var i = 0; i < CountRoutes; i++)
            {
                var routeStart     = algorithm.CalculateRoutesFromCity(_random.Next(distances.Length));
                var optimizedRoute = algorithmLocalSearch.OptimizeRouteFromCity(i, routeStart);

                result.Add(new KeyValuePair <int, int[]>(i, optimizedRoute));
            }
            IsCalculated     = true;
            CalculatedRoutes = result;

            return(result);
        }
        public override IList <KeyValuePair <int, int[]> > CalculateRoutes(
            IDictionary <int, int>[] distances)
        {
            Distances = distances;
            ((TspAlgorithmBase)_algorithmNonDeterministic).Distances = distances;
            _algorithmLocalSearch.Distances = distances;

            var result = new List <KeyValuePair <int, int[]> >();


            for (var i = 0; i < CountRepeatStartAlgorithm; i++)
            {
                var routeStart     = CalculateRoutesFromCity(_random.Next(Distances.Length));
                var optimizedRoute = _algorithmLocalSearch.OptimizeRouteFromCity(i, routeStart);
                result.Add(new KeyValuePair <int, int[]>(optimizedRoute.First(), optimizedRoute));
            }

            IsCalculated     = true;
            CalculatedRoutes = result;
            return(result);
        }