Free cookie consent management tool by TermsFeed Policy Generator

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