Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SuccessProgressAnalysis/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/SuccessProgressAnalysis/SuccessProgressAnalyzer.cs @ 5492

Last change on this file since 5492 was 5492, checked in by svonolfe, 12 years ago

Implemented various changes as proposed by vdorfer and swinkler (#1392)

  • Collecting the successful values now works when elites are enabled
  • The initial iteration (0) is not collected
  • The DataTable now has a Name
File size: 5.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.OffspringSelectionAlgorithm.SuccessProgressAnalysis {
36  /// <summary>
37  /// An operator for analyzing the solution diversity in a population.
38  /// </summary>
39  [Item("SuccessProgressAnalyzer", "An operator for analyzing the success progress in a population.")]
40  [StorableClass]
41  public sealed class SuccessProgressAnalyzer: 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 LookupParameter<IntValue> Generations {
55      get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
56    }
57   
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new SuccessProgressAnalyzer(this, cloner);
60    }   
61    [StorableConstructor]
62    private SuccessProgressAnalyzer(bool deserializing) : base(deserializing) { }
63    private SuccessProgressAnalyzer(SuccessProgressAnalyzer original, Cloner cloner) : base(original, cloner) { }
64    public SuccessProgressAnalyzer()
65      : base() {
66        Parameters.Add(new ValueParameter<StringValue>("SuccessfulOffspringFlag", "Indicates if the individual was successful.", new StringValue("SuccessfulOffspring")));
67        Parameters.Add(new ValueParameter<ItemCollection<StringValue>>("CollectedValues", "The values that should be collected.", new ItemCollection<StringValue>()));
68        Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the succedd progress analysis results should be stored."));
69        Parameters.Add(new LookupParameter<IntValue>("Generations", "The current number of generations."));
70    }
71
72    public override IOperation Apply() {
73      ResultCollection results = ResultsParameter.ActualValue;
74     
75      ItemCollection<StringValue> collectedValues = CollectedValues.Value;
76        foreach (StringValue collected in collectedValues) {
77          Dictionary<String, int> counts = new Dictionary<String, int>();
78          for (int i = 0; i < ExecutionContext.Scope.SubScopes.Count; i++) {
79            IScope child = ExecutionContext.Scope.SubScopes[i];
80            string successfulOffspringFlag = SuccessfulOffspringFlag.Value.Value;
81            if (child.Variables.ContainsKey(collected.Value) &&
82                child.Variables.ContainsKey(successfulOffspringFlag) &&
83                (child.Variables[successfulOffspringFlag].Value is BoolValue) &&
84                (child.Variables[successfulOffspringFlag].Value as BoolValue).Value) {
85              String key = child.Variables[collected.Value].Value.ToString();
86
87              if (!counts.ContainsKey(key))
88                counts.Add(key, 1);
89              else
90                counts[key]++;
91            }
92          }
93
94          DataTable successProgressAnalysis;
95          string resultKey = "Success Progress " + collected.Value;
96          if (!results.ContainsKey(resultKey)) {
97            successProgressAnalysis = new DataTable();
98            successProgressAnalysis.Name = "Success Progress Analysis";
99            results.Add(new Result(resultKey, successProgressAnalysis));
100          } else {
101            successProgressAnalysis = results[resultKey].Value as DataTable;
102          }
103
104          int successfulCount = 0;
105          foreach (string key in counts.Keys) {
106            successfulCount += counts[key];
107          }
108
109          foreach(String value in counts.Keys) {           
110            DataRow row;
111            if (!successProgressAnalysis.Rows.ContainsKey(value)) {
112              row = new DataRow(value);
113              int iterations = Generations.ActualValue.Value;
114
115              for (int i = 1; i < iterations; i++)
116                row.Values.Add(0);
117
118              successProgressAnalysis.Rows.Add(row);
119            } else {
120              row = successProgressAnalysis.Rows[value];
121            }
122
123            row.Values.Add(counts[value] / (double)successfulCount);
124          }
125
126          foreach (DataRow row in successProgressAnalysis.Rows) {
127            if (!counts.ContainsKey(row.Name))
128              row.Values.Add(0);
129          }
130        } 
131
132      return base.Apply();
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.