Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 53 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 4.5 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;
30
31namespace HeuristicLab.Functions {
32  public class Variable : FunctionBase {
33
34    private ConstrainedIntData variable;
35    private ConstrainedDoubleData weight;
36    private ConstrainedIntData sampleOffset;
37
38    public double SampleOffset {
39      get { return sampleOffset.Data; }
40    }
41
42    public int VariableIndex {
43      get { return variable.Data; }
44    }
45
46    public double Weight {
47      get { return weight.Data; }
48    }
49
50    public override string Description {
51      get { return @"Variable reads a value from a dataset, multiplies that value with a given factor and returns the result.
52The variable 'SampleOffset' can be used to read a value from previous or following rows.
53The index of the row that is actually read is SampleIndex+SampleOffset)."; }
54    }
55
56    public Variable()
57      : base() {
58      AddVariableInfo(new VariableInfo("Variable", "Index of the variable in the dataset representing this feature", typeof(ConstrainedIntData), VariableKind.None));
59      GetVariableInfo("Variable").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("SampleOffset", "SampleOffset is added to the sample index", typeof(ConstrainedIntData), VariableKind.None));
63      GetVariableInfo("SampleOffset").Local = true;
64
65      variable = new ConstrainedIntData();
66      AddLocalVariable(new HeuristicLab.Core.Variable("Variable", variable));
67
68      weight = new ConstrainedDoubleData();
69      // initialize a totally arbitrary range for the weight = [-20.0, 20.0]
70      weight.AddConstraint(new DoubleBoundedConstraint(-20.0, 20.0));
71      AddLocalVariable(new HeuristicLab.Core.Variable("Weight", weight));
72
73      sampleOffset = new ConstrainedIntData();
74      // initialize a totally arbitrary default range for sampleoffset = [-10, 10]
75      sampleOffset.AddConstraint(new IntBoundedConstraint(0, 0));
76      AddLocalVariable(new HeuristicLab.Core.Variable("SampleOffset", sampleOffset));
77
78      // samplefeature can't have suboperators
79      AddConstraint(new NumberOfSubOperatorsConstraint(0, 0));
80    }
81
82    public Variable(Variable source, IDictionary<Guid, object> clonedObjects)
83      : base(source, clonedObjects) {
84
85      variable = (ConstrainedIntData)GetVariable("Variable").Value;
86      weight = (ConstrainedDoubleData)GetVariable("Weight").Value;
87      sampleOffset = (ConstrainedIntData)GetVariable("SampleOffset").Value;
88    }
89
90    public override object Clone(IDictionary<Guid, object> clonedObjects) {
91      Variable clone = new Variable(this, clonedObjects);
92      clonedObjects.Add(clone.Guid, clone);
93      return clone;
94    }
95
96    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
97      base.Populate(node, restoredObjects);
98
99      variable = (ConstrainedIntData)GetVariable("Variable").Value;
100      weight = (ConstrainedDoubleData)GetVariable("Weight").Value;
101      sampleOffset = (ConstrainedIntData)GetVariable("SampleOffset").Value;
102    }
103
104
105    public override double Evaluate(Dataset dataset, int sampleIndex) {
106      // local variables
107      int v = variable.Data;
108      double w = weight.Data;
109      int offset = sampleOffset.Data;
110
111      return w * dataset.GetValue(sampleIndex + offset, v);
112    }
113
114    public override void Accept(IFunctionVisitor visitor) {
115      visitor.Visit(this);
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.