Free cookie consent management tool by TermsFeed Policy Generator

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

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

Removed max. and min. time offset constraints as algorithm parameters and from all engines. The time constraints were added to the relevant terminal symbols (variable & differential) instead. The time offset constraint can be changed by editing the symbols in the function library. #880 (Max and min time offsets for variable symbols are not set correctly by FunctionLibraryInjectors)

File size: 7.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 HeuristicLab.GP.Interfaces;
23using HeuristicLab.Operators;
24using HeuristicLab.Random;
25using HeuristicLab.Data;
26using System.Xml;
27
28namespace HeuristicLab.GP.StructureIdentification {
29  public class Variable : Terminal {
30    public const string WEIGHT = "Weight";
31    public const string OFFSET = "SampleOffset";
32    public const string VARIABLENAME = "Variable";
33
34    private int minOffset;
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
48    private int maxOffset;
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    }
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
70
71    public override IFunctionTree GetTreeNode() {
72      return new VariableFunctionTree(this);
73    }
74
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();
84      UniformItemChooser variableRandomizer = new UniformItemChooser();
85      variableRandomizer.GetVariableInfo("Value").ActualName = VARIABLENAME;
86      variableRandomizer.GetVariableInfo("Values").ActualName = "InputVariables";
87      variableRandomizer.Name = "Variable randomizer";
88      NormalRandomizer weightRandomizer = new NormalRandomizer();
89      weightRandomizer.Mu = 0.0;
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);
100      combinedOp.OperatorGraph.AddOperator(variableRandomizer);
101      combinedOp.OperatorGraph.AddOperator(weightRandomizer);
102      combinedOp.OperatorGraph.AddOperator(offsetRandomizer);
103      combinedOp.OperatorGraph.InitialOperator = seq;
104      seq.AddSubOperator(variableRandomizer);
105      seq.AddSubOperator(weightRandomizer);
106      seq.AddSubOperator(offsetRandomizer);
107      Initializer = combinedOp;
108    }
109
110    private void SetupManipulation() {
111      // manipulation operator
112      CombinedOperator combinedOp = new CombinedOperator();
113      SequentialProcessor seq = new SequentialProcessor();
114      UniformItemChooser variableRandomizer = new UniformItemChooser();
115      variableRandomizer.GetVariableInfo("Value").ActualName = VARIABLENAME;
116      variableRandomizer.GetVariableInfo("Values").ActualName = "InputVariables";
117      variableRandomizer.Name = "Variable randomizer";
118      NormalRandomAdder weightRandomAdder = new NormalRandomAdder();
119      weightRandomAdder.Mu = 0.0;
120      weightRandomAdder.Sigma = 1.0;
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";
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)));
132
133      combinedOp.OperatorGraph.AddOperator(seq);
134      combinedOp.OperatorGraph.AddOperator(variableRandomizer);
135      combinedOp.OperatorGraph.AddOperator(weightRandomAdder);
136      combinedOp.OperatorGraph.AddOperator(offsetRandomAdder);
137      combinedOp.OperatorGraph.InitialOperator = seq;
138      seq.AddSubOperator(variableRandomizer);
139      seq.AddSubOperator(weightRandomAdder);
140      seq.AddSubOperator(offsetRandomAdder);
141      Manipulator = combinedOp;
142    }
143
144    public override HeuristicLab.Core.IView CreateView() {
145      return new VariableView(this);
146    }
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);
167      MinTimeOffset = XmlConvert.ToInt32(node.Attributes["MinTimeOffset"].Value);
168      MaxTimeOffset = XmlConvert.ToInt32(node.Attributes["MaxTimeOffset"].Value);
169    }
170    #endregion
171  }
172}
Note: See TracBrowser for help on using the repository browser.