Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/DuplicateSolutionsAnalyzer.cs @ 8498

Last change on this file since 8498 was 8498, checked in by ascheibe, 12 years ago

#1886 added helper classes for generating charts

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.TravelingSalesman;
31
32namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
33  [Item("DuplicateSolutionsAnalyzer", "An operator that analyzes duplicate solutions in the population.")]
34  [StorableClass]
35  public class DuplicateSolutionsAnalyzer : SingleSuccessorOperator, IAnalyzer {
36    private const string ResultsParameterName = "Results";
37
38    #region IAnalyzer Members
39    public bool EnabledByDefault {
40      get { return false; }
41    }
42    #endregion
43
44    #region Parameter properties
45    public ILookupParameter<ResultCollection> ResultsParameter {
46      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
47    }
48    public IValueParameter<SingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
49      get { return (IValueParameter<SingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
50    }
51    #endregion
52
53    #region Properties
54    public ResultCollection Results {
55      get { return ResultsParameter.ActualValue; }
56    }
57
58    private DataTableHelper chartingHelper;
59    #endregion
60
61    [StorableConstructor]
62    private DuplicateSolutionsAnalyzer(bool deserializing) : base(deserializing) { }
63    private DuplicateSolutionsAnalyzer(DuplicateSolutionsAnalyzer original, Cloner cloner) : base(original, cloner) { }
64    public DuplicateSolutionsAnalyzer()
65      : base() {
66      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
67      Parameters.Add(new ValueParameter<SingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator", new TSPSimilarityCalculator()));
68      SimilarityCalculatorParameter.Value.SolutionVariableName = "TSPTour";
69      SimilarityCalculatorParameter.Value.QualityVariableName = "TSPTourLength";
70
71      chartingHelper = new DataTableHelper();
72    }
73
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new DuplicateSolutionsAnalyzer(this, cloner);
76    }
77
78    public override IOperation Apply() {
79      chartingHelper.InitializeChart(Results, "Duplicate Solutions", "Duplicate Solutions per Generation");
80
81      var similarities = SimilarityCalculatorParameter.Value.CalculateSolutionCrowdSimilarity(ExecutionContext.Scope);
82      int counter = 0;
83      for (int i = 0; i < similarities.Length; i++) {
84        for (int j = 0; j < similarities[i].Length; j++) {
85          if (similarities[i][j] == 1.0 && i != j) {
86            counter++;
87            break;
88          }
89        }
90      }
91
92      chartingHelper.AddPoint(counter);
93      return base.Apply();
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.