/// <summary>
        /// Executes the actual algorithm.
        /// </summary>
        protected override void DoRun()
        {
            // calculate weight matrix.
            if (!_weightMatrixAlgorithm.HasRun)
            { // only run if it has not been run yet.
                _weightMatrixAlgorithm.Run();
            }
            if (!_weightMatrixAlgorithm.HasSucceeded)
            { // algorithm has not succeeded.
                this.ErrorMessage = string.Format("Could not calculate weight matrix: {0}",
                                                  _weightMatrixAlgorithm.ErrorMessage);
                return;
            }

            // check if an entry in the sequence was not found.
            this.ErrorMessage = string.Empty;
            var toRemove = new HashSet <int>();

            foreach (var c in _sequence)
            {
                LocationError    locationError;
                RouterPointError routerPointError;
                if (_weightMatrixAlgorithm.TryGetError(c, out locationError, out routerPointError))
                {
                    if (locationError != null)
                    {
                        this.ErrorMessage += string.Format("The location at index {0} is in error: {1}", c,
                                                           locationError.ToInvariantString());
                    }
                    else if (routerPointError != null)
                    {
                        this.ErrorMessage += string.Format("The location at index {0} is in error: {1}", c,
                                                           routerPointError.ToInvariantString());
                    }
                    else
                    {
                        this.ErrorMessage += string.Format("The location at index {0} is in error.", c);
                    }

                    toRemove.Add(c);
                }
            }

            // remove all invalids.
            var validRoute = _sequence.Clone() as Tour;

            foreach (var invalid in toRemove)
            {
                validRoute.Remove(invalid);
            }

            // build problem.
            var problem = new SequenceDirectedProblem(validRoute, _weightMatrixAlgorithm.Weights, _turnPenalty);

            // execute the solver.
            if (_solver == null)
            {
                _tour = problem.Solve();
            }
            else
            {
                _tour = problem.Solve(_solver);
            }

            this.HasSucceeded = true;
        }
示例#2
0
        /// <summary>
        /// Executes the actual algorithm.
        /// </summary>
        protected override void DoRun()
        {
            // calculate weight matrix.
            if (!_weightMatrixAlgorithm.HasRun)
            { // only run if it has not been run yet.
                _weightMatrixAlgorithm.Run();
            }
            if (!_weightMatrixAlgorithm.HasSucceeded)
            { // algorithm has not succeeded.
                this.ErrorMessage = string.Format("Could not calculate weight matrix: {0}",
                                                  _weightMatrixAlgorithm.ErrorMessage);
                return;
            }

            LocationError    le;
            RouterPointError rpe;

            if (_weightMatrixAlgorithm.TryGetError(_first, out le, out rpe))
            { // if the last location is set and it could not be resolved everything fails.
                if (le != null)
                {
                    this.ErrorMessage = string.Format("Could resolve first location: {0}",
                                                      le);
                }
                else if (rpe != null)
                {
                    this.ErrorMessage = string.Format("Could route to/from first location: {0}",
                                                      rpe);
                }
                else
                {
                    this.ErrorMessage = string.Format("First location was in error list.");
                }
                return;
            }

            // build problem.
            var        first   = _first;
            STSProblem problem = null;

            if (_last.HasValue)
            {     // the last customer was set.
                if (_weightMatrixAlgorithm.TryGetError(_last.Value, out le, out rpe))
                { // if the last location is set and it could not be resolved everything fails.
                    if (le != null)
                    {
                        this.ErrorMessage = string.Format("Could resolve last location: {0}",
                                                          le);
                    }
                    else if (rpe != null)
                    {
                        this.ErrorMessage = string.Format("Could route to/from last location: {0}",
                                                          rpe);
                    }
                    else
                    {
                        this.ErrorMessage = string.Format("Last location was in error list.");
                    }
                    return;
                }

                problem = new STSProblem(_weightMatrixAlgorithm.WeightIndex(first), _weightMatrixAlgorithm.WeightIndex(_last.Value),
                                         _weightMatrixAlgorithm.Weights, _turnPenalty, _max);
            }
            else
            { // the last customer was not set.
                problem = new STSProblem(_weightMatrixAlgorithm.WeightIndex(first), _weightMatrixAlgorithm.Weights, _turnPenalty,
                                         _max);
            }

            // execute the solver.
            if (_solver == null)
            {
                _tour = problem.Solve();
            }
            else
            {
                _tour = problem.Solve(_solver);
            }

            this.HasSucceeded = true;
        }
示例#3
0
        /// <summary>
        /// Executes the actual algorithm.
        /// </summary>
        protected override void DoRun()
        {
            // calculate weight matrix.
            if (!_weightMatrixAlgorithm.HasRun)
            { // only run if it has not been run yet.
                _weightMatrixAlgorithm.Run();
            }
            if (!_weightMatrixAlgorithm.HasSucceeded)
            { // algorithm has not succeeded.
                this.ErrorMessage = string.Format("Could not calculate weight matrix: {0}",
                                                  _weightMatrixAlgorithm.ErrorMessage);
                return;
            }

            LocationError    le;
            RouterPointError rpe;

            if (_weightMatrixAlgorithm.TryGetError(_first, out le, out rpe))
            { // if the last location is set and it could not be resolved everything fails.
                if (le != null)
                {
                    this.ErrorMessage = string.Format("Could resolve first location: {0}",
                                                      le);
                }
                else if (rpe != null)
                {
                    this.ErrorMessage = string.Format("Could route to/from first location: {0}",
                                                      rpe);
                }
                else
                {
                    this.ErrorMessage = string.Format("First location was in error list.");
                }
                return;
            }

            // map the windows if needed.
            var windows = _windows;

            if (_weightMatrixAlgorithm.Errors.Count > 0 ||
                _weightMatrixAlgorithm.MassResolver.Errors.Count > 0)
            {
                var newWindows = new List <TimeWindow>();
                for (var i = 0; i < _windows.Length; i++)
                {
                    if (_weightMatrixAlgorithm.MassResolver.Errors.ContainsKey(i))
                    {
                        continue;
                    }
                    var resolvedIndex = _weightMatrixAlgorithm.MassResolver.ResolvedIndexOf(i);
                    if (_weightMatrixAlgorithm.Errors.ContainsKey(resolvedIndex))
                    {
                        continue;
                    }
                    newWindows.Add(_windows[i]);
                }
                windows = newWindows.ToArray();
            }

            // build problem.
            var          first   = _first;
            TSPTWProblem problem = null;

            if (_last.HasValue)
            {     // the last customer was set.
                if (_weightMatrixAlgorithm.TryGetError(_last.Value, out le, out rpe))
                { // if the last location is set and it could not be resolved everything fails.
                    if (le != null)
                    {
                        this.ErrorMessage = string.Format("Could resolve last location: {0}",
                                                          le);
                    }
                    else if (rpe != null)
                    {
                        this.ErrorMessage = string.Format("Could route to/from last location: {0}",
                                                          rpe);
                    }
                    else
                    {
                        this.ErrorMessage = string.Format("Last location was in error list.");
                    }
                    return;
                }

                problem = new TSPTWProblem(_weightMatrixAlgorithm.WeightIndex(first), _weightMatrixAlgorithm.WeightIndex(_last.Value),
                                           _weightMatrixAlgorithm.Weights, windows, _turnPenalty);
            }
            else
            { // the last customer was not set.
                problem = new TSPTWProblem(_weightMatrixAlgorithm.WeightIndex(first), _weightMatrixAlgorithm.Weights, windows, _turnPenalty);
            }

            // execute the solver.
            if (_solver == null)
            {
                _tour = problem.Solve();
            }
            else
            {
                _tour = problem.Solve(_solver);
            }

            this.HasSucceeded = true;
        }