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