Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/SuccessfulOffspringAnalysis/SuccessfulOffspringAnalyzer.cs @ 5683

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

Integrated successful offspring analysis into trunk (#1392)

File size: 6.6 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> SuccessfulOffspringFlag {
43      get { return (ValueParameter<StringValue>)Parameters["SuccessfulOffspringFlag"]; }
44    }
45
46    public ValueParameter<ItemCollection<StringValue>> CollectedValues {
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 ILookupParameter<IntValue> GenerationsParameter {
55      get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
56    }
57
58    public ValueParameter<IntValue> DepthParameter {
59      get { return (ValueParameter<IntValue>)Parameters["Depth"]; }
60    }
61   
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new SuccessfulOffspringAnalyzer(this, cloner);
64    }   
65    [StorableConstructor]
66    private SuccessfulOffspringAnalyzer(bool deserializing) : base(deserializing) { }
67    private SuccessfulOffspringAnalyzer(SuccessfulOffspringAnalyzer original, Cloner cloner) : base(original, cloner) { }
68    public SuccessfulOffspringAnalyzer()
69      : base() {
70        Parameters.Add(new ValueParameter<StringValue>("SuccessfulOffspringFlag", "The name of the flag which indicates if the individual was successful.", new StringValue("SuccessfulOffspring")));
71        Parameters.Add(new ValueParameter<ItemCollection<StringValue>>("CollectedValues", "The properties of the successful offspring that should be collected.", new ItemCollection<StringValue>()));
72        Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the succedd progress analysis results should be stored."));
73        Parameters.Add(new LookupParameter<IntValue>("Generations", "The current number of generations."));
74        Parameters.Add(new ValueParameter<IntValue>("Depth", "The depth of the individuals in the scope tree.", new IntValue(1)));
75    }
76
77    public override IOperation Apply() {
78      ResultCollection results = ResultsParameter.ActualValue;
79
80      List<IScope> scopes = new List<IScope>() { ExecutionContext.Scope };
81      for (int i = 0; i < DepthParameter.Value.Value; i++)
82        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)).ToList();
83
84      ItemCollection<StringValue> collectedValues = CollectedValues.Value;
85        foreach (StringValue collected in collectedValues) {
86          //collect the values of the successful offspring
87          Dictionary<String, int> counts = new Dictionary<String, int>();
88          for (int i = 0; i < scopes.Count; i++) {
89            IScope child = scopes[i];
90            string successfulOffspringFlag = SuccessfulOffspringFlag.Value.Value;
91            if (child.Variables.ContainsKey(collected.Value) &&
92                child.Variables.ContainsKey(successfulOffspringFlag) &&
93                (child.Variables[successfulOffspringFlag].Value is BoolValue) &&
94                (child.Variables[successfulOffspringFlag].Value as BoolValue).Value) {
95              String key = child.Variables[collected.Value].Value.ToString();
96
97              if (!counts.ContainsKey(key))
98                counts.Add(key, 1);
99              else
100                counts[key]++;
101            }
102          }
103
104          //create a data table containing the collected values
105          DataTable successProgressAnalysis;
106          string resultKey = "Success Progress " + collected.Value;
107          if (!results.ContainsKey(resultKey)) {
108            successProgressAnalysis = new DataTable();
109            successProgressAnalysis.Name = "Success Progress Analysis";
110            results.Add(new Result(resultKey, successProgressAnalysis));
111          } else {
112            successProgressAnalysis = results[resultKey].Value as DataTable;
113          }
114
115          int successfulCount = 0;
116          foreach (string key in counts.Keys) {
117            successfulCount += counts[key];
118          }
119
120          foreach(String value in counts.Keys) {           
121            DataRow row;
122            if (!successProgressAnalysis.Rows.ContainsKey(value)) {
123              row = new DataRow(value);
124              int iterations = GenerationsParameter.ActualValue.Value;
125
126              //fill up all values seen the first time
127              for (int i = 1; i < iterations; i++)
128                row.Values.Add(0);
129
130              successProgressAnalysis.Rows.Add(row);
131            } else {
132              row = successProgressAnalysis.Rows[value];
133            }
134
135            row.Values.Add(counts[value] / (double)successfulCount);
136          }
137
138          //fill up all values that are not present in the current generation
139          foreach (DataRow row in successProgressAnalysis.Rows) {
140            if (!counts.ContainsKey(row.Name))
141              row.Values.Add(0);
142          }
143        } 
144
145      return base.Apply();
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.