/// <summary> /// Calculates the fitness value of the given solution. /// </summary> public override float Calculate(TSPTWProblem problem, IEnumerable <int> tour, out int violated, out float violatedTime, out float waitTime, out float time, ref bool[] validFlags) { // calculate everything here. violated = problem.TimeAndViolations(tour, out time, out waitTime, out violatedTime, ref validFlags); // here only violated time is usefull, this objective is built to only make a tour valid. return(violatedTime); }
/// <summary> /// Calculates the time for a part of the tour including the turns at first and last position. /// </summary> public float CalculateTimeForPart(TSPTWProblem problem, IEnumerable <int> part, float timeBefore, out int violated, out float violatedTime, out float waitTime, ref bool[] validFlags) { // calculate everything here. float time; violated = problem.TimeAndViolationsForPart(part, timeBefore, out time, out waitTime, out violatedTime, ref validFlags); return(time); }
/// <summary> /// Calculates the time for a part of the tour including the turns at first and last position. /// </summary> public float CalculateTimeForPart(TSPTWProblem problem, IEnumerable <int> part, float timeBefore, out int violated, out float violatedTime, out float waitTime) { if (_validFlags == null) { _validFlags = new bool[problem.Times.Length / 2]; } return(this.CalculateTimeForPart(problem, part, timeBefore, out violated, out violatedTime, out waitTime, ref _validFlags)); }
/// <summary> /// Calculates the fitness value of the given solution. /// </summary> public override float Calculate(TSPTWProblem problem, IEnumerable <int> tour, out int violated, out float violatedTime, out float waitTime, out float time, ref bool[] validFlags) { // calculate everything here. violated = problem.TimeAndViolations(tour, out time, out waitTime, out violatedTime, ref validFlags); // TODO:there is need to seperate the violations, waiting time and weights. // expand the logistics library to allow for pareto-optimal or other // more advanced weight comparisons. // => this may not be needed anymore for the TSP-TW but for other problems it may be useful. return(time + violatedTime * 1000); }
/// <summary> /// Calculates the fitness of a TSP solution. /// </summary> /// <returns></returns> public sealed override float Calculate(TSPTWProblem problem, IEnumerable <int> solution) { if (_validFlags == null) { _validFlags = new bool[problem.Times.Length / 2]; } // calculate everything here. float violatedTime, time, waitTime; var violated = problem.TimeAndViolations(solution, out time, out waitTime, out violatedTime, ref _validFlags); // here only violated time is usefull, this objective is built to only make a tour valid. return(violatedTime); }
/// <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; clone.TurnPenalties = this.TurnPenalties; return(clone); }
/// <summary> /// Calculates the fitness value of the given solution. /// </summary> public sealed override float Calculate(TSPTWProblem problem, Tour solution) { if (_validFlags == null) { _validFlags = new bool[solution.Count]; } // calculate everything here. float violatedTime, waitTime, time; var violated = problem.TimeAndViolations(solution, out time, out waitTime, out violatedTime, ref _validFlags); // TODO:there is need to seperate the violations, waiting time and weights. // expand the logistics library to allow for pareto-optimal or other // more advanced weight comparisons. // => this may not be needed anymore for the TSP-TW but for other problems it may be useful. return(time + violatedTime * 1000); }
/// <summary> /// Subtracts the given fitness values. /// </summary> public sealed override float Subtract(TSPTWProblem problem, float fitness1, float fitness2) { return(fitness1 - fitness2); }
/// <summary> /// Returns true if the given fitness value is zero. /// </summary> public sealed override bool IsZero(TSPTWProblem problem, float fitness) { return(fitness == 0); }
/// <summary> /// Compares the two fitness values. /// </summary> public sealed override int CompareTo(TSPTWProblem problem, float fitness1, float fitness2) { return(fitness1.CompareTo(fitness2)); }
/// <summary> /// Adds the two given fitness values. /// </summary> public sealed override float Add(TSPTWProblem problem, float fitness1, float fitness2) { return(fitness1 + fitness2); }
/// <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; }