Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4227 was 4223, checked in by gkronber, 14 years ago

Corrected calculation of relative quality in dynamic depth limit comparator to work correctly for maximization problems and changed operator to update best of run quality immediately. #1142

File size: 5.7 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 alglib;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
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    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() {
83      if (ResultParameter.ActualValue == null || ResultParameter.ActualValue.Value) {
84        ResultParameter.ActualValue = new BoolValue(true);
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;
93        double relativeQuality = maximization ? (leftQuality - bestQuality) / leftQuality : (bestQuality - leftQuality) / leftQuality;
94        if (tree.Height <= ddl) {
95          // height is smaller than ddl => check improvement and reduce ddl
96          if (relativeQuality >= (ddl - tree.Height) * cLower) {
97            ddl = Math.Max(tree.Height, InitialDepthLimitParameter.ActualValue.Value);
98          }
99        } else {
100          // height is larger than dll => check improvement and increase ddl
101          if (relativeQuality >= (tree.Height - ddl) * cRaise) {
102            ddl = tree.Height;
103          } else {
104            // height is larger but no improvement => reject
105            ResultParameter.ActualValue.Value = false;
106          }
107        }
108
109        // update best quality
110        if (relativeQuality > 0 && ResultParameter.ActualValue.Value)
111          BestQualityParameter.ActualValue.Value = leftQuality;
112
113        DynamicDepthLimitParameter.ActualValue.Value = ddl;
114      }
115      return base.Apply();
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.