Changeset 7172 for trunk/sources/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/SuccessfulOffspringAnalysis/SuccessfulOffspringAnalyzer.cs
- Timestamp:
- 12/12/11 03:39:04 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/SuccessfulOffspringAnalysis/SuccessfulOffspringAnalyzer.cs
r5725 r7172 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Text; 25 using HeuristicLab.Analysis; 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 using HeuristicLab. Persistence.Default.CompositeSerializers.Storable;28 using HeuristicLab.Data; 28 29 using HeuristicLab.Operators; 29 30 using HeuristicLab.Optimization; 30 using HeuristicLab.Common;31 31 using HeuristicLab.Parameters; 32 using HeuristicLab.Data; 33 using HeuristicLab.Analysis; 32 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 34 33 35 34 namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm { … … 40 39 [StorableClass] 41 40 public sealed class SuccessfulOffspringAnalyzer : SingleSuccessorOperator, IAnalyzer { 41 public bool EnabledByDefault { 42 get { return false; } 43 } 44 42 45 public ValueParameter<StringValue> SuccessfulOffspringFlagParameter { 43 46 get { return (ValueParameter<StringValue>)Parameters["SuccessfulOffspringFlag"]; } … … 53 56 54 57 public LookupParameter<ResultCollection> SuccessfulOffspringAnalysisParameter { 55 get { return (LookupParameter<ResultCollection>)Parameters["SuccessfulOffspringAnalysis"]; 58 get { return (LookupParameter<ResultCollection>)Parameters["SuccessfulOffspringAnalysis"]; } 56 59 } 57 60 … … 63 66 get { return (ValueParameter<IntValue>)Parameters["Depth"]; } 64 67 } 65 68 66 69 public override IDeepCloneable Clone(Cloner cloner) { 67 70 return new SuccessfulOffspringAnalyzer(this, cloner); 68 } 71 } 69 72 [StorableConstructor] 70 73 private SuccessfulOffspringAnalyzer(bool deserializing) : base(deserializing) { } … … 72 75 public SuccessfulOffspringAnalyzer() 73 76 : base() { 74 75 76 77 Parameters.Add(new LookupParameter<IntValue>("Generations", "The current number of generations."));78 79 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))); 80 83 } 81 84 … … 88 91 89 92 ItemCollection<StringValue> collectedValues = CollectedValuesParameter.Value; 90 91 92 93 94 95 96 if (child.Variables.ContainsKey(collected.Value) &&97 child.Variables.ContainsKey(successfulOffspringFlag) &&98 99 100 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(); 101 104 102 if (!counts.ContainsKey(key)) 103 counts.Add(key, 1); 104 else 105 counts[key]++; 106 } 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]; 107 156 } 108 157 109 //create a data table containing the collected values110 ResultCollection successfulOffspringAnalysis;158 row.Values.Add(counts[value] / (double)successfulCount); 159 } 111 160 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 } 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 } 164 167 165 168 return base.Apply();
Note: See TracChangeset
for help on using the changeset viewer.