Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/SuccessfulOffspringAnalysis/SuccessfulOffspringAnalyzer.cs

Last change on this file was 11646, checked in by mkommend, 10 years ago

#1997: Merged trunk changes to data analysis island GA branch.

File size: 7.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
35  /// <summary>
36  /// An operator for analyzing the solution diversity in a population.
37  /// </summary>
38  [Item("SuccessfulOffspringAnalyzer", "An operator for analyzing certain properties in the successful offspring. The properties to be analyzed can be specified in the CollectedValues parameter.")]
39  [StorableClass]
40  public sealed class SuccessfulOffspringAnalyzer : SingleSuccessorOperator, IAnalyzer {
41    public bool EnabledByDefault {
42      get { return false; }
43    }
44
45    public ValueParameter<StringValue> SuccessfulOffspringFlagParameter {
46      get { return (ValueParameter<StringValue>)Parameters["SuccessfulOffspringFlag"]; }
47    }
48
49    public ValueParameter<ItemCollection<StringValue>> CollectedValuesParameter {
50      get { return (ValueParameter<ItemCollection<StringValue>>)Parameters["CollectedValues"]; }
51    }
52
53    public ValueLookupParameter<ResultCollection> ResultsParameter {
54      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
55    }
56
57    public LookupParameter<ResultCollection> SuccessfulOffspringAnalysisParameter {
58      get { return (LookupParameter<ResultCollection>)Parameters["SuccessfulOffspringAnalysis"]; }
59    }
60
61    public ILookupParameter<IntValue> GenerationsParameter {
62      get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
63    }
64
65    public ValueParameter<IntValue> DepthParameter {
66      get { return (ValueParameter<IntValue>)Parameters["Depth"]; }
67    }
68
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new SuccessfulOffspringAnalyzer(this, cloner);
71    }
72    [StorableConstructor]
73    private SuccessfulOffspringAnalyzer(bool deserializing) : base(deserializing) { }
74    private SuccessfulOffspringAnalyzer(SuccessfulOffspringAnalyzer original, Cloner cloner) : base(original, cloner) { }
75    public SuccessfulOffspringAnalyzer()
76      : base() {
77      Parameters.Add(new ValueParameter<StringValue>("SuccessfulOffspringFlag", "The name of the flag which indicates if the individual was successful.", new StringValue("SuccessfulOffspring")));
78      Parameters.Add(new ValueParameter<ItemCollection<StringValue>>("CollectedValues", "The properties of the successful offspring that should be collected.", new ItemCollection<StringValue>()));
79      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the succedd progress analysis results should be stored."));
80      Parameters.Add(new LookupParameter<IntValue>("Generations", "The current number of generations."));
81      Parameters.Add(new LookupParameter<ResultCollection>("SuccessfulOffspringAnalysis", "The successful offspring analysis which is created."));
82      Parameters.Add(new ValueParameter<IntValue>("Depth", "The depth of the individuals in the scope tree.", new IntValue(1)));
83
84      CollectedValuesParameter.Value.Add(new StringValue("SelectedCrossoverOperator"));
85      CollectedValuesParameter.Value.Add(new StringValue("SelectedManipulationOperator"));
86    }
87
88    public override IOperation Apply() {
89      ResultCollection results = ResultsParameter.ActualValue;
90
91      List<IScope> scopes = new List<IScope>() { ExecutionContext.Scope };
92      for (int i = 0; i < DepthParameter.Value.Value; i++)
93        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)).ToList();
94
95      ItemCollection<StringValue> collectedValues = CollectedValuesParameter.Value;
96      foreach (StringValue collected in collectedValues) {
97        //collect the values of the successful offspring
98        Dictionary<String, int> counts = new Dictionary<String, int>();
99        for (int i = 0; i < scopes.Count; i++) {
100          IScope child = scopes[i];
101          string successfulOffspringFlag = SuccessfulOffspringFlagParameter.Value.Value;
102          if (child.Variables.ContainsKey(collected.Value) &&
103              child.Variables.ContainsKey(successfulOffspringFlag) &&
104              (child.Variables[successfulOffspringFlag].Value is BoolValue) &&
105              (child.Variables[successfulOffspringFlag].Value as BoolValue).Value) {
106            String key = child.Variables[collected.Value].Value.ToString();
107
108            if (!counts.ContainsKey(key))
109              counts.Add(key, 1);
110            else
111              counts[key]++;
112          }
113        }
114
115        if (counts.Count > 0) {
116          //create a data table containing the collected values
117          ResultCollection successfulOffspringAnalysis;
118
119          if (SuccessfulOffspringAnalysisParameter.ActualValue == null) {
120            successfulOffspringAnalysis = new ResultCollection();
121            SuccessfulOffspringAnalysisParameter.ActualValue = successfulOffspringAnalysis;
122          } else {
123            successfulOffspringAnalysis = SuccessfulOffspringAnalysisParameter.ActualValue;
124          }
125
126          string resultKey = "SuccessfulOffspringAnalyzer Results";
127          if (!results.ContainsKey(resultKey)) {
128            results.Add(new Result(resultKey, successfulOffspringAnalysis));
129          } else {
130            results[resultKey].Value = successfulOffspringAnalysis;
131          }
132
133          DataTable successProgressAnalysis;
134          if (!successfulOffspringAnalysis.ContainsKey(collected.Value)) {
135            successProgressAnalysis = new DataTable();
136            successProgressAnalysis.Name = collected.Value;
137            successfulOffspringAnalysis.Add(new Result(collected.Value, successProgressAnalysis));
138          } else {
139            successProgressAnalysis = successfulOffspringAnalysis[collected.Value].Value as DataTable;
140          }
141
142          int successfulCount = 0;
143          foreach (string key in counts.Keys) {
144            successfulCount += counts[key];
145          }
146
147          foreach (String value in counts.Keys) {
148            DataRow row;
149            if (!successProgressAnalysis.Rows.ContainsKey(value)) {
150              row = new DataRow(value);
151              int iterations = GenerationsParameter.ActualValue.Value;
152
153              //fill up all values seen the first time
154              for (int i = 1; i < iterations; i++)
155                row.Values.Add(0);
156
157              successProgressAnalysis.Rows.Add(row);
158            } else {
159              row = successProgressAnalysis.Rows[value];
160            }
161
162            row.Values.Add(counts[value] / (double)successfulCount);
163          }
164
165          //fill up all values that are not present in the current generation
166          foreach (DataRow row in successProgressAnalysis.Rows) {
167            if (!counts.ContainsKey(row.Name))
168              row.Values.Add(0);
169          }
170        }
171      }
172
173      return base.Apply();
174    }
175  }
176}
Note: See TracBrowser for help on using the repository browser.