#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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.Encodings.ParameterConfigurationEncoding; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.MetaOptimization { /// /// An analyzer for the qualities of each defined problem. /// [Item("PMOProblemQualitiesAnalyzer", "An analyzer for the qualities of each defined problem.")] [StorableClass] public sealed class PMOProblemQualitiesAnalyzer : SingleSuccessorOperator, IAnalyzer { public bool EnabledByDefault { get { return true; } } public ScopeTreeLookupParameter ParameterConfigurationParameter { get { return (ScopeTreeLookupParameter)Parameters["ParameterConfigurationTree"]; } } public ScopeTreeLookupParameter QualityParameter { get { return (ScopeTreeLookupParameter)Parameters["Quality"]; } } public ValueLookupParameter ResultsParameter { get { return (ValueLookupParameter)Parameters["Results"]; } } public LookupParameter> ProblemsParameter { get { return (LookupParameter>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; } } public LookupParameter MaximizationParameter { get { return (LookupParameter)Parameters["Maximization"]; } } #region Constructors and Cloning public PMOProblemQualitiesAnalyzer() : base() { Parameters.Add(new ScopeTreeLookupParameter("ParameterConfigurationTree", "")); Parameters.Add(new ScopeTreeLookupParameter("Quality", "")); Parameters.Add(new ValueLookupParameter("Results", "")); Parameters.Add(new LookupParameter>(MetaOptimizationProblem.ProblemsParameterName)); Parameters.Add(new LookupParameter("Maximization", "Set to false if the problem should be minimized.")); } [StorableConstructor] private PMOProblemQualitiesAnalyzer(bool deserializing) : base(deserializing) { } private PMOProblemQualitiesAnalyzer(PMOProblemQualitiesAnalyzer original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new PMOProblemQualitiesAnalyzer(this, cloner); } #endregion public override IOperation Apply() { ItemArray qualities = QualityParameter.ActualValue; ResultCollection results = ResultsParameter.ActualValue; ItemArray parameterTrees = ParameterConfigurationParameter.ActualValue; bool maximization = MaximizationParameter.ActualValue.Value; int idxBest; int idxWorst; var sortedQualities = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value); if (maximization) { idxBest = sortedQualities.Last().index; idxWorst = sortedQualities.First().index; } else { idxBest = sortedQualities.First().index; idxWorst = sortedQualities.Last().index; } int problemCount = ProblemsParameter.ActualValue.Count; double[][] problemQualities = GetProblemQualities(parameterTrees, problemCount); for (int i = 0; i < problemCount; i++) { DataTable problemQualitiesDataTable; string resultKey = "Problem." + i; if (!results.ContainsKey(resultKey)) { problemQualitiesDataTable = new DataTable(); problemQualitiesDataTable.Name = resultKey + " Qualities"; results.Add(new Result(resultKey, problemQualitiesDataTable)); } else { problemQualitiesDataTable = results[resultKey].Value as DataTable; } AddValue(problemQualitiesDataTable, parameterTrees[idxBest].AverageQualities[i], "BestQuality", "BestQuality"); AddValue(problemQualitiesDataTable, problemQualities[i].Average(), "AverageQuality", "BestQuality"); AddValue(problemQualitiesDataTable, parameterTrees[idxWorst].AverageQualities[i], "WorstQuality", "BestQuality"); } return base.Apply(); } private static double[][] GetProblemQualities(ItemArray parameterTrees, int problemCount) { double[][] problemQualities = new double[problemCount][]; for (int i = 0; i < problemCount; i++) { problemQualities[i] = new double[parameterTrees.Length]; for (int j = 0; j < parameterTrees.Length; j++) { problemQualities[i][j] = parameterTrees[j].AverageQualities[i]; } } return problemQualities; } private static void AddValue(DataTable table, double data, string name, string description) { DataRow row; table.Rows.TryGetValue(name, out row); if (row == null) { row = new DataRow(name, description); row.Values.Add(data); table.Rows.Add(row); } else { row.Values.Add(data); } } } }