1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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.Linq;
|
---|
24 | using HEAL.Attic;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Problems.Instances;
|
---|
31 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
32 | using HeuristicLab.Random;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.PTSP {
|
---|
35 | [Item("Probabilistic TSP (pTSP)", "Represents a Probabilistic Traveling Salesman Problem.")]
|
---|
36 | [StorableType("86041a8c-14e6-46e1-b20f-566892c871f6")]
|
---|
37 | public abstract class ProbabilisticTSP : PermutationProblem,
|
---|
38 | IProblemInstanceConsumer<PTSPData> {
|
---|
39 | protected bool SuppressEvents { get; set; }
|
---|
40 |
|
---|
41 | public static int DistanceMatrixSizeLimit = 1000;
|
---|
42 |
|
---|
43 | #region Parameter Properties
|
---|
44 | [Storable] public ValueParameter<IProbabilisticTSPData> PTSPDataParameter { get; private set; }
|
---|
45 | [Storable] public OptionalValueParameter<IProbabilisticTSPSolution> BestKnownSolutionParameter { get; private set; }
|
---|
46 | #endregion
|
---|
47 |
|
---|
48 | #region Properties
|
---|
49 | public IProbabilisticTSPData ProbabilisticTSPData {
|
---|
50 | get { return PTSPDataParameter.Value; }
|
---|
51 | set { PTSPDataParameter.Value = value; }
|
---|
52 | }
|
---|
53 | public IProbabilisticTSPSolution BestKnownSolution {
|
---|
54 | get { return BestKnownSolutionParameter.Value; }
|
---|
55 | set { BestKnownSolutionParameter.Value = value; }
|
---|
56 | }
|
---|
57 | #endregion
|
---|
58 |
|
---|
59 | [StorableConstructor]
|
---|
60 | protected ProbabilisticTSP(StorableConstructorFlag _) : base(_) { }
|
---|
61 | protected ProbabilisticTSP(ProbabilisticTSP original, Cloner cloner)
|
---|
62 | : base(original, cloner) {
|
---|
63 | PTSPDataParameter = cloner.Clone(original.PTSPDataParameter);
|
---|
64 | BestKnownSolutionParameter = cloner.Clone(original.BestKnownSolutionParameter);
|
---|
65 | }
|
---|
66 | protected ProbabilisticTSP() : base(new PermutationEncoding("Tour")) {
|
---|
67 | Maximization = false;
|
---|
68 | Parameters.Add(PTSPDataParameter = new ValueParameter<IProbabilisticTSPData>("PTSP Data", "The main parameters for the pTSP."));
|
---|
69 | Parameters.Add(BestKnownSolutionParameter = new OptionalValueParameter<IProbabilisticTSPSolution>("BestKnownSolution", "The best known solution of this pTSP instance."));
|
---|
70 |
|
---|
71 | ProbabilisticTSPData = new ProbabilisticTSPData();
|
---|
72 | Encoding.Length = ProbabilisticTSPData.TSPData.Cities;
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected override void OnEncodingChanged() {
|
---|
76 | base.OnEncodingChanged();
|
---|
77 | Encoding.Length = ProbabilisticTSPData.TSPData.Cities;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public override void Analyze(Permutation[] solutions, double[] qualities, ResultCollection results, IRandom random) {
|
---|
81 | base.Analyze(solutions, qualities, results, random);
|
---|
82 | var max = Maximization;
|
---|
83 |
|
---|
84 | var i = !max ? qualities.Select((x, index) => new { index, Quality = x }).OrderBy(x => x.Quality).First().index
|
---|
85 | : qualities.Select((x, index) => new { index, Quality = x }).OrderByDescending(x => x.Quality).First().index;
|
---|
86 |
|
---|
87 | if (double.IsNaN(BestKnownQuality) ||
|
---|
88 | max && qualities[i] > BestKnownQuality ||
|
---|
89 | !max && qualities[i] < BestKnownQuality) {
|
---|
90 | BestKnownQuality = qualities[i];
|
---|
91 | BestKnownSolution = ProbabilisticTSPData.GetSolution((Permutation)solutions[i].Clone(), qualities[i]);
|
---|
92 | }
|
---|
93 |
|
---|
94 | IResult bestSolutionResult;
|
---|
95 | if (results.TryGetValue("Best pTSP Solution", out bestSolutionResult)) {
|
---|
96 | var bestSolution = bestSolutionResult.Value as ITSPSolution;
|
---|
97 | if (bestSolution == null || Maximization && bestSolution.TourLength.Value < qualities[i]
|
---|
98 | || !Maximization && bestSolution.TourLength.Value > qualities[i]) {
|
---|
99 | bestSolutionResult.Value = ProbabilisticTSPData.GetSolution(solutions[i], qualities[i]);
|
---|
100 | }
|
---|
101 | } else results.Add(new Result("Best pTSP Solution", ProbabilisticTSPData.GetSolution(solutions[i], qualities[i])));
|
---|
102 | }
|
---|
103 |
|
---|
104 | public virtual void Load(PTSPData data) {
|
---|
105 | if (data.Coordinates == null && data.Distances == null)
|
---|
106 | throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!");
|
---|
107 | if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == DistanceMeasure.Att
|
---|
108 | || data.DistanceMeasure == DistanceMeasure.Manhattan
|
---|
109 | || data.DistanceMeasure == DistanceMeasure.Maximum))
|
---|
110 | throw new System.IO.InvalidDataException("The given instance uses an unsupported distance measure and is too large for using a distance matrix.");
|
---|
111 | if (data.Coordinates != null && data.Coordinates.GetLength(1) != 2)
|
---|
112 | 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.");
|
---|
113 |
|
---|
114 | Encoding.Length = data.Dimension;
|
---|
115 | Name = data.Name;
|
---|
116 | Description = data.Description;
|
---|
117 |
|
---|
118 | var tspData = TSP.GetDataFromInstance(data);
|
---|
119 | ProbabilisticTSPData = new ProbabilisticTSPData(tspData, data.Probabilities);
|
---|
120 | BestKnownSolution = null;
|
---|
121 | BestKnownQuality = double.NaN;
|
---|
122 |
|
---|
123 | if (data.BestKnownTour != null) {
|
---|
124 | try {
|
---|
125 | var tour = new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour);
|
---|
126 | var tourLength = Evaluate(tour, new MersenneTwister(1));
|
---|
127 | BestKnownSolution = ProbabilisticTSPData.GetSolution(tour, tourLength);
|
---|
128 | BestKnownQuality = tourLength;
|
---|
129 | } catch (InvalidOperationException) {
|
---|
130 | if (data.BestKnownQuality.HasValue)
|
---|
131 | BestKnownQuality = data.BestKnownQuality.Value;
|
---|
132 | }
|
---|
133 | } else if (data.BestKnownQuality.HasValue) {
|
---|
134 | BestKnownQuality = data.BestKnownQuality.Value;
|
---|
135 | }
|
---|
136 | OnReset();
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|