[4193] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using alglib;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.DataAnalysis.Operators {
|
---|
| 34 | [Item("DynamicDepthLimitComparator", "As implemented by Ciprian.")]
|
---|
| 35 | [StorableClass]
|
---|
| 36 | public class DynamicDepthLimitComparator : SingleSuccessorOperator, ISubScopesQualityComparator {
|
---|
| 37 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 38 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 39 | }
|
---|
| 40 | public ILookupParameter<BoolValue> ResultParameter {
|
---|
| 41 | get { return (ILookupParameter<BoolValue>)Parameters["Result"]; }
|
---|
| 42 | }
|
---|
| 43 | public ILookupParameter<DoubleValue> LeftSideParameter {
|
---|
| 44 | get { return (ILookupParameter<DoubleValue>)Parameters["LeftSide"]; }
|
---|
| 45 | }
|
---|
| 46 | public ILookupParameter<ItemArray<DoubleValue>> RightSideParameter {
|
---|
| 47 | get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["RightSide"]; }
|
---|
| 48 | }
|
---|
| 49 | public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 50 | get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
|
---|
| 51 | }
|
---|
| 52 | public IValueLookupParameter<IntValue> DynamicDepthLimitParameter {
|
---|
| 53 | get { return (IValueLookupParameter<IntValue>)Parameters["DynamicDepthLimit"]; }
|
---|
| 54 | }
|
---|
| 55 | public IValueLookupParameter<IntValue> InitialDepthLimitParameter {
|
---|
| 56 | get { return (IValueLookupParameter<IntValue>)Parameters["InitialDepthLimit"]; }
|
---|
| 57 | }
|
---|
| 58 | public IValueParameter<DoubleValue> CLowerParameter {
|
---|
| 59 | get { return (IValueParameter<DoubleValue>)Parameters["cLower"]; }
|
---|
| 60 | }
|
---|
| 61 | public IValueParameter<DoubleValue> CRaiseParameter {
|
---|
| 62 | get { return (IValueParameter<DoubleValue>)Parameters["cRaise"]; }
|
---|
| 63 | }
|
---|
| 64 | public ILookupParameter<DoubleValue> BestQualityParameter {
|
---|
| 65 | get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public DynamicDepthLimitComparator()
|
---|
| 69 | : base() {
|
---|
| 70 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
|
---|
| 71 | Parameters.Add(new LookupParameter<BoolValue>("Result", "The result of the comparison: True means Quality is better, False means it is worse than parents."));
|
---|
| 72 | Parameters.Add(new LookupParameter<DoubleValue>("LeftSide", "The quality of the child."));
|
---|
| 73 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSide", "The qualities of the parents."));
|
---|
| 74 | Parameters.Add(new ValueLookupParameter<IntValue>("DynamicDepthLimit", "The current depth limit."));
|
---|
| 75 | Parameters.Add(new ValueLookupParameter<IntValue>("InitialDepthLimit"));
|
---|
| 76 | Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The newly created child."));
|
---|
| 77 | Parameters.Add(new LookupParameter<DoubleValue>("BestQuality"));
|
---|
| 78 | Parameters.Add(new ValueParameter<DoubleValue>("cLower", "", new DoubleValue(0.03)));
|
---|
| 79 | Parameters.Add(new ValueParameter<DoubleValue>("cRaise", "", new DoubleValue(0.015)));
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public override IOperation Apply() {
|
---|
[4223] | 83 | if (ResultParameter.ActualValue == null || ResultParameter.ActualValue.Value) {
|
---|
| 84 | ResultParameter.ActualValue = new BoolValue(true);
|
---|
[4193] | 85 | double leftQuality = LeftSideParameter.ActualValue.Value;
|
---|
| 86 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
| 87 |
|
---|
| 88 | SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
|
---|
| 89 | int ddl = DynamicDepthLimitParameter.ActualValue.Value;
|
---|
| 90 | double cRaise = CRaiseParameter.Value.Value;
|
---|
| 91 | double cLower = CLowerParameter.Value.Value;
|
---|
| 92 | double bestQuality = BestQualityParameter.ActualValue.Value;
|
---|
[4228] | 93 | double relativeQuality = maximization ? leftQuality / bestQuality - 1 : bestQuality / leftQuality - 1;
|
---|
[4193] | 94 | if (tree.Height <= ddl) {
|
---|
| 95 | // height is smaller than ddl => check improvement and reduce ddl
|
---|
[4223] | 96 | if (relativeQuality >= (ddl - tree.Height) * cLower) {
|
---|
| 97 | ddl = Math.Max(tree.Height, InitialDepthLimitParameter.ActualValue.Value);
|
---|
[4193] | 98 | }
|
---|
| 99 | } else {
|
---|
| 100 | // height is larger than dll => check improvement and increase ddl
|
---|
[4223] | 101 | if (relativeQuality >= (tree.Height - ddl) * cRaise) {
|
---|
[4193] | 102 | ddl = tree.Height;
|
---|
| 103 | } else {
|
---|
[4223] | 104 | // height is larger but no improvement => reject
|
---|
[4193] | 105 | ResultParameter.ActualValue.Value = false;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[4223] | 109 | // update best quality
|
---|
| 110 | if (relativeQuality > 0 && ResultParameter.ActualValue.Value)
|
---|
| 111 | BestQualityParameter.ActualValue.Value = leftQuality;
|
---|
| 112 |
|
---|
[4193] | 113 | DynamicDepthLimitParameter.ActualValue.Value = ddl;
|
---|
| 114 | }
|
---|
| 115 | return base.Apply();
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|