Free cookie consent management tool by TermsFeed Policy Generator

Changeset 7621


Ignore:
Timestamp:
03/15/12 14:29:53 (12 years ago)
Author:
abeham
Message:

#1396:

  • Fixed loading of instances that did not specify coordinates (visual or actual ones)
  • Turned coordinates into an OptionalValueParameter
  • Added checks in relevant operators that throw an exception if they can neither find coordinates or distances
  • Writing a message to the visualization if coordinates are not defined
Location:
trunk/sources
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman.Views/3.3/PathTSPTourView.cs

    r7259 r7621  
    118118                graphics.FillRectangle(Brushes.Red, points[i].X - 2, points[i].Y - 2, 6, 6);
    119119            }
     120          } else {
     121            using (Graphics graphics = Graphics.FromImage(bitmap)) {
     122              graphics.Clear(Color.White);
     123              Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
     124              string text = "No coordinates defined or in wrong format.";
     125              SizeF strSize = graphics.MeasureString(text, font);
     126              graphics.DrawString(text, font, Brushes.Black, (float)(pictureBox.Width - strSize.Width) / 2.0f, (float)(pictureBox.Height - strSize.Height) / 2.0f);
     127            }
    120128          }
    121129          pictureBox.Image = bitmap;
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/TSPAlleleFrequencyAnalyzer.cs

    r7259 r7621  
    6060      DoubleMatrix coords = CoordinatesParameter.ActualValue;
    6161      DistanceMatrix dm = DistanceMatrixParameter.ActualValue;
     62      if (dm == null && coords == null) throw new InvalidOperationException("Neither a distance matrix nor coordinates were given.");
    6263      int source, target, h;
    6364      double impact;
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Evaluators/TSPCoordinatesPathEvaluator.cs

    r7558 r7621  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    9394            if (dm == null) {  // check again to avoid race condition
    9495              DoubleMatrix c = CoordinatesParameter.ActualValue;
     96              if (c == null) throw new InvalidOperationException("Neither a distance matrix nor coordinates were given.");
    9597              dm = new DistanceMatrix(c.Rows, c.Rows);
    9698              for (int i = 0; i < dm.Rows; i++) {
     
    111113        Permutation p = PermutationParameter.ActualValue;
    112114        DoubleMatrix c = CoordinatesParameter.ActualValue;
    113 
     115        if (c == null) throw new InvalidOperationException("No coordinates were given.");
    114116        double length = 0;
    115117        for (int i = 0; i < p.Length - 1; i++)
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TSPPathMoveEvaluator.cs

    r7259 r7621  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    7879        DistanceMatrix distanceMatrix = DistanceMatrixParameter.ActualValue;
    7980        if (distanceMatrix == null) {
     81          if (coordinates == null) throw new InvalidOperationException("Neither a distance matrix nor coordinates were given.");
    8082          distanceMatrix = CalculateDistanceMatrix(coordinates);
    8183          DistanceMatrixParameter.ActualValue = distanceMatrix;
    8284        }
    8385        relativeQualityDifference = EvaluateByDistanceMatrix(permutation, distanceMatrix);
    84       } else relativeQualityDifference = EvaluateByCoordinates(permutation, coordinates);
     86      } else {
     87        if (coordinates == null) throw new InvalidOperationException("No coordinates were given.");
     88        relativeQualityDifference = EvaluateByCoordinates(permutation, coordinates);
     89      }
    8590      DoubleValue moveQuality = MoveQualityParameter.ActualValue;
    8691      if (moveQuality == null) MoveQualityParameter.ActualValue = new DoubleValue(QualityParameter.ActualValue.Value + relativeQualityDifference);
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/TravelingSalesmanProblem.cs

    r7558 r7621  
    4444
    4545    #region Parameter Properties
    46     public ValueParameter<DoubleMatrix> CoordinatesParameter {
    47       get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
     46    public OptionalValueParameter<DoubleMatrix> CoordinatesParameter {
     47      get { return (OptionalValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
    4848    }
    4949    public OptionalValueParameter<DistanceMatrix> DistanceMatrixParameter {
     
    110110    public TravelingSalesmanProblem()
    111111      : base(new TSPRoundedEuclideanPathEvaluator(), new RandomPermutationCreator()) {
    112       Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
     112      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
    113113      Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
    114114      Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
     
    148148      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    149149      ParameterizeEvaluator();
     150      ParameterizeSolutionCreator();
    150151      UpdateMoveEvaluators();
    151152      ParameterizeAnalyzers();
     
    153154    }
    154155    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
    155       Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
    156       Coordinates.Reset += new EventHandler(Coordinates_Reset);
     156      if (Coordinates != null) {
     157        Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
     158        Coordinates.Reset += new EventHandler(Coordinates_Reset);
     159      }
    157160      ParameterizeSolutionCreator();
    158161      ClearDistanceMatrix();
     
    197200      }
    198201
     202      ValueParameter<DoubleMatrix> oldCoordinates = (Parameters["Coordinates"] as ValueParameter<DoubleMatrix>);
     203      if (oldCoordinates != null) {
     204        Parameters.Remove(oldCoordinates);
     205        Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", oldCoordinates.Value, oldCoordinates.GetsCollected));
     206      }
     207
    199208      if (Operators.Count == 0) InitializeOperators();
    200209      #endregion
     
    204213    private void RegisterEventHandlers() {
    205214      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
    206       Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
    207       Coordinates.Reset += new EventHandler(Coordinates_Reset);
     215      if (Coordinates != null) {
     216        Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
     217        Coordinates.Reset += new EventHandler(Coordinates_Reset);
     218      }
    208219      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
    209220      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
     
    229240    }
    230241    private void ParameterizeSolutionCreator() {
    231       SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);
    232       SolutionCreator.LengthParameter.Hidden = true;
     242      if (Evaluator is ITSPDistanceMatrixEvaluator && DistanceMatrix != null)
     243        SolutionCreator.LengthParameter.Value = new IntValue(DistanceMatrix.Rows);
     244      else if (Evaluator is ITSPCoordinatesPathEvaluator && Coordinates != null)
     245        SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);
     246      else SolutionCreator.LengthParameter.Value = null;
     247      SolutionCreator.LengthParameter.Hidden = SolutionCreator.LengthParameter.Value != null;
    233248      SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected);
    234249      SolutionCreator.PermutationTypeParameter.Hidden = true;
     
    324339    public void Load(TSPData data) {
    325340      if (data.Coordinates == null && data.Distances == null)
    326         throw new System.IO.InvalidDataException("The given instance does not specify neither coordinates or distances!");
     341        throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!");
    327342      if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == TSPDistanceMeasure.Att
    328343        || data.DistanceMeasure == TSPDistanceMeasure.Manhattan
     
    338353      if (data.Coordinates != null && data.Coordinates.GetLength(0) > 0)
    339354        Coordinates = new DoubleMatrix(data.Coordinates);
     355      else Coordinates = null;
    340356
    341357      TSPEvaluator evaluator;
     
    392408
    393409      double quality;
    394       if (Evaluator is TSPDistanceMatrixEvaluator) {
     410      if (Evaluator is ITSPDistanceMatrixEvaluator) {
    395411        quality = TSPDistanceMatrixEvaluator.Apply(DistanceMatrix, route);
    396       } else if (Evaluator is TSPCoordinatesPathEvaluator) {
     412      } else if (Evaluator is ITSPCoordinatesPathEvaluator) {
    397413        quality = TSPCoordinatesPathEvaluator.Apply((TSPCoordinatesPathEvaluator)Evaluator, Coordinates, route);
    398414      } else {
Note: See TracChangeset for help on using the changeset viewer.