using System.Linq; 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; namespace HeuristicLab.Problems.MetaOptimization { /// /// TODO /// [Item("PMOProblemQualitiesAnalyzer", "")] [StorableClass] public sealed class PMOProblemQualitiesAnalyzer : SingleSuccessorOperator, IAnalyzer { 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"]; } } 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); } 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; if (maximization) { idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).Last().index; idxWorst = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index; } else { idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index; idxWorst = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).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); } } } }