Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMoveEvaluator.cs @ 8214

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

#1847: bug fixes and improvements discussed with andreas

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
33  [Item("ReplaceBranchMoveEvaluator", "")]
34  [StorableClass]
35  public class ReplaceBranchMoveEvaluator : SingleSuccessorOperator, ISymbolicRegressionMoveEvaluator {
36
37    public override bool CanChangeName {
38      get { return false; }
39    }
40
41    public ILookupParameter<ReplaceBranchMove> MoveParameter {
42      get { return (ILookupParameter<ReplaceBranchMove>)Parameters["ReplaceBranchMove"]; }
43    }
44    public ILookupParameter<DoubleValue> QualityParameter {
45      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
46    }
47
48    public ILookupParameter<DoubleValue> MoveQualityParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
50    }
51
52    public ILookupParameter<ISymbolicRegressionSingleObjectiveEvaluator> EvaluatorParameter {
53      get { return (ILookupParameter<ISymbolicRegressionSingleObjectiveEvaluator>)Parameters["Evaluator"]; }
54    }
55    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
56      get { return (ILookupParameter<IRegressionProblemData>)Parameters["ProblemData"]; }
57    }
58    public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
59      get { return (ILookupParameter<IntRange>)Parameters["FitnessCalculationPartition"]; }
60    }
61
62    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
63      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
64    }
65    [StorableConstructor]
66    protected ReplaceBranchMoveEvaluator(bool deserializing) : base(deserializing) { }
67    protected ReplaceBranchMoveEvaluator(ReplaceBranchMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
68    public ReplaceBranchMoveEvaluator()
69      : base() {
70      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of a symbolic regression solution."));
71      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The evaluated quality of a move on a symbolic regression solution."));
72      Parameters.Add(new LookupParameter<ISymbolicRegressionSingleObjectiveEvaluator>("Evaluator", ""));
73      Parameters.Add(new LookupParameter<ReplaceBranchMove>("ReplaceBranchMove", ""));
74      Parameters.Add(new LookupParameter<IRegressionProblemData>("ProblemData", ""));
75      Parameters.Add(new LookupParameter<IntRange>("FitnessCalculationPartition", ""));
76      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", ""));
77    }
78
79    public override IOperation Apply() {
80      var evaluator = EvaluatorParameter.ActualValue;
81
82      var move = MoveParameter.ActualValue;
83
84      // calculate linear scaling
85      OnlineCalculatorError errorState;
86      double alpha, beta;
87      OnlineLinearScalingParameterCalculator.Calculate(move.NewOutput, move.OriginalOutput, out alpha, out beta, out errorState);
88
89      // prevent scaling to a constant value
90      if (beta.IsAlmost(0.0)) beta = 10E-5;
91
92      move.Alpha = alpha;
93      move.Beta = beta;
94
95      var oldBranch = move.Parent.GetSubtree(move.SubtreeIndex);
96      move.Parent.RemoveSubtree(move.SubtreeIndex);
97      move.Parent.InsertSubtree(move.SubtreeIndex, Scale(move.NewBranch, alpha, beta));
98
99      var problemData = ProblemDataParameter.ActualValue;
100      IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
101
102      var fitnessPartition = FitnessCalculationPartitionParameter.ActualValue;
103      var rows = Enumerable.Range(fitnessPartition.Start, fitnessPartition.End - fitnessPartition.Start);
104      var quality = evaluator.Evaluate(childContext, move.Tree, problemData, rows);
105
106      // revert changes to tree
107      move.Parent.RemoveSubtree(move.SubtreeIndex);
108      move.Parent.InsertSubtree(move.SubtreeIndex, oldBranch);
109
110      MoveQualityParameter.ActualValue = new DoubleValue(quality);
111      return base.Apply();
112    }
113
114    private Addition add = new Addition();
115    private Multiplication mul = new Multiplication();
116    private Constant constant = new Constant();
117
118    private ISymbolicExpressionTreeNode Scale(ISymbolicExpressionTreeNode node, double alpha, double beta) {
119      var constAlpha = (ConstantTreeNode)constant.CreateTreeNode();
120      var constBeta = (ConstantTreeNode)constant.CreateTreeNode();
121      constAlpha.Value = alpha;
122      constBeta.Value = beta;
123      var sum = add.CreateTreeNode();
124      var prod = mul.CreateTreeNode();
125      prod.AddSubtree(node); prod.AddSubtree(constBeta);
126      sum.AddSubtree(prod); sum.AddSubtree(constAlpha);
127      return sum;
128    }
129
130    public override IDeepCloneable Clone(Cloner cloner) {
131      return new ReplaceBranchMoveEvaluator(this, cloner);
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.