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