Changeset 189 for trunk/sources/HeuristicLab.Functions
- Timestamp:
- 04/25/08 12:13:34 (17 years ago)
- Location:
- trunk/sources/HeuristicLab.Functions
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Functions/And.cs
r155 r189 42 42 } 43 43 44 // special form 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 if(result == 0.0) return 0.0; // one sub-tree is 0.0 (false) => return false 49 else if(result != 1.0) return double.NaN; 50 } 51 // all sub-trees evaluated to 1.0 (true) => return 1.0 (true) 52 return 1.0; 44 public override IFunctionTree GetTreeNode() { 45 return new AndFunctionTree(this); 53 46 } 54 47 48 // special form 55 49 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 56 50 throw new NotImplementedException(); … … 61 55 } 62 56 } 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 } 63 72 } -
trunk/sources/HeuristicLab.Functions/Constant.cs
r177 r189 52 52 } 53 53 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; 54 public override IFunctionTree GetTreeNode() { 55 return new ConstantFunctionTree(this); 57 56 } 58 57 … … 66 65 } 67 66 } 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 = (ConstantFunctionTree)base.Clone(clonedObjects); 85 clone.UpdateCachedValues(); 86 return clone; 87 } 88 89 public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) { 90 base.Populate(node, restoredObjects); 91 UpdateCachedValues(); 92 } 93 } 68 94 } -
trunk/sources/HeuristicLab.Functions/FunctionBase.cs
r165 r189 35 35 public abstract class FunctionBase : OperatorBase, IFunction { 36 36 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 for(int i = 0; i < evaluationResults.Length; i++) {41 evaluationResults[i] = tree.SubTrees[i].Evaluate(dataset, sampleIndex);42 }43 return Apply(dataset, sampleIndex, evaluationResults);44 } else {45 return Apply(dataset, sampleIndex, null);46 }47 }48 49 37 public abstract double Apply(Dataset dataset, int sampleIndex, double[] args); 50 38 51 39 public virtual void Accept(IFunctionVisitor visitor) { 52 40 visitor.Visit(this); 41 } 42 43 public virtual IFunctionTree GetTreeNode() { 44 return new FunctionTree(this); 53 45 } 54 46 -
trunk/sources/HeuristicLab.Functions/FunctionTree.cs
r157 r189 35 35 private IFunction function; 36 36 37 public FunctionTree() : base() { 37 public FunctionTree() 38 : base() { 38 39 subTrees = new List<IFunctionTree>(); 39 40 localVariables = new List<IVariable>(); 40 41 } 41 42 42 public FunctionTree(IFunction function) : this() { 43 internal FunctionTree(IFunction function) 44 : this() { 43 45 this.function = function; 44 46 // create and store clones of all local variables of the function … … 91 93 } 92 94 93 public double Evaluate(Dataset dataset, int sampleIndex) { 94 return function.Evaluate(dataset, sampleIndex, this); 95 public virtual double Evaluate(Dataset dataset, int sampleIndex) { 96 double[] evaluationResults = new double[SubTrees.Count]; 97 for(int i = 0; i < evaluationResults.Length; i++) { 98 evaluationResults[i] = SubTrees[i].Evaluate(dataset, sampleIndex); 99 } 100 return function.Apply(dataset, sampleIndex, evaluationResults); 95 101 } 96 102 #endregion -
trunk/sources/HeuristicLab.Functions/IFunction.cs
r155 r189 28 28 namespace HeuristicLab.Functions { 29 29 public interface IFunction : IOperator { 30 double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree);30 IFunctionTree GetTreeNode(); 31 31 double Apply(Dataset dataset, int sampleIndex, double[] args); 32 32 void Accept(IFunctionVisitor visitor); -
trunk/sources/HeuristicLab.Functions/IfThenElse.cs
r155 r189 41 41 } 42 42 43 // special form 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 else return double.NaN; 43 public override IFunctionTree GetTreeNode() { 44 return new IfThenElseFunctionTree(this); 49 45 } 50 46 47 // special form 51 48 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 52 49 throw new NotImplementedException(); … … 57 54 } 58 55 } 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 } 59 68 } -
trunk/sources/HeuristicLab.Functions/Or.cs
r155 r189 41 41 } 42 42 43 public override double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) { 44 foreach(IFunctionTree subTree in tree.SubTrees) { 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 } 50 public override void Accept(IFunctionVisitor visitor) { 51 visitor.Visit(this); 52 } 53 } 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) { 45 61 double result = Math.Round(subTree.Evaluate(dataset, sampleIndex)); 46 62 if(result == 1.0) return 1.0; // sub-tree evaluates to 1.0 (true) return 1.0 … … 50 66 return 0.0; 51 67 } 52 53 public override double Apply(Dataset dataset, int sampleIndex, double[] args) {54 throw new NotImplementedException();55 }56 public override void Accept(IFunctionVisitor visitor) {57 visitor.Visit(this);58 }59 68 } 60 69 } -
trunk/sources/HeuristicLab.Functions/ProgrammableFunction.cs
r165 r189 119 119 } 120 120 121 public double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) { 122 // evaluate sub-trees 123 double[] evaluationResults = new double[tree.SubTrees.Count]; 124 for(int subTree=0; subTree < tree.SubTrees.Count; subTree++) { 125 evaluationResults[subTree] = tree.SubTrees[subTree].Evaluate(dataset, sampleIndex); 126 } 121 public IFunctionTree GetTreeNode() { 122 return new ProgrammableFunctionTree(this); 123 } 124 125 // application of programmable-function is not possible 126 public double Apply(Dataset dataset, int sampleIndex, double[] args) { 127 throw new NotSupportedException(); 128 } 129 130 internal double Call(object[] parameters) { 127 131 // lazy activation of the user-programmed code 128 132 if(applyMethod == null) { 129 133 Compile(); 130 134 } 135 return (double)applyMethod.Invoke(null, parameters); 136 } 137 138 #endregion 139 140 #region disabled operator functionality 141 // operator-tree style evaluation is not supported for functions. 142 public override IOperation Apply(IScope scope) { 143 throw new NotSupportedException(); 144 } 145 146 private static readonly List<IOperator> emptySubOperatorList = new List<IOperator>(); 147 public override IList<IOperator> SubOperators { 148 get { return emptySubOperatorList; } 149 } 150 151 public override void AddSubOperator(IOperator subOperator) { 152 throw new NotSupportedException(); 153 } 154 155 public override bool TryAddSubOperator(IOperator subOperator) { 156 throw new NotSupportedException(); 157 } 158 159 public override bool TryAddSubOperator(IOperator subOperator, int index) { 160 throw new NotSupportedException(); 161 } 162 163 public override bool TryAddSubOperator(IOperator subOperator, int index, out ICollection<IConstraint> violatedConstraints) { 164 throw new NotSupportedException(); 165 } 166 167 public override bool TryAddSubOperator(IOperator subOperator, out ICollection<IConstraint> violatedConstraints) { 168 throw new NotSupportedException(); 169 } 170 171 public override void AddSubOperator(IOperator subOperator, int index) { 172 throw new NotSupportedException(); 173 } 174 175 public override void RemoveSubOperator(int index) { 176 throw new NotSupportedException(); 177 } 178 179 public override bool TryRemoveSubOperator(int index) { 180 throw new NotSupportedException(); 181 } 182 183 public override bool TryRemoveSubOperator(int index, out ICollection<IConstraint> violatedConstraints) { 184 throw new NotSupportedException(); 185 } 186 #endregion 187 } 188 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 } 131 201 132 202 // collect parameters 133 object[] parameters = new object[ VariableInfos.Count + 3];203 object[] parameters = new object[LocalVariables.Count + 3]; 134 204 parameters[0] = dataset; 135 205 parameters[1] = sampleIndex; 136 206 int i = 2; 137 207 // all local variables are available in the custom function 138 foreach(IVariable variable in tree.LocalVariables) {208 foreach(IVariable variable in LocalVariables) { 139 209 parameters[i] = variable; 140 210 i++; 141 211 } 142 212 parameters[i] = evaluationResults; 143 return (double)applyMethod.Invoke(null, parameters); 144 } 145 146 // application of programmable-function is not possible 147 public double Apply(Dataset dataset, int sampleIndex, double[] args) { 148 throw new NotSupportedException(); 149 } 150 151 #endregion 152 153 #region disabled operator functionality 154 // operator-tree style evaluation is not supported for functions. 155 public override IOperation Apply(IScope scope) { 156 throw new NotSupportedException(); 157 } 158 159 private static readonly List<IOperator> emptySubOperatorList = new List<IOperator>(); 160 public override IList<IOperator> SubOperators { 161 get { return emptySubOperatorList; } 162 } 163 164 public override void AddSubOperator(IOperator subOperator) { 165 throw new NotSupportedException(); 166 } 167 168 public override bool TryAddSubOperator(IOperator subOperator) { 169 throw new NotSupportedException(); 170 } 171 172 public override bool TryAddSubOperator(IOperator subOperator, int index) { 173 throw new NotSupportedException(); 174 } 175 176 public override bool TryAddSubOperator(IOperator subOperator, int index, out ICollection<IConstraint> violatedConstraints) { 177 throw new NotSupportedException(); 178 } 179 180 public override bool TryAddSubOperator(IOperator subOperator, out ICollection<IConstraint> violatedConstraints) { 181 throw new NotSupportedException(); 182 } 183 184 public override void AddSubOperator(IOperator subOperator, int index) { 185 throw new NotSupportedException(); 186 } 187 188 public override void RemoveSubOperator(int index) { 189 throw new NotSupportedException(); 190 } 191 192 public override bool TryRemoveSubOperator(int index) { 193 throw new NotSupportedException(); 194 } 195 196 public override bool TryRemoveSubOperator(int index, out ICollection<IConstraint> violatedConstraints) { 197 throw new NotSupportedException(); 198 } 199 #endregion 213 return progFun.Call(parameters); 214 } 200 215 } 201 216 } -
trunk/sources/HeuristicLab.Functions/Variable.cs
r177 r189 68 68 } 69 69 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); 70 public override IFunctionTree GetTreeNode() { 71 return new VariableFunctionTree(this); 79 72 } 80 73 … … 88 81 } 89 82 } 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 return weight.Data * dataset.GetValue(sampleIndex + offset.Data, index.Data); 101 } 102 103 public override object Clone(IDictionary<Guid, object> clonedObjects) { 104 VariableFunctionTree clone = (VariableFunctionTree)base.Clone(clonedObjects); 105 clone.UpdateCachedValues(); 106 return clone; 107 } 108 109 public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) { 110 base.Populate(node, restoredObjects); 111 UpdateCachedValues(); 112 } 113 } 90 114 }
Note: See TracChangeset
for help on using the changeset viewer.