Free cookie consent management tool by TermsFeed Policy Generator

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

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

Created a feature/exploration branch for new data analysis features #1142

File size: 6.0 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.Value) {
84        double leftQuality = LeftSideParameter.ActualValue.Value;
85        ItemArray<DoubleValue> rightQualities = RightSideParameter.ActualValue;
86
87        if (rightQualities.Length < 1) throw new InvalidOperationException(Name + ": No subscopes found.");
88        bool maximization = MaximizationParameter.ActualValue.Value;
89
90        int bestParentIndex;
91        double bestParentQuality;
92
93        if (maximization)
94          bestParentQuality = rightQualities.Max(x => x.Value);
95        else
96          bestParentQuality = rightQualities.Min(x => x.Value);
97        bestParentIndex = rightQualities.FindIndex(x => x.Value == bestParentQuality);
98
99        SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
100        int ddl = DynamicDepthLimitParameter.ActualValue.Value;
101        double cRaise = CRaiseParameter.Value.Value;
102        double cLower = CLowerParameter.Value.Value;
103        double bestQuality = BestQualityParameter.ActualValue.Value;
104        double qualityPercentageChange = maximization ? -(bestQuality / leftQuality - 1) : (bestQuality / leftQuality - 1);
105        if (tree.Height <= ddl) {
106          // height is smaller than ddl => check improvement and reduce ddl
107          if (qualityPercentageChange >= (ddl - tree.Height) * cLower) {
108            ddl = Math.Max(tree.Height, InitialDepthLimitParameter.Value.Value);
109          }
110        } else {
111          // height is larger than dll => check improvement and increase ddl
112          if (qualityPercentageChange >= (tree.Height - ddl) * cRaise) {
113            ddl = tree.Height;
114          } else {
115            // height is larger but no improvment => reject
116            ResultParameter.ActualValue.Value = false;
117          }
118        }
119
120        DynamicDepthLimitParameter.ActualValue.Value = ddl;
121      }
122      return base.Apply();
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.