[5607] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// An operator that analyzes the validation best symbolic data analysis solution for single objective symbolic data analysis problems.
|
---|
| 35 | /// </summary>
|
---|
| 36 | [Item("SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer", "An operator that analyzes the validation best symbolic data analysis solution for single objective symbolic data analysis problems.")]
|
---|
| 37 | [StorableClass]
|
---|
[5747] | 38 | public abstract class SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> : SymbolicDataAnalysisSingleObjectiveValidationAnalyzer<T, U>
|
---|
[5607] | 39 | where S : class, ISymbolicDataAnalysisSolution
|
---|
| 40 | where T : class, ISymbolicDataAnalysisSingleObjectiveEvaluator<U>
|
---|
| 41 | where U : class, IDataAnalysisProblemData {
|
---|
| 42 | private const string ValidationBestSolutionParameterName = "Best validation solution";
|
---|
| 43 | private const string ValidationBestSolutionQualityParameterName = "Best validation solution quality";
|
---|
| 44 |
|
---|
| 45 | #region parameter properties
|
---|
| 46 | public ILookupParameter<S> ValidationBestSolutionParameter {
|
---|
| 47 | get { return (ILookupParameter<S>)Parameters[ValidationBestSolutionParameterName]; }
|
---|
| 48 | }
|
---|
| 49 | public ILookupParameter<DoubleValue> ValidationBestSolutionQualityParameter {
|
---|
| 50 | get { return (ILookupParameter<DoubleValue>)Parameters[ValidationBestSolutionQualityParameterName]; }
|
---|
| 51 | }
|
---|
| 52 | #endregion
|
---|
| 53 | #region properties
|
---|
| 54 | public S ValidationBestSolution {
|
---|
| 55 | get { return ValidationBestSolutionParameter.ActualValue; }
|
---|
| 56 | set { ValidationBestSolutionParameter.ActualValue = value; }
|
---|
| 57 | }
|
---|
| 58 | public DoubleValue ValidationBestSolutionQuality {
|
---|
| 59 | get { return ValidationBestSolutionQualityParameter.ActualValue; }
|
---|
| 60 | set { ValidationBestSolutionQualityParameter.ActualValue = value; }
|
---|
| 61 | }
|
---|
| 62 | #endregion
|
---|
| 63 |
|
---|
| 64 | [StorableConstructor]
|
---|
| 65 | protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 66 | protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { }
|
---|
| 67 | public SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer()
|
---|
| 68 | : base() {
|
---|
[5722] | 69 | Parameters.Add(new LookupParameter<S>(ValidationBestSolutionParameterName, "The validation best symbolic data analyis solution."));
|
---|
[5607] | 70 | Parameters.Add(new LookupParameter<DoubleValue>(ValidationBestSolutionQualityParameterName, "The quality of the validation best symbolic data analysis solution."));
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | public override IOperation Apply() {
|
---|
| 74 | #region find best tree
|
---|
| 75 | double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
|
---|
| 76 | ISymbolicExpressionTree bestTree = null;
|
---|
[5882] | 77 | ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
|
---|
[5759] | 78 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
[6760] | 79 | var problemData = ProblemDataParameter.ActualValue;
|
---|
[5882] | 80 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
[5907] | 81 | if (!rows.Any()) return base.Apply();
|
---|
[5759] | 82 |
|
---|
[5722] | 83 | IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
|
---|
[6760] | 84 | var quality = tree
|
---|
| 85 | .AsParallel()
|
---|
| 86 | .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
|
---|
| 87 | .ToArray();
|
---|
| 88 |
|
---|
[5607] | 89 | for (int i = 0; i < tree.Length; i++) {
|
---|
| 90 | if (IsBetter(quality[i], bestQuality, Maximization.Value)) {
|
---|
| 91 | bestQuality = quality[i];
|
---|
| 92 | bestTree = tree[i];
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | #endregion
|
---|
| 96 |
|
---|
| 97 | var results = ResultCollection;
|
---|
| 98 | if (ValidationBestSolutionQuality == null ||
|
---|
| 99 | IsBetter(bestQuality, ValidationBestSolutionQuality.Value, Maximization.Value)) {
|
---|
| 100 | ValidationBestSolution = CreateSolution(bestTree, bestQuality);
|
---|
| 101 | ValidationBestSolutionQuality = new DoubleValue(bestQuality);
|
---|
| 102 |
|
---|
[5747] | 103 | if (!results.ContainsKey(ValidationBestSolutionParameter.Name)) {
|
---|
| 104 | results.Add(new Result(ValidationBestSolutionParameter.Name, ValidationBestSolutionParameter.Description, ValidationBestSolution));
|
---|
| 105 | results.Add(new Result(ValidationBestSolutionQualityParameter.Name, ValidationBestSolutionQualityParameter.Description, ValidationBestSolutionQuality));
|
---|
[5607] | 106 | } else {
|
---|
[5747] | 107 | results[ValidationBestSolutionParameter.Name].Value = ValidationBestSolution;
|
---|
| 108 | results[ValidationBestSolutionQualityParameter.Name].Value = ValidationBestSolutionQuality;
|
---|
[5607] | 109 | }
|
---|
| 110 | }
|
---|
| 111 | return base.Apply();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality);
|
---|
| 115 |
|
---|
| 116 | private bool IsBetter(double lhs, double rhs, bool maximization) {
|
---|
| 117 | if (maximization) return lhs > rhs;
|
---|
| 118 | else return lhs < rhs;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|