- Timestamp:
- 04/25/08 12:13:34 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.