Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/Variable.cs @ 2202

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

Created a branch for #713

File size: 7.4 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;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using System.Diagnostics;
27using HeuristicLab.Data;
28using HeuristicLab.Constraints;
29using HeuristicLab.DataAnalysis;
30using HeuristicLab.Random;
31using HeuristicLab.Operators;
32
33namespace HeuristicLab.GP.StructureIdentification {
34  public class Variable : Terminal {   
35    public const string WEIGHT = "Weight";
36    public const string OFFSET = "SampleOffset";
37    public const string INDEX = "Variable";
38
39    private BakedFunctionTree variableNodeTemplate;
40
41    private int minOffset;
42    private int maxOffset;
43
44    public override string Description {
45      get {
46        return @"Variable reads a value from a dataset, multiplies that value with a given factor and returns the result.
47The variable 'SampleOffset' can be used to read a value from previous or following rows.
48The index of the row that is actually read is SampleIndex+SampleOffset).";
49      }
50    }
51
52    public override IEnumerable<string> LocalParameterNames {
53      get {
54        return new string[] { WEIGHT, OFFSET, INDEX };
55      }
56    }
57
58    public override IFunctionTree GetTreeNode() {
59      return (IFunctionTree)variableNodeTemplate.Clone();
60    }
61
62    public Variable()
63      : base() {
64      //AddVariableInfo(new VariableInfo(INDEX, "The variable name", typeof(StringData), VariableKind.None));
65      //GetVariableInfo(INDEX).Local = true;
66      //AddVariableInfo(new VariableInfo(WEIGHT, "Weight is multiplied to the feature value", typeof(DoubleData), VariableKind.None));
67      //GetVariableInfo(WEIGHT).Local = true;
68      //AddVariableInfo(new VariableInfo(OFFSET, "SampleOffset is added to the sample index", typeof(ConstrainedIntData), VariableKind.None));
69      //GetVariableInfo(OFFSET).Local = true;
70      variableNodeTemplate = new BakedFunctionTree(this);
71
72      DoubleData weight = new DoubleData();
73      variableNodeTemplate.AddVariable(new HeuristicLab.Core.Variable(WEIGHT, weight));
74
75      StringData variable = new StringData();
76      variableNodeTemplate.AddVariable(new HeuristicLab.Core.Variable(INDEX, variable));
77
78      ConstrainedIntData sampleOffset = new ConstrainedIntData();
79      variableNodeTemplate.AddVariable(new HeuristicLab.Core.Variable(OFFSET, sampleOffset));
80
81      SetupInitialization();
82      SetupManipulation();
83    }
84
85    private void SetupInitialization() {
86      CombinedOperator combinedOp = new CombinedOperator();
87      SequentialProcessor seq = new SequentialProcessor();
88      UniformItemChooser variableRandomizer = new UniformItemChooser();
89      variableRandomizer.GetVariableInfo("Value").ActualName = INDEX;
90      variableRandomizer.GetVariableInfo("Values").ActualName = "InputVariables";
91      variableRandomizer.Name = "Variable randomizer";
92      NormalRandomizer weightRandomizer = new NormalRandomizer();
93      weightRandomizer.Mu = 0.0;
94      weightRandomizer.Sigma = 1.0;
95      weightRandomizer.GetVariableInfo("Value").ActualName = WEIGHT;
96      weightRandomizer.Name = "Weight Randomizer";
97      UniformRandomizer offsetRandomizer = new UniformRandomizer();
98      offsetRandomizer.Min = minOffset;
99      offsetRandomizer.Max = maxOffset + 1;
100      offsetRandomizer.GetVariableInfo("Value").ActualName = OFFSET;
101      offsetRandomizer.Name = "Offset Randomizer";
102
103      combinedOp.OperatorGraph.AddOperator(seq);
104      combinedOp.OperatorGraph.AddOperator(variableRandomizer);
105      combinedOp.OperatorGraph.AddOperator(weightRandomizer);
106      combinedOp.OperatorGraph.AddOperator(offsetRandomizer);
107      combinedOp.OperatorGraph.InitialOperator = seq;
108      seq.AddSubOperator(variableRandomizer);
109      seq.AddSubOperator(weightRandomizer);
110      seq.AddSubOperator(offsetRandomizer);
111      Initializer = combinedOp;
112    }
113
114    private void SetupManipulation() {
115      // manipulation operator
116      CombinedOperator combinedOp = new CombinedOperator();
117      SequentialProcessor seq = new SequentialProcessor();
118      UniformItemChooser variableRandomizer = new UniformItemChooser();
119      variableRandomizer.GetVariableInfo("Value").ActualName = INDEX;
120      variableRandomizer.GetVariableInfo("Values").ActualName = "InputVariables";
121      variableRandomizer.Name = "Variable randomizer";
122      NormalRandomAdder weightRandomAdder = new NormalRandomAdder();
123      weightRandomAdder.Mu = 0.0;
124      weightRandomAdder.Sigma = 1.0;
125      weightRandomAdder.GetVariableInfo("Value").ActualName = WEIGHT;
126      weightRandomAdder.Name = "Weight Adder";
127      NormalRandomAdder offsetRandomAdder = new NormalRandomAdder();
128      offsetRandomAdder.Mu = 0.0;
129      offsetRandomAdder.Sigma = 1.0;
130      offsetRandomAdder.GetVariableInfo("Value").ActualName = OFFSET;
131      offsetRandomAdder.Name = "Offset Adder";
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 void SetConstraints(int minSampleOffset, int maxSampleOffset) {
145      ConstrainedIntData offset = (ConstrainedIntData)variableNodeTemplate.GetLocalVariable(OFFSET).Value;
146      IntBoundedConstraint offsetConstraint = new IntBoundedConstraint();
147      this.minOffset = minSampleOffset;
148      this.maxOffset = maxSampleOffset;
149      offsetConstraint.LowerBound = minSampleOffset;
150      offsetConstraint.LowerBoundEnabled = true;
151      offsetConstraint.LowerBoundIncluded = true;
152      offsetConstraint.UpperBound = maxSampleOffset;
153      offsetConstraint.UpperBoundEnabled = true;
154      offsetConstraint.UpperBoundIncluded = true;
155      offset.AddConstraint(offsetConstraint);
156
157      //ConstrainedIntData index = GetVariableValue<ConstrainedIntData>(INDEX, null, false);
158      //IntBoundedConstraint indexConstraint = new IntBoundedConstraint();
159      //minIndex = minInputIndex;
160      //maxIndex = maxInputIndex;
161      //indexConstraint.LowerBound = minInputIndex;
162      //indexConstraint.LowerBoundEnabled = true;
163      //indexConstraint.LowerBoundIncluded = true;
164      //indexConstraint.UpperBound = maxInputIndex;
165      //indexConstraint.UpperBoundEnabled = true;
166      //indexConstraint.UpperBoundIncluded = true;
167      //index.AddConstraint(indexConstraint);
168
169      SetupInitialization();
170      SetupManipulation();
171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.