[2997] | 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 HeuristicLab.Core;
|
---|
| 23 | using HeuristicLab.Data;
|
---|
| 24 | using HeuristicLab.Encodings.Permutation;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using System;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.TSP {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// An operator to evaluate 2-opt moves.
|
---|
| 33 | /// </summary>
|
---|
| 34 | [Item("TwoOptMoveTSPEvaluator", "Evaluates a 2-opt move (rounded euclidean distances) by summing up the length of all added edges and subtracting the length of all deleted edges.")]
|
---|
| 35 | [StorableClass(StorableClassType.Empty)]
|
---|
| 36 | public class TwoOptMoveTSPEvaluator : SingleSuccessorOperator {
|
---|
| 37 | public ILookupParameter<DoubleData> QualityParameter {
|
---|
| 38 | get { return (ILookupParameter<DoubleData>)Parameters["Quality"]; }
|
---|
| 39 | }
|
---|
| 40 | public LookupParameter<TwoOptMove> MoveParameter {
|
---|
| 41 | get { return (LookupParameter<TwoOptMove>)Parameters["Move"]; }
|
---|
| 42 | }
|
---|
| 43 | public LookupParameter<DoubleData> MoveQualityParameter {
|
---|
| 44 | get { return (LookupParameter<DoubleData>)Parameters["MoveQuality"]; }
|
---|
| 45 | }
|
---|
| 46 | public ILookupParameter<Permutation> PermutationParameter {
|
---|
| 47 | get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
|
---|
| 48 | }
|
---|
| 49 | public ILookupParameter<DoubleMatrixData> CoordinatesParameter {
|
---|
| 50 | get { return (ILookupParameter<DoubleMatrixData>)Parameters["Coordinates"]; }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public TwoOptMoveTSPEvaluator()
|
---|
| 54 | : base() {
|
---|
| 55 | Parameters.Add(new LookupParameter<DoubleData>("Quality", "The evaluated quality of the TSP solution."));
|
---|
| 56 | Parameters.Add(new LookupParameter<TwoOptMove>("Move", "The move to evaluate."));
|
---|
| 57 | Parameters.Add(new LookupParameter<DoubleData>("MoveQuality", "Where to store the move quality."));
|
---|
| 58 | Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
|
---|
| 59 | Parameters.Add(new LookupParameter<DoubleMatrixData>("Coordinates", "The city's coordinates."));
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public override IOperation Apply() {
|
---|
| 63 | TwoOptMove move = MoveParameter.ActualValue;
|
---|
| 64 | Permutation permutation = PermutationParameter.ActualValue;
|
---|
| 65 | DoubleMatrixData coordinates = CoordinatesParameter.ActualValue;
|
---|
| 66 | double moveQuality = QualityParameter.ActualValue.Value;
|
---|
| 67 | int edge1source = permutation.GetCircular(move.Index1 - 1);
|
---|
| 68 | int edge1target = permutation[move.Index1];
|
---|
| 69 | int edge2source = permutation[move.Index2];
|
---|
| 70 | int edge2target = permutation.GetCircular(move.Index2 + 1);
|
---|
| 71 | // remove two edges
|
---|
| 72 | moveQuality -= CalculateDistance(coordinates[edge1source ,0], coordinates[edge1source, 1],
|
---|
| 73 | coordinates[edge1target, 0], coordinates[edge1target, 1]);
|
---|
| 74 | moveQuality -= CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
|
---|
| 75 | coordinates[edge2target, 0], coordinates[edge2target, 1]);
|
---|
| 76 | // add two edges
|
---|
| 77 | moveQuality += CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
|
---|
| 78 | coordinates[edge2source, 0], coordinates[edge2source, 1]);
|
---|
| 79 | moveQuality += CalculateDistance(coordinates[edge1target, 0], coordinates[edge1target, 1],
|
---|
| 80 | coordinates[edge2target, 0], coordinates[edge2target, 1]);
|
---|
| 81 | MoveQualityParameter.ActualValue = new DoubleData(moveQuality);
|
---|
| 82 | return base.Apply();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | private double CalculateDistance(double x1, double y1, double x2, double y2) {
|
---|
| 86 | return Math.Round(Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|