Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/SymbolicRegressionSolutionLinearScaler.cs @ 3683

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

Added 'special treatment' of operators that use LookupParameters instead of ScopeTreeLookupParameters contained in analyzers. #999

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.Linq;
23using HeuristicLab.Common;
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 HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
32using HeuristicLab.Problems.DataAnalysis.Symbolic;
33using System.Collections.Generic;
34using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
35using HeuristicLab.Problems.DataAnalysis;
36
37namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
38  /// <summary>
39  /// An operator that creates a linearly transformed symbolic regression solution (given alpha and beta).
40  /// </summary>
41  [Item("SymbolicRegressionSolutionLinearScaler", "An operator that creates a linearly transformed symbolic regression solution (given alpha and beta).")]
42  [StorableClass]
43  public sealed class SymbolicRegressionSolutionLinearScaler : SingleSuccessorOperator {
44    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
45    private const string ScaledSymbolicExpressionTreeParameterName = "ScaledSymbolicExpressionTree";
46    private const string AlphaParameterName = "Alpha";
47    private const string BetaParameterName = "Beta";
48
49    public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
50      get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
51    }
52    public ScopeTreeLookupParameter<SymbolicExpressionTree> ScaledSymbolicExpressionTreeParameter {
53      get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[ScaledSymbolicExpressionTreeParameterName]; }
54    }
55    public ScopeTreeLookupParameter<DoubleValue> AlphaParameter {
56      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters[AlphaParameterName]; }
57    }
58    public ScopeTreeLookupParameter<DoubleValue> BetaParameter {
59      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters[BetaParameterName]; }
60    }
61
62    public SymbolicRegressionSolutionLinearScaler()
63      : base() {
64      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to transform."));
65      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(ScaledSymbolicExpressionTreeParameterName, "The resulting symbolic expression trees after transformation."));
66      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(AlphaParameterName, "Alpha parameter for linear transformation."));
67      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(BetaParameterName, "Beta parameter for linear transformation."));
68    }
69
70    public override IOperation Apply() {
71      ItemArray<SymbolicExpressionTree> trees = SymbolicExpressionTreeParameter.ActualValue;
72      ItemArray<DoubleValue> alphas = AlphaParameter.ActualValue;
73      ItemArray<DoubleValue> betas = BetaParameter.ActualValue;
74      ItemArray<SymbolicExpressionTree> scaledTrees = new ItemArray<SymbolicExpressionTree>(trees.Length);
75      for (int i = 0; i < trees.Length; i++) {
76        var mainBranch = trees[i].Root.SubTrees[0].SubTrees[0];
77        var scaledMainBranch = MakeSum(MakeProduct(betas[i].Value, mainBranch), alphas[i].Value);
78
79        // remove the main branch before cloning to prevent cloning of sub-trees
80        trees[i].Root.SubTrees[0].RemoveSubTree(0);
81        var scaledTree = (SymbolicExpressionTree)trees[i].Clone();
82        // insert main branch into the original tree again
83        trees[i].Root.SubTrees[0].InsertSubTree(0, mainBranch);
84        // insert the scaled main branch into the cloned tree
85        scaledTree.Root.SubTrees[0].InsertSubTree(0, scaledMainBranch);
86        scaledTrees[i] = scaledTree;
87      }
88      ScaledSymbolicExpressionTreeParameter.ActualValue = scaledTrees;
89      return base.Apply();
90    }
91
92    private SymbolicExpressionTreeNode MakeSum(SymbolicExpressionTreeNode treeNode, double alpha) {
93      var node = (new Addition()).CreateTreeNode();
94      var alphaConst = MakeConstant(alpha);
95      node.AddSubTree(treeNode);
96      node.AddSubTree(alphaConst);
97      return node;
98    }
99
100    private SymbolicExpressionTreeNode MakeProduct(double beta, SymbolicExpressionTreeNode treeNode) {
101      var node = (new Multiplication()).CreateTreeNode();
102      var betaConst = MakeConstant(beta);
103      node.AddSubTree(treeNode);
104      node.AddSubTree(betaConst);
105      return node;
106    }
107
108    private SymbolicExpressionTreeNode MakeConstant(double c) {
109      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
110      node.Value = c;
111      return node;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.