1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 System.Text;
|
---|
26 | using System.Threading.Tasks;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.PluginInfrastructure;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.Instances;
|
---|
32 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
33 | using HeuristicLab.Common;
|
---|
34 | using HeuristicLab.Parameters;
|
---|
35 | using HeuristicLab.Data;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Problems.PTSP {
|
---|
38 | [Item("Probabilistic Traveling Salesman Problem", "Represents a Probabilistic Traveling Salesman Problem.")]
|
---|
39 | [Creatable("Problems")]
|
---|
40 | [StorableClass]
|
---|
41 | public sealed class ProbabilisticTravelingSalesmanProblem : SingleObjectiveBasicProblem<PermutationEncoding>, IStorableContent,
|
---|
42 | IProblemInstanceConsumer<TSPData> {
|
---|
43 | #region Parameter Properties
|
---|
44 | public OptionalValueParameter<DoubleMatrix> CoordinatesParameter {
|
---|
45 | get { return (OptionalValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
46 | }
|
---|
47 | public OptionalValueParameter<DistanceMatrix> DistanceMatrixParameter {
|
---|
48 | get { return (OptionalValueParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
|
---|
49 | }
|
---|
50 | public ValueParameter<BoolValue> UseDistanceMatrixParameter {
|
---|
51 | get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
|
---|
52 | }
|
---|
53 | public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
|
---|
54 | get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
|
---|
55 | }
|
---|
56 | public ValueParameter<BoolValue> UseAnalyticalEvaluationParameter {
|
---|
57 | get { return (ValueParameter<BoolValue>)Parameters["UseAnalyticalEvaluation"]; }
|
---|
58 | }
|
---|
59 | public OptionalValueParameter<DistanceMatrix> ProbabilityMatrixParameter {
|
---|
60 | get { return (OptionalValueParameter<DistanceMatrix>)Parameters["ProbabilityMatrix"]; }
|
---|
61 | }
|
---|
62 | public ValueParameter<IntValue> SampleSizeParameter {
|
---|
63 | get { return (ValueParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | #endregion
|
---|
67 |
|
---|
68 | #region Properties
|
---|
69 | public DoubleMatrix Coordinates {
|
---|
70 | get { return CoordinatesParameter.Value; }
|
---|
71 | set { CoordinatesParameter.Value = value; }
|
---|
72 | }
|
---|
73 | public DistanceMatrix DistanceMatrix {
|
---|
74 | get { return DistanceMatrixParameter.Value; }
|
---|
75 | set { DistanceMatrixParameter.Value = value; }
|
---|
76 | }
|
---|
77 | public BoolValue UseDistanceMatrix {
|
---|
78 | get { return UseDistanceMatrixParameter.Value; }
|
---|
79 | set { UseDistanceMatrixParameter.Value = value; }
|
---|
80 | }
|
---|
81 | public Permutation BestKnownSolution {
|
---|
82 | get { return BestKnownSolutionParameter.Value; }
|
---|
83 | set { BestKnownSolutionParameter.Value = value; }
|
---|
84 | }
|
---|
85 | public BoolValue UseAnalyticalEvaluation {
|
---|
86 | get { return UseAnalyticalEvaluationParameter.Value; }
|
---|
87 | set { UseAnalyticalEvaluationParameter.Value = value; }
|
---|
88 | }
|
---|
89 | public DistanceMatrix ProbabilityMatrix {
|
---|
90 | get { return ProbabilityMatrixParameter.Value; }
|
---|
91 | set { ProbabilityMatrixParameter.Value = value; }
|
---|
92 | }
|
---|
93 | public IntValue SampleSize {
|
---|
94 | get { return SampleSizeParameter.Value; }
|
---|
95 | set { SampleSizeParameter.Value = value; }
|
---|
96 | }
|
---|
97 | #endregion
|
---|
98 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
99 | Permutation p = individual.Permutation();
|
---|
100 | if (UseAnalyticalEvaluation.Value) {
|
---|
101 | // Analytical evaluation
|
---|
102 | double firstSum = 0;
|
---|
103 | for (int i = 0; i < p.Length - 1; i++) {
|
---|
104 | for (int j = i + 1; j < p.Length - 1; j++) {
|
---|
105 | double sum1 = DistanceMatrix[p[i], p[j]] * ProbabilityMatrix[0, p[i]] * ProbabilityMatrix[0, p[j]];
|
---|
106 | for (int k = i + 1; k < j; k++) {
|
---|
107 | sum1 = sum1 * (1 - ProbabilityMatrix[0, p[k]]);
|
---|
108 | }
|
---|
109 | firstSum += sum1;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | double secondSum = 0;
|
---|
113 | for (int j = 0; j < p.Length - 1; j++) {
|
---|
114 | for (int i = 0; i < j; i++) {
|
---|
115 | double sum2 = DistanceMatrix[p[j], p[i]] * ProbabilityMatrix[0, p[i]] * ProbabilityMatrix[0, p[j]];
|
---|
116 | for (int k = j + 1; k < p.Length - 1; k++) {
|
---|
117 | sum2 = sum2 * (1 - ProbabilityMatrix[0, p[k]]);
|
---|
118 | }
|
---|
119 | for (int k = 1; k < i; k++) {
|
---|
120 | sum2 = sum2 * (1 - ProbabilityMatrix[0, p[k]]);
|
---|
121 | }
|
---|
122 | secondSum += sum2;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | return firstSum+secondSum;
|
---|
126 | } else {
|
---|
127 | // Estimation-based evaluation
|
---|
128 | Random r = new Random();
|
---|
129 | double estimatedSum = 0;
|
---|
130 | for (int i = 0; i < SampleSize.Value; i++) {
|
---|
131 | int singleRealization = -1;
|
---|
132 | for (int j = 0; j < p.Length - 1; j++) {
|
---|
133 | if (ProbabilityMatrix[0, p[j]] > r.NextDouble()) {
|
---|
134 | if (singleRealization != -1) {
|
---|
135 | estimatedSum += DistanceMatrix[singleRealization, p[j]];
|
---|
136 | }
|
---|
137 | singleRealization = p[j];
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | return estimatedSum / SampleSize.Value;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | public override bool Maximization {
|
---|
146 | get { return false; }
|
---|
147 | }
|
---|
148 |
|
---|
149 | [StorableConstructor]
|
---|
150 | private ProbabilisticTravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
|
---|
151 | private ProbabilisticTravelingSalesmanProblem(ProbabilisticTravelingSalesmanProblem original, Cloner cloner)
|
---|
152 | : base(original, cloner) {
|
---|
153 | }
|
---|
154 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
155 | return new ProbabilisticTravelingSalesmanProblem(this, cloner);
|
---|
156 | }
|
---|
157 | public ProbabilisticTravelingSalesmanProblem() {
|
---|
158 | Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
|
---|
159 | Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
160 | Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if the coordinates based evaluators should calculate the distance matrix from the coordinates and use it for evaluation similar to the distance matrix evaluator, otherwise false.", new BoolValue(true)));
|
---|
161 | Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
|
---|
162 | Parameters.Add(new ValueParameter<BoolValue>("UseAnalyticalEvaluation", "Check to use analytical evaluation, uncheck to use estimation-based approach."));
|
---|
163 | Parameters.Add(new OptionalValueParameter<DistanceMatrix>("ProbabilityMatrix", "The matrix which contains the probabilities of each of the cities."));
|
---|
164 | Parameters.Add(new ValueParameter<IntValue>("SampleSize", "Size of the sample for the estimation-based evaluation"));
|
---|
165 | }
|
---|
166 |
|
---|
167 | public void Load(TSPData data) {
|
---|
168 | SampleSize = new IntValue(20);
|
---|
169 | DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
|
---|
170 | // Get Probabilities of cities using random with seed from hash function on the Name of the instance
|
---|
171 | ProbabilityMatrix = new DistanceMatrix(1, data.Dimension);
|
---|
172 | Random r = new Random(data.Name.GetHashCode());
|
---|
173 | for (int i = 0; i < data.Dimension; i++) {
|
---|
174 | ProbabilityMatrix[0,i] = r.NextDouble();
|
---|
175 | }
|
---|
176 | Encoding.Length = data.Dimension;
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|