Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12219


Ignore:
Timestamp:
03/18/15 12:01:21 (10 years ago)
Author:
apolidur
Message:

#2221: First version of 1-shift and 2-p-opt moves

Location:
branches/PTSP/HeuristicLab.Problems.PTSP/3.3
Files:
3 added
1 deleted
6 edited

Legend:

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

    r12191 r12219  
    3333    public override double Evaluate(Individual individual, IRandom random) {
    3434      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     
    3564      // Analytical evaluation
    3665      double firstSum = 0;
    3766      for (int i = 0; i < p.Length - 1; i++) {
    3867        for (int j = i + 1; j < p.Length - 1; j++) {
    39           double sum1 = DistanceMatrix[p[i], p[j]] * ProbabilityMatrix[0, p[i]] * ProbabilityMatrix[0, p[j]];
     68          double sum1 = DistanceMatrix[p[i], p[j]] * ProbabilityMatrix[p[i]].Value * ProbabilityMatrix[p[j]].Value;
    4069          for (int k = i + 1; k < j; k++) {
    41             sum1 = sum1 * (1 - ProbabilityMatrix[0, p[k]]);
     70            sum1 = sum1 * (1 - ProbabilityMatrix[p[k]].Value);
    4271          }
     72          A[i, j - 1] = sum1;
    4373          firstSum += sum1;
    4474        }
     
    4777      for (int j = 0; j < p.Length - 1; j++) {
    4878        for (int i = 0; i < j; i++) {
    49           double sum2 = DistanceMatrix[p[j], p[i]] * ProbabilityMatrix[0, p[i]] * ProbabilityMatrix[0, p[j]];
     79          double sum2 = DistanceMatrix[p[j], p[i]] * ProbabilityMatrix[p[i]].Value * ProbabilityMatrix[p[j]].Value;
    5080          for (int k = j + 1; k < p.Length - 1; k++) {
    51             sum2 = sum2 * (1 - ProbabilityMatrix[0, p[k]]);
     81            sum2 = sum2 * (1 - ProbabilityMatrix[p[k]].Value);
    5282          }
    5383          for (int k = 1; k < i; k++) {
    54             sum2 = sum2 * (1 - ProbabilityMatrix[0, p[k]]);
     84            sum2 = sum2 * (1 - ProbabilityMatrix[p[k]].Value);
    5585          }
     86          B[i, j] = sum2;
    5687          secondSum += sum2;
    5788        }
     89      }
     90      foreach (var op in Operators.OfType<PTSPAnalyticalInversionMovePathEvaluator>()) {
     91        op.AParameter.Value = A;
     92        op.BParameter.Value = B;
    5893      }
    5994      return firstSum + secondSum;
     
    6196
    6297    public AnalyticalProbabilisticTravelingSalesmanProblem() {
    63       Operators.Add(new PTSPInversionMovePathEvaluator());
     98      Operators.Add(new PTSPAnalyticalInversionMovePathEvaluator());
    6499    }
    65100
    66101    public override void Load(TSPData data) {
    67102      base.Load(data);
    68       // Compute A and B matrices
     103      foreach (var op in Operators.OfType<PTSPAnalyticalInversionMovePathEvaluator>()) {
     104        op.ProbabilitiesParameter.Value = ProbabilityMatrix;
     105      }
    69106    }
    70107
  • branches/PTSP/HeuristicLab.Problems.PTSP/3.3/EstimatedPTSP.cs

    r12191 r12219  
    1111using HeuristicLab.Data;
    1212using System;
     13using System.Linq;
     14using HeuristicLab.Problems.PTSP.MoveEvaluators.OneShift;
    1315
    1416namespace HeuristicLab.Problems.PTSP {
     
    1820  public sealed class EstimatedProbabilisticTravelingSalesmanProblem : ProbabilisticTravelingSalesmanProblem, IStorableContent,
    1921  IProblemInstanceConsumer<TSPData> {
     22
     23    private ItemList<ItemList<IntValue>> realizations;
    2024
    2125    [StorableConstructor]
     
    3438      Random r = new Random();
    3539      double estimatedSum = 0;
    36       for (int i = 0; i < SampleSize.Value; i++) {
     40      for (int i = 0; i < realizations.Count; i++) {
    3741        int singleRealization = -1;
    38         for (int j = 0; j < p.Length - 1; j++) {
    39           if (ProbabilityMatrix[0, p[j]] > r.NextDouble()) {
     42        for (int j = 0; j < realizations[i].Count; j++) {
     43          if (realizations[i][j].Value == 1) {
    4044            if (singleRealization != -1) {
    4145              estimatedSum += DistanceMatrix[singleRealization, p[j]];
     
    5054    public EstimatedProbabilisticTravelingSalesmanProblem() {
    5155      Parameters.Add(new ValueParameter<IntValue>("SampleSize", "Size of the sample for the estimation-based evaluation"));
    52       Operators.Add(new PTSPInversionMovePathEvaluator());
     56      Operators.Add(new PTSPEstimatedInversionMovePathEvaluator());
     57      Operators.Add(new PTSPEstimatedInsertionEvaluator());
     58      Encoding.ConfigureOperators(Operators.OfType<IOperator>());
    5359    }
    5460
     
    5763      // For now uses sample size of 20 but should use Student's t-test
    5864      SampleSize = new IntValue(20);
     65      realizations = new ItemList<ItemList<IntValue>>(SampleSize.Value);
     66      Random r = new Random();
     67      for (int i = 0; i < SampleSize.Value; i++) {
     68        realizations.Add(new ItemList<IntValue>());
     69        for (int j = 0; j < data.Dimension; j++) {
     70          if (ProbabilityMatrix[j].Value > r.NextDouble()) {
     71            realizations.ElementAt(i).Add(new IntValue(1));
     72          } else {
     73            realizations.ElementAt(i).Add(new IntValue(0));
     74          }
     75        }
     76       
     77      }
     78      foreach (var op in Operators.OfType<PTSPPathMoveEvaluator>()) {
     79        op.RealizationsParameter.Value = realizations;
     80      }
    5981    }
    6082  }
  • branches/PTSP/HeuristicLab.Problems.PTSP/3.3/HeuristicLab.Problems.PTSP-3.3.csproj

    r12191 r12219  
    114114    <Compile Include="Interfaces\IPTSPPathMoveEvaluator.cs" />
    115115    <Compile Include="Interfaces\IPTSPMoveEvaluator.cs" />
     116    <Compile Include="MoveEvaluators\OneShift\PTSPEstimatedInsertionEvaluator.cs" />
    116117    <Compile Include="MoveEvaluators\PTSPMoveEvaluator.cs" />
    117118    <Compile Include="MoveEvaluators\PTSPPathMoveEvaluator.cs" />
    118     <Compile Include="MoveEvaluators\TwoOpt\PTSPInversionMovePathEvaluator.cs" />
     119    <Compile Include="MoveEvaluators\TwoOpt\PTSPAnalyticalInversionMovePathEvaluator.cs" />
     120    <Compile Include="MoveEvaluators\TwoOpt\PTSPEstimatedInversionMovePathEvaluator.cs" />
    119121    <Compile Include="PTSP.cs" />
    120122    <Compile Include="Plugin.cs" />
     
    124126    <None Include="HeuristicLab.snk" />
    125127  </ItemGroup>
    126   <ItemGroup>
    127     <Folder Include="MoveEvaluators\OneShift\" />
    128   </ItemGroup>
     128  <ItemGroup />
    129129  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    130130  <PropertyGroup>
  • branches/PTSP/HeuristicLab.Problems.PTSP/3.3/Interfaces/IPTSPPathMoveEvaluator.cs

    r12191 r12219  
    2929    ILookupParameter<DistanceMatrix> DistanceMatrixParameter { get; }
    3030    ILookupParameter<BoolValue> UseDistanceMatrixParameter { get; }
     31
    3132  }
    3233}
  • branches/PTSP/HeuristicLab.Problems.PTSP/3.3/MoveEvaluators/PTSPPathMoveEvaluator.cs

    r12191 r12219  
    4747      get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
    4848    }
     49    public IValueParameter<ItemList<ItemList<IntValue>>> RealizationsParameter {
     50      get { return (IValueParameter<ItemList<ItemList<IntValue>>>)Parameters["Realizations"]; }
     51    }
    4952
    5053    [StorableConstructor]
     
    5760      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
    5861      Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated (if it does not exist already) and used for evaluation, otherwise false."));
     62      Parameters.Add(new ValueParameter<ItemList<ItemList<IntValue>>>("Realizations", "The concrete..."));
    5963    }
    6064
  • branches/PTSP/HeuristicLab.Problems.PTSP/3.3/PTSP.cs

    r12191 r12219  
    5353      get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
    5454    }
    55     public OptionalValueParameter<DistanceMatrix> ProbabilityMatrixParameter {
    56       get { return (OptionalValueParameter<DistanceMatrix>)Parameters["ProbabilityMatrix"]; }
     55    public OptionalValueParameter<ItemList<DoubleValue>> ProbabilityMatrixParameter {
     56      get { return (OptionalValueParameter<ItemList<DoubleValue>>)Parameters["ProbabilityMatrix"]; }
    5757    }
    5858    public ValueParameter<IntValue> SampleSizeParameter {
     
    7979      set { BestKnownSolutionParameter.Value = value; }
    8080    }
    81     public DistanceMatrix ProbabilityMatrix {
     81    public ItemList<DoubleValue> ProbabilityMatrix {
    8282      get { return ProbabilityMatrixParameter.Value; }
    8383      set { ProbabilityMatrixParameter.Value = value; }
     
    105105      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)));
    106106      Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
    107       Parameters.Add(new OptionalValueParameter<DistanceMatrix>("ProbabilityMatrix", "The matrix which contains the probabilities of each of the cities."));
     107      Parameters.Add(new OptionalValueParameter<ItemList<DoubleValue>>("ProbabilityMatrix", "The matrix which contains the probabilities of each of the cities."));
    108108     
    109109    }
     
    112112      DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
    113113      // Get Probabilities of cities using random with seed from hash function on the Name of the instance
    114       ProbabilityMatrix = new DistanceMatrix(1, data.Dimension);
     114      ProbabilityMatrix = new ItemList<DoubleValue>(data.Dimension);
    115115      Random r = new Random(data.Name.GetHashCode());
    116116      for (int i = 0; i < data.Dimension; i++) {
    117         ProbabilityMatrix[0,i] = r.NextDouble();
     117        ProbabilityMatrix.Add(new DoubleValue(r.NextDouble()));
    118118      }
    119119      Encoding.Length = data.Dimension;
Note: See TracChangeset for help on using the changeset viewer.