Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.GP.StructureIdentification/3.4/Variable.cs @ 4695

Last change on this file since 4695 was 1914, checked in by epitzer, 16 years ago

Migration of DataAnalysis, GP, GP.StructureIdentification and Modeling to new Persistence-3.3 (#603)

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