Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/SuccessfulOffspringAnalysis/SuccessfulOffspringAnalyzer.cs @ 13042

Last change on this file since 13042 was 5725, checked in by svonolfe, 14 years ago

Implemented review comments from swagner (#1392)

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