Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/11/08 08:55:19 (16 years ago)
Author:
gkronber
Message:

merged changes (r201 r203 r206 r208 r220 r223 r224 r225 r226 r227) from branch ExperimentalFunctionsBaking into the trunk. (ticket #139)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Functions/Variable.cs

    r193 r229  
    3030
    3131namespace HeuristicLab.Functions {
    32   public class Variable : FunctionBase {
     32  public sealed class Variable : FunctionBase {
    3333
    3434    public const string WEIGHT = "Weight";
     
    3737
    3838    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.
    4041The 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)."; }
     42The index of the row that is actually read is SampleIndex+SampleOffset).";
     43      }
    4244    }
    4345
     
    6870    }
    6971
    70     public override IFunctionTree GetTreeNode() {
    71       return new VariableFunctionTree(this);
    72     }
    73 
    74     // can't apply a variable
    75     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    76       throw new NotSupportedException();
    77     }
    78 
    7972    public override void Accept(IFunctionVisitor visitor) {
    8073      visitor.Visit(this);
    8174    }
    8275  }
    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   }
    11776}
Note: See TracChangeset for help on using the changeset viewer.