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