Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.PTSP/3.3/ProbabilisticTSP.cs @ 17544

Last change on this file since 17544 was 17544, checked in by abeham, 4 years ago

#2521: worked on refactoring, worked a lot on binary encoding / problems

File size: 6.2 KB
Line 
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
22using System;
23using System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Problems.Instances;
31using HeuristicLab.Problems.TravelingSalesman;
32using HeuristicLab.Random;
33
34namespace 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; }
45    [Storable] public OptionalValueParameter<IProbabilisticTSPSolution> BestKnownSolutionParameter { get; }
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      Dimension = ProbabilisticTSPData.Cities;
73    }
74
75    public override void Analyze(Permutation[] solutions, double[] qualities, ResultCollection results, IRandom random) {
76      base.Analyze(solutions, qualities, results, random);
77      var max = Maximization;
78
79      var i = !max ? qualities.Select((x, index) => new { index, Quality = x }).OrderBy(x => x.Quality).First().index
80                   : qualities.Select((x, index) => new { index, Quality = x }).OrderByDescending(x => x.Quality).First().index;
81
82      if (double.IsNaN(BestKnownQuality) ||
83          max && qualities[i] > BestKnownQuality ||
84          !max && qualities[i] < BestKnownQuality) {
85        BestKnownQuality = qualities[i];
86        BestKnownSolution = ProbabilisticTSPData.GetSolution((Permutation)solutions[i].Clone(), qualities[i]);
87      }
88
89      IResult bestSolutionResult;
90      if (results.TryGetValue("Best pTSP Solution", out bestSolutionResult)) {
91        var bestSolution = bestSolutionResult.Value as ITSPSolution;
92        if (bestSolution == null || Maximization && bestSolution.TourLength.Value < qualities[i]
93          || !Maximization && bestSolution.TourLength.Value > qualities[i]) {
94          bestSolutionResult.Value = ProbabilisticTSPData.GetSolution(solutions[i], qualities[i]);
95        }
96      } else results.Add(new Result("Best pTSP Solution", ProbabilisticTSPData.GetSolution(solutions[i], qualities[i])));
97    }
98
99    public virtual void Load(PTSPData data) {
100      if (data.Coordinates == null && data.Distances == null)
101        throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!");
102      if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == DistanceMeasure.Att
103        || data.DistanceMeasure == DistanceMeasure.Manhattan
104        || data.DistanceMeasure == DistanceMeasure.Maximum))
105        throw new System.IO.InvalidDataException("The given instance uses an unsupported distance measure and is too large for using a distance matrix.");
106      if (data.Coordinates != null && data.Coordinates.GetLength(1) != 2)
107        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.");
108
109      Dimension = data.Dimension;
110      Name = data.Name;
111      Description = data.Description;
112
113      var tspData = TSP.GetDataFromInstance(data);
114      ProbabilisticTSPData = new ProbabilisticTSPData(tspData, data.Probabilities);
115      BestKnownSolution = null;
116      BestKnownQuality = double.NaN;
117
118      if (data.BestKnownTour != null) {
119        try {
120          var tour = new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour);
121          var tourLength = Evaluate(tour, new MersenneTwister(1)).Quality;
122          BestKnownSolution = ProbabilisticTSPData.GetSolution(tour, tourLength);
123          BestKnownQuality = tourLength;
124        } catch (InvalidOperationException) {
125          if (data.BestKnownQuality.HasValue)
126            BestKnownQuality = data.BestKnownQuality.Value;
127        }
128      } else if (data.BestKnownQuality.HasValue) {
129        BestKnownQuality = data.BestKnownQuality.Value;
130      }
131      OnReset();
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.