Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Functions/Variable.cs @ 428

Last change on this file since 428 was 425, checked in by gkronber, 16 years ago

moved creation of default init and manipulation operators from !GPOperatorGroup into the functions (ticket #225 "Simplify GP infrastructure (GPOperatorLibrary and Functions)")

File size: 6.6 KB
RevLine 
[2]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;
[425]30using HeuristicLab.Random;
31using HeuristicLab.Operators;
[2]32
33namespace HeuristicLab.Functions {
[229]34  public sealed class Variable : FunctionBase {
[2]35
[177]36    public const string WEIGHT = "Weight";
37    public const string OFFSET = "SampleOffset";
38    public const string INDEX = "Variable";
[2]39
40    public override string Description {
[229]41      get {
42        return @"Variable reads a value from a dataset, multiplies that value with a given factor and returns the result.
[2]43The variable 'SampleOffset' can be used to read a value from previous or following rows.
[229]44The index of the row that is actually read is SampleIndex+SampleOffset).";
45      }
[2]46    }
47
48    public Variable()
49      : base() {
[155]50      AddVariableInfo(new VariableInfo(INDEX, "Index of the variable in the dataset representing this feature", typeof(ConstrainedIntData), VariableKind.None));
51      GetVariableInfo(INDEX).Local = true;
52      AddVariableInfo(new VariableInfo(WEIGHT, "Weight is multiplied to the feature value", typeof(ConstrainedDoubleData), VariableKind.None));
53      GetVariableInfo(WEIGHT).Local = true;
54      AddVariableInfo(new VariableInfo(OFFSET, "SampleOffset is added to the sample index", typeof(ConstrainedIntData), VariableKind.None));
55      GetVariableInfo(OFFSET).Local = true;
[2]56
[155]57      ConstrainedDoubleData weight = new ConstrainedDoubleData();
[2]58      // initialize a totally arbitrary range for the weight = [-20.0, 20.0]
59      weight.AddConstraint(new DoubleBoundedConstraint(-20.0, 20.0));
[155]60      AddVariable(new HeuristicLab.Core.Variable(WEIGHT, weight));
[2]61
[155]62      ConstrainedIntData variable = new ConstrainedIntData();
63      AddVariable(new HeuristicLab.Core.Variable(INDEX, variable));
64
65      ConstrainedIntData sampleOffset = new ConstrainedIntData();
[425]66      // initialize a sample offset for static models
67      IntBoundedConstraint offsetConstraint = new IntBoundedConstraint(0, 0);
68      offsetConstraint.LowerBoundIncluded = true;
69      offsetConstraint.UpperBoundIncluded = true;
70      sampleOffset.AddConstraint(offsetConstraint);
[155]71      AddVariable(new HeuristicLab.Core.Variable(OFFSET, sampleOffset));
[2]72
[425]73      SetupInitialization();
74      SetupManipulation();
75
[155]76      // variable can't have suboperators
[2]77      AddConstraint(new NumberOfSubOperatorsConstraint(0, 0));
78    }
79
[425]80    private void SetupInitialization() {
81      AddVariableInfo(new VariableInfo(INITIALIZATION, "Initialization operator for variables", typeof(CombinedOperator), VariableKind.None));
82      GetVariableInfo(INITIALIZATION).Local = false;
83      CombinedOperator combinedOp = new CombinedOperator();
84      SequentialProcessor seq = new SequentialProcessor();
85      UniformRandomizer indexRandomizer = new UniformRandomizer();
86      indexRandomizer.Min = 0;
87      indexRandomizer.Max = 10;
88      indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;
89      indexRandomizer.Name = "Index Randomizer";
90      NormalRandomizer weightRandomizer = new NormalRandomizer();
91      weightRandomizer.Mu = 1.0;
92      weightRandomizer.Sigma = 1.0;
93      weightRandomizer.GetVariableInfo("Value").ActualName = WEIGHT;
94      weightRandomizer.Name = "Weight Randomizer";
95      UniformRandomizer offsetRandomizer = new UniformRandomizer();
96      offsetRandomizer.Min = 0.0;
97      offsetRandomizer.Max = 1.0;
98      offsetRandomizer.GetVariableInfo("Value").ActualName = OFFSET;
99      offsetRandomizer.Name = "Offset Randomizer";
100
101      combinedOp.OperatorGraph.AddOperator(seq);
102      combinedOp.OperatorGraph.AddOperator(indexRandomizer);
103      combinedOp.OperatorGraph.AddOperator(weightRandomizer);
104      combinedOp.OperatorGraph.AddOperator(offsetRandomizer);
105      combinedOp.OperatorGraph.InitialOperator = seq;
106      seq.AddSubOperator(indexRandomizer);
107      seq.AddSubOperator(weightRandomizer);
108      seq.AddSubOperator(offsetRandomizer);
109      AddVariable(new HeuristicLab.Core.Variable(INITIALIZATION, combinedOp));
110    }
111
112    private void SetupManipulation() {
113      // manipulation operator
114      AddVariableInfo(new VariableInfo(MANIPULATION, "Manipulation operator for variables", typeof(CombinedOperator), VariableKind.None));
115      GetVariableInfo(MANIPULATION).Local = false;
116      CombinedOperator combinedOp = new CombinedOperator();
117      SequentialProcessor seq = new SequentialProcessor();
118      UniformRandomizer indexRandomizer = new UniformRandomizer();
119      indexRandomizer.Min = 0;
120      indexRandomizer.Max = 10;
121      indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;
122      indexRandomizer.Name = "Index Randomizer";
123      NormalRandomAdder weightRandomAdder = new NormalRandomAdder();
124      weightRandomAdder.Mu = 0.0;
125      weightRandomAdder.Sigma = 0.1;
126      weightRandomAdder.GetVariableInfo("Value").ActualName = WEIGHT;
127      weightRandomAdder.Name = "Weight Adder";
128      NormalRandomAdder offsetRandomAdder = new NormalRandomAdder();
129      offsetRandomAdder.Mu = 0.0;
130      offsetRandomAdder.Sigma = 1.0;
131      offsetRandomAdder.GetVariableInfo("Value").ActualName = OFFSET;
132      offsetRandomAdder.Name = "Offset Adder";
133
134      combinedOp.OperatorGraph.AddOperator(seq);
135      combinedOp.OperatorGraph.AddOperator(indexRandomizer);
136      combinedOp.OperatorGraph.AddOperator(weightRandomAdder);
137      combinedOp.OperatorGraph.AddOperator(offsetRandomAdder);
138      combinedOp.OperatorGraph.InitialOperator = seq;
139      seq.AddSubOperator(indexRandomizer);
140      seq.AddSubOperator(weightRandomAdder);
141      seq.AddSubOperator(offsetRandomAdder);
142      AddVariable(new HeuristicLab.Core.Variable(MANIPULATION, combinedOp));
143    }
144
[2]145    public override void Accept(IFunctionVisitor visitor) {
146      visitor.Visit(this);
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.