Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9418 was 9418, checked in by ascheibe, 11 years ago

#1886

  • changed duplicate solutions analyzer to also track avg. diversity as the standard diversity analyzer needs too much memory
  • fixed project references of eslGA
File size: 4.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 System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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<ISingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
49      get { return (IValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
50    }
51    public ILookupParameter<ItemCollection<IItem>> OperatorsParameter {
52      get { return (ILookupParameter<ItemCollection<IItem>>)Parameters["Operators"]; }
53    }
54    #endregion
55
56    #region Properties
57    public ResultCollection Results {
58      get { return ResultsParameter.ActualValue; }
59    }
60
61    [Storable]
62    private DataTableHelper chartingHelper, similarityChartHelper;
63    #endregion
64
65    [StorableConstructor]
66    private DuplicateSolutionsAnalyzer(bool deserializing) : base(deserializing) { }
67
68    private DuplicateSolutionsAnalyzer(DuplicateSolutionsAnalyzer original, Cloner cloner)
69      : base(original, cloner) {
70      chartingHelper = (DataTableHelper)original.chartingHelper.Clone(cloner);
71      similarityChartHelper = (DataTableHelper)original.similarityChartHelper.Clone(cloner);
72    }
73
74    public DuplicateSolutionsAnalyzer()
75      : base() {
76      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
77      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
78      Parameters.Add(new LookupParameter<ItemCollection<IItem>>("Operators", "The operators and items that the problem provides to the algorithms."));
79
80      chartingHelper = new DataTableHelper();
81      similarityChartHelper = new DataTableHelper();
82    }
83
84    public override IDeepCloneable Clone(Cloner cloner) {
85      return new DuplicateSolutionsAnalyzer(this, cloner);
86    }
87
88    public override IOperation Apply() {
89      if (SimilarityCalculatorParameter.Value == null) {
90        SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
91      }
92      chartingHelper.InitializeChart(Results, "Duplicate Solutions", new string[] { "Duplicate Solutions per Generation" });
93      similarityChartHelper.InitializeChart(Results, "Average Similarity", new string[] { "Avg. Similarity per Generation" });
94
95      var similarities = SimilarityCalculatorParameter.Value.CalculateSolutionCrowdSimilarity(ExecutionContext.Scope);
96      List<double> similaritiesArr = new List<double>();
97      int counter = 0;
98      for (int i = 0; i < similarities.Length; i++) {
99        for (int j = 0; j < similarities[i].Length; j++) {
100          if (similarities[i][j] == 1.0 && i != j) {
101            counter++;
102            break;
103          }
104        }
105        for (int j = 0; j < similarities[i].Length; j++) {
106          if (i != j) {
107            similaritiesArr.Add(similarities[i][j]);
108          }
109        }
110      }
111
112      similarityChartHelper.AddPoint(similaritiesArr.Average());
113      chartingHelper.AddPoint(counter / (double)similarities.Length);
114      return base.Apply();
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.