Changeset 229 for trunk/sources/HeuristicLab.Functions/Variable.cs
- Timestamp:
- 05/11/08 08:55:19 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Functions/Variable.cs
r193 r229 30 30 31 31 namespace HeuristicLab.Functions { 32 public class Variable : FunctionBase {32 public sealed class Variable : FunctionBase { 33 33 34 34 public const string WEIGHT = "Weight"; … … 37 37 38 38 public override string Description { 39 get { return @"Variable reads a value from a dataset, multiplies that value with a given factor and returns the result. 39 get { 40 return @"Variable reads a value from a dataset, multiplies that value with a given factor and returns the result. 40 41 The variable 'SampleOffset' can be used to read a value from previous or following rows. 41 The index of the row that is actually read is SampleIndex+SampleOffset)."; } 42 The index of the row that is actually read is SampleIndex+SampleOffset)."; 43 } 42 44 } 43 45 … … 68 70 } 69 71 70 public override IFunctionTree GetTreeNode() {71 return new VariableFunctionTree(this);72 }73 74 // can't apply a variable75 public override double Apply(Dataset dataset, int sampleIndex, double[] args) {76 throw new NotSupportedException();77 }78 79 72 public override void Accept(IFunctionVisitor visitor) { 80 73 visitor.Visit(this); 81 74 } 82 75 } 83 class VariableFunctionTree : FunctionTree {84 private ConstrainedDoubleData weight;85 private ConstrainedIntData index;86 private ConstrainedIntData offset;87 88 public VariableFunctionTree() : base() { }89 public VariableFunctionTree(Variable variable) : base(variable) {90 UpdateCachedValues();91 }92 93 protected void UpdateCachedValues() {94 weight = (ConstrainedDoubleData)GetLocalVariable(Variable.WEIGHT).Value;95 index = (ConstrainedIntData)GetLocalVariable(Variable.INDEX).Value;96 offset = (ConstrainedIntData)GetLocalVariable(Variable.OFFSET).Value;97 }98 99 public override double Evaluate(Dataset dataset, int sampleIndex) {100 if(sampleIndex + offset.Data < 0 || sampleIndex + offset.Data >= dataset.Rows) return double.NaN;101 return weight.Data * dataset.GetValue(sampleIndex + offset.Data, index.Data);102 }103 104 public override object Clone(IDictionary<Guid, object> clonedObjects) {105 VariableFunctionTree clone = new VariableFunctionTree();106 clonedObjects.Add(clone.Guid, clone);107 FillClone(clone, clonedObjects);108 clone.UpdateCachedValues();109 return clone;110 }111 112 public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {113 base.Populate(node, restoredObjects);114 UpdateCachedValues();115 }116 }117 76 }
Note: See TracChangeset
for help on using the changeset viewer.