Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 added an analyzer for tracking duplicate solutions

File size: 3.8 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 DataRow dtRow;
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
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new DuplicateSolutionsAnalyzer(this, cloner);
74    }
75
76    public override IOperation Apply() {
77      if (!Results.ContainsKey("Duplicate Solutions")) {
78        DataTable dt = new DataTable("Duplicate Solutions");
79        dtRow = new DataRow("Duplicate Solutions per Generation");
80        dt.Rows.Add(dtRow);
81        Results.Add(new Result("Duplicate Solutions", dt));
82      }
83
84      var similarities = SimilarityCalculatorParameter.Value.CalculateSolutionCrowdSimilarity(ExecutionContext.Scope);
85      int counter = 0;
86      for (int i = 0; i < similarities.Length; i++) {
87        for (int j = 0; j < similarities[i].Length; j++) {
88          if (similarities[i][j] == 1.0 && i != j) {
89            counter++;
90            break;
91          }
92        }
93      }
94      dtRow.Values.Add(counter);
95
96      return base.Apply();
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.