#region License Information /* HeuristicLab * Copyright (C) 2002-2015 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.Linq; using HeuristicLab.Analysis; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.ProgramSynthesis { [Item("SelectionSchemeAnalyzer", "An analyzer which gives information about the relative amount of individuals that get selected each generation")] [StorableClass] public class SelectionSchemeAnalyzer : EvolutionTrackingAnalyzer { private const string StoreHistoryParameterName = "StoreHistory"; public IFixedValueParameter StoreHistoryParameter { get { return (IFixedValueParameter)Parameters[StoreHistoryParameterName]; } } public SelectionSchemeAnalyzer() { UpdateCounterParameter.ActualName = "SelectionSchemeAnalyzerUpdateCounter"; Parameters.Add(new FixedValueParameter(StoreHistoryParameterName, new BoolValue(false))); } public bool StoreHistory { get { return StoreHistoryParameter.Value.Value; } } [StorableConstructor] protected SelectionSchemeAnalyzer(bool deserializing) : base(deserializing) { } protected SelectionSchemeAnalyzer(SelectionSchemeAnalyzer original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new SelectionSchemeAnalyzer(this, cloner); } public override IOperation Apply() { int updateInterval = UpdateIntervalParameter.Value.Value; IntValue updateCounter = UpdateCounterParameter.ActualValue; // if counter does not yet exist then initialize it with update interval // to make sure the solutions are analyzed on the first application of this operator if (updateCounter == null) { updateCounter = new IntValue(0); UpdateCounterParameter.ActualValue = updateCounter; } updateCounter.Value++; //analyze solutions only every 'updateInterval' times if (updateCounter.Value != updateInterval) return base.Apply(); updateCounter.Value = 0; if (PopulationGraph == null) return base.Apply(); double selectionRatio = 1; if (Generations.Value > 0) { var previousRank = PopulationGraph.GetByRank(Generation.Value - 1); selectionRatio = (double)previousRank.Count(x => x.OutDegree > 0) / PopulationSize.Value; } // the selection ratio represents the percentage of the old population that survived to reproduce DataTable table; if (!Results.ContainsKey("SelectionRatio")) { table = new DataTable("Selection ratio"); Results.Add(new Result("SelectionRatio", table)); var row = new DataRow("Selection ratio") { VisualProperties = { StartIndexZero = true } }; row.Values.Add(selectionRatio); table.Rows.Add(row); } else { table = (DataTable)Results["SelectionRatio"].Value; table.Rows["Selection ratio"].Values.Add(selectionRatio); } DataTable contributionCountTable; var contributions = PopulationGraph.GetByRank(Generation.Value - 1).OrderByDescending(x => x.Quality).Select(x => (double)x.OutDegree); if (Results.ContainsKey("ContributionCount")) { contributionCountTable = (DataTable)Results["ContributionCount"].Value; var row = contributionCountTable.Rows["Contribution count"]; row.Values.Replace(contributions); } else { contributionCountTable = new DataTable("Contribution count"); Results.Add(new Result("ContributionCount", contributionCountTable)); var row = new DataRow("Contribution count") { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Columns } }; row.Values.AddRange(contributions); contributionCountTable.Rows.Add(row); } if (StoreHistory) { DataTableHistory history; if (Results.ContainsKey("ContributionCountHistory")) { history = (DataTableHistory)Results["ContributionCountHistory"].Value; } else { history = new DataTableHistory(); Results.Add(new Result("ContributionCountHistory", history)); } history.Add((DataTable)contributionCountTable.Clone()); } return base.Apply(); } } }