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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using System.Collections.Generic;
|
---|
33 |
|
---|
34 | namespace 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, IMultiPopulationAnalyzer {
|
---|
41 | public LookupParameter<DoubleMatrix> CoordinatesParameter {
|
---|
42 | get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
43 | }
|
---|
44 | public SubScopesSubScopesLookupParameter<Permutation> PermutationParameter {
|
---|
45 | get { return (SubScopesSubScopesLookupParameter<Permutation>)Parameters["Permutation"]; }
|
---|
46 | }
|
---|
47 | public SubScopesSubScopesLookupParameter<DoubleValue> QualityParameter {
|
---|
48 | get { return (SubScopesSubScopesLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
49 | }
|
---|
50 | public LookupParameter<PathTSPTour> BestSolutionParameter {
|
---|
51 | get { return (LookupParameter<PathTSPTour>)Parameters["BestSolution"]; }
|
---|
52 | }
|
---|
53 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
54 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public MultiPopulationBestTSPSolutionAnalyzer()
|
---|
58 | : base() {
|
---|
59 | Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
|
---|
60 | Parameters.Add(new SubScopesSubScopesLookupParameter<Permutation>("Permutation", "The TSP solutions given in path representation from which the best solution should be analyzed."));
|
---|
61 | Parameters.Add(new SubScopesSubScopesLookupParameter<DoubleValue>("Quality", "The qualities of the TSP solutions which should be analyzed."));
|
---|
62 | Parameters.Add(new LookupParameter<PathTSPTour>("BestSolution", "The best TSP solution."));
|
---|
63 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best TSP solution should be stored."));
|
---|
64 | }
|
---|
65 |
|
---|
66 | public override IOperation Apply() {
|
---|
67 | DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
|
---|
68 | ItemArray<ItemArray<Permutation>> permutations = PermutationParameter.ActualValue;
|
---|
69 | ItemArray<ItemArray<DoubleValue>> qualities = QualityParameter.ActualValue;
|
---|
70 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
71 |
|
---|
72 | DoubleValue bestQuality = new DoubleValue(double.MaxValue);
|
---|
73 | Permutation bestPermutation = null;
|
---|
74 |
|
---|
75 | for (int i = 0; i < qualities.Length; i++) {
|
---|
76 | for (int j = 0; j < qualities[i].Length; j++) {
|
---|
77 | if (qualities[i][j].Value < bestQuality.Value) {
|
---|
78 | bestQuality = qualities[i][j];
|
---|
79 | bestPermutation = permutations[i][j];
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | PathTSPTour tour = BestSolutionParameter.ActualValue;
|
---|
85 | if (tour == null) {
|
---|
86 | tour = new PathTSPTour(coordinates, bestPermutation, bestQuality);
|
---|
87 | BestSolutionParameter.ActualValue = tour;
|
---|
88 | results.Add(new Result("Best TSP Solution", tour));
|
---|
89 | } else {
|
---|
90 | if (tour.Quality.Value > bestQuality.Value) {
|
---|
91 | tour.Coordinates = coordinates;
|
---|
92 | tour.Permutation = bestPermutation;
|
---|
93 | tour.Quality = bestQuality;
|
---|
94 | results["Best TSP Solution"].Value = tour;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | return base.Apply();
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|