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