Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/05/10 00:48:18 (15 years ago)
Author:
swagner
Message:

Worked on refactoring of algorithm analysis and tracing (#999)

  • adapted GA and TSP
  • removed stuff related to visualizers
Location:
trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3
Files:
4 deleted
3 edited
1 copied
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/BestTSPSolutionAnalyzer.cs

    r3612 r3616  
    3232namespace HeuristicLab.Problems.TravelingSalesman {
    3333  /// <summary>
    34   /// An operator for visualizing the best tour of Traveling Salesman Problems given in path representation using city coordinates.
     34  /// An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.
    3535  /// </summary>
    36   [Item("BestPathTSPTourVisualizer", "An operator for visualizing the best tour of Traveling Salesman Problems given in path representation using city coordinates.")]
     36  [Item("BestTSPSolutionAnalyzer", "An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
    3737  [StorableClass]
    38   public sealed class BestPathTSPTourVisualizer : SingleSuccessorOperator, IPathCoordinatesTSPSolutionsVisualizer {
    39     public ILookupParameter<DoubleMatrix> CoordinatesParameter {
    40       get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
     38  public sealed class BestTSPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
     39    public LookupParameter<DoubleMatrix> CoordinatesParameter {
     40      get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
    4141    }
    42     public ILookupParameter<ItemArray<Permutation>> PermutationParameter {
    43       get { return (ILookupParameter<ItemArray<Permutation>>)Parameters["Permutation"]; }
     42    public SubScopesLookupParameter<Permutation> PermutationParameter {
     43      get { return (SubScopesLookupParameter<Permutation>)Parameters["Permutation"]; }
    4444    }
    45     public ILookupParameter<ItemArray<DoubleValue>> QualityParameter {
    46       get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["Quality"]; }
     45    public SubScopesLookupParameter<DoubleValue> QualityParameter {
     46      get { return (SubScopesLookupParameter<DoubleValue>)Parameters["Quality"]; }
    4747    }
    48     public ILookupParameter<PathTSPTour> PathTSPTourParameter {
    49       get { return (ILookupParameter<PathTSPTour>)Parameters["PathTSPTour"]; }
     48    public LookupParameter<PathTSPTour> BestSolutionParameter {
     49      get { return (LookupParameter<PathTSPTour>)Parameters["BestSolution"]; }
    5050    }
    51     ILookupParameter ISolutionsVisualizer.VisualizationParameter {
    52       get { return PathTSPTourParameter; }
     51    public ValueLookupParameter<ResultCollection> ResultsParameter {
     52      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
    5353    }
    5454
    55     public BestPathTSPTourVisualizer()
     55    public BestTSPSolutionAnalyzer()
    5656      : base() {
    5757      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
    58       Parameters.Add(new SubScopesLookupParameter<Permutation>("Permutation", "The TSP solutions given in path representation from which the best solution should be visualized."));
    59       Parameters.Add(new SubScopesLookupParameter<DoubleValue>("Quality", "The qualities of the TSP solutions which should be visualized."));
    60       Parameters.Add(new LookupParameter<PathTSPTour>("PathTSPTour", "The visual representation of the best TSP solution."));
     58      Parameters.Add(new SubScopesLookupParameter<Permutation>("Permutation", "The TSP solutions given in path representation from which the best solution should be analyzed."));
     59      Parameters.Add(new SubScopesLookupParameter<DoubleValue>("Quality", "The qualities of the TSP solutions which should be analyzed."));
     60      Parameters.Add(new LookupParameter<PathTSPTour>("BestSolution", "The best TSP solution."));
     61      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best TSP solution should be stored."));
    6162    }
    6263
     
    6566      ItemArray<Permutation> permutations = PermutationParameter.ActualValue;
    6667      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
     68      ResultCollection results = ResultsParameter.ActualValue;
    6769
    6870      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
    6971
    70       PathTSPTour tour = PathTSPTourParameter.ActualValue;
    71       if (tour == null) PathTSPTourParameter.ActualValue = new PathTSPTour(coordinates, permutations[i]);
    72       else {
    73         tour.Coordinates = coordinates;
    74         tour.Permutation = permutations[i];
     72      PathTSPTour tour = BestSolutionParameter.ActualValue;
     73      if (tour == null) {
     74        tour = new PathTSPTour(coordinates, permutations[i], qualities[i]);
     75        BestSolutionParameter.ActualValue = tour;
     76        results.Add(new Result("Best TSP Solution", tour));
     77      } else {
     78        if (tour.Quality.Value > qualities[i].Value) {
     79          tour.Coordinates = coordinates;
     80          tour.Permutation = permutations[i];
     81          tour.Quality = qualities[i];
     82          results["Best TSP Solution"].Value = tour;
     83        }
    7584      }
     85
    7686      return base.Apply();
    7787    }
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/HeuristicLab.Problems.TravelingSalesman-3.3.csproj

    r3384 r3616  
    8484  </ItemGroup>
    8585  <ItemGroup>
     86    <Compile Include="Analyzers\BestTSPSolutionAnalyzer.cs" />
    8687    <Compile Include="Evaluators\TSPEuclideanPathEvaluator.cs" />
    8788    <Compile Include="Evaluators\TSPGeoPathEvaluator.cs" />
     
    9798    <Compile Include="TravelingSalesmanProblem.cs" />
    9899    <Compile Include="TSPLIBTourParser.cs" />
    99     <Compile Include="Interfaces\ICoordinatesTSPSolutionsVisualizer.cs" />
    100     <Compile Include="Interfaces\IPathCoordinatesTSPSolutionsVisualizer.cs" />
    101     <Compile Include="Interfaces\ITSPSolutionsVisualizer.cs" />
    102100    <Compile Include="PathTSPTour.cs" />
    103     <Compile Include="Visualizers\BestPathTSPTourVisualizer.cs" />
    104101    <Compile Include="Evaluators\TSPCoordinatesPathEvaluator.cs" />
    105102    <Compile Include="Evaluators\TSPEvaluator.cs" />
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/PathTSPTour.cs

    r3431 r3616  
    6565      }
    6666    }
     67    [Storable]
     68    private DoubleValue quality;
     69    public DoubleValue Quality {
     70      get { return quality; }
     71      set {
     72        if (quality != value) {
     73          if (quality != null) DeregisterQualityEvents();
     74          quality = value;
     75          if (quality != null) RegisterQualityEvents();
     76          OnQualityChanged();
     77        }
     78      }
     79    }
    6780
    6881    public PathTSPTour() : base() { }
     
    7891      Initialize();
    7992    }
     93    public PathTSPTour(DoubleMatrix coordinates, Permutation permutation, DoubleValue quality)
     94      : base() {
     95      this.coordinates = coordinates;
     96      this.permutation = permutation;
     97      this.quality = quality;
     98      Initialize();
     99    }
    80100    [StorableConstructor]
    81101    private PathTSPTour(bool deserializing) : base(deserializing) { }
     
    85105      if (coordinates != null) RegisterCoordinatesEvents();
    86106      if (permutation != null) RegisterPermutationEvents();
     107      if (quality != null) RegisterQualityEvents();
    87108    }
    88109
     
    92113      clone.coordinates = (DoubleMatrix)cloner.Clone(coordinates);
    93114      clone.permutation = (Permutation)cloner.Clone(permutation);
     115      clone.quality = (DoubleValue)cloner.Clone(quality);
    94116      clone.Initialize();
    95117      return clone;
     
    106128    private void OnPermutationChanged() {
    107129      var changed = PermutationChanged;
     130      if (changed != null)
     131        changed(this, EventArgs.Empty);
     132    }
     133    public event EventHandler QualityChanged;
     134    private void OnQualityChanged() {
     135      var changed = QualityChanged;
    108136      if (changed != null)
    109137        changed(this, EventArgs.Empty);
     
    126154      Permutation.Reset -= new EventHandler(Permutation_Reset);
    127155    }
     156    private void RegisterQualityEvents() {
     157      Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
     158    }
     159    private void DeregisterQualityEvents() {
     160      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
     161    }
    128162
    129163    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
     
    139173      OnPermutationChanged();
    140174    }
     175    private void Quality_ValueChanged(object sender, EventArgs e) {
     176      OnQualityChanged();
     177    }
    141178    #endregion
    142179  }
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/TravelingSalesmanProblem.cs

    r3504 r3616  
    7171      get { return EvaluatorParameter; }
    7272    }
    73     public OptionalValueParameter<ITSPSolutionsVisualizer> VisualizerParameter {
    74       get { return (OptionalValueParameter<ITSPSolutionsVisualizer>)Parameters["Visualizer"]; }
    75     }
    76     IParameter IProblem.VisualizerParameter {
    77       get { return VisualizerParameter; }
    78     }
    7973    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
    8074      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
     
    118112      get { return EvaluatorParameter.Value; }
    119113    }
    120     public ITSPSolutionsVisualizer Visualizer {
    121       get { return VisualizerParameter.Value; }
    122       set { VisualizerParameter.Value = value; }
    123     }
    124     ISolutionsVisualizer IProblem.Visualizer {
    125       get { return VisualizerParameter.Value; }
    126     }
    127114    public DoubleValue BestKnownQuality {
    128115      get { return BestKnownQualityParameter.Value; }
     
    133120      set { BestKnownSolutionParameter.Value = value; }
    134121    }
    135     private List<IPermutationOperator> operators;
     122    private List<IOperator> operators;
    136123    public IEnumerable<IOperator> Operators {
    137       get { return operators.Cast<IOperator>(); }
     124      get { return operators; }
     125    }
     126    private BestTSPSolutionAnalyzer Analyzer {
     127      get { return operators.OfType<BestTSPSolutionAnalyzer>().FirstOrDefault(); }
    138128    }
    139129    #endregion
     
    143133      RandomPermutationCreator creator = new RandomPermutationCreator();
    144134      TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
    145       BestPathTSPTourVisualizer visualizer = new BestPathTSPTourVisualizer();
    146135
    147136      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false)));
     
    151140      Parameters.Add(new ValueParameter<IPermutationCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));
    152141      Parameters.Add(new ValueParameter<ITSPEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));
    153       Parameters.Add(new OptionalValueParameter<ITSPSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize TSP solutions.", visualizer));
    154142      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
    155143      Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
     
    166154      ParameterizeSolutionCreator();
    167155      ParameterizeEvaluator();
    168       ParameterizeVisualizer();
    169156
    170157      Initialize();
     
    221208        EvaluatorChanged(this, EventArgs.Empty);
    222209    }
    223     public event EventHandler VisualizerChanged;
    224     private void OnVisualizerChanged() {
    225       if (VisualizerChanged != null)
    226         VisualizerChanged(this, EventArgs.Empty);
    227     }
    228210    public event EventHandler OperatorsChanged;
    229211    private void OnOperatorsChanged() {
     
    249231      ParameterizeSolutionCreator();
    250232      ParameterizeEvaluator();
    251       ParameterizeVisualizer();
     233      ParameterizeAnalyzer();
    252234      ParameterizeOperators();
    253235      OnSolutionCreatorChanged();
     
    255237    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
    256238      ParameterizeEvaluator();
    257       ParameterizeVisualizer();
     239      ParameterizeAnalyzer();
    258240      ParameterizeOperators();
    259241    }
     
    262244      ParameterizeEvaluator();
    263245      UpdateMoveEvaluators();
    264       ParameterizeVisualizer();
     246      ParameterizeAnalyzer();
    265247      ClearDistanceMatrix();
    266248      OnEvaluatorChanged();
    267249    }
    268250    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
    269       ParameterizeVisualizer();
    270     }
    271     private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
    272       ParameterizeVisualizer();
    273       OnVisualizerChanged();
     251      ParameterizeAnalyzer();
    274252    }
    275253    private void MoveGenerator_InversionMoveParameter_ActualNameChanged(object sender, EventArgs e) {
     
    298276      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
    299277      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    300       VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
    301278    }
    302279
    303280    private void InitializeOperators() {
    304       operators = new List<IPermutationOperator>();
    305       operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
     281      operators = new List<IOperator>();
     282      operators.Add(new BestTSPSolutionAnalyzer());
     283      ParameterizeAnalyzer();
     284      operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>().Cast<IOperator>());
    306285      ParameterizeOperators();
    307286      UpdateMoveEvaluators();
     
    344323      }
    345324    }
    346     private void ParameterizeVisualizer() {
    347       if (Visualizer != null) {
    348         Visualizer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
    349         if (Visualizer is ICoordinatesTSPSolutionsVisualizer)
    350           ((ICoordinatesTSPSolutionsVisualizer)Visualizer).CoordinatesParameter.ActualName = CoordinatesParameter.Name;
    351         if (Visualizer is IPathCoordinatesTSPSolutionsVisualizer)
    352           ((IPathCoordinatesTSPSolutionsVisualizer)Visualizer).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     325    private void ParameterizeAnalyzer() {
     326      if (Analyzer != null) {
     327        Analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
     328        Analyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
     329        Analyzer.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     330        Analyzer.ResultsParameter.ActualName = "Results";
    353331      }
    354332    }
Note: See TracChangeset for help on using the changeset viewer.