Changeset 147 for branches/FunctionsAndStructIdRefactoring
- Timestamp:
- 04/22/08 10:19:53 (17 years ago)
- Location:
- branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/And.cs ¶
r142 r147 43 43 44 44 // special form 45 public override double Evaluate(Dataset dataset, int sampleIndex, I List<IFunctionTree> subFunctions) {46 foreach(IFunctionTree sub Function in subFunctions) {47 double result = Math.Round(sub Function.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)); 48 48 if(result == 0.0) return 0.0; // one sub-tree is 0.0 (false) => return false 49 49 else if(result != 1.0) return double.NaN; -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Constant.cs ¶
r146 r147 37 37 } 38 38 39 private ConstrainedDoubleData value;40 public ConstrainedDoubleData Value {41 get {42 return value;43 }44 }45 46 39 public Constant() 47 40 : base() { … … 52 45 // initialize a default range for the contant value 53 46 valueData.AddConstraint(new DoubleBoundedConstraint(-20.0, 20.0)); 54 55 // create the local variable56 47 HeuristicLab.Core.Variable value = new HeuristicLab.Core.Variable(VALUE, valueData); 57 48 AddVariable(value); … … 61 52 } 62 53 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; 67 57 } 68 58 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 74 60 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 75 return value.Data;61 throw new NotSupportedException(); 76 62 } 77 63 -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/FunctionBase.cs ¶
r145 r147 35 35 public abstract class FunctionBase : OperatorBase, IFunction { 36 36 37 public virtual double Evaluate(Dataset dataset, int sampleIndex, I List<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]; 40 40 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); 42 42 } 43 43 return Apply(dataset, sampleIndex, evaluationResults); -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/FunctionTree.cs ¶
r145 r147 71 71 72 72 public double Evaluate(Dataset dataset, int sampleIndex) { 73 return function.Evaluate(dataset, sampleIndex, subTrees);73 return function.Evaluate(dataset, sampleIndex, this); 74 74 } 75 75 #endregion -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/IFunction.cs ¶
r142 r147 28 28 namespace HeuristicLab.Functions { 29 29 public interface IFunction : IOperator { 30 double Evaluate(Dataset dataset, int sampleIndex, I List<IFunctionTree> subTrees);30 double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree); 31 31 double Apply(Dataset dataset, int sampleIndex, double[] args); 32 32 void Accept(IFunctionVisitor visitor); -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/IfThenElse.cs ¶
r142 r147 42 42 43 43 // special form 44 public override double Evaluate(Dataset dataset, int sampleIndex, I List<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); 48 48 else return double.NaN; 49 49 } -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Or.cs ¶
r142 r147 41 41 } 42 42 43 public override double Evaluate(Dataset dataset, int sampleIndex, I List<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) { 45 45 double result = Math.Round(subTree.Evaluate(dataset, sampleIndex)); 46 46 if(result == 1.0) return 1.0; // sub-tree evaluates to 1.0 (true) return 1.0 -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Variable.cs ¶
r146 r147 36 36 public static readonly string INDEX = "Variable"; 37 37 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 54 38 public override string Description { 55 39 get { return @"Variable reads a value from a dataset, multiplies that value with a given factor and returns the result. … … 67 51 GetVariableInfo(OFFSET).Local = true; 68 52 69 variable = new ConstrainedIntData(); 70 AddVariable(new HeuristicLab.Core.Variable(INDEX, variable)); 71 72 weight = new ConstrainedDoubleData(); 53 ConstrainedDoubleData weight = new ConstrainedDoubleData(); 73 54 // initialize a totally arbitrary range for the weight = [-20.0, 20.0] 74 55 weight.AddConstraint(new DoubleBoundedConstraint(-20.0, 20.0)); 75 56 AddVariable(new HeuristicLab.Core.Variable(WEIGHT, weight)); 76 57 77 sampleOffset = new ConstrainedIntData(); 58 ConstrainedIntData variable = new ConstrainedIntData(); 59 AddVariable(new HeuristicLab.Core.Variable(INDEX, variable)); 60 61 ConstrainedIntData sampleOffset = new ConstrainedIntData(); 78 62 // initialize a totally arbitrary default range for sampleoffset = [-10, 10] 79 63 sampleOffset.AddConstraint(new IntBoundedConstraint(0, 0)); 80 64 AddVariable(new HeuristicLab.Core.Variable(OFFSET, sampleOffset)); 81 65 82 // samplefeature can't have suboperators66 // variable can't have suboperators 83 67 AddConstraint(new NumberOfSubOperatorsConstraint(0, 0)); 84 68 } 85 69 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); 99 79 } 100 80 81 // can't apply a variable 101 82 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(); 109 84 } 110 85
Note: See TracChangeset
for help on using the changeset viewer.