Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 added configuration of frequency with which the ab measures are calculated

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