#region License Information /* HeuristicLab * Copyright (C) 2002-2014 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 HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.DataAnalysis.Symbolic { /// /// An operator that collects the Pareto-best symbolic data analysis solutions for single objective symbolic data analysis problems. /// [Item("SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer", "An operator that analyzes the Pareto-best symbolic data analysis solution for single objective symbolic data analysis problems.")] [StorableClass] public abstract class SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer, ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator where T : class, ISymbolicDataAnalysisSolution where S : class, IDataAnalysisProblemData { private const string ProblemDataParameterName = "ProblemData"; private const string TrainingBestSolutionsParameterName = "Best training solutions"; private const string TrainingBestSolutionQualitiesParameterName = "Best training solution qualities"; private const string ComplexityParameterName = "Complexity"; private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter"; private const string EstimationLimitsParameterName = "EstimationLimits"; public override bool EnabledByDefault { get { return false; } } #region parameter properties public ILookupParameter> TrainingBestSolutionsParameter { get { return (ILookupParameter>)Parameters[TrainingBestSolutionsParameterName]; } } public ILookupParameter> TrainingBestSolutionQualitiesParameter { get { return (ILookupParameter>)Parameters[TrainingBestSolutionQualitiesParameterName]; } } public IScopeTreeLookupParameter ComplexityParameter { get { return (IScopeTreeLookupParameter)Parameters[ComplexityParameterName]; } } public ILookupParameter SymbolicDataAnalysisTreeInterpreterParameter { get { return (ILookupParameter)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; } } public ILookupParameter ProblemDataParameter { get { return (ILookupParameter)Parameters[ProblemDataParameterName]; } } public IValueLookupParameter EstimationLimitsParameter { get { return (IValueLookupParameter)Parameters[EstimationLimitsParameterName]; } } #endregion #region properties public ItemList TrainingBestSolutions { get { return TrainingBestSolutionsParameter.ActualValue; } set { TrainingBestSolutionsParameter.ActualValue = value; } } public ItemList TrainingBestSolutionQualities { get { return TrainingBestSolutionQualitiesParameter.ActualValue; } set { TrainingBestSolutionQualitiesParameter.ActualValue = value; } } #endregion [StorableConstructor] protected SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer(bool deserializing) : base(deserializing) { } protected SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { } public SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer() : base() { Parameters.Add(new LookupParameter(ProblemDataParameterName, "The problem data for the symbolic data analysis solution.")); Parameters.Add(new LookupParameter>(TrainingBestSolutionsParameterName, "The training best (Pareto-optimal) symbolic data analysis solutions.")); Parameters.Add(new LookupParameter>(TrainingBestSolutionQualitiesParameterName, "The qualities of the training best (Pareto-optimal) solutions.")); Parameters.Add(new ScopeTreeLookupParameter(ComplexityParameterName, "The complexity of each tree.")); Parameters.Add(new LookupParameter(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree.")); Parameters.Add(new ValueLookupParameter(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic classification model.")); } public override IOperation Apply() { var results = ResultCollection; // create empty parameter and result values if (TrainingBestSolutions == null) { TrainingBestSolutions = new ItemList(); TrainingBestSolutionQualities = new ItemList(); results.Add(new Result(TrainingBestSolutionQualitiesParameter.Name, TrainingBestSolutionQualitiesParameter.Description, TrainingBestSolutionQualities)); results.Add(new Result(TrainingBestSolutionsParameter.Name, TrainingBestSolutionsParameter.Description, TrainingBestSolutions)); } IList> trainingBestQualities = TrainingBestSolutionQualities .Select(x => Tuple.Create(x[0], x[1])) .ToList(); #region find best trees IList nonDominatedIndexes = new List(); ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray(); List qualities = Quality.Select(x => x.Value).ToList(); List complexities; if (ComplexityParameter.ActualValue != null && ComplexityParameter.ActualValue.Length == qualities.Count) { complexities = ComplexityParameter.ActualValue.Select(x => x.Value).ToList(); } else { complexities = tree.Select(t => (double)t.Length).ToList(); } List> fitness = new List>(); for (int i = 0; i < qualities.Count; i++) fitness.Add(Tuple.Create(qualities[i], complexities[i])); var maximization = Tuple.Create(Maximization.Value, false);// complexity must be minimized List> newNonDominatedQualities = new List>(); for (int i = 0; i < tree.Length; i++) { if (IsNonDominated(fitness[i], trainingBestQualities, maximization) && IsNonDominated(fitness[i], newNonDominatedQualities, maximization) && IsNonDominated(fitness[i], fitness.Skip(i + 1), maximization)) { if (!newNonDominatedQualities.Contains(fitness[i])) { newNonDominatedQualities.Add(fitness[i]); nonDominatedIndexes.Add(i); } } } #endregion #region update Pareto-optimal solution archive if (nonDominatedIndexes.Count > 0) { ItemList nonDominatedQualities = new ItemList(); ItemList nonDominatedSolutions = new ItemList(); // add all new non-dominated solutions to the archive foreach (var index in nonDominatedIndexes) { T solution = CreateSolution(tree[index]); nonDominatedSolutions.Add(solution); nonDominatedQualities.Add(new DoubleArray(new double[] { fitness[index].Item1, fitness[index].Item2 })); } // add old non-dominated solutions only if they are not dominated by one of the new solutions for (int i = 0; i < trainingBestQualities.Count; i++) { if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization)) { if (!newNonDominatedQualities.Contains(trainingBestQualities[i])) { nonDominatedSolutions.Add(TrainingBestSolutions[i]); nonDominatedQualities.Add(TrainingBestSolutionQualities[i]); } } } // make sure solutions and qualities are ordered in the results var orderedIndexes = nonDominatedSolutions.Select((s, i) => i).OrderBy(i => nonDominatedQualities[i][0]).ToArray(); var orderedNonDominatedSolutions = new ItemList(); var orderedNonDominatedQualities = new ItemList(); foreach (var i in orderedIndexes) { orderedNonDominatedQualities.Add(nonDominatedQualities[i]); orderedNonDominatedSolutions.Add(nonDominatedSolutions[i]); } TrainingBestSolutions = orderedNonDominatedSolutions; TrainingBestSolutionQualities = orderedNonDominatedQualities; results[TrainingBestSolutionsParameter.Name].Value = orderedNonDominatedSolutions; results[TrainingBestSolutionQualitiesParameter.Name].Value = orderedNonDominatedQualities; } #endregion return base.Apply(); } protected abstract T CreateSolution(ISymbolicExpressionTree bestTree); private bool IsNonDominated(Tuple point, IEnumerable> points, Tuple maximization) { return !points.Any(p => IsBetterOrEqual(p.Item1, point.Item1, maximization.Item1) && IsBetterOrEqual(p.Item2, point.Item2, maximization.Item2)); } private bool IsBetterOrEqual(double lhs, double rhs, bool maximization) { if (maximization) return lhs >= rhs; else return lhs <= rhs; } } }