Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented #302 (Show variable names instead of var<index> in GP function trees).

  • Added a new operator that chooses a random value from a list of possible values.
  • Changed mutation operator for variables and differentials
  • Changed internal linear representation of function trees to support different types for local data.
File size: 7.8 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 minOffset;
41    private int maxOffset;
42
43    public override string Description {
44      get {
45        return @"Variable reads a value from a dataset, multiplies that value with a given factor and returns the result.
46The variable 'SampleOffset' can be used to read a value from previous or following rows.
47The index of the row that is actually read is SampleIndex+SampleOffset).";
48      }
49    }
50
51    public Variable()
52      : base() {
53      AddVariableInfo(new VariableInfo(INDEX, "The variable name", typeof(StringData), VariableKind.None));
54      GetVariableInfo(INDEX).Local = true;
55      AddVariableInfo(new VariableInfo(WEIGHT, "Weight is multiplied to the feature value", typeof(DoubleData), VariableKind.None));
56      GetVariableInfo(WEIGHT).Local = true;
57      AddVariableInfo(new VariableInfo(OFFSET, "SampleOffset is added to the sample index", typeof(ConstrainedIntData), VariableKind.None));
58      GetVariableInfo(OFFSET).Local = true;
59      AddVariableInfo(new VariableInfo(INITIALIZATION, "Initialization operator for variables", typeof(CombinedOperator), VariableKind.None));
60      GetVariableInfo(INITIALIZATION).Local = false;
61      AddVariableInfo(new VariableInfo(MANIPULATION, "Manipulation operator for variables", typeof(CombinedOperator), VariableKind.None));
62      GetVariableInfo(MANIPULATION).Local = false;
63
64      DoubleData weight = new DoubleData();
65      AddVariable(new HeuristicLab.Core.Variable(WEIGHT, weight));
66
67      StringData variable = new StringData();
68      AddVariable(new HeuristicLab.Core.Variable(INDEX, variable));
69
70      ConstrainedIntData sampleOffset = new ConstrainedIntData();
71      AddVariable(new HeuristicLab.Core.Variable(OFFSET, sampleOffset));
72
73      SetupInitialization();
74      SetupManipulation();
75
76      // variable can't have suboperators
77      AddConstraint(new NumberOfSubOperatorsConstraint(0, 0));
78    }
79
80    private void SetupInitialization() {
81      CombinedOperator combinedOp = new CombinedOperator();
82      SequentialProcessor seq = new SequentialProcessor();
83      UniformItemChooser variableRandomizer = new UniformItemChooser();
84      variableRandomizer.GetVariableInfo("Value").ActualName = INDEX;
85      variableRandomizer.GetVariableInfo("Values").ActualName = "InputVariables";
86      variableRandomizer.Name = "Variable randomizer";
87      NormalRandomizer weightRandomizer = new NormalRandomizer();
88      weightRandomizer.Mu = 0.0;
89      weightRandomizer.Sigma = 1.0;
90      weightRandomizer.GetVariableInfo("Value").ActualName = WEIGHT;
91      weightRandomizer.Name = "Weight Randomizer";
92      UniformRandomizer offsetRandomizer = new UniformRandomizer();
93      offsetRandomizer.Min = minOffset;
94      offsetRandomizer.Max = maxOffset + 1;
95      offsetRandomizer.GetVariableInfo("Value").ActualName = OFFSET;
96      offsetRandomizer.Name = "Offset Randomizer";
97
98      combinedOp.OperatorGraph.AddOperator(seq);
99      combinedOp.OperatorGraph.AddOperator(variableRandomizer);
100      combinedOp.OperatorGraph.AddOperator(weightRandomizer);
101      combinedOp.OperatorGraph.AddOperator(offsetRandomizer);
102      combinedOp.OperatorGraph.InitialOperator = seq;
103      seq.AddSubOperator(variableRandomizer);
104      seq.AddSubOperator(weightRandomizer);
105      seq.AddSubOperator(offsetRandomizer);
106      HeuristicLab.Core.IVariable initOp = GetVariable(INITIALIZATION);
107      if(initOp == null) {
108        AddVariable(new HeuristicLab.Core.Variable(INITIALIZATION, combinedOp));
109      } else {
110        initOp.Value = combinedOp;
111      }
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      HeuristicLab.Core.IVariable manipulationOp = GetVariable(MANIPULATION);
142      if(manipulationOp == null) {
143        AddVariable(new HeuristicLab.Core.Variable(MANIPULATION, combinedOp));
144      } else {
145        manipulationOp.Value = combinedOp;
146      }
147    }
148
149    public void SetConstraints(int minSampleOffset, int maxSampleOffset) {
150      ConstrainedIntData offset = GetVariableValue<ConstrainedIntData>(OFFSET, null, false);
151      IntBoundedConstraint offsetConstraint = new IntBoundedConstraint();
152      this.minOffset = minSampleOffset;
153      this.maxOffset = maxSampleOffset;
154      offsetConstraint.LowerBound = minSampleOffset;
155      offsetConstraint.LowerBoundEnabled = true;
156      offsetConstraint.LowerBoundIncluded = true;
157      offsetConstraint.UpperBound = maxSampleOffset;
158      offsetConstraint.UpperBoundEnabled = true;
159      offsetConstraint.UpperBoundIncluded = true;
160      offset.AddConstraint(offsetConstraint);
161
162      //ConstrainedIntData index = GetVariableValue<ConstrainedIntData>(INDEX, null, false);
163      //IntBoundedConstraint indexConstraint = new IntBoundedConstraint();
164      //minIndex = minInputIndex;
165      //maxIndex = maxInputIndex;
166      //indexConstraint.LowerBound = minInputIndex;
167      //indexConstraint.LowerBoundEnabled = true;
168      //indexConstraint.LowerBoundIncluded = true;
169      //indexConstraint.UpperBound = maxInputIndex;
170      //indexConstraint.UpperBoundEnabled = true;
171      //indexConstraint.UpperBoundIncluded = true;
172      //index.AddConstraint(indexConstraint);
173
174      SetupInitialization();
175      SetupManipulation();
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.