Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gteufl/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/SuccessfulOffspringAnalysis/SuccessfulOffspringAnalyzer.cs @ 11497

Last change on this file since 11497 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 7.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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
85    public override IOperation Apply() {
86      ResultCollection results = ResultsParameter.ActualValue;
87
88      List<IScope> scopes = new List<IScope>() { ExecutionContext.Scope };
89      for (int i = 0; i < DepthParameter.Value.Value; i++)
90        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)).ToList();
91
92      ItemCollection<StringValue> collectedValues = CollectedValuesParameter.Value;
93      foreach (StringValue collected in collectedValues) {
94        //collect the values of the successful offspring
95        Dictionary<String, int> counts = new Dictionary<String, int>();
96        for (int i = 0; i < scopes.Count; i++) {
97          IScope child = scopes[i];
98          string successfulOffspringFlag = SuccessfulOffspringFlagParameter.Value.Value;
99          if (child.Variables.ContainsKey(collected.Value) &&
100              child.Variables.ContainsKey(successfulOffspringFlag) &&
101              (child.Variables[successfulOffspringFlag].Value is BoolValue) &&
102              (child.Variables[successfulOffspringFlag].Value as BoolValue).Value) {
103            String key = child.Variables[collected.Value].Value.ToString();
104
105            if (!counts.ContainsKey(key))
106              counts.Add(key, 1);
107            else
108              counts[key]++;
109          }
110        }
111
112        //create a data table containing the collected values
113        ResultCollection successfulOffspringAnalysis;
114
115        if (SuccessfulOffspringAnalysisParameter.ActualValue == null) {
116          successfulOffspringAnalysis = new ResultCollection();
117          SuccessfulOffspringAnalysisParameter.ActualValue = successfulOffspringAnalysis;
118        } else {
119          successfulOffspringAnalysis = SuccessfulOffspringAnalysisParameter.ActualValue;
120        }
121
122        string resultKey = "SuccessfulOffspringAnalyzer Results";
123        if (!results.ContainsKey(resultKey)) {
124          results.Add(new Result(resultKey, successfulOffspringAnalysis));
125        } else {
126          results[resultKey].Value = successfulOffspringAnalysis;
127        }
128
129        DataTable successProgressAnalysis;
130        if (!successfulOffspringAnalysis.ContainsKey(collected.Value)) {
131          successProgressAnalysis = new DataTable();
132          successProgressAnalysis.Name = collected.Value;
133          successfulOffspringAnalysis.Add(new Result(collected.Value, successProgressAnalysis));
134        } else {
135          successProgressAnalysis = successfulOffspringAnalysis[collected.Value].Value as DataTable;
136        }
137
138        int successfulCount = 0;
139        foreach (string key in counts.Keys) {
140          successfulCount += counts[key];
141        }
142
143        foreach (String value in counts.Keys) {
144          DataRow row;
145          if (!successProgressAnalysis.Rows.ContainsKey(value)) {
146            row = new DataRow(value);
147            int iterations = GenerationsParameter.ActualValue.Value;
148
149            //fill up all values seen the first time
150            for (int i = 1; i < iterations; i++)
151              row.Values.Add(0);
152
153            successProgressAnalysis.Rows.Add(row);
154          } else {
155            row = successProgressAnalysis.Rows[value];
156          }
157
158          row.Values.Add(counts[value] / (double)successfulCount);
159        }
160
161        //fill up all values that are not present in the current generation
162        foreach (DataRow row in successProgressAnalysis.Rows) {
163          if (!counts.ContainsKey(row.Name))
164            row.Values.Add(0);
165        }
166      }
167
168      return base.Apply();
169    }
170  }
171}
Note: See TracBrowser for help on using the repository browser.