#region License Information /* HeuristicLab * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.TravelingSalesman { /// /// An operator which evaluates TSP solutions given in path representation using the GEO distance metric (globe coordinates). /// [Item("TSPGeoPathEvaluator", "An operator which evaluates TSP solutions given in path representation using the GEO distance metric (globe coordinates).")] [StorableClass] public sealed class TSPGeoPathEvaluator : TSPCoordinatesPathEvaluator { private const double PI = 3.141592; private const double RADIUS = 6378.388; [StorableConstructor] private TSPGeoPathEvaluator(bool deserializing) : base(deserializing) { } private TSPGeoPathEvaluator(TSPGeoPathEvaluator original, Cloner cloner) : base(original, cloner) { } public TSPGeoPathEvaluator() : base() { } public override IDeepCloneable Clone(Cloner cloner) { return new TSPGeoPathEvaluator(this, cloner); } /// /// Calculates the distance between two points using the GEO distance metric (globe coordinates). /// /// The x-coordinate of point 1. /// The y-coordinate of point 1. /// The x-coordinate of point 2. /// The y-coordinate of point 2. /// The calculated distance. protected override double CalculateDistance(double x1, double y1, double x2, double y2) { double latitude1, longitude1, latitude2, longitude2; double q1, q2, q3; double length; latitude1 = ConvertToRadian(x1); longitude1 = ConvertToRadian(y1); latitude2 = ConvertToRadian(x2); longitude2 = ConvertToRadian(y2); q1 = Math.Cos(longitude1 - longitude2); q2 = Math.Cos(latitude1 - latitude2); q3 = Math.Cos(latitude1 + latitude2); length = (int)(RADIUS * Math.Acos(0.5 * ((1.0 + q1) * q2 - (1.0 - q1) * q3)) + 1.0); return (length); } private double ConvertToRadian(double x) { return PI * (Math.Truncate(x) + 5.0 * (x - Math.Truncate(x)) / 3.0) / 180.0; } } }