Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5265 was 5265, checked in by gkronber, 13 years ago

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