#region License Information /* HeuristicLab * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Common; using HeuristicLab.Parameters; using HeuristicLab.Data; using HeuristicLab.Analysis; namespace HeuristicLab.Algorithms.OffspringSelectionAlgorithm.SuccessProgressAnalysis { /// /// An operator for analyzing the solution diversity in a population. /// [Item("SuccessProgressAnalyzer", "An operator for analyzing the success progress in a population.")] [StorableClass] public sealed class SuccessProgressAnalyzer: SingleSuccessorOperator, IAnalyzer { public ValueParameter SuccessfulOffspringFlag { get { return (ValueParameter)Parameters["SuccessfulOffspringFlag"]; } } public ValueParameter> CollectedValues { get { return (ValueParameter>)Parameters["CollectedValues"]; } } public ValueLookupParameter ResultsParameter { get { return (ValueLookupParameter)Parameters["Results"]; } } public LookupParameter Generations { get { return (LookupParameter)Parameters["Generations"]; } } public override IDeepCloneable Clone(Cloner cloner) { return new SuccessProgressAnalyzer(this, cloner); } [StorableConstructor] private SuccessProgressAnalyzer(bool deserializing) : base(deserializing) { } private SuccessProgressAnalyzer(SuccessProgressAnalyzer original, Cloner cloner) : base(original, cloner) { } public SuccessProgressAnalyzer() : base() { Parameters.Add(new ValueParameter("SuccessfulOffspringFlag", "Indicates if the individual was successful.", new StringValue("SuccessfulOffspring"))); Parameters.Add(new ValueParameter>("CollectedValues", "The values that should be collected.", new ItemCollection())); Parameters.Add(new ValueLookupParameter("Results", "The result collection where the succedd progress analysis results should be stored.")); Parameters.Add(new LookupParameter("Generations", "The current number of generations.")); } public override IOperation Apply() { ResultCollection results = ResultsParameter.ActualValue; ItemCollection collectedValues = CollectedValues.Value; foreach (StringValue collected in collectedValues) { Dictionary counts = new Dictionary(); for (int i = 0; i < ExecutionContext.Scope.SubScopes.Count; i++) { IScope child = ExecutionContext.Scope.SubScopes[i]; string successfulOffspringFlag = SuccessfulOffspringFlag.Value.Value; if (child.Variables.ContainsKey(collected.Value) && child.Variables.ContainsKey(successfulOffspringFlag) && (child.Variables[successfulOffspringFlag].Value is BoolValue) && (child.Variables[successfulOffspringFlag].Value as BoolValue).Value) { String key = child.Variables[collected.Value].Value.ToString(); if (!counts.ContainsKey(key)) counts.Add(key, 1); else counts[key]++; } } DataTable successProgressAnalysis; string resultKey = "Success Progress " + collected.Value; if (!results.ContainsKey(resultKey)) { successProgressAnalysis = new DataTable(); successProgressAnalysis.Name = "Success Progress Analysis"; results.Add(new Result(resultKey, successProgressAnalysis)); } else { successProgressAnalysis = results[resultKey].Value as DataTable; } int successfulCount = 0; foreach (string key in counts.Keys) { successfulCount += counts[key]; } foreach(String value in counts.Keys) { DataRow row; if (!successProgressAnalysis.Rows.ContainsKey(value)) { row = new DataRow(value); int iterations = Generations.ActualValue.Value; for (int i = 1; i < iterations; i++) row.Values.Add(0); successProgressAnalysis.Rows.Add(row); } else { row = successProgressAnalysis.Rows[value]; } row.Values.Add(counts[value] / (double)successfulCount); } foreach (DataRow row in successProgressAnalysis.Rows) { if (!counts.ContainsKey(row.Name)) row.Values.Add(0); } } return base.Apply(); } } }