Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added weights for open parameters. #833 (Genetic Programming based search for equations (modeling with multiple target variables))

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