Free cookie consent management tool by TermsFeed Policy Generator

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

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

Removed range constraint on GP variable weights and constant values. #710

File size: 8.1 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 : FunctionBase {
35
36    public const string WEIGHT = "Weight";
37    public const string OFFSET = "SampleOffset";
38    public const string INDEX = "Variable";
39
40    private int minIndex;
41    private int maxIndex;
42    private int minOffset;
43    private int maxOffset;
44
45    public override string Description {
46      get {
47        return @"Variable reads a value from a dataset, multiplies that value with a given factor and returns the result.
48The variable 'SampleOffset' can be used to read a value from previous or following rows.
49The index of the row that is actually read is SampleIndex+SampleOffset).";
50      }
51    }
52
53    public Variable()
54      : base() {
55      AddVariableInfo(new VariableInfo(INDEX, "Index of the variable in the dataset representing this feature", typeof(ConstrainedIntData), VariableKind.None));
56      GetVariableInfo(INDEX).Local = true;
57      AddVariableInfo(new VariableInfo(WEIGHT, "Weight is multiplied to the feature value", typeof(ConstrainedDoubleData), VariableKind.None));
58      GetVariableInfo(WEIGHT).Local = true;
59      AddVariableInfo(new VariableInfo(OFFSET, "SampleOffset is added to the sample index", typeof(ConstrainedIntData), VariableKind.None));
60      GetVariableInfo(OFFSET).Local = true;
61      AddVariableInfo(new VariableInfo(INITIALIZATION, "Initialization operator for variables", typeof(CombinedOperator), VariableKind.None));
62      GetVariableInfo(INITIALIZATION).Local = false;
63      AddVariableInfo(new VariableInfo(MANIPULATION, "Manipulation operator for variables", typeof(CombinedOperator), VariableKind.None));
64      GetVariableInfo(MANIPULATION).Local = false;
65
66      DoubleData weight = new DoubleData();
67      AddVariable(new HeuristicLab.Core.Variable(WEIGHT, weight));
68
69      ConstrainedIntData variable = new ConstrainedIntData();
70      AddVariable(new HeuristicLab.Core.Variable(INDEX, variable));
71      minIndex = 0; maxIndex = 1000;
72
73      ConstrainedIntData sampleOffset = new ConstrainedIntData();
74      AddVariable(new HeuristicLab.Core.Variable(OFFSET, sampleOffset));
75
76      SetupInitialization();
77      SetupManipulation();
78
79      // variable can't have suboperators
80      AddConstraint(new NumberOfSubOperatorsConstraint(0, 0));
81    }
82
83    private void SetupInitialization() {
84      CombinedOperator combinedOp = new CombinedOperator();
85      SequentialProcessor seq = new SequentialProcessor();
86      UniformRandomizer indexRandomizer = new UniformRandomizer();
87      indexRandomizer.Min = minIndex;
88      indexRandomizer.Max = maxIndex + 1; // uniform randomizer generates numbers in the range [min, max[
89      indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;
90      indexRandomizer.Name = "Index Randomizer";
91      NormalRandomizer weightRandomizer = new NormalRandomizer();
92      weightRandomizer.Mu = 0.0;
93      weightRandomizer.Sigma = 1.0;
94      weightRandomizer.GetVariableInfo("Value").ActualName = WEIGHT;
95      weightRandomizer.Name = "Weight Randomizer";
96      UniformRandomizer offsetRandomizer = new UniformRandomizer();
97      offsetRandomizer.Min = minOffset;
98      offsetRandomizer.Max = maxOffset + 1;
99      offsetRandomizer.GetVariableInfo("Value").ActualName = OFFSET;
100      offsetRandomizer.Name = "Offset Randomizer";
101
102      combinedOp.OperatorGraph.AddOperator(seq);
103      combinedOp.OperatorGraph.AddOperator(indexRandomizer);
104      combinedOp.OperatorGraph.AddOperator(weightRandomizer);
105      combinedOp.OperatorGraph.AddOperator(offsetRandomizer);
106      combinedOp.OperatorGraph.InitialOperator = seq;
107      seq.AddSubOperator(indexRandomizer);
108      seq.AddSubOperator(weightRandomizer);
109      seq.AddSubOperator(offsetRandomizer);
110      HeuristicLab.Core.IVariable initOp = GetVariable(INITIALIZATION);
111      if(initOp == null) {
112        AddVariable(new HeuristicLab.Core.Variable(INITIALIZATION, combinedOp));
113      } else {
114        initOp.Value = combinedOp;
115      }
116    }
117
118    private void SetupManipulation() {
119      // manipulation operator
120      CombinedOperator combinedOp = new CombinedOperator();
121      SequentialProcessor seq = new SequentialProcessor();
122      UniformRandomizer indexRandomizer = new UniformRandomizer();
123      indexRandomizer.Min = minIndex;
124      indexRandomizer.Max = maxIndex + 1;
125      indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;
126      indexRandomizer.Name = "Index Randomizer";
127      NormalRandomAdder weightRandomAdder = new NormalRandomAdder();
128      weightRandomAdder.Mu = 0.0;
129      weightRandomAdder.Sigma = 1.0;
130      weightRandomAdder.GetVariableInfo("Value").ActualName = WEIGHT;
131      weightRandomAdder.Name = "Weight Adder";
132      NormalRandomAdder offsetRandomAdder = new NormalRandomAdder();
133      offsetRandomAdder.Mu = 0.0;
134      offsetRandomAdder.Sigma = 1.0;
135      offsetRandomAdder.GetVariableInfo("Value").ActualName = OFFSET;
136      offsetRandomAdder.Name = "Offset Adder";
137
138      combinedOp.OperatorGraph.AddOperator(seq);
139      combinedOp.OperatorGraph.AddOperator(indexRandomizer);
140      combinedOp.OperatorGraph.AddOperator(weightRandomAdder);
141      combinedOp.OperatorGraph.AddOperator(offsetRandomAdder);
142      combinedOp.OperatorGraph.InitialOperator = seq;
143      seq.AddSubOperator(indexRandomizer);
144      seq.AddSubOperator(weightRandomAdder);
145      seq.AddSubOperator(offsetRandomAdder);
146      HeuristicLab.Core.IVariable manipulationOp = GetVariable(MANIPULATION);
147      if(manipulationOp == null) {
148        AddVariable(new HeuristicLab.Core.Variable(MANIPULATION, combinedOp));
149      } else {
150        manipulationOp.Value = combinedOp;
151      }
152    }
153
154    public void SetConstraints(int minInputIndex, int maxInputIndex, int minSampleOffset, int maxSampleOffset) {
155      ConstrainedIntData offset = GetVariableValue<ConstrainedIntData>(OFFSET, null, false);
156      IntBoundedConstraint offsetConstraint = new IntBoundedConstraint();
157      this.minOffset = minSampleOffset;
158      this.maxOffset = maxSampleOffset;
159      offsetConstraint.LowerBound = minSampleOffset;
160      offsetConstraint.LowerBoundEnabled = true;
161      offsetConstraint.LowerBoundIncluded = true;
162      offsetConstraint.UpperBound = maxSampleOffset;
163      offsetConstraint.UpperBoundEnabled = true;
164      offsetConstraint.UpperBoundIncluded = true;
165      offset.AddConstraint(offsetConstraint);
166
167      ConstrainedIntData index = GetVariableValue<ConstrainedIntData>(INDEX, null, false);
168      IntBoundedConstraint indexConstraint = new IntBoundedConstraint();
169      minIndex = minInputIndex;
170      maxIndex = maxInputIndex;
171      indexConstraint.LowerBound = minInputIndex;
172      indexConstraint.LowerBoundEnabled = true;
173      indexConstraint.LowerBoundIncluded = true;
174      indexConstraint.UpperBound = maxInputIndex;
175      indexConstraint.UpperBoundEnabled = true;
176      indexConstraint.UpperBoundIncluded = true;
177      index.AddConstraint(indexConstraint);
178
179      SetupInitialization();
180      SetupManipulation();
181    }
182  }
183}
Note: See TracBrowser for help on using the repository browser.