[3097] | 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 |
|
---|
[3634] | 22 | using System;
|
---|
[3107] | 23 | using System.Linq;
|
---|
[3376] | 24 | using HeuristicLab.Common;
|
---|
[3097] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
[3107] | 29 | using HeuristicLab.Optimization;
|
---|
[3097] | 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3634] | 32 | using System.Collections.Generic;
|
---|
[3097] | 33 |
|
---|
[3158] | 34 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
[3097] | 35 | /// <summary>
|
---|
[3616] | 36 | /// An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.
|
---|
[3097] | 37 | /// </summary>
|
---|
[3634] | 38 | [Item("MultiPopulationBestTSPSolutionAnalyzer", "An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
|
---|
[3097] | 39 | [StorableClass]
|
---|
[3658] | 40 | public sealed class MultiPopulationBestTSPSolutionAnalyzer : SingleSuccessorOperator, IBestTSPSolutionAnalyzer, IAnalyzer {
|
---|
[3635] | 41 | public ILookupParameter<DoubleMatrix> CoordinatesParameter {
|
---|
| 42 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
[3097] | 43 | }
|
---|
[3635] | 44 | public ILookupParameter<ItemArray<ItemArray<Permutation>>> PermutationParameter {
|
---|
| 45 | get { return (ILookupParameter<ItemArray<ItemArray<Permutation>>>)Parameters["Permutation"]; }
|
---|
[3097] | 46 | }
|
---|
[3635] | 47 | ILookupParameter IBestTSPSolutionAnalyzer.PermutationParameter {
|
---|
| 48 | get { return PermutationParameter; }
|
---|
[3097] | 49 | }
|
---|
[3635] | 50 | public ILookupParameter<ItemArray<ItemArray<DoubleValue>>> QualityParameter {
|
---|
| 51 | get { return (ILookupParameter<ItemArray<ItemArray<DoubleValue>>>)Parameters["Quality"]; }
|
---|
[3107] | 52 | }
|
---|
[3635] | 53 | ILookupParameter IBestTSPSolutionAnalyzer.QualityParameter {
|
---|
| 54 | get { return QualityParameter; }
|
---|
[3107] | 55 | }
|
---|
[3635] | 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 | }
|
---|
[3097] | 62 |
|
---|
[3634] | 63 | public MultiPopulationBestTSPSolutionAnalyzer()
|
---|
[3097] | 64 | : base() {
|
---|
| 65 | Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
|
---|
[3634] | 66 | Parameters.Add(new SubScopesSubScopesLookupParameter<Permutation>("Permutation", "The TSP solutions given in path representation from which the best solution should be analyzed."));
|
---|
| 67 | Parameters.Add(new SubScopesSubScopesLookupParameter<DoubleValue>("Quality", "The qualities of the TSP solutions which should be analyzed."));
|
---|
[3616] | 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."));
|
---|
[3097] | 70 | }
|
---|
| 71 |
|
---|
| 72 | public override IOperation Apply() {
|
---|
| 73 | DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
|
---|
[3634] | 74 | ItemArray<ItemArray<Permutation>> permutations = PermutationParameter.ActualValue;
|
---|
| 75 | ItemArray<ItemArray<DoubleValue>> qualities = QualityParameter.ActualValue;
|
---|
[3616] | 76 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
[3107] | 77 |
|
---|
[3634] | 78 | DoubleValue bestQuality = new DoubleValue(double.MaxValue);
|
---|
| 79 | Permutation bestPermutation = null;
|
---|
[3107] | 80 |
|
---|
[3634] | 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 |
|
---|
[3616] | 90 | PathTSPTour tour = BestSolutionParameter.ActualValue;
|
---|
| 91 | if (tour == null) {
|
---|
[3634] | 92 | tour = new PathTSPTour(coordinates, bestPermutation, bestQuality);
|
---|
[3616] | 93 | BestSolutionParameter.ActualValue = tour;
|
---|
| 94 | results.Add(new Result("Best TSP Solution", tour));
|
---|
| 95 | } else {
|
---|
[3634] | 96 | if (tour.Quality.Value > bestQuality.Value) {
|
---|
[3616] | 97 | tour.Coordinates = coordinates;
|
---|
[3634] | 98 | tour.Permutation = bestPermutation;
|
---|
| 99 | tour.Quality = bestQuality;
|
---|
[3616] | 100 | results["Best TSP Solution"].Value = tour;
|
---|
| 101 | }
|
---|
[3107] | 102 | }
|
---|
[3616] | 103 |
|
---|
[3097] | 104 | return base.Apply();
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|