Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.3/Operators/CovariantParsimonyPressure.cs @ 4233

Last change on this file since 4233 was 4233, checked in by gkronber, 12 years ago

Added operator to apply covariant parsimony pressure. #1142

File size: 4.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;
32using System.Collections.Generic;
33using HeuristicLab.Problems.DataAnalysis.Evaluators;
34
35namespace HeuristicLab.Problems.DataAnalysis.Operators {
36  [Item("Covariant Parsimony Pressure", "Covariant Parsimony Pressure.")]
37  [StorableClass]
38  public class CovariantParsimonyPressure : SingleSuccessorOperator {
39    public IScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
40      get { return (IScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
41    }
42    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
43      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
44    }
45    public ILookupParameter<BoolValue> MaximizationParameter {
46      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
47    }
48    public IValueLookupParameter<DoubleValue> KParameter {
49      get { return (IValueLookupParameter<DoubleValue>)Parameters["K"]; }
50    }
51
52
53    public CovariantParsimonyPressure(bool deserializing) : base(deserializing) { }
54    public CovariantParsimonyPressure()
55      : base() {
56      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree"));
57      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality"));
58      Parameters.Add(new LookupParameter<BoolValue>("Maximization"));
59      Parameters.Add(new ValueLookupParameter<DoubleValue>("K", new DoubleValue(1.0)));
60    }
61
62    [StorableHook(Persistence.Default.CompositeSerializers.Storable.HookType.AfterDeserialization)]
63    private void AfterDeserialization() {
64      if (!Parameters.ContainsKey("Maximization"))
65        Parameters.Add(new LookupParameter<BoolValue>("Maximization"));
66      if (!Parameters.ContainsKey("K"))
67        Parameters.Add(new ValueLookupParameter<DoubleValue>("K", new DoubleValue(1.0)));
68    }
69
70    public override IOperation Apply() {
71      var trees = SymbolicExpressionTreeParameter.ActualValue;
72      var qualities = QualityParameter.ActualValue;
73      var lengths = from tree in trees
74                    select tree.Size;
75      double k = KParameter.ActualValue.Value;
76
77      // calculate cov(f, l) and cov(l, l^k)
78      OnlineCovarianceEvaluator lengthFitnessCovEvaluator = new OnlineCovarianceEvaluator();
79      OnlineCovarianceEvaluator lengthAdjLengthCovEvaluator = new OnlineCovarianceEvaluator();
80      var lengthEnumerator = lengths.GetEnumerator();
81      var qualityEnumerator = qualities.GetEnumerator();
82      while (lengthEnumerator.MoveNext() & qualityEnumerator.MoveNext()) {
83        double fitness = qualityEnumerator.Current.Value;
84        if (!MaximizationParameter.ActualValue.Value) {
85          // use f = 1 / (1 + quality) for minimization problems
86          fitness = 1.0 / (1.0 + fitness);
87        }
88        lengthFitnessCovEvaluator.Add(lengthEnumerator.Current, fitness);
89        lengthAdjLengthCovEvaluator.Add(lengthEnumerator.Current, Math.Pow(lengthEnumerator.Current, k));
90      }
91
92      // c = cov(l, f) / cov(l, l^k)
93      double c = lengthFitnessCovEvaluator.Covariance / lengthAdjLengthCovEvaluator.Covariance;
94
95      // adjust fitness
96      bool maximization = MaximizationParameter.ActualValue.Value;
97
98      lengthEnumerator = lengths.GetEnumerator();
99      qualityEnumerator = qualities.GetEnumerator();
100      while (lengthEnumerator.MoveNext() & qualityEnumerator.MoveNext()) {
101        qualityEnumerator.Current.Value = qualityEnumerator.Current.Value - c * Math.Pow(lengthEnumerator.Current, k);
102      }
103
104      return base.Apply();
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.