Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Symbols/VariableFunctionTree.cs @ 2770

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

Fixed #323 (The default representation of FunctionTreeView should show coefficients and time-offsets of variables and values of constants).

File size: 4.9 KB
RevLine 
[2210]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;
[2218]26using System.Xml;
[2210]27
28namespace HeuristicLab.GP.StructureIdentification {
[2216]29  public class VariableFunctionTree : TerminalTreeNode {
[2210]30    public double Weight { get; set; }
31    public string VariableName { get; set; }
32    public int SampleOffset { get; set; }
33
[2216]34    public VariableFunctionTree(Variable variable)
35      : base(variable) {
[2210]36    }
37
[2216]38    protected VariableFunctionTree(VariableFunctionTree original)
39      : base(original) {
[2210]40      Weight = original.Weight;
41      VariableName = original.VariableName;
42      SampleOffset = original.SampleOffset;
43    }
44
45    public override bool HasLocalParameters {
46      get {
47        return true;
48      }
49    }
50
51    public override IOperation CreateShakingOperation(IScope scope) {
52      Scope myVariableScope = new Scope();
53      scope.AddSubScope(myVariableScope);
54      myVariableScope.AddVariable(CreateWeightVariable());
55      myVariableScope.AddVariable(CreateVariableNameVariable());
56      myVariableScope.AddVariable(CreateSampleOffsetVariable());
57      return new AtomicOperation(Function.Manipulator, myVariableScope);
58    }
59
60    public override IOperation CreateInitOperation(IScope scope) {
61      Scope myVariableScope = new Scope();
[2216]62      scope.AddSubScope(myVariableScope);
[2210]63      myVariableScope.AddVariable(CreateWeightVariable());
64      myVariableScope.AddVariable(CreateVariableNameVariable());
65      myVariableScope.AddVariable(CreateSampleOffsetVariable());
66      return new AtomicOperation(Function.Initializer, myVariableScope);
67    }
68
[2235]69    public override string ToString() {
70      return SymbolicExpressionExporter.GetName(this);
71    }
72
73    #region serialization
[2218]74    public override object Clone() {
[2210]75      return new VariableFunctionTree(this);
76    }
77
[2218]78    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<System.Guid, IStorable> persistedObjects) {
[2219]79      XmlNode node = document.CreateElement(name);
[2218]80      XmlAttribute weightAttr = document.CreateAttribute("Weight");
81      weightAttr.Value = XmlConvert.ToString(Weight);
82      XmlAttribute variableAttr = document.CreateAttribute("Variable");
83      variableAttr.Value = VariableName;
84      XmlAttribute sampleOffsetAttr = document.CreateAttribute("SampleOffset");
85      sampleOffsetAttr.Value = XmlConvert.ToString(SampleOffset);
86      node.Attributes.Append(weightAttr);
87      node.Attributes.Append(sampleOffsetAttr);
88      node.Attributes.Append(variableAttr);
89      return node;
90    }
91
92    public override void Populate(XmlNode node, IDictionary<System.Guid, IStorable> restoredObjects) {
93      base.Populate(node, restoredObjects);
94      Weight = XmlConvert.ToDouble(node.Attributes["Weight"].Value);
95      SampleOffset = XmlConvert.ToInt32(node.Attributes["SampleOffset"].Value);
96      VariableName = node.Attributes["Variable"].Value;
97    }
[2235]98    #endregion
[2218]99
[2210]100    private IVariable CreateSampleOffsetVariable() {
101      IntData data = new IntData(SampleOffset);
102      data.Changed += (sender, args) => SampleOffset = data.Data;
103      var variable = new HeuristicLab.Core.Variable(Variable.OFFSET, data);
104      variable.ValueChanged += (sender, args) => SampleOffset = ((IntData)variable.Value).Data;
105      return variable;
106    }
107
108    private IVariable CreateVariableNameVariable() {
109      StringData data = new StringData(VariableName);
110      data.Changed += (sender, args) => VariableName = data.Data;
111      var variable = new HeuristicLab.Core.Variable(Variable.VARIABLENAME, data);
112      variable.ValueChanged += (sender, args) => VariableName = ((StringData)variable.Value).Data;
113      return variable;
114    }
115
116    private IVariable CreateWeightVariable() {
117      DoubleData data = new DoubleData(Weight);
118      data.Changed += (sender, args) => Weight = data.Data;
119      var variable = new HeuristicLab.Core.Variable(Variable.WEIGHT, data);
120      variable.ValueChanged += (sender, args) => Weight = ((DoubleData)variable.Value).Data;
121      return variable;
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.