Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 added a comparison factor parameter to the crossover analyzer so that it can be defined what a successful crossover is

File size: 4.2 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 System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
32  [Item("DuplicateSolutionsAnalyzer", "An operator that analyzes duplicate solutions in the population.")]
33  [StorableClass]
34  public class DuplicateSolutionsAnalyzer : SingleSuccessorOperator, IAnalyzer {
35    private const string ResultsParameterName = "Results";
36
37    #region IAnalyzer Members
38    public bool EnabledByDefault {
39      get { return false; }
40    }
41    #endregion
42
43    #region Parameter properties
44    public ILookupParameter<ResultCollection> ResultsParameter {
45      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
46    }
47    public IValueParameter<ISingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
48      get { return (IValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
49    }
50    public ILookupParameter<ItemCollection<IItem>> OperatorsParameter {
51      get { return (ILookupParameter<ItemCollection<IItem>>)Parameters["Operators"]; }
52    }
53    #endregion
54
55    #region Properties
56    public ResultCollection Results {
57      get { return ResultsParameter.ActualValue; }
58    }
59
60    [Storable]
61    private DataTableHelper chartingHelper;
62    #endregion
63
64    [StorableConstructor]
65    private DuplicateSolutionsAnalyzer(bool deserializing) : base(deserializing) { }
66
67    private DuplicateSolutionsAnalyzer(DuplicateSolutionsAnalyzer original, Cloner cloner)
68      : base(original, cloner) {
69      chartingHelper = (DataTableHelper)original.chartingHelper.Clone(cloner);
70    }
71
72    public DuplicateSolutionsAnalyzer()
73      : base() {
74      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
75      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
76      Parameters.Add(new LookupParameter<ItemCollection<IItem>>("Operators", "The operators and items that the problem provides to the algorithms."));
77
78      chartingHelper = new DataTableHelper();
79    }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new DuplicateSolutionsAnalyzer(this, cloner);
83    }
84
85    public override IOperation Apply() {
86      if (SimilarityCalculatorParameter.Value == null) {
87        SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
88      }
89      chartingHelper.InitializeChart(Results, "Duplicate Solutions", new string[] { "Duplicate Solutions per Generation" });
90
91      var similarities = SimilarityCalculatorParameter.Value.CalculateSolutionCrowdSimilarity(ExecutionContext.Scope);
92      int counter = 0;
93      for (int i = 0; i < similarities.Length; i++) {
94        for (int j = 0; j < similarities[i].Length; j++) {
95          if (similarities[i][j] == 1.0 && i != j) {
96            counter++;
97            break;
98          }
99        }
100      }
101
102      chartingHelper.AddPoint(counter / (double)similarities.Length);
103      return base.Apply();
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.