Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.PTSP/3.3/ProbabilisticTSPData.cs @ 17382

Last change on this file since 17382 was 17335, checked in by abeham, 5 years ago

#2521: Refactored pTSP to use compositional pattern

File size: 3.9 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.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Problems.Instances;
30using HeuristicLab.Problems.TravelingSalesman;
31
32namespace HeuristicLab.Problems.PTSP {
33  [StorableType("dd2d0ecc-372e-46f1-846f-fb4ca2afa124")]
34  public interface IProbabilisticTSPData : INamedItem {
35    ITSPData TSPData { get; }
36
37    double GetProbability(int city);
38    IProbabilisticTSPSolution GetSolution(Permutation tspTour, double tourLength);
39    PTSPData Export();
40  }
41
42  [Item("Probabilistic TSP Data", "An extension to the TSP where customers have to be served with a certain probability only.")]
43  [StorableType("b79f0ad6-b7f7-49c5-9a62-8089db6af1aa")]
44  public class ProbabilisticTSPData : NamedItem, IProbabilisticTSPData {
45    [Storable] public ITSPData TSPData { get; protected set; }
46    [Storable] public PercentArray Probabilities { get; protected set; }
47
48    [StorableConstructor]
49    protected ProbabilisticTSPData(StorableConstructorFlag _) : base(_) { }
50    protected ProbabilisticTSPData(ProbabilisticTSPData original, Cloner cloner) : base(original, cloner) {
51      TSPData = original.TSPData;
52      Probabilities = original.Probabilities;
53    }
54    public ProbabilisticTSPData() {
55      TSPData = new EuclideanTSPData();
56      Name = TSPData.Name;
57      Probabilities = new PercentArray(Enumerable.Repeat(0.5, TSPData.Cities).ToArray(), @readonly: true);
58    }
59    public ProbabilisticTSPData(ITSPData tspData, double[] probabilities)
60      : base(tspData.Name, tspData.Description) {
61      if (tspData.Cities != probabilities.Length) throw new ArgumentException("Unequal number of cities and probabilities.");
62      TSPData = tspData;
63      Probabilities = new PercentArray(probabilities, @readonly: true);
64    }
65    public ProbabilisticTSPData(ITSPData tspData, PercentArray probabilities)
66      : base(tspData.Name, tspData.Description) {
67      if (tspData.Cities != probabilities.Length) throw new ArgumentException("Unequal number of cities and probabilities.");
68      TSPData = tspData;
69      Probabilities = probabilities.AsReadOnly();
70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new ProbabilisticTSPData(this, cloner);
74    }
75
76    public double GetProbability(int city) => Probabilities[city];
77
78    public IProbabilisticTSPSolution GetSolution(Permutation tspTour, double tourLength) {
79      var tspSolution = TSPData.GetSolution(tspTour, tourLength);
80      return new ProbabilisticTSPSolution(tspSolution, Probabilities);
81    }
82
83    public PTSPData Export() {
84      var tspExport = TSPData.Export();
85      return new PTSPData() {
86        Name = tspExport.Name,
87        Description = tspExport.Description,
88        Coordinates = tspExport.Coordinates,
89        Dimension = tspExport.Dimension,
90        DistanceMeasure = tspExport.DistanceMeasure,
91        Distances = tspExport.Distances,
92        Probabilities = Probabilities.CloneAsArray()
93      };
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.