#region License Information /* HeuristicLab * Copyright (C) 2002-2015 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.Data; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.Instances; namespace HeuristicLab.Problems.PTSP { [Item("Probabilistic Traveling Salesman Problem (PTSP)", "Represents a Probabilistic Traveling Salesman Problem.")] [StorableClass] public abstract class ProbabilisticTravelingSalesmanProblem : SingleObjectiveBasicProblem, IProblemInstanceConsumer { private static readonly int DistanceMatrixSizeLimit = 1000; #region Parameter Properties public OptionalValueParameter CoordinatesParameter { get { return (OptionalValueParameter)Parameters["Coordinates"]; } } public OptionalValueParameter DistanceCalculatorParameter { get { return (OptionalValueParameter)Parameters["DistanceCalculator"]; } } public OptionalValueParameter DistanceMatrixParameter { get { return (OptionalValueParameter)Parameters["DistanceMatrix"]; } } public IFixedValueParameter UseDistanceMatrixParameter { get { return (IFixedValueParameter)Parameters["UseDistanceMatrix"]; } } public OptionalValueParameter BestKnownSolutionParameter { get { return (OptionalValueParameter)Parameters["BestKnownSolution"]; } } public IValueParameter ProbabilitiesParameter { get { return (IValueParameter)Parameters["Probabilities"]; } } #endregion #region Properties public DoubleMatrix Coordinates { get { return CoordinatesParameter.Value; } set { CoordinatesParameter.Value = value; } } public DistanceCalculator DistanceCalculator { get { return DistanceCalculatorParameter.Value; } set { DistanceCalculatorParameter.Value = value; } } public DistanceMatrix DistanceMatrix { get { return DistanceMatrixParameter.Value; } set { DistanceMatrixParameter.Value = value; } } public bool UseDistanceMatrix { get { return UseDistanceMatrixParameter.Value.Value; } set { UseDistanceMatrixParameter.Value.Value = value; } } public Permutation BestKnownSolution { get { return BestKnownSolutionParameter.Value; } set { BestKnownSolutionParameter.Value = value; } } public DoubleArray Probabilities { get { return ProbabilitiesParameter.Value; } set { ProbabilitiesParameter.Value = value; } } #endregion public override bool Maximization { get { return false; } } [StorableConstructor] protected ProbabilisticTravelingSalesmanProblem(bool deserializing) : base(deserializing) { } protected ProbabilisticTravelingSalesmanProblem(ProbabilisticTravelingSalesmanProblem original, Cloner cloner) : base(original, cloner) { } protected ProbabilisticTravelingSalesmanProblem() { Parameters.Add(new OptionalValueParameter("Coordinates", "The x- and y-Coordinates of the cities.")); Parameters.Add(new OptionalValueParameter("DistanceCalculator", "Calculates the distance between two rows in the coordinates matrix.")); Parameters.Add(new OptionalValueParameter("DistanceMatrix", "The matrix which contains the distances between the cities.")); Parameters.Add(new FixedValueParameter("UseDistanceMatrix", "True if the coordinates based evaluators should calculate the distance matrix from the coordinates and use it for evaluation similar to the distance matrix evaluator, otherwise false.", new BoolValue(true))); Parameters.Add(new OptionalValueParameter("BestKnownSolution", "The best known solution of this TSP instance.")); Parameters.Add(new ValueParameter("Probabilities", "This list describes for each city the probability of appearing in a realized instance.")); Coordinates = new DoubleMatrix(new double[,] { { 100, 100 }, { 100, 200 }, { 100, 300 }, { 100, 400 }, { 200, 100 }, { 200, 200 }, { 200, 300 }, { 200, 400 }, { 300, 100 }, { 300, 200 }, { 300, 300 }, { 300, 400 }, { 400, 100 }, { 400, 200 }, { 400, 300 }, { 400, 400 } }); Probabilities = new DoubleArray(new double[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }); } public override double Evaluate(Individual individual, IRandom random) { return Evaluate(individual.Permutation(), random); } public abstract double Evaluate(Permutation tour, IRandom random); public virtual void Load(PTSPData data) { if (data.Coordinates == null && data.Distances == null) throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!"); if (data.Dimension > DistanceMatrixSizeLimit && (data.Coordinates == null || data.Coordinates.GetLength(0) != data.Dimension || data.Coordinates.GetLength(1) != 2)) throw new System.IO.InvalidDataException("The given instance is too large for using a distance matrix and there is a problem with the coordinates."); if (data.Coordinates != null && (data.Coordinates.GetLength(0) != data.Dimension || data.Coordinates.GetLength(1) != 2)) throw new System.IO.InvalidDataException("The coordinates of the given instance are not in the right format, there need to be one row for each customer and two columns for the x and y coordinates respectively."); switch (data.DistanceMeasure) { case DistanceMeasure.Direct: DistanceCalculator = null; if (data.Dimension > DistanceMatrixSizeLimit && Coordinates != null) { DistanceCalculator = new EuclideanDistance(); UseDistanceMatrix = false; } else UseDistanceMatrix = true; break; case DistanceMeasure.Att: DistanceCalculator = new AttDistance(); break; case DistanceMeasure.Euclidean: DistanceCalculator = new EuclideanDistance(); break; case DistanceMeasure.Geo: DistanceCalculator = new GeoDistance(); break; case DistanceMeasure.Manhattan: DistanceCalculator = new ManhattanDistance(); break; case DistanceMeasure.Maximum: DistanceCalculator = new MaximumDistance(); break; case DistanceMeasure.RoundedEuclidean: DistanceCalculator = new RoundedEuclideanDistance(); break; case DistanceMeasure.UpperEuclidean: DistanceCalculator = new UpperEuclideanDistance(); break; default: throw new ArgumentException("Distance measure is unknown"); } Name = data.Name; Description = data.Description; Probabilities = new DoubleArray(data.Probabilities); BestKnownSolution = data.BestKnownTour != null ? new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour) : null; Coordinates = data.Coordinates != null && data.Coordinates.GetLength(0) > 0 ? new DoubleMatrix(data.Coordinates) : null; DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix()); Encoding.Length = data.Dimension; OnReset(); } } }