Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.3/Operators/DynOpEqComparator.cs @ 4224

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

Added experimental anti-overfitting code to dynamic operator equalizer to prevent code growth when the current validation quality is below the best validation quality. #1142.

File size: 10.3 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;
32using System.Collections.Generic;
33
34namespace HeuristicLab.Problems.DataAnalysis.Operators {
35  [Item("DynOpEqComparator", "Dynamic Operator Equalization.")]
36  [StorableClass]
37  public class DynOpEqComparator : SingleSuccessorOperator, ISubScopesQualityComparator {
38    public IValueLookupParameter<BoolValue> MaximizationParameter {
39      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
40    }
41    public ILookupParameter<BoolValue> ResultParameter {
42      get { return (ILookupParameter<BoolValue>)Parameters["Result"]; }
43    }
44    public ILookupParameter<DoubleValue> LeftSideParameter {
45      get { return (ILookupParameter<DoubleValue>)Parameters["LeftSide"]; }
46    }
47    public ILookupParameter<ItemArray<DoubleValue>> RightSideParameter {
48      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["RightSide"]; }
49    }
50    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
51      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
52    }
53    public ILookupParameter<DoubleValue> BestQualityParameter {
54      get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; }
55    }
56    public IValueLookupParameter<IntValue> BinSizeParameter {
57      get { return (IValueLookupParameter<IntValue>)Parameters["BinSize"]; }
58    }
59    public ILookupParameter<ItemList<IntValue>> BinCapacityParameter {
60      get { return (ILookupParameter<ItemList<IntValue>>)Parameters["BinCapacity"]; }
61    }
62    public ILookupParameter<ItemList<ItemList<DoubleValue>>> AcceptedBinQualitiesParameter {
63      get { return (ILookupParameter<ItemList<ItemList<DoubleValue>>>)Parameters["AcceptedBinQualities"]; }
64    }
65    public ILookupParameter<ItemList<IntValue>> AcceptedCountsParameter {
66      get { return (ILookupParameter<ItemList<IntValue>>)Parameters["AcceptedCounts"]; }
67    }
68    public ILookupParameter<ItemList<IntValue>> TotalCountsParameter {
69      get { return (ILookupParameter<ItemList<IntValue>>)Parameters["TotalCounts"]; }
70    }
71    public IValueLookupParameter<BoolValue> AntiOverfitParameter {
72      get { return (IValueLookupParameter<BoolValue>)Parameters["AntiOverfit"]; }
73    }
74    public ILookupParameter<DoubleValue> CurrentBestValidationQualityParameter {
75      get { return (ILookupParameter<DoubleValue>)Parameters["Current best validation quality"]; }
76    }
77    public ILookupParameter<DoubleValue> BestValidationQualityParameter {
78      get { return (ILookupParameter<DoubleValue>)Parameters["Best solution quality (validation)"]; }
79    }
80
81    public DynOpEqComparator()
82      : base() {
83      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
84      Parameters.Add(new LookupParameter<BoolValue>("Result", "The result of the comparison: True means Quality is better, False means it is worse than parents."));
85      Parameters.Add(new LookupParameter<DoubleValue>("LeftSide", "The quality of the child."));
86      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSide", "The qualities of the parents."));
87      Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The newly created child."));
88      Parameters.Add(new LookupParameter<DoubleValue>("BestQuality"));
89      Parameters.Add(new ValueLookupParameter<IntValue>("BinSize", new IntValue(5)));
90      Parameters.Add(new LookupParameter<ItemList<IntValue>>("BinCapacity"));
91      Parameters.Add(new LookupParameter<ItemList<ItemList<DoubleValue>>>("AcceptedBinQualities"));
92      Parameters.Add(new LookupParameter<ItemList<IntValue>>("AcceptedCounts"));
93      Parameters.Add(new LookupParameter<ItemList<IntValue>>("TotalCounts"));
94      Parameters.Add(new ValueLookupParameter<BoolValue>("AntiOverfit", new BoolValue(false)));
95      Parameters.Add(new LookupParameter<DoubleValue>("Current best validation quality"));
96      Parameters.Add(new LookupParameter<DoubleValue>("Best solution quality (validation)"));
97    }
98
99    public override IOperation Apply() {
100      if (ResultParameter.ActualValue == null || ResultParameter.ActualValue.Value == true) {
101        var tree = SymbolicExpressionTreeParameter.ActualValue;
102        int size = tree.Size;
103        int bin = GetBinIndexForSize(size);
104        if (LeftSideParameter.ActualValue == null) {
105          // not yet evaluated
106          #region debugging
107          ItemList<IntValue> totalCounts = TotalCountsParameter.ActualValue;
108          while (bin >= totalCounts.Count) totalCounts.Add(new IntValue(0));
109          totalCounts[bin].Value = totalCounts[bin].Value + 1;
110          #endregion
111          if (!Exists(bin)) {
112
113            if (AntiOverfitParameter.ActualValue.Value) {
114              // reject more complex solutions if the current validation quality is worse than the best so far
115              ResultParameter.ActualValue = new BoolValue(!IsOverfitting());
116            } else {
117              // new bin -> evaluate and check later
118              ResultParameter.ActualValue = new BoolValue(true);
119            }
120          } else {
121            // bin exists:
122            // if bin is full -> reject
123            // otherwise -> evaluate and check success criterion
124            ResultParameter.ActualValue = new BoolValue(IsNotFull(bin));
125          }
126        } else {
127          double leftQuality = LeftSideParameter.ActualValue.Value;
128          ResultParameter.ActualValue = new BoolValue(Accept(size, bin, leftQuality));
129        }
130      }
131      return base.Apply();
132    }
133
134    private bool IsOverfitting() {
135      bool maximization = MaximizationParameter.ActualValue.Value;
136      if (CurrentBestValidationQualityParameter.ActualValue != null && BestValidationQualityParameter.ActualValue != null) {
137        double currentValidationQuality = CurrentBestValidationQualityParameter.ActualValue.Value;
138        double bestValidationQuality = BestValidationQualityParameter.ActualValue.Value;
139        return maximization ? currentValidationQuality < bestValidationQuality : currentValidationQuality > bestValidationQuality;
140      } else
141        return false;
142    }
143
144    private int GetBinIndexForSize(int size) {
145      return (int)Math.Floor((size - 3.0) / BinSizeParameter.ActualValue.Value);
146    }
147
148    private bool Accept(int size, int binIndex, double solutionQuality) {
149      bool accept = false;
150      if (Exists(binIndex)) {
151        //if (IsNotFull(binIndex) /*||
152          //NewBestOfBin(solutionQuality, binIndex)*/) {       
153        AddToBin(solutionQuality, binIndex);
154          accept = true;
155        //}
156      } else if (NewBestOfRun(solutionQuality)) {
157        CreateNewBin(binIndex);
158        AddToBin(solutionQuality, binIndex);
159        accept = true;
160      }
161      return accept;
162    }
163
164    private void AddToBin(double solutionQuality, int binIndex) {
165      ItemList<DoubleValue> acceptedBinQualities = AcceptedBinQualitiesParameter.ActualValue[binIndex];
166      ItemList<IntValue> acceptedCounts = AcceptedCountsParameter.ActualValue;
167      acceptedBinQualities.Add(new DoubleValue(solutionQuality));
168      acceptedCounts[binIndex].Value = acceptedCounts[binIndex].Value + 1;
169    }
170
171    private bool NewBestOfRun(double solutionQuality) {
172      bool maximization = MaximizationParameter.ActualValue.Value;
173      double bestQuality = BestQualityParameter.ActualValue.Value;
174      return maximization ? solutionQuality > bestQuality : solutionQuality < bestQuality;
175    }
176
177    private void CreateNewBin(int binIndex) {
178      ItemList<IntValue> binCapacities = BinCapacityParameter.ActualValue;
179      ItemList<ItemList<DoubleValue>> acceptedQualities = AcceptedBinQualitiesParameter.ActualValue;
180      ItemList<IntValue> acceptedCounts = AcceptedCountsParameter.ActualValue;
181
182      // create empty bins of capacity one up to the newly created bin
183      for (int i = binCapacities.Count; i <= binIndex; i++) {
184        binCapacities.Add(new IntValue(1));
185        acceptedQualities.Add(new ItemList<DoubleValue>(10));
186        acceptedCounts.Add(new IntValue(0));
187      }
188    }
189
190    //private bool NewBestOfBin(double solutionQuality, int binIndex) {
191    //  ItemList<ItemList<DoubleValue>> acceptedQualities = AcceptedBinQualitiesParameter.ActualValue;
192    //  if (acceptedQualities[binIndex].Count == 0) return true;
193    //  bool maximization = MaximizationParameter.ActualValue.Value;
194    //  IEnumerable<double> binQualities = acceptedQualities[binIndex].Select(x => x.Value);
195    //  // binQualities are always sorted so that the best is in bin 0
196    //  return maximization ? solutionQuality > binQualities.First() :
197    //    solutionQuality < binQualities.First();
198    //}
199
200    private bool IsNotFull(int binIndex) {
201      ItemList<IntValue> binCapacities = BinCapacityParameter.ActualValue;
202      ItemList<ItemList<DoubleValue>> acceptedQualities = AcceptedBinQualitiesParameter.ActualValue;
203      return acceptedQualities[binIndex].Count < binCapacities[binIndex].Value;
204    }
205
206    private bool Exists(int binIndex) {
207      // if the bin has a capacity set then it exists
208      ItemList<IntValue> binCapacities = BinCapacityParameter.ActualValue;
209      return binIndex < binCapacities.Count;
210    }
211  }
212}
Note: See TracBrowser for help on using the repository browser.