[12166] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 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 |
|
---|
[13412] | 22 | using System;
|
---|
[13202] | 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[12166] | 27 | using HeuristicLab.Optimization;
|
---|
[13202] | 28 | using HeuristicLab.Parameters;
|
---|
[12166] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Problems.Instances;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.PTSP {
|
---|
[13412] | 33 | [Item("Probabilistic Traveling Salesman Problem (PTSP)", "Represents a Probabilistic Traveling Salesman Problem.")]
|
---|
[12166] | 34 | [StorableClass]
|
---|
[13412] | 35 | public abstract class ProbabilisticTravelingSalesmanProblem : SingleObjectiveBasicProblem<PermutationEncoding>,
|
---|
[12306] | 36 | IProblemInstanceConsumer<PTSPData> {
|
---|
[12228] | 37 | private static readonly int DistanceMatrixSizeLimit = 1000;
|
---|
| 38 |
|
---|
[12183] | 39 | #region Parameter Properties
|
---|
| 40 | public OptionalValueParameter<DoubleMatrix> CoordinatesParameter {
|
---|
| 41 | get { return (OptionalValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
| 42 | }
|
---|
[13412] | 43 | public OptionalValueParameter<DistanceCalculator> DistanceCalculatorParameter {
|
---|
| 44 | get { return (OptionalValueParameter<DistanceCalculator>)Parameters["DistanceCalculator"]; }
|
---|
| 45 | }
|
---|
[12183] | 46 | public OptionalValueParameter<DistanceMatrix> DistanceMatrixParameter {
|
---|
| 47 | get { return (OptionalValueParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
|
---|
| 48 | }
|
---|
[13412] | 49 | public IFixedValueParameter<BoolValue> UseDistanceMatrixParameter {
|
---|
| 50 | get { return (IFixedValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
|
---|
[12183] | 51 | }
|
---|
| 52 | public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
|
---|
| 53 | get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
|
---|
| 54 | }
|
---|
[13412] | 55 | public IValueParameter<DoubleArray> ProbabilitiesParameter {
|
---|
| 56 | get { return (IValueParameter<DoubleArray>)Parameters["Probabilities"]; }
|
---|
[12183] | 57 | }
|
---|
| 58 | #endregion
|
---|
| 59 |
|
---|
| 60 | #region Properties
|
---|
| 61 | public DoubleMatrix Coordinates {
|
---|
| 62 | get { return CoordinatesParameter.Value; }
|
---|
| 63 | set { CoordinatesParameter.Value = value; }
|
---|
| 64 | }
|
---|
[13412] | 65 | public DistanceCalculator DistanceCalculator {
|
---|
| 66 | get { return DistanceCalculatorParameter.Value; }
|
---|
| 67 | set { DistanceCalculatorParameter.Value = value; }
|
---|
| 68 | }
|
---|
[12183] | 69 | public DistanceMatrix DistanceMatrix {
|
---|
| 70 | get { return DistanceMatrixParameter.Value; }
|
---|
| 71 | set { DistanceMatrixParameter.Value = value; }
|
---|
| 72 | }
|
---|
[13412] | 73 | public bool UseDistanceMatrix {
|
---|
| 74 | get { return UseDistanceMatrixParameter.Value.Value; }
|
---|
| 75 | set { UseDistanceMatrixParameter.Value.Value = value; }
|
---|
[12183] | 76 | }
|
---|
| 77 | public Permutation BestKnownSolution {
|
---|
| 78 | get { return BestKnownSolutionParameter.Value; }
|
---|
| 79 | set { BestKnownSolutionParameter.Value = value; }
|
---|
| 80 | }
|
---|
[13412] | 81 | public DoubleArray Probabilities {
|
---|
| 82 | get { return ProbabilitiesParameter.Value; }
|
---|
| 83 | set { ProbabilitiesParameter.Value = value; }
|
---|
[12183] | 84 | }
|
---|
[13202] | 85 |
|
---|
[12183] | 86 | #endregion
|
---|
[12166] | 87 |
|
---|
| 88 | public override bool Maximization {
|
---|
| 89 | get { return false; }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | [StorableConstructor]
|
---|
[12191] | 93 | protected ProbabilisticTravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
|
---|
[13412] | 94 | protected ProbabilisticTravelingSalesmanProblem(ProbabilisticTravelingSalesmanProblem original, Cloner cloner) : base(original, cloner) { }
|
---|
| 95 | protected ProbabilisticTravelingSalesmanProblem() {
|
---|
[12183] | 96 | Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
|
---|
[13412] | 97 | Parameters.Add(new OptionalValueParameter<DistanceCalculator>("DistanceCalculator", "Calculates the distance between two rows in the coordinates matrix."));
|
---|
[12183] | 98 | Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
[13412] | 99 | Parameters.Add(new FixedValueParameter<BoolValue>("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)));
|
---|
[12183] | 100 | Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
|
---|
[13412] | 101 | Parameters.Add(new ValueParameter<DoubleArray>("Probabilities", "This list describes for each city the probability of appearing in a realized instance."));
|
---|
[12228] | 102 |
|
---|
| 103 | Coordinates = new DoubleMatrix(new double[,] {
|
---|
| 104 | { 100, 100 }, { 100, 200 }, { 100, 300 }, { 100, 400 },
|
---|
| 105 | { 200, 100 }, { 200, 200 }, { 200, 300 }, { 200, 400 },
|
---|
| 106 | { 300, 100 }, { 300, 200 }, { 300, 300 }, { 300, 400 },
|
---|
| 107 | { 400, 100 }, { 400, 200 }, { 400, 300 }, { 400, 400 }
|
---|
| 108 | });
|
---|
| 109 |
|
---|
[13412] | 110 | Probabilities = new DoubleArray(new double[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });
|
---|
| 111 | }
|
---|
[13202] | 112 |
|
---|
[13412] | 113 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
| 114 | return Evaluate(individual.Permutation(), random);
|
---|
[12166] | 115 | }
|
---|
| 116 |
|
---|
[13412] | 117 | public abstract double Evaluate(Permutation tour, IRandom random);
|
---|
| 118 |
|
---|
[12306] | 119 | public virtual void Load(PTSPData data) {
|
---|
[12228] | 120 | if (data.Coordinates == null && data.Distances == null)
|
---|
| 121 | throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!");
|
---|
[13412] | 122 | if (data.Dimension > DistanceMatrixSizeLimit && (data.Coordinates == null || data.Coordinates.GetLength(0) != data.Dimension || data.Coordinates.GetLength(1) != 2))
|
---|
| 123 | throw new System.IO.InvalidDataException("The given instance is too large for using a distance matrix and there is a problem with the coordinates.");
|
---|
| 124 | if (data.Coordinates != null && (data.Coordinates.GetLength(0) != data.Dimension || data.Coordinates.GetLength(1) != 2))
|
---|
| 125 | 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.");
|
---|
[12228] | 126 |
|
---|
[13412] | 127 | switch (data.DistanceMeasure) {
|
---|
| 128 | case DistanceMeasure.Direct:
|
---|
| 129 | DistanceCalculator = null;
|
---|
| 130 | if (data.Dimension > DistanceMatrixSizeLimit && Coordinates != null) {
|
---|
| 131 | DistanceCalculator = new EuclideanDistance();
|
---|
| 132 | UseDistanceMatrix = false;
|
---|
| 133 | } else UseDistanceMatrix = true;
|
---|
| 134 | break;
|
---|
| 135 | case DistanceMeasure.Att: DistanceCalculator = new AttDistance(); break;
|
---|
| 136 | case DistanceMeasure.Euclidean: DistanceCalculator = new EuclideanDistance(); break;
|
---|
| 137 | case DistanceMeasure.Geo: DistanceCalculator = new GeoDistance(); break;
|
---|
| 138 | case DistanceMeasure.Manhattan: DistanceCalculator = new ManhattanDistance(); break;
|
---|
| 139 | case DistanceMeasure.Maximum: DistanceCalculator = new MaximumDistance(); break;
|
---|
| 140 | case DistanceMeasure.RoundedEuclidean: DistanceCalculator = new RoundedEuclideanDistance(); break;
|
---|
| 141 | case DistanceMeasure.UpperEuclidean: DistanceCalculator = new UpperEuclideanDistance(); break;
|
---|
| 142 | default: throw new ArgumentException("Distance measure is unknown");
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[12228] | 145 | Name = data.Name;
|
---|
| 146 | Description = data.Description;
|
---|
| 147 |
|
---|
[13412] | 148 | Probabilities = new DoubleArray(data.Probabilities);
|
---|
| 149 | BestKnownSolution = data.BestKnownTour != null ? new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour) : null;
|
---|
[13202] | 150 | Coordinates = data.Coordinates != null && data.Coordinates.GetLength(0) > 0 ? new DoubleMatrix(data.Coordinates) : null;
|
---|
| 151 | DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
|
---|
[12228] | 152 |
|
---|
[12183] | 153 | Encoding.Length = data.Dimension;
|
---|
[12228] | 154 |
|
---|
| 155 | OnReset();
|
---|
[12166] | 156 | }
|
---|
| 157 | }
|
---|
| 158 | }
|
---|