Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/15/12 09:11:17 (13 years ago)
Author:
gkronber
Message:

#1081 merged r7462:7609 from trunk into time series branch

Location:
branches/HeuristicLab.TimeSeries
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TimeSeries

  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.TravelingSalesman/3.3/TravelingSalesmanProblem.cs

    r7460 r7615  
    3232using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3333using HeuristicLab.PluginInfrastructure;
     34using HeuristicLab.Problems.Instances;
    3435
    3536namespace HeuristicLab.Problems.TravelingSalesman {
     
    3738  [Creatable("Problems")]
    3839  [StorableClass]
    39   public sealed class TravelingSalesmanProblem : SingleObjectiveHeuristicOptimizationProblem<ITSPEvaluator, IPermutationCreator>, IStorableContent {
     40  public sealed class TravelingSalesmanProblem : SingleObjectiveHeuristicOptimizationProblem<ITSPEvaluator, IPermutationCreator>, IStorableContent,
     41    IProblemInstanceConsumer<TSPData> {
     42    private static readonly int DistanceMatrixSizeLimit = 1000;
    4043    public string Filename { get; set; }
    4144
     
    107110    public TravelingSalesmanProblem()
    108111      : base(new TSPRoundedEuclideanPathEvaluator(), new RandomPermutationCreator()) {
    109 
    110112      Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
    111113      Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
     
    247249        evaluator.UseDistanceMatrixParameter.Hidden = true;
    248250      }
     251      if (Evaluator is ITSPDistanceMatrixEvaluator) {
     252        var evaluator = (ITSPDistanceMatrixEvaluator)Evaluator;
     253        evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
     254        evaluator.DistanceMatrixParameter.Hidden = true;
     255      }
    249256    }
    250257    private void ParameterizeAnalyzers() {
     
    310317
    311318    private void ClearDistanceMatrix() {
    312       DistanceMatrixParameter.Value = null;
     319      if (!(Evaluator is ITSPDistanceMatrixEvaluator))
     320        DistanceMatrixParameter.Value = null;
    313321    }
    314322    #endregion
    315323
    316     public void ImportFromTSPLIB(string tspFileName, string optimalTourFileName) {
    317       TSPLIBParser tspParser = new TSPLIBParser(tspFileName);
    318       tspParser.Parse();
    319       Name = tspParser.Name + " TSP (imported from TSPLIB)";
    320       if (!string.IsNullOrEmpty(tspParser.Comment)) Description = tspParser.Comment;
    321       Coordinates = new DoubleMatrix(tspParser.Vertices);
    322       if (tspParser.WeightType == TSPLIBParser.TSPLIBEdgeWeightType.EUC_2D) {
    323         TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
    324         evaluator.QualityParameter.ActualName = "TSPTourLength";
    325         Evaluator = evaluator;
    326       } else if (tspParser.WeightType == TSPLIBParser.TSPLIBEdgeWeightType.GEO) {
    327         TSPGeoPathEvaluator evaluator = new TSPGeoPathEvaluator();
    328         evaluator.QualityParameter.ActualName = "TSPTourLength";
    329         Evaluator = evaluator;
    330       }
     324    public void Load(TSPData data) {
     325      if (data.Coordinates == null && data.Distances == null)
     326        throw new System.IO.InvalidDataException("The given instance does not specify neither coordinates or distances!");
     327      if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == TSPDistanceMeasure.Att
     328        || data.DistanceMeasure == TSPDistanceMeasure.Manhattan
     329        || data.DistanceMeasure == TSPDistanceMeasure.Maximum
     330        || data.DistanceMeasure == TSPDistanceMeasure.UpperEuclidean))
     331        throw new System.IO.InvalidDataException("The given instance uses an unsupported distance measure and is too large for using a distance matrix.");
     332      if (data.Coordinates != null && data.Coordinates.GetLength(1) != 2)
     333        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.");
     334
     335      Name = data.Name;
     336      Description = data.Description;
     337
     338      if (data.Coordinates != null && data.Coordinates.GetLength(0) > 0)
     339        Coordinates = new DoubleMatrix(data.Coordinates);
     340
     341      TSPEvaluator evaluator;
     342      if (data.DistanceMeasure == TSPDistanceMeasure.Att
     343        || data.DistanceMeasure == TSPDistanceMeasure.Manhattan
     344        || data.DistanceMeasure == TSPDistanceMeasure.Maximum
     345        || data.DistanceMeasure == TSPDistanceMeasure.UpperEuclidean) {
     346        evaluator = new TSPDistanceMatrixEvaluator();
     347        UseDistanceMatrix = new BoolValue(true);
     348        DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
     349      } else if (data.DistanceMeasure == TSPDistanceMeasure.Direct && data.Distances != null) {
     350        evaluator = new TSPDistanceMatrixEvaluator();
     351        UseDistanceMatrix = new BoolValue(true);
     352        DistanceMatrix = new DistanceMatrix(data.Distances);
     353      } else {
     354        DistanceMatrix = null;
     355        UseDistanceMatrix = new BoolValue(data.Dimension <= DistanceMatrixSizeLimit);
     356        switch (data.DistanceMeasure) {
     357          case TSPDistanceMeasure.Euclidean:
     358            evaluator = new TSPEuclideanPathEvaluator();
     359            break;
     360          case TSPDistanceMeasure.RoundedEuclidean:
     361            evaluator = new TSPRoundedEuclideanPathEvaluator();
     362            break;
     363          case TSPDistanceMeasure.Geo:
     364            evaluator = new TSPGeoPathEvaluator();
     365            break;
     366          default:
     367            throw new InvalidDataException("An unknown distance measure is given in the instance!");
     368        }
     369      }
     370      evaluator.QualityParameter.ActualName = "TSPTourLength";
     371      Evaluator = evaluator;
     372
     373      BestKnownSolution = null;
    331374      BestKnownQuality = null;
    332       BestKnownSolution = null;
    333 
    334       if (!string.IsNullOrEmpty(optimalTourFileName)) {
    335         TSPLIBTourParser tourParser = new TSPLIBTourParser(optimalTourFileName);
    336         tourParser.Parse();
    337         if (tourParser.Tour.Length != Coordinates.Rows) throw new InvalidDataException("Length of optimal tour is not equal to number of cities.");
    338         BestKnownSolution = new Permutation(PermutationTypes.RelativeUndirected, tourParser.Tour);
     375
     376      if (data.BestKnownTour != null) {
     377        try {
     378          EvaluateAndLoadTour(data.BestKnownTour);
     379        } catch (InvalidOperationException) {
     380          if (data.BestKnownQuality.HasValue)
     381            BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
     382        }
     383      } else if (data.BestKnownQuality.HasValue) {
     384        BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
    339385      }
    340386      OnReset();
    341387    }
    342     public void ImportFromTSPLIB(string tspFileName, string optimalTourFileName, double bestKnownQuality) {
    343       ImportFromTSPLIB(tspFileName, optimalTourFileName);
    344       BestKnownQuality = new DoubleValue(bestKnownQuality);
     388
     389    public void EvaluateAndLoadTour(int[] tour) {
     390      var route = new Permutation(PermutationTypes.RelativeUndirected, tour);
     391      BestKnownSolution = route;
     392
     393      double quality;
     394      if (Evaluator is TSPDistanceMatrixEvaluator) {
     395        quality = TSPDistanceMatrixEvaluator.Apply(DistanceMatrix, route);
     396      } else if (Evaluator is TSPCoordinatesPathEvaluator) {
     397        quality = TSPCoordinatesPathEvaluator.Apply((TSPCoordinatesPathEvaluator)Evaluator, Coordinates, route);
     398      } else {
     399        throw new InvalidOperationException("Cannot calculate solution quality, evaluator type is unknown.");
     400      }
     401      BestKnownQuality = new DoubleValue(quality);
    345402    }
    346403  }
Note: See TracChangeset for help on using the changeset viewer.