Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.PTSP/3.3/AnalyticalPTSP.cs @ 17525

Last change on this file since 17525 was 17382, checked in by mkommend, 5 years ago

#2521: Refactored single-objective problems to use EvaluationResult instead of double as return type from Evaluate.

File size: 4.0 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.Threading;
23using HEAL.Attic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Optimization;
28
29namespace HeuristicLab.Problems.PTSP {
30  [Item("Analytical Probabilistic TSP (pTSP)", "Represents a probabilistic traveling salesman problem where the expected tour length is calculated exactly.")]
31  [Creatable(CreatableAttribute.Categories.CombinatorialProblems)]
32  [StorableType("509B6AB5-F4DE-4144-A031-43EEBAD02CA6")]
33  public sealed class AnalyticalPTSP : ProbabilisticTSP {
34
35    [StorableConstructor]
36    private AnalyticalPTSP(StorableConstructorFlag _) : base(_) { }
37    private AnalyticalPTSP(AnalyticalPTSP original, Cloner cloner) : base(original, cloner) { }
38    public AnalyticalPTSP() {
39      Operators.Add(new PTSPAnalyticalInversionMoveEvaluator());
40      Operators.Add(new PTSPAnalyticalInsertionMoveEvaluator());
41      Operators.Add(new PTSPAnalyticalInversionLocalImprovement());
42      Operators.Add(new PTSPAnalyticalInsertionLocalImprovement());
43      Operators.Add(new PTSPAnalyticalTwoPointFiveLocalImprovement());
44
45      Operators.Add(new ExhaustiveTwoPointFiveMoveGenerator());
46      Operators.Add(new StochasticTwoPointFiveMultiMoveGenerator());
47      Operators.Add(new StochasticTwoPointFiveSingleMoveGenerator());
48      Operators.Add(new TwoPointFiveMoveMaker());
49      Operators.Add(new PTSPAnalyticalTwoPointFiveMoveEvaluator());
50
51      Encoding.ConfigureOperators(Operators);
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new AnalyticalPTSP(this, cloner);
56    }
57
58    protected override void OnEncodingChanged() {
59      base.OnEncodingChanged();
60      Encoding.ConfigureOperators(Operators);
61    }
62
63    public override ISingleObjectiveEvaluationResult Evaluate(Permutation tour, IRandom random, CancellationToken cancellationToken) {
64      var quality = Evaluate(tour, ProbabilisticTSPData, cancellationToken);
65      return new SingleObjectiveEvaluationResult(quality);
66    }
67
68    public static double Evaluate(Permutation tour, IProbabilisticTSPData data, CancellationToken cancellationToken) {
69      // Analytical evaluation
70      var firstSum = 0.0;
71      for (var i = 0; i < tour.Length - 1; i++) {
72        for (var j = i + 1; j < tour.Length; j++) {
73          var prod1 = data.TSPData.GetDistance(tour[i], tour[j]) * data.GetProbability(tour[i]) * data.GetProbability(tour[j]);
74          for (var k = i + 1; k < j; k++) {
75            prod1 *= (1 - data.GetProbability(tour[k]));
76          }
77          firstSum += prod1;
78        }
79      }
80      var secondSum = 0.0;
81      for (var j = 0; j < tour.Length; j++) {
82        for (var i = 0; i < j; i++) {
83          var prod2 = data.TSPData.GetDistance(tour[j], tour[i]) * data.GetProbability(tour[i]) * data.GetProbability(tour[j]);
84          for (var k = j + 1; k < tour.Length; k++) {
85            prod2 *= (1 - data.GetProbability(tour[k]));
86          }
87          for (var k = 0; k < i; k++) {
88            prod2 *= (1 - data.GetProbability(tour[k]));
89          }
90          secondSum += prod2;
91        }
92      }
93      return firstSum + secondSum;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.