Changeset 12891 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs
- Timestamp:
- 08/22/15 14:27:37 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs
r12265 r12891 18 18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>. 19 19 */ 20 #endregion 20 #endregion License Information 21 21 22 22 using System.Collections.Generic; … … 35 35 [Item("GenealogyAnalyzer", "An analyzer which performs the necessary instrumentation to record the evolution of a genetic algorithm.")] 36 36 public class GenealogyAnalyzer<T> : SingleSuccessorOperator, IAnalyzer 37 where T : class, IItem {37 where T : class, IItem { 38 38 #region parameter names 39 39 private const string GenerationsParameterName = "Generations"; … … 56 56 private const string EnableManipulatorTrackingParameterName = "EnableManipulatorTracking"; 57 57 private const string EnableSolutionCreatorTrackingParameterName = "EnableSolutionCreatorTracking"; // should always be enabled. maybe superfluous 58 #endregion 58 #endregion parameter names 59 59 60 60 #region parameter properties … … 62 62 get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; } 63 63 } 64 64 65 public IScopeTreeLookupParameter<T> PopulationParameter { 65 66 get { return (IScopeTreeLookupParameter<T>)Parameters[PopulationParameterName]; } 66 67 } 68 67 69 public IValueParameter<ICrossoverOperator<T>> BeforeCrossoverOperatorParameter { 68 70 get { return (IValueParameter<ICrossoverOperator<T>>)Parameters[BeforeCrossoverOperatorParameterName]; } 69 71 } 72 70 73 public IValueParameter<ICrossoverOperator<T>> AfterCrossoverOperatorParameter { 71 74 get { return (IValueParameter<ICrossoverOperator<T>>)Parameters[AfterCrossoverOperatorParameterName]; } 72 75 } 76 73 77 public IValueParameter<IManipulatorOperator<T>> BeforeManipulatorOperatorParameter { 74 78 get { return (IValueParameter<IManipulatorOperator<T>>)Parameters[BeforeManipulatorOperatorParameterName]; } 75 79 } 80 76 81 public IValueParameter<IManipulatorOperator<T>> AfterManipulatorOperatorParameter { 77 82 get { return (IValueParameter<IManipulatorOperator<T>>)Parameters[AfterManipulatorOperatorParameterName]; } 78 83 } 84 79 85 public ILookupParameter<ResultCollection> ResultsParameter { 80 86 get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; } 81 87 } 88 82 89 public ILookupParameter<IntValue> GenerationsParameter { 83 90 get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; } 84 91 } 92 85 93 public IValueParameter<BoolValue> EnableCrossoverTrackingParameter { 86 94 get { return (IValueParameter<BoolValue>)Parameters[EnableCrossoverTrackingParameterName]; } 87 95 } 96 88 97 public IValueParameter<BoolValue> EnableManipulatorTrackingParameter { 89 98 get { return (IValueParameter<BoolValue>)Parameters[EnableManipulatorTrackingParameterName]; } 90 99 } 100 91 101 public IValueParameter<BoolValue> EnableSolutionCreatorTrackingParameter { 92 102 get { return (IValueParameter<BoolValue>)Parameters[EnableSolutionCreatorTrackingParameterName]; } 93 103 } 104 94 105 public ILookupParameter<ICrossover> CrossoverParameter { 95 106 get { return (ILookupParameter<ICrossover>)Parameters[CrossoverParameterName]; } 96 107 } 108 97 109 public ILookupParameter<IManipulator> ManipulatorParameter { 98 110 get { return (ILookupParameter<IManipulator>)Parameters[ManipulatorParameterName]; } … … 102 114 get { return (ILookupParameter<ISolutionCreator>)Parameters[SolutionCreatorParameterName]; } 103 115 } 104 #endregion 116 #endregion parameter properties 105 117 106 118 #region properties … … 108 120 get { return BeforeCrossoverOperatorParameter.Value; } 109 121 } 122 110 123 public ICrossoverOperator<T> AfterCrossoverOperator { 111 124 get { return AfterCrossoverOperatorParameter.Value; } 112 125 } 126 113 127 public IManipulatorOperator<T> BeforeManipulatorOperator { 114 128 get { return BeforeManipulatorOperatorParameter.Value; } 115 129 } 130 116 131 public IManipulatorOperator<T> AfterManipulatorOperator { 117 132 get { return AfterManipulatorOperatorParameter.Value; } 118 133 } 134 119 135 public BoolValue EnableCrossoverTracking { 120 136 get { return EnableCrossoverTrackingParameter.Value; } 121 137 } 138 122 139 public BoolValue EnableManipulatorTracking { 123 140 get { return EnableManipulatorTrackingParameter.Value; } 124 141 } 142 125 143 public BoolValue EnableSolutionCreatorTracking { 126 144 get { return EnableSolutionCreatorTrackingParameter.Value; } 127 145 } 128 #endregion 146 #endregion properties 129 147 130 148 public GenealogyAnalyzer() { … … 147 165 Parameters.Add(new ValueParameter<IManipulatorOperator<T>>(BeforeManipulatorOperatorParameterName)); 148 166 Parameters.Add(new ValueParameter<IManipulatorOperator<T>>(AfterManipulatorOperatorParameterName)); 149 #endregion 150 } 167 #endregion add parameters 168 } 169 151 170 public override IDeepCloneable Clone(Cloner cloner) { 152 171 return new GenealogyAnalyzer<T>(this, cloner); 153 172 } 173 154 174 protected GenealogyAnalyzer(GenealogyAnalyzer<T> original, Cloner cloner) 155 175 : base(original, cloner) { … … 276 296 ExecutionContext.Scope.SubScopes[index].Variables["Id"].Value = new StringValue(v.Id); 277 297 } 278 #endregion 279 280 // ComputeSuccessRatios(genealogyGraph); 298 #endregion add elite in the graph and connect it with the previous elite 281 299 } 282 300 // update qualities … … 285 303 vertex.Quality = qualities[i].Value; 286 304 } 287 288 305 // remove extra graph nodes (added by the instrumented operators in the case of offspring selection) 289 306 var pop = new HashSet<T>(population); … … 294 311 var p = v.Parents.First(); 295 312 if (p.Rank.Equals(generation - 0.5)) 296 // the individual was a product of mutation. since it wasn't selected, we must remove both it and its parent 313 // the individual was a product of mutation. since it wasn't selected, we must remove both it and its parent 297 314 // ("intermediate" vertex result of crossover) 298 315 discarded.Add(v.Parents.First()); … … 303 320 return base.Apply(); 304 321 } 305 306 // for accurate statistics this method should be called before the discarded offspring are removed from the genealogy graph307 private void ComputeSuccessRatios(IGenealogyGraph<T> genealogyGraph) {308 const string successfulOffspringRatioTableHistoryName = "Successful offspring ratios history";309 const string successfulOffspringAbsoluteValuesTableHistoryName = "Successful offspring values history";310 const string successfulOffspringRatioHeatMapName = "Successful offspring ratios heatmap";311 const string successfulOffspringValuesHeatMapName = "Successful offspring values heatmap";312 313 var population = PopulationParameter.ActualValue;314 var generation = GenerationsParameter.ActualValue.Value;315 // compute the weight of each genealogy graph node as the ratio (produced offspring) / (surviving offspring)316 foreach (var ind in population) {317 var v = genealogyGraph.GetByContent(ind);318 if (v.Parents.Count() == 1) {319 var p = v.Parents.First();320 foreach (var pp in p.Parents)321 pp.Weight++;322 } else {323 foreach (var p in v.Parents) {324 p.Weight++;325 }326 }327 }328 329 var results = ResultsParameter.ActualValue;330 331 DataTableHistory successfulOffspringRatioHistory;332 DataTableHistory successfulOffspringAbsoluteHistory;333 if (!results.ContainsKey(successfulOffspringRatioTableHistoryName)) {334 successfulOffspringRatioHistory = new DataTableHistory();335 successfulOffspringAbsoluteHistory = new DataTableHistory();336 results.Add(new Result(successfulOffspringRatioTableHistoryName, successfulOffspringRatioHistory));337 results.Add(new Result(successfulOffspringAbsoluteValuesTableHistoryName, successfulOffspringAbsoluteHistory));338 } else {339 successfulOffspringRatioHistory = (DataTableHistory)results[successfulOffspringRatioTableHistoryName].Value;340 successfulOffspringAbsoluteHistory = (DataTableHistory)results[successfulOffspringAbsoluteValuesTableHistoryName].Value;341 }342 var successfulOffspringRatioTable = new DataTable();343 var successfulOffspringRatioRow = new DataRow("Successful Offspring Ratio") {344 VisualProperties = { ChartType = DataRowVisualProperties.DataRowChartType.Columns, StartIndexZero = true }345 };346 successfulOffspringRatioRow.Values.Replace(genealogyGraph.GetByRank(generation).OrderByDescending(x => x.Quality).Select(x => x.OutDegree > 0 ? x.Weight / x.OutDegree : 0));347 successfulOffspringRatioTable.Rows.Add(successfulOffspringRatioRow);348 successfulOffspringRatioHistory.Add(successfulOffspringRatioTable);349 // create heatmap350 double[,] ratios = new double[population.Length, successfulOffspringRatioHistory.Count];351 var ratiosHistoryList = successfulOffspringRatioHistory.AsReadOnly().ToList();352 for (int i = 0; i < ratiosHistoryList.Count; ++i) {353 var values = ratiosHistoryList[i].Rows.First().Values;354 for (int j = 0; j < values.Count; ++j) {355 ratios[j, i] = values[j];356 }357 }358 var successfulOffspringRatios = new HeatMap(ratios);359 if (!results.ContainsKey(successfulOffspringRatioHeatMapName)) {360 results.Add(new Result(successfulOffspringRatioHeatMapName, successfulOffspringRatios));361 } else {362 results[successfulOffspringRatioHeatMapName].Value = successfulOffspringRatios;363 }364 365 var successfulOffspringValuesTable = new DataTable();366 var successfulOffspringValuesRow = new DataRow("Successful Offspring Values") {367 VisualProperties = { ChartType = DataRowVisualProperties.DataRowChartType.Columns, StartIndexZero = true }368 };369 successfulOffspringValuesRow.Values.Replace(genealogyGraph.GetByRank(generation).OrderByDescending(x => x.Quality).Select(x => x.Weight));370 successfulOffspringValuesTable.Rows.Add(successfulOffspringValuesRow);371 successfulOffspringAbsoluteHistory.Add(successfulOffspringValuesTable);372 // create heatmap373 double min = 0, max = 0;374 double[,] elements = new double[population.Length, successfulOffspringAbsoluteHistory.Count];375 var absoluteHistoryList = successfulOffspringAbsoluteHistory.AsReadOnly().ToList();376 for (int i = 0; i < absoluteHistoryList.Count; ++i) {377 var values = absoluteHistoryList[i].Rows.First().Values;378 for (int j = 0; j < values.Count; ++j) {379 elements[j, i] = values[j];380 if (max < values[j])381 max = values[j];382 if (min > values[j])383 min = values[j];384 }385 }386 var heatmap = new HeatMap(elements);387 heatmap.Maximum = max;388 heatmap.Minimum = min;389 if (!results.ContainsKey(successfulOffspringValuesHeatMapName)) {390 results.Add(new Result(successfulOffspringValuesHeatMapName, heatmap));391 } else {392 results[successfulOffspringValuesHeatMapName].Value = heatmap;393 }394 }395 322 } 396 323 }
Note: See TracChangeset
for help on using the changeset viewer.