[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 |
|
---|
[13202] | 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[12166] | 26 | using HeuristicLab.Optimization;
|
---|
[13202] | 27 | using HeuristicLab.Parameters;
|
---|
[12166] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | using HeuristicLab.Problems.Instances;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.PTSP {
|
---|
| 32 | [Item("Probabilistic Traveling Salesman Problem", "Represents a Probabilistic Traveling Salesman Problem.")]
|
---|
| 33 | [StorableClass]
|
---|
[12191] | 34 | public abstract class ProbabilisticTravelingSalesmanProblem : SingleObjectiveBasicProblem<PermutationEncoding>, IStorableContent,
|
---|
[12306] | 35 | IProblemInstanceConsumer<PTSPData> {
|
---|
[12228] | 36 | private static readonly int DistanceMatrixSizeLimit = 1000;
|
---|
| 37 |
|
---|
[12183] | 38 | #region Parameter Properties
|
---|
| 39 | public OptionalValueParameter<DoubleMatrix> CoordinatesParameter {
|
---|
| 40 | get { return (OptionalValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
| 41 | }
|
---|
| 42 | public OptionalValueParameter<DistanceMatrix> DistanceMatrixParameter {
|
---|
| 43 | get { return (OptionalValueParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
|
---|
| 44 | }
|
---|
| 45 | public ValueParameter<BoolValue> UseDistanceMatrixParameter {
|
---|
| 46 | get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
|
---|
| 47 | }
|
---|
| 48 | public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
|
---|
| 49 | get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
|
---|
| 50 | }
|
---|
[12228] | 51 | public ValueParameter<DoubleArray> ProbabilityMatrixParameter {
|
---|
| 52 | get { return (ValueParameter<DoubleArray>)Parameters["ProbabilityMatrix"]; }
|
---|
[12183] | 53 | }
|
---|
| 54 |
|
---|
[13202] | 55 |
|
---|
[12183] | 56 | #endregion
|
---|
| 57 |
|
---|
| 58 | #region Properties
|
---|
| 59 | public DoubleMatrix Coordinates {
|
---|
| 60 | get { return CoordinatesParameter.Value; }
|
---|
| 61 | set { CoordinatesParameter.Value = value; }
|
---|
| 62 | }
|
---|
| 63 | public DistanceMatrix DistanceMatrix {
|
---|
| 64 | get { return DistanceMatrixParameter.Value; }
|
---|
| 65 | set { DistanceMatrixParameter.Value = value; }
|
---|
| 66 | }
|
---|
| 67 | public BoolValue UseDistanceMatrix {
|
---|
| 68 | get { return UseDistanceMatrixParameter.Value; }
|
---|
| 69 | set { UseDistanceMatrixParameter.Value = value; }
|
---|
| 70 | }
|
---|
| 71 | public Permutation BestKnownSolution {
|
---|
| 72 | get { return BestKnownSolutionParameter.Value; }
|
---|
| 73 | set { BestKnownSolutionParameter.Value = value; }
|
---|
| 74 | }
|
---|
[12228] | 75 | public DoubleArray ProbabilityMatrix {
|
---|
[12183] | 76 | get { return ProbabilityMatrixParameter.Value; }
|
---|
| 77 | set { ProbabilityMatrixParameter.Value = value; }
|
---|
| 78 | }
|
---|
[13202] | 79 |
|
---|
[12183] | 80 | #endregion
|
---|
[12166] | 81 |
|
---|
[13202] | 82 |
|
---|
[12166] | 83 | public override bool Maximization {
|
---|
| 84 | get { return false; }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | [StorableConstructor]
|
---|
[12191] | 88 | protected ProbabilisticTravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
|
---|
| 89 | protected ProbabilisticTravelingSalesmanProblem(ProbabilisticTravelingSalesmanProblem original, Cloner cloner)
|
---|
[12166] | 90 | : base(original, cloner) {
|
---|
| 91 | }
|
---|
[12191] | 92 |
|
---|
[12166] | 93 | public ProbabilisticTravelingSalesmanProblem() {
|
---|
[12183] | 94 | Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
|
---|
| 95 | Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
| 96 | Parameters.Add(new ValueParameter<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)));
|
---|
| 97 | Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
|
---|
[12228] | 98 | Parameters.Add(new ValueParameter<DoubleArray>("ProbabilityMatrix", "The matrix which contains the probabilities of each of the cities."));
|
---|
| 99 |
|
---|
| 100 | Coordinates = new DoubleMatrix(new double[,] {
|
---|
| 101 | { 100, 100 }, { 100, 200 }, { 100, 300 }, { 100, 400 },
|
---|
| 102 | { 200, 100 }, { 200, 200 }, { 200, 300 }, { 200, 400 },
|
---|
| 103 | { 300, 100 }, { 300, 200 }, { 300, 300 }, { 300, 400 },
|
---|
| 104 | { 400, 100 }, { 400, 200 }, { 400, 300 }, { 400, 400 }
|
---|
| 105 | });
|
---|
| 106 |
|
---|
[13202] | 107 | ProbabilityMatrix = new DoubleArray(new double[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });
|
---|
| 108 |
|
---|
[12166] | 109 | }
|
---|
| 110 |
|
---|
[12306] | 111 | public virtual void Load(PTSPData data) {
|
---|
[12228] | 112 | if (data.Coordinates == null && data.Distances == null)
|
---|
| 113 | throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!");
|
---|
| 114 | if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == DistanceMeasure.Att
|
---|
| 115 | || data.DistanceMeasure == DistanceMeasure.Manhattan
|
---|
| 116 | || data.DistanceMeasure == DistanceMeasure.Maximum))
|
---|
| 117 | throw new System.IO.InvalidDataException("The given instance uses an unsupported distance measure and is too large for using a distance matrix.");
|
---|
| 118 | if (data.Coordinates != null && data.Coordinates.GetLength(1) != 2)
|
---|
| 119 | 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.");
|
---|
| 120 |
|
---|
| 121 | Name = data.Name;
|
---|
| 122 | Description = data.Description;
|
---|
| 123 |
|
---|
[12306] | 124 | ProbabilityMatrix = new DoubleArray(data.Probabilities);
|
---|
[12261] | 125 |
|
---|
| 126 | if (data.BestKnownTour != null) {
|
---|
| 127 | BestKnownSolution = new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour);
|
---|
| 128 | }
|
---|
[12228] | 129 |
|
---|
[13202] | 130 | Coordinates = data.Coordinates != null && data.Coordinates.GetLength(0) > 0 ? new DoubleMatrix(data.Coordinates) : null;
|
---|
| 131 | DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
|
---|
[12228] | 132 |
|
---|
[12183] | 133 | Encoding.Length = data.Dimension;
|
---|
[12228] | 134 |
|
---|
| 135 | OnReset();
|
---|
[12166] | 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|