Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PTSP/HeuristicLab.Problems.PTSP/3.3/AnalyticalPTSP.cs @ 13423

Last change on this file since 13423 was 13412, checked in by abeham, 9 years ago

#2221:

  • Completely refactored PTSP branch
    • Added two sets of problem instances based on TSPLIB: homogeneous and heterogeneous
    • Implemented missing EvaluateByCoordinates for 1-shift moves
    • Made it clear that move evaluators are for estimated PTSP only
    • Changed parameter realization from a rather strange list of list of ints to a list of bool arrays
    • Reusing code of the 2-opt and 1-shift move evaluators in 2.5 move evaluator
    • Introducing distance calculators to properly handle the case when no distance matrix is given (previous code only worked with distance matrix and without only with euclidean distance in some settings)
    • Fixed several smaller code issues: protected, static, method parameters, copy & paste, interfaces, naming, parameters, serialization hooks, license headers, doc comments, data types
File size: 4.1 KB
RevLine 
[13412]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
22using HeuristicLab.Common;
[12191]23using HeuristicLab.Core;
[13412]24using HeuristicLab.Data;
25using HeuristicLab.Encodings.PermutationEncoding;
[12191]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.PTSP {
[13412]29  [Item("Analytical Probabilistic Traveling Salesman Problem (PTSP)", "Represents a probabilistic traveling salesman problem where the expected tour length is calculated exactly.")]
30  [Creatable(CreatableAttribute.Categories.CombinatorialProblems)]
[12191]31  [StorableClass]
[13412]32  public sealed class AnalyticalProbabilisticTravelingSalesmanProblem : ProbabilisticTravelingSalesmanProblem {
[12191]33
34    [StorableConstructor]
35    private AnalyticalProbabilisticTravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
[13412]36    private AnalyticalProbabilisticTravelingSalesmanProblem(AnalyticalProbabilisticTravelingSalesmanProblem original, Cloner cloner) : base(original, cloner) { }
37    public AnalyticalProbabilisticTravelingSalesmanProblem() {
38      Operators.Add(new BestPTSPSolutionAnalyzer());
[12191]39    }
40
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new AnalyticalProbabilisticTravelingSalesmanProblem(this, cloner);
43    }
44
[13412]45    public override double Evaluate(Permutation tour, IRandom random) {
[12191]46      // Analytical evaluation
47      double firstSum = 0;
[13412]48      for (int i = 0; i < tour.Length - 1; i++) {
49        for (int j = i + 1; j < tour.Length - 1; j++) {
50          double sum1 = DistanceMatrix[tour[i], tour[j]] * Probabilities[tour[i]] * Probabilities[tour[j]];
[12191]51          for (int k = i + 1; k < j; k++) {
[13412]52            sum1 = sum1 * (1 - Probabilities[tour[k]]);
[12191]53          }
54          firstSum += sum1;
55        }
56      }
57      double secondSum = 0;
[13412]58      for (int j = 0; j < tour.Length - 1; j++) {
[12191]59        for (int i = 0; i < j; i++) {
[13412]60          double sum2 = DistanceMatrix[tour[j], tour[i]] * Probabilities[tour[i]] * Probabilities[tour[j]];
61          for (int k = j + 1; k < tour.Length - 1; k++) {
62            sum2 = sum2 * (1 - Probabilities[tour[k]]);
[12191]63          }
64          for (int k = 1; k < i; k++) {
[13412]65            sum2 = sum2 * (1 - Probabilities[tour[k]]);
[12191]66          }
67          secondSum += sum2;
68        }
69      }
70      return firstSum + secondSum;
71    }
72
[13412]73    public static double Evaluate(Permutation tour, DistanceMatrix distanceMatrix, DoubleArray probabilities) {
[12269]74      // Analytical evaluation
[13412]75      var firstSum = 0.0;
76      for (var i = 0; i < tour.Length; i++) {
77        for (var j = i + 1; j < tour.Length; j++) {
78          var sum1 = distanceMatrix[tour[i], tour[j]] * probabilities[tour[i]] * probabilities[tour[j]];
79          for (var k = i + 1; k < j; k++) {
80            sum1 = sum1 * (1 - probabilities[tour[k]]);
[12269]81          }
82          firstSum += sum1;
83        }
84      }
[13412]85      var secondSum = 0.0;
86      for (var j = 0; j < tour.Length; j++) {
87        for (var i = 0; i < j; i++) {
88          var sum2 = distanceMatrix[tour[j], tour[i]] * probabilities[tour[i]] * probabilities[tour[j]];
89          for (var k = j + 1; k < tour.Length; k++) {
90            sum2 = sum2 * (1 - probabilities[tour[k]]);
[12269]91          }
[13412]92          for (var k = 0; k < i; k++) {
93            sum2 = sum2 * (1 - probabilities[tour[k]]);
[12269]94          }
95          secondSum += sum2;
96        }
97      }
98      return firstSum + secondSum;
99    }
[12191]100  }
101}
Note: See TracBrowser for help on using the repository browser.