Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlReaderWriterBranch/HeuristicLab.Functions/Variable.cs @ 175

Last change on this file since 175 was 125, checked in by gkronber, 17 years ago

created a branch that combines the XmlReader and XmlWriter branches

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