Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.Networks/3.2/Symbols/OpenParameterFunctionTree.cs @ 2616

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

Initial commit of plugin for GP based network or equation modeling. #833

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