Free cookie consent management tool by TermsFeed Policy Generator

Changeset 147


Ignore:
Timestamp:
04/22/08 10:19:53 (16 years ago)
Author:
gkronber
Message:
  • changed signature of interface method Evaluate()
  • fixed evaluation of variables and constants
  • removed variable-cache fields and properties in variable and constant since they are not needed anymore. and removed the obsolete overrides of Populate() and Clone()

(ticket #112)

Location:
branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/And.cs

    r142 r147  
    4343
    4444    // special form
    45     public override double Evaluate(Dataset dataset, int sampleIndex, IList<IFunctionTree> subFunctions) {
    46       foreach(IFunctionTree subFunction in subFunctions) {
    47         double result = Math.Round(subFunction.Evaluate(dataset, sampleIndex));
     45    public override double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) {
     46      foreach(IFunctionTree subTree in tree.SubTrees) {
     47        double result = Math.Round(subTree.Evaluate(dataset, sampleIndex));
    4848        if(result == 0.0) return 0.0; // one sub-tree is 0.0 (false) => return false
    4949        else if(result != 1.0) return double.NaN;
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Constant.cs

    r146 r147  
    3737    }
    3838
    39     private ConstrainedDoubleData value;
    40     public ConstrainedDoubleData Value {
    41       get {
    42         return value;
    43       }
    44     }
    45 
    4639    public Constant()
    4740      : base() {
     
    5245      // initialize a default range for the contant value
    5346      valueData.AddConstraint(new DoubleBoundedConstraint(-20.0, 20.0));
    54 
    55       // create the local variable
    5647      HeuristicLab.Core.Variable value = new HeuristicLab.Core.Variable(VALUE, valueData);
    5748      AddVariable(value);
     
    6152    }
    6253
    63     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    64       Constant clone = (Constant)base.Clone(clonedObjects);
    65       clone.value = (ConstrainedDoubleData)clone.GetVariable(VALUE).Value;
    66       return clone;
     54    // constant is evaluated directly. Evaluation reads the local variable value from the tree and returns
     55    public override double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) {
     56      return ((ConstrainedDoubleData)tree.GetLocalVariable(VALUE).Value).Data;
    6757    }
    6858
    69     public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
    70       base.Populate(node, restoredObjects);
    71       value = (ConstrainedDoubleData)GetVariable(VALUE).Value;
    72     }
    73 
     59    // can't apply a constant
    7460    public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    75       return value.Data;
     61      throw new NotSupportedException();
    7662    }
    7763
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/FunctionBase.cs

    r145 r147  
    3535  public abstract class FunctionBase : OperatorBase, IFunction {
    3636   
    37     public virtual double Evaluate(Dataset dataset, int sampleIndex, IList<IFunctionTree> subTrees) {
    38       if(subTrees.Count > 0) {
    39         double[] evaluationResults = new double[subTrees.Count];
     37    public virtual double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) {
     38      if(tree.SubTrees.Count > 0) {
     39        double[] evaluationResults = new double[tree.SubTrees.Count];
    4040        for(int i = 0; i < evaluationResults.Length; i++) {
    41           evaluationResults[i] = subTrees[i].Evaluate(dataset, sampleIndex);
     41          evaluationResults[i] = tree.SubTrees[i].Evaluate(dataset, sampleIndex);
    4242        }
    4343        return Apply(dataset, sampleIndex, evaluationResults);
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/FunctionTree.cs

    r145 r147  
    7171
    7272    public double Evaluate(Dataset dataset, int sampleIndex) {
    73       return function.Evaluate(dataset, sampleIndex, subTrees);
     73      return function.Evaluate(dataset, sampleIndex, this);
    7474    }
    7575    #endregion
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/IFunction.cs

    r142 r147  
    2828namespace HeuristicLab.Functions {
    2929  public interface IFunction : IOperator {
    30     double Evaluate(Dataset dataset, int sampleIndex, IList<IFunctionTree> subTrees);
     30    double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree);
    3131    double Apply(Dataset dataset, int sampleIndex, double[] args);
    3232    void Accept(IFunctionVisitor visitor);
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/IfThenElse.cs

    r142 r147  
    4242
    4343    // special form
    44     public override double Evaluate(Dataset dataset, int sampleIndex, IList<IFunctionTree> subTrees) {
    45       double condition = Math.Round(subTrees[0].Evaluate(dataset, sampleIndex));
    46       if(condition < .5) return subTrees[1].Evaluate(dataset, sampleIndex);
    47       else if(condition >= .5) return subTrees[2].Evaluate(dataset, sampleIndex);
     44    public override double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) {
     45      double condition = Math.Round(tree.SubTrees[0].Evaluate(dataset, sampleIndex));
     46      if(condition < .5) return tree.SubTrees[1].Evaluate(dataset, sampleIndex);
     47      else if(condition >= .5) return tree.SubTrees[2].Evaluate(dataset, sampleIndex);
    4848      else return double.NaN;
    4949    }
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Or.cs

    r142 r147  
    4141    }
    4242
    43     public override double Evaluate(Dataset dataset, int sampleIndex, IList<IFunctionTree> subTrees) {
    44       foreach(IFunctionTree subTree in subTrees) {
     43    public override double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) {
     44      foreach(IFunctionTree subTree in tree.SubTrees) {
    4545        double result = Math.Round(subTree.Evaluate(dataset, sampleIndex));
    4646        if(result == 1.0) return 1.0; // sub-tree evaluates to 1.0 (true) return 1.0
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Variable.cs

    r146 r147  
    3636    public static readonly string INDEX = "Variable";
    3737
    38     private ConstrainedIntData variable;
    39     private ConstrainedDoubleData weight;
    40     private ConstrainedIntData sampleOffset;
    41 
    42     public double SampleOffset {
    43       get { return sampleOffset.Data; }
    44     }
    45 
    46     public int VariableIndex {
    47       get { return variable.Data; }
    48     }
    49 
    50     public double Weight {
    51       get { return weight.Data; }
    52     }
    53 
    5438    public override string Description {
    5539      get { return @"Variable reads a value from a dataset, multiplies that value with a given factor and returns the result.
     
    6751      GetVariableInfo(OFFSET).Local = true;
    6852
    69       variable = new ConstrainedIntData();
    70       AddVariable(new HeuristicLab.Core.Variable(INDEX, variable));
    71 
    72       weight = new ConstrainedDoubleData();
     53      ConstrainedDoubleData weight = new ConstrainedDoubleData();
    7354      // initialize a totally arbitrary range for the weight = [-20.0, 20.0]
    7455      weight.AddConstraint(new DoubleBoundedConstraint(-20.0, 20.0));
    7556      AddVariable(new HeuristicLab.Core.Variable(WEIGHT, weight));
    7657
    77       sampleOffset = new ConstrainedIntData();
     58      ConstrainedIntData variable = new ConstrainedIntData();
     59      AddVariable(new HeuristicLab.Core.Variable(INDEX, variable));
     60
     61      ConstrainedIntData sampleOffset = new ConstrainedIntData();
    7862      // initialize a totally arbitrary default range for sampleoffset = [-10, 10]
    7963      sampleOffset.AddConstraint(new IntBoundedConstraint(0, 0));
    8064      AddVariable(new HeuristicLab.Core.Variable(OFFSET, sampleOffset));
    8165
    82       // samplefeature can't have suboperators
     66      // variable can't have suboperators
    8367      AddConstraint(new NumberOfSubOperatorsConstraint(0, 0));
    8468    }
    8569
    86     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    87       HeuristicLab.Functions.Variable clone = (HeuristicLab.Functions.Variable)base.Clone(clonedObjects);
    88       clone.variable = (ConstrainedIntData)clone.GetVariable(INDEX).Value;
    89       clone.weight = (ConstrainedDoubleData)clone.GetVariable(WEIGHT).Value;
    90       clone.sampleOffset = (ConstrainedIntData)clone.GetVariable(OFFSET).Value;
    91       return clone;
    92     }
    93    
    94     public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    95       base.Populate(node, restoredObjects);
    96       variable = (ConstrainedIntData)GetVariable(INDEX).Value;
    97       weight = (ConstrainedDoubleData)GetVariable(WEIGHT).Value;
    98       sampleOffset = (ConstrainedIntData)GetVariable(OFFSET).Value;
     70    // variable can be evaluated directly
     71    // evaluation reads local variables weight, index, offset from function-tree and returns the variable-value * weight
     72    public override double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) {
     73      double w = ((ConstrainedDoubleData)tree.GetLocalVariable(WEIGHT).Value).Data;
     74      int v = ((ConstrainedIntData)tree.GetLocalVariable(INDEX).Value).Data;
     75      int offset = ((ConstrainedIntData)tree.GetLocalVariable(OFFSET).Value).Data;
     76
     77      if(sampleIndex + offset < 0 || sampleIndex + offset >= dataset.Rows) return double.NaN;
     78      return w * dataset.GetValue(sampleIndex + offset, v);
    9979    }
    10080
     81    // can't apply a variable
    10182    public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    102       // local variables
    103       int v = variable.Data;
    104       double w = weight.Data;
    105       int offset = sampleOffset.Data;
    106 
    107       if(sampleIndex+offset<0 || sampleIndex+offset>=dataset.Rows) return double.NaN;
    108       return w * dataset.GetValue(sampleIndex + offset, v);
     83      throw new NotSupportedException();
    10984    }
    11085
Note: See TracChangeset for help on using the changeset viewer.