Changeset 17248 for branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/TSP.cs
- Timestamp:
- 09/11/19 21:35:59 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/TSP.cs
r17241 r17248 1 using System; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 * 5 * This file is part of HeuristicLab. 6 * 7 * HeuristicLab is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * HeuristicLab is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 #endregion 21 22 using System; 23 using System.Collections.Generic; 2 24 using System.Linq; 3 25 using HEAL.Attic; … … 25 47 26 48 [Storable] public IValueParameter<ITSPData> TSPDataParameter { get; private set; } 27 [Storable] public IValueParameter< Permutation> BestKnownSolutionParameter { get; private set; }49 [Storable] public IValueParameter<ITSPSolution> BestKnownSolutionParameter { get; private set; } 28 50 29 51 public ITSPData TSPData { … … 31 53 set { TSPDataParameter.Value = value; } 32 54 } 33 public Permutation BestKnownSolution {55 public ITSPSolution BestKnownSolution { 34 56 get { return BestKnownSolutionParameter.Value; } 35 57 set { BestKnownSolutionParameter.Value = value; } … … 47 69 public TSP() : base(new PermutationEncoding("Tour", 16, PermutationTypes.RelativeUndirected)) { 48 70 Parameters.Add(TSPDataParameter = new ValueParameter<ITSPData>("TSPData", "The main parameters of the TSP.")); 49 Parameters.Add(BestKnownSolutionParameter = new OptionalValueParameter< Permutation>("BestKnownSolution", "The best known solution."));71 Parameters.Add(BestKnownSolutionParameter = new OptionalValueParameter<ITSPSolution>("BestKnownSolution", "The best known solution.")); 50 72 51 73 TSPData = new EuclideanTSPData() { … … 85 107 i = qualities.Select((x, index) => new { index, Fitness = x }).OrderBy(x => x.Fitness).First().index; 86 108 else i = qualities.Select((x, index) => new { index, Fitness = x }).OrderByDescending(x => x.Fitness).First().index; 109 var solution = TSPData.GetSolution(solutions[i], qualities[i]); 87 110 88 111 if (double.IsNaN(BestKnownQuality) || 89 112 Maximization && qualities[i] > BestKnownQuality || 90 113 !Maximization && qualities[i] < BestKnownQuality) { 91 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i]); 92 BestKnownSolutionParameter.ActualValue = (Permutation)solutions[i].Clone(); 93 } 94 95 var solution = TSPData.GetSolution(solutions[i], qualities[i]); 114 BestKnownQualityParameter.Value = new DoubleValue(qualities[i]); 115 BestKnownSolutionParameter.Value = solution; 116 } 96 117 results.AddOrUpdateResult("Best TSP Solution", solution); 118 } 119 120 public override IEnumerable<Permutation> GetNeighbors(Permutation solution, IRandom random) { 121 foreach (var move in ExhaustiveInversionMoveGenerator.Generate(solution)) { 122 var clone = (Permutation)solution.Clone(); 123 InversionManipulator.Apply(clone, move.Index1, move.Index2); 124 yield return clone; 125 } 97 126 } 98 127 … … 141 170 if (data.BestKnownTour != null) { 142 171 try { 143 BestKnownSolution = new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour); 144 BestKnownQuality = Evaluate(BestKnownSolution); 172 var tour = new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour); 173 var tourLength = Evaluate(tour); 174 BestKnownSolution = new TSPSolution(data.Coordinates != null ? new DoubleMatrix(data.Coordinates) : null, tour, new DoubleValue(tourLength)); 175 BestKnownQuality = tourLength; 145 176 } catch (InvalidOperationException) { 146 177 if (data.BestKnownQuality.HasValue) … … 170 201 171 202 Operators.Add(new TSPAlleleFrequencyAnalyzer()); 172 foreach (var op in ApplicationManager.Manager.GetInstances<ITSPMoveEvaluator>()) 203 foreach (var op in ApplicationManager.Manager.GetInstances<ITSPMoveEvaluator>()) { 204 Encoding.ConfigureOperator(op); 173 205 Operators.Add(op); 174 206 } 175 207 ParameterizeOperators(); 176 208 }
Note: See TracChangeset
for help on using the changeset viewer.