Ejemplo n.º 1
0
        /// <summary>
        /// Creates a deep-copy of this problem.
        /// </summary>
        /// <returns></returns>
        public TSPTWProblem Clone()
        {
            var weights = new float[this.Times.Length][];

            for (var i = 0; i < this.Times.Length; i++)
            {
                weights[i] = this.Times[i].Clone() as float[];
            }
            var clone = new TSPTWProblem();

            clone.First   = this.First;
            clone.Last    = this.Last;
            clone.Times   = this.Times;
            clone.Windows = this.Windows;
            return(clone);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Excutes 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);
            }
            else
            { // the last customer was not set.
                problem = new TSPTWProblem(_weightMatrixAlgorithm.WeightIndex(first), _weightMatrixAlgorithm.Weights, windows);
            }

            // solve.
            if (_solver == null)
            {
                _tour = problem.Solve();
            }
            else
            {
                _tour = problem.Solve(_solver);
            }

            this.HasSucceeded = true;
        }