#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 HeuristicLab.Analysis; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using System.Linq; namespace HeuristicLab.BioBoost.Analysis { [Item("MultiOperatorUsageAnalyzer", "An operator for analyzing utilization of different choices of a multi manipulation operatoutilization of different choices of a multi manipulation operator.")] [StorableClass] public class MultiOperatorUsageAnalyzer : SingleSuccessorOperator, IAnalyzer { public bool EnabledByDefault { get { return false; } } #region Parameters public ScopeTreeLookupParameter SelectedOperatorParameter { get { return (ScopeTreeLookupParameter) Parameters["SelectedOperator"]; } } public ValueLookupParameter ResultsParameter { get { return (ValueLookupParameter)Parameters["Results"]; } } #endregion #region Construction & Cloning [StorableConstructor] protected MultiOperatorUsageAnalyzer(bool isDeserializing) : base(isDeserializing) {} protected MultiOperatorUsageAnalyzer(MultiOperatorUsageAnalyzer orig, Cloner cloner) : base(orig, cloner) {} public MultiOperatorUsageAnalyzer() { Parameters.Add(new ScopeTreeLookupParameter("SelectedOperator", "The selected operators in a population.")); Parameters.Add(new ValueLookupParameter("Results", "The result collection where the best solution should be stored.")); } public override IDeepCloneable Clone(Cloner cloner) { return new MultiOperatorUsageAnalyzer(this, cloner); } #endregion public override IOperation Apply() { var operators = SelectedOperatorParameter.ActualValue; var table = GetOrCreateTable(SelectedOperatorParameter.ActualName + "Usages"); AddValues(table, operators); var avgTable = GetOrCreateTable(SelectedOperatorParameter.ActualName + "Avg Usages"); foreach (var row in table.Rows) { DataRow avgRow; if (!avgTable.Rows.TryGetValue(row.Name, out avgRow)) { avgRow = new DataRow(row.Name); avgRow.Values.AddRange(Enumerable.Repeat(0d, row.Values.Count - 1)); avgTable.Rows.Add(avgRow); } avgRow.Values.Add(row.Values.Average()); } return base.Apply(); } private static void AddValues(DataTable table, ItemArray operators) { var n = operators.Length; var groups = operators .GroupBy(v => v.Value) .ToDictionary(g => g.Key, g => 1.0*g.Count()/n); var len = table.Rows.Count == 0 ? 0 : table.Rows.Max(r => r.Values.Count); foreach (var row in table.Rows) { var probability = 0d; if (groups.TryGetValue(row.Name, out probability)) groups.Remove(row.Name); row.Values.Add(probability); } foreach (var newOp in groups) { var row = new DataRow(newOp.Key); row.Values.AddRange(Enumerable.Repeat(0d, len)); row.Values.Add(newOp.Value); table.Rows.Add(row); } } private DataTable GetOrCreateTable(string tableName) { IResult result = null; if (ResultsParameter.ActualValue.TryGetValue(tableName, out result)) { return (DataTable)result.Value; } else { var table = new DataTable(tableName); ResultsParameter.ActualValue.Add(new Result(tableName, table)); return table; } } } }