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.Data;
|
---|
28 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
29 | using HeuristicLab.Problems.Instances;
|
---|
30 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
31 |
|
---|
32 | namespace 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 | }
|
---|