Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.3/Operators/DynamicDepthLimitComparator.cs @ 7708

Last change on this file since 7708 was 5275, checked in by gkronber, 14 years ago

Merged changes from trunk to data analysis exploration branch and added fractional distance metric evaluator. #1142

File size: 6.1 KB
Line 
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
22using System;
23using System.Linq;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.Common;
32
33namespace 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    [StorableConstructor]
69    protected DynamicDepthLimitComparator(bool deserializing) : base(deserializing) { }
70    protected DynamicDepthLimitComparator(DynamicDepthLimitComparator original, Cloner cloner)
71      : base(original, cloner) {
72    }
73
74    public DynamicDepthLimitComparator()
75      : base() {
76      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
77      Parameters.Add(new LookupParameter<BoolValue>("Result", "The result of the comparison: True means Quality is better, False means it is worse than parents."));
78      Parameters.Add(new LookupParameter<DoubleValue>("LeftSide", "The quality of the child."));
79      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSide", "The qualities of the parents."));
80      Parameters.Add(new ValueLookupParameter<IntValue>("DynamicDepthLimit", "The current depth limit."));
81      Parameters.Add(new ValueLookupParameter<IntValue>("InitialDepthLimit"));
82      Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The newly created child."));
83      Parameters.Add(new LookupParameter<DoubleValue>("BestQuality"));
84      Parameters.Add(new ValueParameter<DoubleValue>("cLower", "", new DoubleValue(0.03)));
85      Parameters.Add(new ValueParameter<DoubleValue>("cRaise", "", new DoubleValue(0.015)));
86    }
87
88    public override IDeepCloneable Clone(Cloner cloner) {
89      return new DynamicDepthLimitComparator(this, cloner);
90    }
91
92    public override IOperation Apply() {
93      if (ResultParameter.ActualValue == null || ResultParameter.ActualValue.Value) {
94        ResultParameter.ActualValue = new BoolValue(true);
95        double leftQuality = LeftSideParameter.ActualValue.Value;
96        bool maximization = MaximizationParameter.ActualValue.Value;
97
98        SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
99        int ddl = DynamicDepthLimitParameter.ActualValue.Value;
100        double cRaise = CRaiseParameter.Value.Value;
101        double cLower = CLowerParameter.Value.Value;
102        double bestQuality = BestQualityParameter.ActualValue.Value;
103        double relativeQuality = maximization ? leftQuality / bestQuality - 1 : bestQuality / leftQuality - 1;
104        if (tree.Height <= ddl) {
105          // height is smaller than ddl => check improvement and reduce ddl
106          if (relativeQuality >= (ddl - tree.Height) * cLower) {
107            ddl = Math.Max(tree.Height, InitialDepthLimitParameter.ActualValue.Value);
108          }
109        } else {
110          // height is larger than dll => check improvement and increase ddl
111          if (relativeQuality >= (tree.Height - ddl) * cRaise) {
112            ddl = tree.Height;
113          } else {
114            // height is larger but no improvement => reject
115            ResultParameter.ActualValue.Value = false;
116          }
117        }
118
119        // update best quality
120        if (relativeQuality > 0 && ResultParameter.ActualValue.Value)
121          BestQualityParameter.ActualValue.Value = leftQuality;
122
123        DynamicDepthLimitParameter.ActualValue.Value = ddl;
124      }
125      return base.Apply();
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.