Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5210


Ignore:
Timestamp:
01/05/11 00:49:56 (14 years ago)
Author:
swagner
Message:

Adapted distance matrix calculation in TSPCoordinatesPathEvaluator to avoid that multiple distance matrices are created, if the evaluator is executed in parallel (#1333)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Evaluators/TSPCoordinatesPathEvaluator.cs

    r4825 r5210  
    3434  [StorableClass]
    3535  public abstract class TSPCoordinatesPathEvaluator : TSPEvaluator, ITSPCoordinatesPathEvaluator {
     36    private object locker = new object();
     37
    3638    public ILookupParameter<Permutation> PermutationParameter {
    3739      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
     
    7779
    7880        if (dm == null) {  // calculate distance matrix
    79           DoubleMatrix c = CoordinatesParameter.ActualValue;
    80 
    81           dm = new DistanceMatrix(c.Rows, c.Rows);
    82           for (int i = 0; i < dm.Rows; i++) {
    83             for (int j = 0; j < dm.Columns; j++)
    84               dm[i, j] = CalculateDistance(c[i, 0], c[i, 1], c[j, 0], c[j, 1]);
     81          lock (locker) {
     82            dm = DistanceMatrixParameter.ActualValue;
     83            if (dm == null) {  // check again to avoid race condition
     84              DoubleMatrix c = CoordinatesParameter.ActualValue;
     85              dm = new DistanceMatrix(c.Rows, c.Rows);
     86              for (int i = 0; i < dm.Rows; i++) {
     87                for (int j = 0; j < dm.Columns; j++)
     88                  dm[i, j] = CalculateDistance(c[i, 0], c[i, 1], c[j, 0], c[j, 1]);
     89              }
     90              DistanceMatrixParameter.ActualValue = (DistanceMatrix)dm.AsReadOnly();
     91            }
    8592          }
    86           DistanceMatrixParameter.ActualValue = (DistanceMatrix)dm.AsReadOnly();
    8793        }
    8894
Note: See TracChangeset for help on using the changeset viewer.