Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Symbols/Variable.cs @ 3043

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

Fixed a bug in loading of persisted function libraries. #880 (Max and min time offsets for variable symbols are not set correctly by FunctionLibraryInjectors)

File size: 7.2 KB
RevLine 
[645]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
[2212]22using HeuristicLab.GP.Interfaces;
23using HeuristicLab.Operators;
[645]24using HeuristicLab.Random;
[2365]25using HeuristicLab.Data;
[2843]26using System.Xml;
[645]27
28namespace HeuristicLab.GP.StructureIdentification {
[2365]29  public class Variable : Terminal {
[645]30    public const string WEIGHT = "Weight";
31    public const string OFFSET = "SampleOffset";
[2210]32    public const string VARIABLENAME = "Variable";
[645]33
34    private int minOffset;
[2843]35    public int MinTimeOffset {
36      get {
37        return minOffset;
38      }
39      set {
40        if (value != minOffset) {
41          minOffset = value;
42          SetupInitialization();
43          SetupManipulation();
44        }
45      }
46    }
47
[645]48    private int maxOffset;
[2843]49    public int MaxTimeOffset {
50      get {
51        return maxOffset;
52      }
53      set {
54        if (value != maxOffset) {
55          maxOffset = value;
56          SetupManipulation();
57          SetupInitialization();
58        }
59      }
60    }
[645]61
62    public override string Description {
63      get {
64        return @"Variable reads a value from a dataset, multiplies that value with a given factor and returns the result.
65The variable 'SampleOffset' can be used to read a value from previous or following rows.
66The index of the row that is actually read is SampleIndex+SampleOffset).";
67      }
68    }
69
[2218]70
[2202]71    public override IFunctionTree GetTreeNode() {
[2210]72      return new VariableFunctionTree(this);
[2202]73    }
74
[645]75    public Variable()
76      : base() {
77      SetupInitialization();
78      SetupManipulation();
79    }
80
81    private void SetupInitialization() {
82      CombinedOperator combinedOp = new CombinedOperator();
83      SequentialProcessor seq = new SequentialProcessor();
[2174]84      UniformItemChooser variableRandomizer = new UniformItemChooser();
[2210]85      variableRandomizer.GetVariableInfo("Value").ActualName = VARIABLENAME;
[2174]86      variableRandomizer.GetVariableInfo("Values").ActualName = "InputVariables";
87      variableRandomizer.Name = "Variable randomizer";
[645]88      NormalRandomizer weightRandomizer = new NormalRandomizer();
[1618]89      weightRandomizer.Mu = 0.0;
[645]90      weightRandomizer.Sigma = 1.0;
91      weightRandomizer.GetVariableInfo("Value").ActualName = WEIGHT;
92      weightRandomizer.Name = "Weight Randomizer";
93      UniformRandomizer offsetRandomizer = new UniformRandomizer();
94      offsetRandomizer.Min = minOffset;
95      offsetRandomizer.Max = maxOffset + 1;
96      offsetRandomizer.GetVariableInfo("Value").ActualName = OFFSET;
97      offsetRandomizer.Name = "Offset Randomizer";
98
99      combinedOp.OperatorGraph.AddOperator(seq);
[2174]100      combinedOp.OperatorGraph.AddOperator(variableRandomizer);
[645]101      combinedOp.OperatorGraph.AddOperator(weightRandomizer);
102      combinedOp.OperatorGraph.AddOperator(offsetRandomizer);
103      combinedOp.OperatorGraph.InitialOperator = seq;
[2174]104      seq.AddSubOperator(variableRandomizer);
[645]105      seq.AddSubOperator(weightRandomizer);
106      seq.AddSubOperator(offsetRandomizer);
[2202]107      Initializer = combinedOp;
[645]108    }
109
110    private void SetupManipulation() {
111      // manipulation operator
112      CombinedOperator combinedOp = new CombinedOperator();
113      SequentialProcessor seq = new SequentialProcessor();
[2174]114      UniformItemChooser variableRandomizer = new UniformItemChooser();
[2210]115      variableRandomizer.GetVariableInfo("Value").ActualName = VARIABLENAME;
[2174]116      variableRandomizer.GetVariableInfo("Values").ActualName = "InputVariables";
117      variableRandomizer.Name = "Variable randomizer";
[645]118      NormalRandomAdder weightRandomAdder = new NormalRandomAdder();
119      weightRandomAdder.Mu = 0.0;
[1618]120      weightRandomAdder.Sigma = 1.0;
[645]121      weightRandomAdder.GetVariableInfo("Value").ActualName = WEIGHT;
122      weightRandomAdder.Name = "Weight Adder";
123      NormalRandomAdder offsetRandomAdder = new NormalRandomAdder();
124      offsetRandomAdder.Mu = 0.0;
125      offsetRandomAdder.Sigma = 1.0;
126      offsetRandomAdder.GetVariableInfo("Value").ActualName = OFFSET;
127      offsetRandomAdder.Name = "Offset Adder";
[2365]128      offsetRandomAdder.GetVariableInfo("MinValue").Local = true;
129      offsetRandomAdder.AddVariable(new HeuristicLab.Core.Variable("MinValue", new DoubleData(minOffset)));
130      offsetRandomAdder.GetVariableInfo("MaxValue").Local = true;
131      offsetRandomAdder.AddVariable(new HeuristicLab.Core.Variable("MaxValue", new DoubleData(maxOffset + 1)));
[645]132
133      combinedOp.OperatorGraph.AddOperator(seq);
[2174]134      combinedOp.OperatorGraph.AddOperator(variableRandomizer);
[645]135      combinedOp.OperatorGraph.AddOperator(weightRandomAdder);
136      combinedOp.OperatorGraph.AddOperator(offsetRandomAdder);
137      combinedOp.OperatorGraph.InitialOperator = seq;
[2174]138      seq.AddSubOperator(variableRandomizer);
[645]139      seq.AddSubOperator(weightRandomAdder);
140      seq.AddSubOperator(offsetRandomAdder);
[2202]141      Manipulator = combinedOp;
[645]142    }
143
[2843]144    public override HeuristicLab.Core.IView CreateView() {
145      return new VariableView(this);
[645]146    }
[2843]147
148    #region persistence
149    public override object Clone(System.Collections.Generic.IDictionary<System.Guid, object> clonedObjects) {
150      Variable clone = (Variable)base.Clone(clonedObjects);
151      clone.MaxTimeOffset = MaxTimeOffset;
152      clone.MinTimeOffset = MinTimeOffset;
153      return clone;
154    }
155    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, System.Collections.Generic.IDictionary<System.Guid, HeuristicLab.Core.IStorable> persistedObjects) {
156      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
157      var minTimeOffsetAttr = document.CreateAttribute("MinTimeOffset");
158      minTimeOffsetAttr.Value = MinTimeOffset.ToString();
159      var maxTimeOffsetAttr = document.CreateAttribute("MaxTimeOffset");
160      maxTimeOffsetAttr.Value = MaxTimeOffset.ToString();
161      node.Attributes.Append(minTimeOffsetAttr);
162      node.Attributes.Append(maxTimeOffsetAttr);
163      return node;
164    }
165    public override void Populate(System.Xml.XmlNode node, System.Collections.Generic.IDictionary<System.Guid, HeuristicLab.Core.IStorable> restoredObjects) {
166      base.Populate(node, restoredObjects);
[2910]167      if (node.Attributes["MinTimeOffset"] != null)
168        MinTimeOffset = XmlConvert.ToInt32(node.Attributes["MinTimeOffset"].Value);
169      else MinTimeOffset = 0;
170      if (node.Attributes["MaxTimeOffset"] != null)
171        MaxTimeOffset = XmlConvert.ToInt32(node.Attributes["MaxTimeOffset"].Value);
172      else MaxTimeOffset = 0;
[2843]173    }
174    #endregion
[645]175  }
176}
Note: See TracBrowser for help on using the repository browser.