Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/28/15 23:38:51 (9 years ago)
Author:
abeham
Message:

#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:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PTSP/HeuristicLab.Problems.PTSP/3.3/AnalyticalPTSP.cs

    r12306 r13412  
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.Threading.Tasks;
    6 using HeuristicLab.Optimization;
    7 using HeuristicLab.PluginInfrastructure;
     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;
    823using HeuristicLab.Core;
     24using HeuristicLab.Data;
     25using HeuristicLab.Encodings.PermutationEncoding;
    926using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    10 using HeuristicLab.Problems.Instances;
    11 using HeuristicLab.Encodings.PermutationEncoding;
    12 using HeuristicLab.Common;
    13 using HeuristicLab.Parameters;
    14 using HeuristicLab.Data;
    1527
    1628namespace HeuristicLab.Problems.PTSP {
    17   [Item("Analytical Probabilistic Traveling Salesman Problem", "Represents an analytical Probabilistic Traveling Salesman Problem.")]
    18   [Creatable("Problems")]
     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)]
    1931  [StorableClass]
    20   public sealed class AnalyticalProbabilisticTravelingSalesmanProblem : ProbabilisticTravelingSalesmanProblem, IStorableContent,
    21   IProblemInstanceConsumer<PTSPData> {
     32  public sealed class AnalyticalProbabilisticTravelingSalesmanProblem : ProbabilisticTravelingSalesmanProblem {
    2233
    2334    [StorableConstructor]
    2435    private AnalyticalProbabilisticTravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
    25     private AnalyticalProbabilisticTravelingSalesmanProblem(AnalyticalProbabilisticTravelingSalesmanProblem original, Cloner cloner)
    26       : base(original, cloner) {
     36    private AnalyticalProbabilisticTravelingSalesmanProblem(AnalyticalProbabilisticTravelingSalesmanProblem original, Cloner cloner) : base(original, cloner) { }
     37    public AnalyticalProbabilisticTravelingSalesmanProblem() {
     38      Operators.Add(new BestPTSPSolutionAnalyzer());
    2739    }
    2840
     
    3143    }
    3244
    33     public override double Evaluate(Individual individual, IRandom random) {
    34       Permutation p = individual.Permutation();
    35       // Compute A and B matrices
    36       DoubleMatrix A = new DoubleMatrix(p.Length, p.Length - 1);
    37       DoubleMatrix B = new DoubleMatrix(p.Length, p.Length - 1);
    38       //for (int i = 0; i < p.Length; i++) {
    39       //  for (int k = 0; k < p.Length - 1; k++) {
    40       //    double firstPartial = 0;
    41       //    for (int r = k; k < p.Length - 1; r++) {
    42       //       double sum0 = DistanceMatrix[p[i],p[i+r]]* ProbabilityMatrix[0, p[i]] * ProbabilityMatrix[0, p[i+r]];
    43       //       for(int s = i+1; s < i+r; s++) {
    44       //         sum0 = sum0 * (1 - ProbabilityMatrix[0, p[s]]);
    45       //       }
    46       //       firstPartial+=sum0;
    47       //    }
    48       //    double secondPartial = 0;
    49       //    for (int r = k; k < p.Length - 1; r++) {
    50       //       double sum1 = DistanceMatrix[p[i-r],p[i]]* ProbabilityMatrix[0, p[i-r]] * ProbabilityMatrix[0, p[i]];
    51       //       for (int s = i + 1; s < p.Length-1; s++) {
    52       //         sum1 = sum1 * (1 - ProbabilityMatrix[0, p[s]]);
    53       //       }
    54       //       for (int t = 1; t < i-1; t++) {
    55       //         sum1 = sum1 * (1 - ProbabilityMatrix[0, p[t]]);
    56       //       }
    57       //       secondPartial+=sum1;
    58       //    }
    59       //    A[i, k] = firstPartial;
    60       //    B[i, k] = secondPartial;
    61       //  }
    62       //}
    63      
     45    public override double Evaluate(Permutation tour, IRandom random) {
    6446      // Analytical evaluation
    6547      double firstSum = 0;
    66       for (int i = 0; i < p.Length - 1; i++) {
    67         for (int j = i + 1; j < p.Length - 1; j++) {
    68           double sum1 = DistanceMatrix[p[i], p[j]] * ProbabilityMatrix[p[i]] * ProbabilityMatrix[p[j]];
     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]];
    6951          for (int k = i + 1; k < j; k++) {
    70             sum1 = sum1 * (1 - ProbabilityMatrix[p[k]]);
     52            sum1 = sum1 * (1 - Probabilities[tour[k]]);
    7153          }
    72           A[i, j - 1] = sum1;
    7354          firstSum += sum1;
    7455        }
    7556      }
    7657      double secondSum = 0;
    77       for (int j = 0; j < p.Length - 1; j++) {
     58      for (int j = 0; j < tour.Length - 1; j++) {
    7859        for (int i = 0; i < j; i++) {
    79           double sum2 = DistanceMatrix[p[j], p[i]] * ProbabilityMatrix[p[i]] * ProbabilityMatrix[p[j]];
    80           for (int k = j + 1; k < p.Length - 1; k++) {
    81             sum2 = sum2 * (1 - ProbabilityMatrix[p[k]]);
     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]]);
    8263          }
    8364          for (int k = 1; k < i; k++) {
    84             sum2 = sum2 * (1 - ProbabilityMatrix[p[k]]);
     65            sum2 = sum2 * (1 - Probabilities[tour[k]]);
    8566          }
    86           B[i, j] = sum2;
    8767          secondSum += sum2;
    8868        }
    89       }
    90       foreach (var op in Operators.OfType<PTSPAnalyticalInversionMovePathEvaluator>()) {
    91         op.AParameter.Value = A;
    92         op.BParameter.Value = B;
    9369      }
    9470      return firstSum + secondSum;
    9571    }
    9672
    97     public double EvaluateWithParams(DistanceMatrix distances, DoubleArray probabilities, Permutation individual) {
    98       Permutation p = individual;
    99       // Compute A and B matrices
    100       DoubleMatrix A = new DoubleMatrix(p.Length, p.Length - 1);
    101       DoubleMatrix B = new DoubleMatrix(p.Length, p.Length - 1);
     73    public static double Evaluate(Permutation tour, DistanceMatrix distanceMatrix, DoubleArray probabilities) {
    10274      // Analytical evaluation
    103       double firstSum = 0;
    104       for (int i = 0; i < p.Length; i++) {
    105         for (int j = i + 1; j < p.Length; j++) {
    106           double sum1 = distances[p[i], p[j]] * probabilities[p[i]] * probabilities[p[j]];
    107           for (int k = i + 1; k < j; k++) {
    108             sum1 = sum1 * (1 - probabilities[p[k]]);
     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]]);
    10981          }
    110           A[i, j - 1] = sum1;
    11182          firstSum += sum1;
    11283        }
    11384      }
    114       double secondSum = 0;
    115       for (int j = 0; j < p.Length; j++) {
    116         for (int i = 0; i < j; i++) {
    117           double sum2 = distances[p[j], p[i]] * probabilities[p[i]] * probabilities[p[j]];
    118           for (int k = j + 1; k < p.Length; k++) {
    119             sum2 = sum2 * (1 - probabilities[p[k]]);
     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]]);
    12091          }
    121           for (int k = 0; k < i; k++) {
    122             sum2 = sum2 * (1 - probabilities[p[k]]);
     92          for (var k = 0; k < i; k++) {
     93            sum2 = sum2 * (1 - probabilities[tour[k]]);
    12394          }
    124           B[j,i] = sum2;
    12595          secondSum += sum2;
    12696        }
    12797      }
    128       foreach (var op in Operators.OfType<PTSPAnalyticalInversionMovePathEvaluator>()) {
    129         op.AParameter.Value = A;
    130         op.BParameter.Value = B;
    131       }
    13298      return firstSum + secondSum;
    13399    }
    134 
    135     public AnalyticalProbabilisticTravelingSalesmanProblem() {
    136       Operators.Add(new PTSPAnalyticalInversionMovePathEvaluator());
    137     }
    138 
    139     public override void Load(PTSPData data) {
    140       base.Load(data);
    141       foreach (var op in Operators.OfType<PTSPAnalyticalInversionMovePathEvaluator>()) {
    142         op.ProbabilitiesParameter.Value = ProbabilityMatrix;
    143       }
    144     }
    145 
    146100  }
    147101}
Note: See TracChangeset for help on using the changeset viewer.