- Timestamp:
- 05/05/10 00:48:18 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3
- Files:
-
- 4 deleted
- 3 edited
- 1 copied
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/BestTSPSolutionAnalyzer.cs
r3612 r3616 32 32 namespace HeuristicLab.Problems.TravelingSalesman { 33 33 /// <summary> 34 /// An operator for visualizing the best tourof Traveling Salesman Problems given in path representation using city coordinates.34 /// An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates. 35 35 /// </summary> 36 [Item("Best PathTSPTourVisualizer", "An operator for visualizing the best tourof Traveling Salesman Problems given in path representation using city coordinates.")]36 [Item("BestTSPSolutionAnalyzer", "An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")] 37 37 [StorableClass] 38 public sealed class Best PathTSPTourVisualizer : SingleSuccessorOperator, IPathCoordinatesTSPSolutionsVisualizer {39 public ILookupParameter<DoubleMatrix> CoordinatesParameter {40 get { return ( ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }38 public sealed class BestTSPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer { 39 public LookupParameter<DoubleMatrix> CoordinatesParameter { 40 get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; } 41 41 } 42 public ILookupParameter<ItemArray<Permutation>> PermutationParameter {43 get { return ( ILookupParameter<ItemArray<Permutation>>)Parameters["Permutation"]; }42 public SubScopesLookupParameter<Permutation> PermutationParameter { 43 get { return (SubScopesLookupParameter<Permutation>)Parameters["Permutation"]; } 44 44 } 45 public ILookupParameter<ItemArray<DoubleValue>> QualityParameter {46 get { return ( ILookupParameter<ItemArray<DoubleValue>>)Parameters["Quality"]; }45 public SubScopesLookupParameter<DoubleValue> QualityParameter { 46 get { return (SubScopesLookupParameter<DoubleValue>)Parameters["Quality"]; } 47 47 } 48 public ILookupParameter<PathTSPTour> PathTSPTourParameter {49 get { return ( ILookupParameter<PathTSPTour>)Parameters["PathTSPTour"]; }48 public LookupParameter<PathTSPTour> BestSolutionParameter { 49 get { return (LookupParameter<PathTSPTour>)Parameters["BestSolution"]; } 50 50 } 51 ILookupParameter ISolutionsVisualizer.VisualizationParameter {52 get { return PathTSPTourParameter; }51 public ValueLookupParameter<ResultCollection> ResultsParameter { 52 get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; } 53 53 } 54 54 55 public Best PathTSPTourVisualizer()55 public BestTSPSolutionAnalyzer() 56 56 : base() { 57 57 Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.")); 58 Parameters.Add(new SubScopesLookupParameter<Permutation>("Permutation", "The TSP solutions given in path representation from which the best solution should be visualized.")); 59 Parameters.Add(new SubScopesLookupParameter<DoubleValue>("Quality", "The qualities of the TSP solutions which should be visualized.")); 60 Parameters.Add(new LookupParameter<PathTSPTour>("PathTSPTour", "The visual representation of the best TSP solution.")); 58 Parameters.Add(new SubScopesLookupParameter<Permutation>("Permutation", "The TSP solutions given in path representation from which the best solution should be analyzed.")); 59 Parameters.Add(new SubScopesLookupParameter<DoubleValue>("Quality", "The qualities of the TSP solutions which should be analyzed.")); 60 Parameters.Add(new LookupParameter<PathTSPTour>("BestSolution", "The best TSP solution.")); 61 Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best TSP solution should be stored.")); 61 62 } 62 63 … … 65 66 ItemArray<Permutation> permutations = PermutationParameter.ActualValue; 66 67 ItemArray<DoubleValue> qualities = QualityParameter.ActualValue; 68 ResultCollection results = ResultsParameter.ActualValue; 67 69 68 70 int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index; 69 71 70 PathTSPTour tour = PathTSPTourParameter.ActualValue; 71 if (tour == null) PathTSPTourParameter.ActualValue = new PathTSPTour(coordinates, permutations[i]); 72 else { 73 tour.Coordinates = coordinates; 74 tour.Permutation = permutations[i]; 72 PathTSPTour tour = BestSolutionParameter.ActualValue; 73 if (tour == null) { 74 tour = new PathTSPTour(coordinates, permutations[i], qualities[i]); 75 BestSolutionParameter.ActualValue = tour; 76 results.Add(new Result("Best TSP Solution", tour)); 77 } else { 78 if (tour.Quality.Value > qualities[i].Value) { 79 tour.Coordinates = coordinates; 80 tour.Permutation = permutations[i]; 81 tour.Quality = qualities[i]; 82 results["Best TSP Solution"].Value = tour; 83 } 75 84 } 85 76 86 return base.Apply(); 77 87 } -
trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/HeuristicLab.Problems.TravelingSalesman-3.3.csproj
r3384 r3616 84 84 </ItemGroup> 85 85 <ItemGroup> 86 <Compile Include="Analyzers\BestTSPSolutionAnalyzer.cs" /> 86 87 <Compile Include="Evaluators\TSPEuclideanPathEvaluator.cs" /> 87 88 <Compile Include="Evaluators\TSPGeoPathEvaluator.cs" /> … … 97 98 <Compile Include="TravelingSalesmanProblem.cs" /> 98 99 <Compile Include="TSPLIBTourParser.cs" /> 99 <Compile Include="Interfaces\ICoordinatesTSPSolutionsVisualizer.cs" />100 <Compile Include="Interfaces\IPathCoordinatesTSPSolutionsVisualizer.cs" />101 <Compile Include="Interfaces\ITSPSolutionsVisualizer.cs" />102 100 <Compile Include="PathTSPTour.cs" /> 103 <Compile Include="Visualizers\BestPathTSPTourVisualizer.cs" />104 101 <Compile Include="Evaluators\TSPCoordinatesPathEvaluator.cs" /> 105 102 <Compile Include="Evaluators\TSPEvaluator.cs" /> -
trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/PathTSPTour.cs
r3431 r3616 65 65 } 66 66 } 67 [Storable] 68 private DoubleValue quality; 69 public DoubleValue Quality { 70 get { return quality; } 71 set { 72 if (quality != value) { 73 if (quality != null) DeregisterQualityEvents(); 74 quality = value; 75 if (quality != null) RegisterQualityEvents(); 76 OnQualityChanged(); 77 } 78 } 79 } 67 80 68 81 public PathTSPTour() : base() { } … … 78 91 Initialize(); 79 92 } 93 public PathTSPTour(DoubleMatrix coordinates, Permutation permutation, DoubleValue quality) 94 : base() { 95 this.coordinates = coordinates; 96 this.permutation = permutation; 97 this.quality = quality; 98 Initialize(); 99 } 80 100 [StorableConstructor] 81 101 private PathTSPTour(bool deserializing) : base(deserializing) { } … … 85 105 if (coordinates != null) RegisterCoordinatesEvents(); 86 106 if (permutation != null) RegisterPermutationEvents(); 107 if (quality != null) RegisterQualityEvents(); 87 108 } 88 109 … … 92 113 clone.coordinates = (DoubleMatrix)cloner.Clone(coordinates); 93 114 clone.permutation = (Permutation)cloner.Clone(permutation); 115 clone.quality = (DoubleValue)cloner.Clone(quality); 94 116 clone.Initialize(); 95 117 return clone; … … 106 128 private void OnPermutationChanged() { 107 129 var changed = PermutationChanged; 130 if (changed != null) 131 changed(this, EventArgs.Empty); 132 } 133 public event EventHandler QualityChanged; 134 private void OnQualityChanged() { 135 var changed = QualityChanged; 108 136 if (changed != null) 109 137 changed(this, EventArgs.Empty); … … 126 154 Permutation.Reset -= new EventHandler(Permutation_Reset); 127 155 } 156 private void RegisterQualityEvents() { 157 Quality.ValueChanged += new EventHandler(Quality_ValueChanged); 158 } 159 private void DeregisterQualityEvents() { 160 Quality.ValueChanged -= new EventHandler(Quality_ValueChanged); 161 } 128 162 129 163 private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) { … … 139 173 OnPermutationChanged(); 140 174 } 175 private void Quality_ValueChanged(object sender, EventArgs e) { 176 OnQualityChanged(); 177 } 141 178 #endregion 142 179 } -
trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/TravelingSalesmanProblem.cs
r3504 r3616 71 71 get { return EvaluatorParameter; } 72 72 } 73 public OptionalValueParameter<ITSPSolutionsVisualizer> VisualizerParameter {74 get { return (OptionalValueParameter<ITSPSolutionsVisualizer>)Parameters["Visualizer"]; }75 }76 IParameter IProblem.VisualizerParameter {77 get { return VisualizerParameter; }78 }79 73 public OptionalValueParameter<DoubleValue> BestKnownQualityParameter { 80 74 get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; } … … 118 112 get { return EvaluatorParameter.Value; } 119 113 } 120 public ITSPSolutionsVisualizer Visualizer {121 get { return VisualizerParameter.Value; }122 set { VisualizerParameter.Value = value; }123 }124 ISolutionsVisualizer IProblem.Visualizer {125 get { return VisualizerParameter.Value; }126 }127 114 public DoubleValue BestKnownQuality { 128 115 get { return BestKnownQualityParameter.Value; } … … 133 120 set { BestKnownSolutionParameter.Value = value; } 134 121 } 135 private List<I PermutationOperator> operators;122 private List<IOperator> operators; 136 123 public IEnumerable<IOperator> Operators { 137 get { return operators.Cast<IOperator>(); } 124 get { return operators; } 125 } 126 private BestTSPSolutionAnalyzer Analyzer { 127 get { return operators.OfType<BestTSPSolutionAnalyzer>().FirstOrDefault(); } 138 128 } 139 129 #endregion … … 143 133 RandomPermutationCreator creator = new RandomPermutationCreator(); 144 134 TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator(); 145 BestPathTSPTourVisualizer visualizer = new BestPathTSPTourVisualizer();146 135 147 136 Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false))); … … 151 140 Parameters.Add(new ValueParameter<IPermutationCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator)); 152 141 Parameters.Add(new ValueParameter<ITSPEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator)); 153 Parameters.Add(new OptionalValueParameter<ITSPSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize TSP solutions.", visualizer));154 142 Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance.")); 155 143 Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance.")); … … 166 154 ParameterizeSolutionCreator(); 167 155 ParameterizeEvaluator(); 168 ParameterizeVisualizer();169 156 170 157 Initialize(); … … 221 208 EvaluatorChanged(this, EventArgs.Empty); 222 209 } 223 public event EventHandler VisualizerChanged;224 private void OnVisualizerChanged() {225 if (VisualizerChanged != null)226 VisualizerChanged(this, EventArgs.Empty);227 }228 210 public event EventHandler OperatorsChanged; 229 211 private void OnOperatorsChanged() { … … 249 231 ParameterizeSolutionCreator(); 250 232 ParameterizeEvaluator(); 251 Parameterize Visualizer();233 ParameterizeAnalyzer(); 252 234 ParameterizeOperators(); 253 235 OnSolutionCreatorChanged(); … … 255 237 private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) { 256 238 ParameterizeEvaluator(); 257 Parameterize Visualizer();239 ParameterizeAnalyzer(); 258 240 ParameterizeOperators(); 259 241 } … … 262 244 ParameterizeEvaluator(); 263 245 UpdateMoveEvaluators(); 264 Parameterize Visualizer();246 ParameterizeAnalyzer(); 265 247 ClearDistanceMatrix(); 266 248 OnEvaluatorChanged(); 267 249 } 268 250 private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) { 269 ParameterizeVisualizer(); 270 } 271 private void VisualizerParameter_ValueChanged(object sender, EventArgs e) { 272 ParameterizeVisualizer(); 273 OnVisualizerChanged(); 251 ParameterizeAnalyzer(); 274 252 } 275 253 private void MoveGenerator_InversionMoveParameter_ActualNameChanged(object sender, EventArgs e) { … … 298 276 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged); 299 277 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 300 VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);301 278 } 302 279 303 280 private void InitializeOperators() { 304 operators = new List<IPermutationOperator>(); 305 operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>()); 281 operators = new List<IOperator>(); 282 operators.Add(new BestTSPSolutionAnalyzer()); 283 ParameterizeAnalyzer(); 284 operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>().Cast<IOperator>()); 306 285 ParameterizeOperators(); 307 286 UpdateMoveEvaluators(); … … 344 323 } 345 324 } 346 private void ParameterizeVisualizer() { 347 if (Visualizer != null) { 348 Visualizer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 349 if (Visualizer is ICoordinatesTSPSolutionsVisualizer) 350 ((ICoordinatesTSPSolutionsVisualizer)Visualizer).CoordinatesParameter.ActualName = CoordinatesParameter.Name; 351 if (Visualizer is IPathCoordinatesTSPSolutionsVisualizer) 352 ((IPathCoordinatesTSPSolutionsVisualizer)Visualizer).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 325 private void ParameterizeAnalyzer() { 326 if (Analyzer != null) { 327 Analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 328 Analyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name; 329 Analyzer.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 330 Analyzer.ResultsParameter.ActualName = "Results"; 353 331 } 354 332 }
Note: See TracChangeset
for help on using the changeset viewer.