Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/Symbols/ConstantFunctionTree.cs @ 2212

Last change on this file since 2212 was 2212, checked in by gkronber, 15 years ago

GP Refactoring: #713

  • added project GP.Operators
  • moved operators from plugin GP to plugin GP.Operators
  • deleted unused constraints
  • removed dependency of GP plugins on Constraints plugin
  • moved StructID functions into directory Symbols
  • deleted unused class FunView
  • implemented add and remove functionality for the FunctionLibraryView
File size: 2.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.Generic;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.GP.Interfaces;
26
27namespace HeuristicLab.GP.StructureIdentification {
28  public class ConstantFunctionTree : FunctionTreeBase {
29    private static readonly IList<IFunctionTree> subTrees = new List<IFunctionTree>().AsReadOnly();
30    public double Value { get; set; }
31
32    public ConstantFunctionTree(Constant constant) {
33      Function = constant;
34    }
35
36    protected ConstantFunctionTree(ConstantFunctionTree original) {
37      Function = original.Function;
38      Value = original.Value;
39    }
40
41    public override IList<IFunctionTree> SubTrees {
42      get {
43        return subTrees;
44      }
45    }
46
47    public override bool HasLocalParameters {
48      get {
49        return true;
50      }
51    }
52
53    public override IOperation CreateShakingOperation(IScope scope) {
54      Scope myVariableScope = new Scope();
55      scope.AddSubScope(myVariableScope);
56      myVariableScope.AddVariable(CreateValueVariable());
57      return new AtomicOperation(Function.Manipulator, myVariableScope);
58    }
59
60    public override IOperation CreateInitOperation(IScope scope) {
61      Scope myVariableScope = new Scope();
62      scope.AddSubScope(myVariableScope);
63      myVariableScope.AddVariable(CreateValueVariable());
64      return new AtomicOperation(Function.Initializer, myVariableScope);
65    }
66
67    public override IFunctionTree Clone() {
68      return new ConstantFunctionTree(this);
69    }
70
71    private IVariable CreateValueVariable() {
72      DoubleData data = new DoubleData(Value);
73      data.Changed += (sender, args) => Value = data.Data;
74      var variable = new HeuristicLab.Core.Variable(Constant.VALUE, data);
75      variable.ValueChanged += (sender, args) => Value = ((DoubleData)variable.Value).Data;
76      return variable;
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.