Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PTSP/HeuristicLab.Problems.PTSP/3.3/PTSP.cs @ 13322

Last change on this file since 13322 was 13202, checked in by abeham, 9 years ago

#2221: reverse engineered PTSPData and added simple instance provider based on TSPLIB

File size: 6.3 KB
Line 
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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.Instances;
30
31namespace HeuristicLab.Problems.PTSP {
32  [Item("Probabilistic Traveling Salesman Problem", "Represents a Probabilistic Traveling Salesman Problem.")]
33  [StorableClass]
34  public abstract class ProbabilisticTravelingSalesmanProblem : SingleObjectiveBasicProblem<PermutationEncoding>, IStorableContent,
35  IProblemInstanceConsumer<PTSPData> {
36    private static readonly int DistanceMatrixSizeLimit = 1000;
37
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    }
51    public ValueParameter<DoubleArray> ProbabilityMatrixParameter {
52      get { return (ValueParameter<DoubleArray>)Parameters["ProbabilityMatrix"]; }
53    }
54
55
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    }
75    public DoubleArray ProbabilityMatrix {
76      get { return ProbabilityMatrixParameter.Value; }
77      set { ProbabilityMatrixParameter.Value = value; }
78    }
79
80    #endregion
81
82
83    public override bool Maximization {
84      get { return false; }
85    }
86
87    [StorableConstructor]
88    protected ProbabilisticTravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
89    protected ProbabilisticTravelingSalesmanProblem(ProbabilisticTravelingSalesmanProblem original, Cloner cloner)
90      : base(original, cloner) {
91    }
92
93    public ProbabilisticTravelingSalesmanProblem() {
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."));
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
107      ProbabilityMatrix = new DoubleArray(new double[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });
108
109    }
110
111    public virtual void Load(PTSPData data) {
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
124      ProbabilityMatrix = new DoubleArray(data.Probabilities);
125
126      if (data.BestKnownTour != null) {
127        BestKnownSolution = new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour);
128      }
129
130      Coordinates = data.Coordinates != null && data.Coordinates.GetLength(0) > 0 ? new DoubleMatrix(data.Coordinates) : null;
131      DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
132
133      Encoding.Length = data.Dimension;
134
135      OnReset();
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.