Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/MultiPopulationBestTSPSolutionAnalyzer.cs @ 3659

Last change on this file since 3659 was 3659, checked in by swagner, 14 years ago

Worked on refactoring of algorithm analysis and tracing (#999)

  • removed SubScopesSubScopesLookupParameter
  • adapted SubScopesLookupParameter and renamed it into ScopeTreeLookupParameter
File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using System.Collections.Generic;
33
34namespace HeuristicLab.Problems.TravelingSalesman {
35  /// <summary>
36  /// An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.
37  /// </summary>
38  [Item("MultiPopulationBestTSPSolutionAnalyzer", "An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
39  [StorableClass]
40  public sealed class MultiPopulationBestTSPSolutionAnalyzer : SingleSuccessorOperator, IBestTSPSolutionAnalyzer, IAnalyzer {
41    public ILookupParameter<DoubleMatrix> CoordinatesParameter {
42      get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
43    }
44    public ILookupParameter<ItemArray<ItemArray<Permutation>>> PermutationParameter {
45      get { return (ILookupParameter<ItemArray<ItemArray<Permutation>>>)Parameters["Permutation"]; }
46    }
47    ILookupParameter IBestTSPSolutionAnalyzer.PermutationParameter {
48      get { return PermutationParameter; }
49    }
50    public ILookupParameter<ItemArray<ItemArray<DoubleValue>>> QualityParameter {
51      get { return (ILookupParameter<ItemArray<ItemArray<DoubleValue>>>)Parameters["Quality"]; }
52    }
53    ILookupParameter IBestTSPSolutionAnalyzer.QualityParameter {
54      get { return QualityParameter; }
55    }
56    public ILookupParameter<PathTSPTour> BestSolutionParameter {
57      get { return (ILookupParameter<PathTSPTour>)Parameters["BestSolution"]; }
58    }
59    public IValueLookupParameter<ResultCollection> ResultsParameter {
60      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
61    }
62
63    public MultiPopulationBestTSPSolutionAnalyzer()
64      : base() {
65      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
66      Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Permutation", "The TSP solutions given in path representation from which the best solution should be analyzed."));
67      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the TSP solutions which should be analyzed."));
68      Parameters.Add(new LookupParameter<PathTSPTour>("BestSolution", "The best TSP solution."));
69      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best TSP solution should be stored."));
70    }
71
72    public override IOperation Apply() {
73      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
74      ItemArray<ItemArray<Permutation>> permutations = PermutationParameter.ActualValue;
75      ItemArray<ItemArray<DoubleValue>> qualities = QualityParameter.ActualValue;
76      ResultCollection results = ResultsParameter.ActualValue;
77
78      DoubleValue bestQuality = new DoubleValue(double.MaxValue);
79      Permutation bestPermutation = null;
80
81      for (int i = 0; i < qualities.Length; i++) {
82        for (int j = 0; j < qualities[i].Length; j++) {
83          if (qualities[i][j].Value < bestQuality.Value) {
84            bestQuality = qualities[i][j];
85            bestPermutation = permutations[i][j];
86          }
87        }
88      }
89
90      PathTSPTour tour = BestSolutionParameter.ActualValue;
91      if (tour == null) {
92        tour = new PathTSPTour(coordinates, bestPermutation, bestQuality);
93        BestSolutionParameter.ActualValue = tour;
94        results.Add(new Result("Best TSP Solution", tour));
95      } else {
96        if (tour.Quality.Value > bestQuality.Value) {
97          tour.Coordinates = coordinates;
98          tour.Permutation = bestPermutation;
99          tour.Quality = bestQuality;
100          results["Best TSP Solution"].Value = tour;
101        }
102      }
103
104      return base.Apply();
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.