Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.3/Operators/CovariantParsimonyPressureAdder.cs @ 4539

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

Exploring overfitting countermeasures. #1142

File size: 5.4 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;
33using HeuristicLab.Problems.DataAnalysis.Evaluators;
34using HeuristicLab.Analysis;
35
36namespace HeuristicLab.Problems.DataAnalysis.Operators {
37  [Item("Covariant Parsimony Pressure Adder", "Covariant Parsimony Pressure Adder.")]
38  [StorableClass]
39  public class CovariantParsimonyPressureAdder : SingleSuccessorOperator {
40    public IScopeTreeLookupParameter<SymbolicExpressionTree> ParentsParameter {
41      get { return (IScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters["Parents"]; }
42    }
43    public IScopeTreeLookupParameter<DoubleValue> ParentQualityParameter {
44      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["ParentQuality"]; }
45    }
46    public IScopeTreeLookupParameter<DoubleValue> ParentAdjustedQualityParameter {
47      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["ParentAdjustedQuality"]; }
48    }
49    public ILookupParameter<SymbolicExpressionTree> OffspringParameter {
50      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["Offspring"]; }
51    }
52    public ILookupParameter<DoubleValue> OffspringQualityParameter {
53      get { return (ILookupParameter<DoubleValue>)Parameters["OffspringQuality"]; }
54    }
55    public ILookupParameter<DoubleValue> OffspringAdjustedQualityParameter {
56      get { return (ILookupParameter<DoubleValue>)Parameters["OffspringAdjustedQuality"]; }
57    }
58    public IValueLookupParameter<DoubleValue> KParameter {
59      get { return (IValueLookupParameter<DoubleValue>)Parameters["K"]; }
60    }
61    public IValueLookupParameter<BoolValue> ApplyParsimonyPressureParameter {
62      get { return (IValueLookupParameter<BoolValue>)Parameters["ApplyParsimonyPressure"]; }
63    }
64    public ILookupParameter<DoubleValue> CParameter {
65      get { return (ILookupParameter<DoubleValue>)Parameters["C"]; }
66    }
67
68    [StorableConstructor]
69    public CovariantParsimonyPressureAdder(bool deserializing) : base(deserializing) { }
70    public CovariantParsimonyPressureAdder()
71      : base() {
72      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>("Parents"));
73      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentQuality"));
74      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentAdjustedQuality"));
75      Parameters.Add(new LookupParameter<SymbolicExpressionTree>("Offspring"));
76      Parameters.Add(new LookupParameter<DoubleValue>("OffspringQuality"));
77      Parameters.Add(new LookupParameter<DoubleValue>("OffspringAdjustedQuality"));
78      Parameters.Add(new ValueLookupParameter<DoubleValue>("K", new DoubleValue(1)));
79      Parameters.Add(new LookupParameter<DoubleValue>("C"));
80      Parameters.Add(new ValueLookupParameter<BoolValue>("ApplyParsimonyPressure"));
81    }
82
83    [StorableHook(Persistence.Default.CompositeSerializers.Storable.HookType.AfterDeserialization)]
84    private void AfterDeserialization() {
85    }
86
87    public override IOperation Apply() {
88      if (ApplyParsimonyPressureParameter.ActualValue.Value) {
89        double k = KParameter.ActualValue.Value;
90        double c = CParameter.ActualValue.Value;
91
92        // handle offspring
93        var offspringLength = OffspringParameter.ActualValue.Size;
94        OffspringAdjustedQualityParameter.ActualValue = new DoubleValue(OffspringQualityParameter.ActualValue.Value - c * Math.Pow(offspringLength, k));
95
96        // handle parents
97        ItemArray<SymbolicExpressionTree> parents = ParentsParameter.ActualValue;
98        ItemArray<DoubleValue> parentQualities = ParentQualityParameter.ActualValue;
99
100        ItemArray<DoubleValue> adjQualities = new ItemArray<DoubleValue>(parentQualities.Length);
101
102        for (int i = 0; i < adjQualities.Length; i++) {
103          adjQualities[i] = new DoubleValue(parentQualities[i].Value - c * Math.Pow(parents[i].Size, k));
104        }
105        ParentAdjustedQualityParameter.ActualValue = adjQualities;
106      } else {
107        // adjusted fitness is equal to fitness
108        ParentAdjustedQualityParameter.ActualValue = (ItemArray<DoubleValue>)ParentQualityParameter.ActualValue.Clone();
109        OffspringAdjustedQualityParameter.ActualValue = (DoubleValue)OffspringQualityParameter.ActualValue.Clone();
110      }
111      return base.Apply();
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.