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)

Location:
trunk/sources/HeuristicLab.Functions
Files:
1 deleted
26 edited
2 copied

Legend:

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

    r155 r229  
    3030
    3131namespace HeuristicLab.Functions {
    32   public class Addition : FunctionBase {
     32  public sealed class Addition : FunctionBase {
    3333    public override string Description {
    3434      get {
     
    4646    }
    4747
    48     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    49       // (+ 3) => 3
    50       // (+ 2 3) => 5
    51       // (+ 3 4 5) => 12
    52       double sum = 0.0;
    53       for (int i = 0; i < args.Length; i++) {
    54         sum += args[i];
    55       }
    56       return sum;
    57     }
    58 
    5948    public override void Accept(IFunctionVisitor visitor) {
    6049      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/And.cs

    r192 r229  
    2828
    2929namespace HeuristicLab.Functions {
    30   public class And : FunctionBase {
     30  public sealed class And : FunctionBase {
    3131    public override string Description {
    3232      get {
     
    4242    }
    4343
    44     public override IFunctionTree GetTreeNode() {
    45       return new AndFunctionTree(this);
    46     }
    47 
    48     // special form
    49     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    50       throw new NotImplementedException();
    51     }
    52 
    5344    public override void Accept(IFunctionVisitor visitor) {
    5445      visitor.Visit(this);
    5546    }
    5647  }
    57 
    58   class AndFunctionTree : FunctionTree {
    59     public AndFunctionTree() : base() { }
    60     public AndFunctionTree(And and) : base(and) { }
    61 
    62     public override double Evaluate(Dataset dataset, int sampleIndex) {
    63       foreach(IFunctionTree subTree in SubTrees) {
    64         double result = Math.Round(subTree.Evaluate(dataset, sampleIndex));
    65         if(result == 0.0) return 0.0; // one sub-tree is 0.0 (false) => return false
    66         else if(result != 1.0) return double.NaN;
    67       }
    68       // all sub-trees evaluated to 1.0 (true) => return 1.0 (true)
    69       return 1.0;
    70     }
    71 
    72     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    73       AndFunctionTree clone = new AndFunctionTree();
    74       clonedObjects.Add(clone.Guid, clone);
    75       FillClone(clone, clonedObjects);
    76       return clone;
    77     }
    78   }
    7948}
  • trunk/sources/HeuristicLab.Functions/Average.cs

    r155 r229  
    2828
    2929namespace HeuristicLab.Functions {
    30   public class Average : FunctionBase {
     30  public sealed class Average : FunctionBase {
    3131    public override string Description {
    3232      get {
     
    4040    }
    4141
    42     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    43       double sum = 0.0;
    44       for(int i = 0; i < args.Length; i++) {
    45         sum += args[i];
    46       }
    47       return sum / args.Length;
    48     }
    49 
    5042    public override void Accept(IFunctionVisitor visitor) {
    5143      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/Constant.cs

    r192 r229  
    3030
    3131namespace HeuristicLab.Functions {
    32   public class Constant : FunctionBase {
     32  public sealed class Constant : FunctionBase {
    3333    public const string VALUE = "Value";
    3434
     
    5151      AddConstraint(new NumberOfSubOperatorsConstraint(0, 0));
    5252    }
    53 
    54     public override IFunctionTree GetTreeNode() {
    55       return new ConstantFunctionTree(this);
    56     }
    57 
    58     // can't apply a constant
    59     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    60       throw new NotSupportedException();
    61     }
    62 
    6353    public override void Accept(IFunctionVisitor visitor) {
    6454      visitor.Visit(this);
    6555    }
    6656  }
    67 
    68   class ConstantFunctionTree : FunctionTree {
    69     private ConstrainedDoubleData value;
    70     public ConstantFunctionTree() : base() { }
    71     public ConstantFunctionTree(Constant constant) : base(constant) {
    72       UpdateCachedValues();
    73     }
    74 
    75     private void UpdateCachedValues() {
    76       value = (ConstrainedDoubleData)GetLocalVariable(Constant.VALUE).Value;
    77     }
    78 
    79     public override double Evaluate(Dataset dataset, int sampleIndex) {
    80       return value.Data;
    81     }
    82 
    83     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    84       ConstantFunctionTree clone = new ConstantFunctionTree();
    85       clonedObjects.Add(clone.Guid, clone);
    86       FillClone(clone, clonedObjects);
    87       clone.UpdateCachedValues();
    88       return clone;
    89     }
    90 
    91     public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    92       base.Populate(node, restoredObjects);
    93       UpdateCachedValues();
    94     }
    95   }
    9657}
  • trunk/sources/HeuristicLab.Functions/Cosinus.cs

    r155 r229  
    2929
    3030namespace HeuristicLab.Functions {
    31   public class Cosinus : FunctionBase {
     31  public sealed class Cosinus : FunctionBase {
    3232    public override string Description {
    3333      get { return "Returns the cosinus of the first sub-tree."; }
     
    4040    }
    4141
    42     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    43       return Math.Cos(args[0]);
    44     }
    45 
    4642    public override void Accept(IFunctionVisitor visitor) {
    4743      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/Division.cs

    r155 r229  
    3030
    3131namespace HeuristicLab.Functions {
    32   public class Division : FunctionBase {
     32  public sealed class Division : FunctionBase {
    3333    private const double EPSILON = 10.0E-20; // if any divisor is < EPSILON return 0
    3434
     
    5252    }
    5353
    54     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    55       // (/ 3) => 1/3
    56       // (/ 2 3) => 2/3
    57       // (/ 3 4 5) => 3/20
    58 
    59       if(args.Length == 1) {
    60         double divisor = args[0];
    61         if(Math.Abs(divisor) < EPSILON) return 0;
    62         else return 1.0 / divisor;
    63       } else {
    64         double result = args[0];
    65         for(int i = 1; i < args.Length; i++) {
    66           double divisor = args[i];
    67           if(Math.Abs(divisor) < EPSILON) return 0.0;
    68           result /= divisor;
    69         }
    70         return result;
    71       }
    72     }
    73 
    7454    public override void Accept(IFunctionVisitor visitor) {
    7555      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/Equal.cs

    r155 r229  
    2828
    2929namespace HeuristicLab.Functions {
    30   public class Equal : FunctionBase {
     30  public sealed class Equal : FunctionBase {
    3131    public override string Description {
    3232      get {
     
    4040    }
    4141
    42     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    43       if(args[0] == args[1]) return 1.0;
    44       else return 0.0;
    45     }
    46 
    4742    public override void Accept(IFunctionVisitor visitor) {
    4843      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/Exponential.cs

    r155 r229  
    2929
    3030namespace HeuristicLab.Functions {
    31   public class Exponential : FunctionBase {
     31  public sealed class Exponential : FunctionBase {
    3232    public override string Description {
    3333      get { return "Returns returns exponential of the first sub-tree (power(e, x))."; }
     
    4040    }
    4141
    42     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    43       return Math.Exp(args[0]);
    44     }
    45 
    4642    public override void Accept(IFunctionVisitor visitor) {
    4743      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/FunctionBase.cs

    r189 r229  
    3434  /// </summary>
    3535  public abstract class FunctionBase : OperatorBase, IFunction {
    36    
    37     public abstract double Apply(Dataset dataset, int sampleIndex, double[] args);
     36
     37    public virtual double Apply(Dataset dataset, int sampleIndex, double[] args) {
     38      throw new NotImplementedException();
     39    }
    3840
    3941    public virtual void Accept(IFunctionVisitor visitor) {
     
    4244
    4345    public virtual IFunctionTree GetTreeNode() {
    44       return new FunctionTree(this);
     46      return new BakedFunctionTree(this);
    4547    }
    4648
  • trunk/sources/HeuristicLab.Functions/GreaterThan.cs

    r159 r229  
    2828
    2929namespace HeuristicLab.Functions {
    30   public class GreaterThan : FunctionBase {
     30  public sealed class GreaterThan : FunctionBase {
    3131    public override string Description {
    3232      get {
     
    3939      AddConstraint(new NumberOfSubOperatorsConstraint(2, 2));
    4040    }
    41 
    42     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    43       if(args[0] > args[1]) return 1.0;
    44       else return 0.0;
    45     }
    46 
    4741    public override void Accept(IFunctionVisitor visitor) {
    4842      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/HeuristicLab.Functions.csproj

    r185 r229  
    4949  <ItemGroup>
    5050    <Compile Include="Addition.cs" />
    51     <Compile Include="And.cs" />
     51    <Compile Include="And.cs">
     52      <SubType>Code</SubType>
     53    </Compile>
    5254    <Compile Include="Average.cs" />
     55    <Compile Include="BakedFunctionTree.cs" />
     56    <Compile Include="BakedTreeEvaluator.cs" />
    5357    <Compile Include="Constant.cs" />
    5458    <Compile Include="GreaterThan.cs" />
    55     <Compile Include="FunctionTree.cs" />
     59    <Compile Include="IfThenElse.cs">
     60      <SubType>Code</SubType>
     61    </Compile>
    5662    <Compile Include="IFunctionTree.cs" />
    5763    <Compile Include="ModelAnalyzerExporter.cs" />
     64    <Compile Include="Or.cs">
     65      <SubType>Code</SubType>
     66    </Compile>
    5867    <Compile Include="ProgrammableFunction.cs" />
    5968    <Compile Include="Equal.cs" />
     
    6473      <DependentUpon>FunctionView.cs</DependentUpon>
    6574    </Compile>
    66     <Compile Include="IfThenElse.cs" />
    6775    <Compile Include="IFunctionVisitor.cs" />
    6876    <Compile Include="LessThan.cs" />
    6977    <Compile Include="Not.cs" />
    70     <Compile Include="Or.cs" />
    7178    <Compile Include="Signum.cs" />
    7279    <Compile Include="Logarithm.cs" />
  • trunk/sources/HeuristicLab.Functions/IfThenElse.cs

    r192 r229  
    2828
    2929namespace HeuristicLab.Functions {
    30   public class IfThenElse : FunctionBase {
     30  public sealed class IfThenElse : FunctionBase {
    3131    public override string Description {
    3232      get {
     
    4141    }
    4242
    43     public override IFunctionTree GetTreeNode() {
    44       return new IfThenElseFunctionTree(this);
    45     }
    46 
    47     // special form
    48     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    49       throw new NotImplementedException();
    50     }
    51 
    5243    public override void Accept(IFunctionVisitor visitor) {
    5344      visitor.Visit(this);
    5445    }
    5546  }
    56 
    57   class IfThenElseFunctionTree : FunctionTree {
    58     public IfThenElseFunctionTree() : base() { }
    59     public IfThenElseFunctionTree(IfThenElse ifte) : base(ifte) { }
    60 
    61     public override double Evaluate(Dataset dataset, int sampleIndex) {
    62       double condition = Math.Round(SubTrees[0].Evaluate(dataset, sampleIndex));
    63       if(condition < .5) return SubTrees[1].Evaluate(dataset, sampleIndex);
    64       else if(condition >= .5) return SubTrees[2].Evaluate(dataset, sampleIndex);
    65       else return double.NaN;
    66     }
    67 
    68     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    69       IfThenElseFunctionTree clone = new IfThenElseFunctionTree();
    70       clonedObjects.Add(clone.Guid, clone);
    71       FillClone(clone, clonedObjects);
    72       return clone;
    73     }
    74   }
    7547}
  • trunk/sources/HeuristicLab.Functions/LessThan.cs

    r155 r229  
    2828
    2929namespace HeuristicLab.Functions {
    30   public class LessThan : FunctionBase {
     30  public sealed class LessThan : FunctionBase {
    3131    public override string Description {
    3232      get {
     
    4040    }
    4141
    42     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    43       if(args[0] < args[1]) return 1.0;
    44       else return 0.0;
    45     }
    46 
    4742    public override void Accept(IFunctionVisitor visitor) {
    4843      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/Logarithm.cs

    r155 r229  
    2929
    3030namespace HeuristicLab.Functions {
    31   public class Logarithm : FunctionBase {
     31  public sealed class Logarithm : FunctionBase {
    3232    public override string Description {
    3333      get { return "Returns the natural (base e) logarithm of the first sub-tree."; }
     
    4040    }
    4141
    42     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    43       return Math.Log(args[0]);
    44     }
    45 
    4642    public override void Accept(IFunctionVisitor visitor) {
    4743      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/Multiplication.cs

    r155 r229  
    3030
    3131namespace HeuristicLab.Functions {
    32   public class Multiplication : FunctionBase {
     32  public sealed class Multiplication : FunctionBase {
    3333    public override string Description {
    3434      get {
     
    4646    }
    4747
    48     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    49       // (* 3) => 3
    50       // (* 2 3) => 6
    51       // (* 3 4 5) => 60
    52       double result = 1.0;
    53       for(int i = 0; i < args.Length; i++) {
    54         result *= args[i];
    55       }
    56       return result;
    57     }
    58 
    5948    public override void Accept(IFunctionVisitor visitor) {
    6049      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/Not.cs

    r155 r229  
    2828
    2929namespace HeuristicLab.Functions {
    30   public class Not : FunctionBase {
     30  public sealed class Not : FunctionBase {
    3131    public override string Description {
    3232      get {
     
    4040    }
    4141
    42     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    43       double result = Math.Round(args[0]);
    44       if(result == 0.0) return 1.0;
    45       else if(result == 1.0) return 0.0;
    46       else return double.NaN;
    47     }
    48 
    4942    public override void Accept(IFunctionVisitor visitor) {
    5043      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/Or.cs

    r192 r229  
    2828
    2929namespace HeuristicLab.Functions {
    30   public class Or : FunctionBase {
     30  public sealed class Or : FunctionBase {
    3131    public override string Description {
    3232      get {
     
    4141    }
    4242
    43     public override IFunctionTree GetTreeNode() {
    44       return new OrFunctionTree(this);
    45     }
    46     // or is a special form and can't be applied
    47     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    48       throw new NotImplementedException();
    49     }
    5043    public override void Accept(IFunctionVisitor visitor) {
    5144      visitor.Visit(this);
    5245    }
    5346  }
    54 
    55   class OrFunctionTree : FunctionTree {
    56     public OrFunctionTree() : base() { }
    57     public OrFunctionTree(Or or) : base(or) { }
    58 
    59     public override double Evaluate(Dataset dataset, int sampleIndex) {
    60       foreach(IFunctionTree subTree in SubTrees) {
    61         double result = Math.Round(subTree.Evaluate(dataset, sampleIndex));
    62         if(result == 1.0) return 1.0; // sub-tree evaluates to 1.0 (true) return 1.0
    63         else if(result != 0.0) return double.NaN;
    64       }
    65       // all sub-trees evaluated to 0.0 (false) return false
    66       return 0.0;
    67     }
    68 
    69     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    70       OrFunctionTree clone = new OrFunctionTree();
    71       clonedObjects.Add(clone.Guid, clone);
    72       FillClone(clone, clonedObjects);
    73       return clone;
    74     }
    75   }
    7647}
  • trunk/sources/HeuristicLab.Functions/Power.cs

    r155 r229  
    2929
    3030namespace HeuristicLab.Functions {
    31   public class Power : FunctionBase {
     31  public sealed class Power : FunctionBase {
    3232    public override string Description {
    3333      get { return "Returns the result of the first sub-tree to the power of the second sub-tree (power(x, y))."; }
     
    4040    }
    4141
    42     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    43       return Math.Pow(args[0], args[1]);
    44     }
    45 
    4642    public override void Accept(IFunctionVisitor visitor) {
    4743      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/ProgrammableFunction.cs

    r189 r229  
    3636
    3737namespace HeuristicLab.Functions {
    38   public class ProgrammableFunction : ProgrammableOperator, IFunction {
     38  public sealed class ProgrammableFunction : ProgrammableOperator, IFunction {
    3939    private MethodInfo applyMethod;
    4040    public ProgrammableFunction()
     
    120120
    121121    public IFunctionTree GetTreeNode() {
    122       return new ProgrammableFunctionTree(this);
     122      return new BakedFunctionTree(this);
    123123    }
    124124
     
    187187  }
    188188
    189   class ProgrammableFunctionTree : FunctionTree {
    190     private ProgrammableFunction progFun;
    191     public ProgrammableFunctionTree() : base() { }
    192     public ProgrammableFunctionTree(ProgrammableFunction progFun) : base(progFun) {
    193       this.progFun = progFun;
    194     }
    195     public override double Evaluate(Dataset dataset, int sampleIndex) {
    196       // evaluate sub-trees
    197       double[] evaluationResults = new double[SubTrees.Count];
    198       for(int subTree = 0; subTree < SubTrees.Count; subTree++) {
    199         evaluationResults[subTree] = SubTrees[subTree].Evaluate(dataset, sampleIndex);
    200       }
    201 
    202       // collect parameters
    203       object[] parameters = new object[LocalVariables.Count + 3];
    204       parameters[0] = dataset;
    205       parameters[1] = sampleIndex;
    206       int i = 2;
    207       // all local variables are available in the custom function
    208       foreach(IVariable variable in LocalVariables) {
    209         parameters[i] = variable;
    210         i++;
    211       }
    212       parameters[i] = evaluationResults;
    213       return progFun.Call(parameters);
    214     }
    215   }
     189  //class ProgrammableFunctionTree : FunctionTree {
     190  //  private ProgrammableFunction progFun;
     191  //  public ProgrammableFunctionTree() : base() { }
     192  //  public ProgrammableFunctionTree(ProgrammableFunction progFun) : base(progFun) {
     193  //    this.progFun = progFun;
     194  //  }
     195  //  public override double Evaluate(Dataset dataset, int sampleIndex) {
     196  //    // evaluate sub-trees
     197  //    double[] evaluationResults = new double[SubTrees.Count];
     198  //    for(int subTree = 0; subTree < SubTrees.Count; subTree++) {
     199  //      evaluationResults[subTree] = SubTrees[subTree].Evaluate(dataset, sampleIndex);
     200  //    }
     201
     202  //    // collect parameters
     203  //    object[] parameters = new object[LocalVariables.Count + 3];
     204  //    parameters[0] = dataset;
     205  //    parameters[1] = sampleIndex;
     206  //    int i = 2;
     207  //    // all local variables are available in the custom function
     208  //    foreach(IVariable variable in LocalVariables) {
     209  //      parameters[i] = variable;
     210  //      i++;
     211  //    }
     212  //    parameters[i] = evaluationResults;
     213  //    return progFun.Call(parameters);
     214  //  }
     215  //}
    216216}
  • trunk/sources/HeuristicLab.Functions/Signum.cs

    r155 r229  
    2929
    3030namespace HeuristicLab.Functions {
    31   public class Signum : FunctionBase {
     31  public sealed class Signum : FunctionBase {
    3232    public override string Description {
    3333      get { return "Returns the signum of the first sub-tree."; }
     
    4040    }
    4141
    42     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    43       double value = args[0];
    44       if(value < 0) return -1;
    45       if(value > 0) return 1;
    46       return 0;
    47     }
    48 
    4942    public override void Accept(IFunctionVisitor visitor) {
    5043      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/Sinus.cs

    r155 r229  
    2929
    3030namespace HeuristicLab.Functions {
    31   public class Sinus : FunctionBase {
     31  public sealed class Sinus : FunctionBase {
    3232    public override string Description {
    3333      get { return "Returns the sinus of the first sub-tree."; }
     
    4040    }
    4141
    42     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    43       return Math.Sin(args[0]);
    44     }
    45 
    4642    public override void Accept(IFunctionVisitor visitor) {
    4743      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/Sqrt.cs

    r155 r229  
    2929
    3030namespace HeuristicLab.Functions {
    31   public class Sqrt : FunctionBase {
     31  public sealed class Sqrt : FunctionBase {
    3232    public override string Description {
    3333      get { return "Returns the square root of the first sub-tree."; }
     
    4040    }
    4141
    42 
    43     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    44       return Math.Sqrt(args[0]);
    45     }
    46 
    4742    public override void Accept(IFunctionVisitor visitor) {
    4843      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/Substraction.cs

    r155 r229  
    3030
    3131namespace HeuristicLab.Functions {
    32   public class Substraction : FunctionBase {
     32  public sealed class Substraction : FunctionBase {
    3333    public override string Description {
    3434      get {
     
    4646    }
    4747
    48 
    49     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    50       if(args.Length == 1) {
    51         return -args[0];
    52       } else {
    53         double result = args[0];
    54         for(int i = 1; i < args.Length; i++) {
    55           result -= args[i];
    56         }
    57         return result;
    58       }
    59     }
    60 
    6148    public override void Accept(IFunctionVisitor visitor) {
    6249      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/Tangens.cs

    r155 r229  
    2929
    3030namespace HeuristicLab.Functions {
    31   public class Tangens : FunctionBase {
     31  public sealed class Tangens : FunctionBase {
    3232    public override string Description {
    3333      get { return "Returns the tangens of the first sub-tree."; }
     
    4040    }
    4141
    42     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    43       return Math.Tan(args[0]);
    44     }
    45 
    4642    public override void Accept(IFunctionVisitor visitor) {
    4743      visitor.Visit(this);
  • 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}
  • trunk/sources/HeuristicLab.Functions/Xor.cs

    r155 r229  
    2828
    2929namespace HeuristicLab.Functions {
    30   public class Xor : FunctionBase {
     30  public sealed class Xor : FunctionBase {
    3131    public override string Description {
    3232      get {
     
    4040    }
    4141
    42     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    43       if(args[0] == 0.0 && args[1] == 0.0) return 0.0;
    44       if(args[0] * args[1] == 0.0) return 1.0;
    45       return 0.0;
    46     }
    47 
    4842    public override void Accept(IFunctionVisitor visitor) {
    4943      visitor.Visit(this);
Note: See TracChangeset for help on using the changeset viewer.