Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged changes from trunk to data analysis exploration branch and added fractional distance metric evaluator. #1142

File size: 5.6 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 HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using System.Collections.Generic;
32using HeuristicLab.Problems.DataAnalysis.Evaluators;
33using HeuristicLab.Analysis;
34using HeuristicLab.Common;
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    protected CovariantParsimonyPressureAdder(bool deserializing) : base(deserializing) { }
70    protected CovariantParsimonyPressureAdder(CovariantParsimonyPressureAdder original, Cloner cloner) : base(original, cloner) { }
71    public CovariantParsimonyPressureAdder()
72      : base() {
73      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>("Parents"));
74      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentQuality"));
75      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentAdjustedQuality"));
76      Parameters.Add(new LookupParameter<SymbolicExpressionTree>("Offspring"));
77      Parameters.Add(new LookupParameter<DoubleValue>("OffspringQuality"));
78      Parameters.Add(new LookupParameter<DoubleValue>("OffspringAdjustedQuality"));
79      Parameters.Add(new ValueLookupParameter<DoubleValue>("K", new DoubleValue(1)));
80      Parameters.Add(new LookupParameter<DoubleValue>("C"));
81      Parameters.Add(new ValueLookupParameter<BoolValue>("ApplyParsimonyPressure"));
82    }
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new CovariantParsimonyPressureAdder(this, cloner);
85    }
86    [StorableHook(Persistence.Default.CompositeSerializers.Storable.HookType.AfterDeserialization)]
87    private void AfterDeserialization() {
88    }
89
90    public override IOperation Apply() {
91      if (ApplyParsimonyPressureParameter.ActualValue.Value) {
92        double k = KParameter.ActualValue.Value;
93        double c = CParameter.ActualValue.Value;
94
95        // handle offspring
96        var offspringLength = OffspringParameter.ActualValue.Size;
97        OffspringAdjustedQualityParameter.ActualValue = new DoubleValue(OffspringQualityParameter.ActualValue.Value - c * Math.Pow(offspringLength, k));
98
99        // handle parents
100        ItemArray<SymbolicExpressionTree> parents = ParentsParameter.ActualValue;
101        ItemArray<DoubleValue> parentQualities = ParentQualityParameter.ActualValue;
102
103        ItemArray<DoubleValue> adjQualities = new ItemArray<DoubleValue>(parentQualities.Length);
104
105        for (int i = 0; i < adjQualities.Length; i++) {
106          adjQualities[i] = new DoubleValue(parentQualities[i].Value - c * Math.Pow(parents[i].Size, k));
107        }
108        ParentAdjustedQualityParameter.ActualValue = adjQualities;
109      } else {
110        // adjusted fitness is equal to fitness
111        ParentAdjustedQualityParameter.ActualValue = (ItemArray<DoubleValue>)ParentQualityParameter.ActualValue.Clone();
112        OffspringAdjustedQualityParameter.ActualValue = (DoubleValue)OffspringQualityParameter.ActualValue.Clone();
113      }
114      return base.Apply();
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.