Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/19/15 17:16:39 (9 years ago)
Author:
apolidur
Message:

#2221: Local improvement operator for VNS

File:
1 edited

Legend:

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

    r12219 r12228  
    4040  public abstract class ProbabilisticTravelingSalesmanProblem : SingleObjectiveBasicProblem<PermutationEncoding>, IStorableContent,
    4141  IProblemInstanceConsumer<TSPData> {
     42    private static readonly int DistanceMatrixSizeLimit = 1000;
     43
    4244    #region Parameter Properties
    4345    public OptionalValueParameter<DoubleMatrix> CoordinatesParameter {
     
    5355      get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
    5456    }
    55     public OptionalValueParameter<ItemList<DoubleValue>> ProbabilityMatrixParameter {
    56       get { return (OptionalValueParameter<ItemList<DoubleValue>>)Parameters["ProbabilityMatrix"]; }
     57    public ValueParameter<DoubleArray> ProbabilityMatrixParameter {
     58      get { return (ValueParameter<DoubleArray>)Parameters["ProbabilityMatrix"]; }
    5759    }
    5860    public ValueParameter<IntValue> SampleSizeParameter {
     
    7981      set { BestKnownSolutionParameter.Value = value; }
    8082    }
    81     public ItemList<DoubleValue> ProbabilityMatrix {
     83    public DoubleArray ProbabilityMatrix {
    8284      get { return ProbabilityMatrixParameter.Value; }
    8385      set { ProbabilityMatrixParameter.Value = value; }
     
    105107      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)));
    106108      Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
    107       Parameters.Add(new OptionalValueParameter<ItemList<DoubleValue>>("ProbabilityMatrix", "The matrix which contains the probabilities of each of the cities."));
     109      Parameters.Add(new ValueParameter<DoubleArray>("ProbabilityMatrix", "The matrix which contains the probabilities of each of the cities."));
     110
     111      Coordinates = new DoubleMatrix(new double[,] {
     112        { 100, 100 }, { 100, 200 }, { 100, 300 }, { 100, 400 },
     113        { 200, 100 }, { 200, 200 }, { 200, 300 }, { 200, 400 },
     114        { 300, 100 }, { 300, 200 }, { 300, 300 }, { 300, 400 },
     115        { 400, 100 }, { 400, 200 }, { 400, 300 }, { 400, 400 }
     116      });
     117
     118      ProbabilityMatrix = new DoubleArray(new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
    108119     
    109120    }
    110121
    111122    public virtual void Load(TSPData data) {
     123      if (data.Coordinates == null && data.Distances == null)
     124        throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!");
     125      if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == DistanceMeasure.Att
     126        || data.DistanceMeasure == DistanceMeasure.Manhattan
     127        || data.DistanceMeasure == DistanceMeasure.Maximum))
     128        throw new System.IO.InvalidDataException("The given instance uses an unsupported distance measure and is too large for using a distance matrix.");
     129      if (data.Coordinates != null && data.Coordinates.GetLength(1) != 2)
     130        throw new System.IO.InvalidDataException("The coordinates of the given instance are not in the right format, there need to be one row for each customer and two columns for the x and y coordinates.");
     131
     132      Name = data.Name;
     133      Description = data.Description;
     134
     135      bool clearCoordinates = false, clearDistanceMatrix = false;
     136      if (data.Coordinates != null && data.Coordinates.GetLength(0) > 0)
     137        Coordinates = new DoubleMatrix(data.Coordinates);
     138      else clearCoordinates = true;
     139
     140      // reset them after assigning the evaluator
     141      if (clearCoordinates) Coordinates = null;
     142      if (clearDistanceMatrix) DistanceMatrix = null;
     143
    112144      DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
    113145      // Get Probabilities of cities using random with seed from hash function on the Name of the instance
    114       ProbabilityMatrix = new ItemList<DoubleValue>(data.Dimension);
     146      ProbabilityMatrix = new DoubleArray(data.Dimension);
    115147      Random r = new Random(data.Name.GetHashCode());
    116148      for (int i = 0; i < data.Dimension; i++) {
    117         ProbabilityMatrix.Add(new DoubleValue(r.NextDouble()));
     149        ProbabilityMatrix[i] = r.NextDouble();
    118150      }
    119151      Encoding.Length = data.Dimension;
     152
     153      OnReset();
    120154    }
    121155  }
Note: See TracChangeset for help on using the changeset viewer.