- Timestamp:
- 04/21/08 00:17:54 (17 years ago)
- Location:
- branches/FunctionsAndStructIdRefactoring
- Files:
-
- 3 added
- 27 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Addition.cs ¶
r2 r142 33 33 public override string Description { 34 34 get { 35 return @"Returns the sum of all sub- operatorresults.35 return @"Returns the sum of all sub-tree results. 36 36 (+ 3) => 3 37 37 (+ 2 3) => 5 … … 46 46 } 47 47 48 public Addition(Addition source, IDictionary<Guid, object> clonedObjects) 49 : base(source, clonedObjects) { 50 } 51 52 53 public override double Evaluate(Dataset dataset, int sampleIndex) { 48 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 54 49 // (+ 3) => 3 55 50 // (+ 2 3) => 5 56 51 // (+ 3 4 5) => 12 57 52 double sum = 0.0; 58 for (int i = SubFunctions.Count - 1; i >= 0; i--) {59 sum += SubFunctions[i].Evaluate(dataset, sampleIndex);53 for (int i = 0; i < args.Length; i++) { 54 sum += args[i]; 60 55 } 61 56 return sum; 62 }63 64 public override object Clone(IDictionary<Guid, object> clonedObjects) {65 Addition clone = new Addition(this, clonedObjects);66 clonedObjects.Add(clone.Guid, clone);67 return clone;68 57 } 69 58 -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/And.cs ¶
r2 r142 31 31 public override string Description { 32 32 get { 33 return @"Logical AND operation. Only defined for sub-operator-results 0.0 and 1.0."; 33 return @"Logical AND operation. Only defined for sub-tree-results 0.0 and 1.0. 34 AND is a special form, sub-trees are evaluated from the first to the last. Evaluation is 35 stopped as soon as one of the sub-trees evaluates to 0.0 (false)."; 34 36 } 35 37 } … … 40 42 } 41 43 42 public And(And source, IDictionary<Guid, object> clonedObjects) 43 : base(source, clonedObjects) { 44 } 45 46 47 public override double Evaluate(Dataset dataset, int sampleIndex) { 48 foreach(IFunction subFunction in SubFunctions) { 44 // special form 45 public override double Evaluate(Dataset dataset, int sampleIndex, IList<IFunctionTree> subFunctions) { 46 foreach(IFunctionTree subFunction in subFunctions) { 49 47 double result = Math.Round(subFunction.Evaluate(dataset, sampleIndex)); 50 if(result == 0.0) return 0.0; 48 if(result == 0.0) return 0.0; // one sub-tree is 0.0 (false) => return false 51 49 else if(result != 1.0) return double.NaN; 52 50 } 51 // all sub-trees evaluated to 1.0 (true) => return 1.0 (true) 53 52 return 1.0; 54 53 } 55 54 56 public override object Clone(IDictionary<Guid, object> clonedObjects) { 57 And clone = new And(this, clonedObjects); 58 clonedObjects.Add(clone.Guid, clone); 59 return clone; 55 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 56 throw new NotImplementedException(); 60 57 } 61 58 -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Average.cs ¶
r2 r142 31 31 public override string Description { 32 32 get { 33 return @"Returns the average (arithmetic mean) of all sub- operatorresults.";33 return @"Returns the average (arithmetic mean) of all sub-tree results."; 34 34 } 35 35 } … … 40 40 } 41 41 42 public Average(Average source, IDictionary<Guid, object> clonedObjects) 43 : base(source, clonedObjects) { 44 } 45 46 47 public override double Evaluate(Dataset dataset, int sampleIndex) { 42 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 48 43 double sum = 0.0; 49 for(int i = 0; i < SubFunctions.Count; i++) {50 sum += SubFunctions[i].Evaluate(dataset, sampleIndex);44 for(int i = 0; i < args.Length; i++) { 45 sum += args[i]; 51 46 } 52 return sum / SubFunctions.Count; 53 } 54 55 public override object Clone(IDictionary<Guid, object> clonedObjects) { 56 Average clone = new Average(this, clonedObjects); 57 clonedObjects.Add(clone.Guid, clone); 58 return clone; 47 return sum / args.Length; 59 48 } 60 49 -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Constant.cs ¶
r2 r142 31 31 namespace HeuristicLab.Functions { 32 32 public class Constant : FunctionBase { 33 34 private IVariable value;35 36 33 public override string Description { 37 34 get { return "Returns the value of local variable 'Value'."; } 38 35 } 39 36 37 private ConstrainedDoubleData value; 40 38 public ConstrainedDoubleData Value { 41 39 get { 42 return (ConstrainedDoubleData)value.Value;40 return value; 43 41 } 44 42 } … … 54 52 55 53 // create the local variable 56 value = new HeuristicLab.Core.Variable("Value", valueData);57 Add LocalVariable(value);54 HeuristicLab.Core.Variable value = new HeuristicLab.Core.Variable("Value", valueData); 55 AddVariable(value); 58 56 59 57 // constant can't have suboperators … … 61 59 } 62 60 63 public Constant(Constant source, IDictionary<Guid, object> clonedObjects)64 : base(source, clonedObjects) {65 value = GetVariable("Value");66 }67 68 61 public override object Clone(IDictionary<Guid, object> clonedObjects) { 69 Constant clone = new Constant(this,clonedObjects);70 clone dObjects.Add(clone.Guid, clone);62 Constant clone = (Constant)base.Clone(clonedObjects); 63 clone.value = (ConstrainedDoubleData)clone.GetVariable("Value").Value; 71 64 return clone; 72 65 } 73 66 74 75 67 public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) { 76 68 base.Populate(node, restoredObjects); 77 78 value = GetVariable("Value"); 69 value = (ConstrainedDoubleData)GetVariable("Value").Value; 79 70 } 80 71 81 public override double Evaluate(Dataset dataset, int sampleIndex) {82 return ((ConstrainedDoubleData)value.Value).Data;72 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 73 return value.Data; 83 74 } 84 75 -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Cosinus.cs ¶
r2 r142 31 31 public class Cosinus : FunctionBase { 32 32 public override string Description { 33 get { return "Returns the cosinus of the first sub- operator."; }33 get { return "Returns the cosinus of the first sub-tree."; } 34 34 } 35 35 36 36 public Cosinus() 37 37 : base() { 38 39 38 // must have exactly one subfunction 40 39 AddConstraint(new NumberOfSubOperatorsConstraint(1, 1)); 41 40 } 42 41 43 public Cosinus(Cosinus source, IDictionary<Guid, object> clonedObjects)44 : base(source, clonedObjects) {42 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 43 return Math.Cos(args[0]); 45 44 } 46 47 public override double Evaluate(Dataset dataset, int sampleIndex) {48 return Math.Cos(SubFunctions[0].Evaluate(dataset, sampleIndex));49 }50 51 public override object Clone(IDictionary<Guid, object> clonedObjects) {52 Cosinus clone = new Cosinus(this, clonedObjects);53 clonedObjects.Add(clone.Guid, clone);54 return clone;55 }56 57 45 58 46 public override void Accept(IFunctionVisitor visitor) { -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Division.cs ¶
r2 r142 36 36 get { 37 37 return @"Protected division 38 Divides the result of the first sub- operator by the results of the following sub-operators.38 Divides the result of the first sub-tree by the results of the following sub-tree. 39 39 In case one of the divisors is 0 returns 0. 40 40 (/ 3) => 1/3 … … 48 48 public Division() 49 49 : base() { 50 51 50 // 2 - 3 seems like an reasonable defaut (used for +,-,*,/) (discussion with swinkler and maffenze) 52 51 AddConstraint(new NumberOfSubOperatorsConstraint(2, 3)); 53 52 } 54 53 55 public Division(Division source, IDictionary<Guid, object> clonedObjects) 56 : base(source, clonedObjects) { 57 } 58 59 public override double Evaluate(Dataset dataset, int sampleIndex) { 54 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 60 55 // (/ 3) => 1/3 61 56 // (/ 2 3) => 2/3 62 57 // (/ 3 4 5) => 3/20 63 58 64 if( SubFunctions.Count== 1) {65 double divisor = SubFunctions[0].Evaluate(dataset, sampleIndex);59 if(args.Length == 1) { 60 double divisor = args[0]; 66 61 if(Math.Abs(divisor) < EPSILON) return 0; 67 62 else return 1.0 / divisor; 68 63 } else { 69 double result = SubFunctions[0].Evaluate(dataset, sampleIndex);70 for(int i = 1; i < SubFunctions.Count; i++) {71 double divisor = SubFunctions[i].Evaluate(dataset, sampleIndex);64 double result = args[0]; 65 for(int i = 1; i < args.Length; i++) { 66 double divisor = args[i]; 72 67 if(Math.Abs(divisor) < EPSILON) return 0.0; 73 68 result /= divisor; … … 77 72 } 78 73 79 public override object Clone(IDictionary<Guid, object> clonedObjects) {80 Division clone = new Division(this, clonedObjects);81 clonedObjects.Add(clone.Guid, clone);82 return clone;83 }84 85 74 public override void Accept(IFunctionVisitor visitor) { 86 75 visitor.Visit(this); -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Equal.cs ¶
r2 r142 31 31 public override string Description { 32 32 get { 33 return @"Equal condition. Returns 1.0 if both sub- functions evaluate to the same value and 0.0 if they differ.";33 return @"Equal condition. Returns 1.0 if both sub-trees evaluate to the same value and 0.0 if they differ."; 34 34 } 35 35 } … … 40 40 } 41 41 42 public Equal(Equal source, IDictionary<Guid, object> clonedObjects) 43 : base(source, clonedObjects) { 44 } 45 46 47 public override double Evaluate(Dataset dataset, int sampleIndex) { 48 if(SubFunctions[0].Evaluate(dataset, sampleIndex) == SubFunctions[1].Evaluate(dataset, sampleIndex)) return 1.0; 42 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 43 if(args[0] == args[1]) return 1.0; 49 44 else return 0.0; 50 }51 52 public override object Clone(IDictionary<Guid, object> clonedObjects) {53 Equal clone = new Equal(this, clonedObjects);54 clonedObjects.Add(clone.Guid, clone);55 return clone;56 45 } 57 46 -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Exponential.cs ¶
r2 r142 31 31 public class Exponential : FunctionBase { 32 32 public override string Description { 33 get { return "Returns returns exponential of the first sub- operator(power(e, x))."; }33 get { return "Returns returns exponential of the first sub-tree (power(e, x))."; } 34 34 } 35 35 … … 39 39 AddConstraint(new NumberOfSubOperatorsConstraint(1, 1)); 40 40 } 41 public Exponential(Exponential source, IDictionary<Guid, object> clonedObjects) 42 : base(source, clonedObjects) { 41 42 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 43 return Math.Exp(args[0]); 43 44 } 44 45 46 public override double Evaluate(Dataset dataset, int sampleIndex) {47 return Math.Exp(SubFunctions[0].Evaluate(dataset, sampleIndex));48 }49 50 public override object Clone(IDictionary<Guid, object> clonedObjects) {51 Exponential clone = new Exponential(this, clonedObjects);52 clonedObjects.Add(clone.Guid, clone);53 return clone;54 }55 56 45 57 46 public override void Accept(IFunctionVisitor visitor) { -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/FunctionBase.cs ¶
r2 r142 29 29 30 30 namespace HeuristicLab.Functions { 31 /// <summary> 32 /// Functions are like operators except that they do not allow sub-operators and the normal form of evaluation 33 /// is to evaluate all children first. 34 /// </summary> 31 35 public abstract class FunctionBase : OperatorBase, IFunction { 32 private List<IFunction> subFunctions; 33 // instance subfunctions 34 public IList<IFunction> SubFunctions { 35 get { 36 return subFunctions; 36 37 public virtual double Evaluate(Dataset dataset, int sampleIndex, IList<IFunctionTree> subTrees) { 38 if(subTrees.Count > 0) { 39 double[] evaluationResults = new double[subTrees.Count]; 40 for(int i = 0; i < evaluationResults.Length; i++) { 41 evaluationResults[i] = subTrees[i].Evaluate(dataset, sampleIndex); 42 } 43 return Apply(dataset, sampleIndex, evaluationResults); 44 } else { 45 return Apply(dataset, sampleIndex, null); 37 46 } 38 47 } 39 48 40 // instance variables 41 private List<IVariable> variables; 42 public ICollection<IVariable> LocalVariables { 43 get { return variables.AsReadOnly(); } 49 public abstract double Apply(Dataset dataset, int sampleIndex, double[] args); 50 51 // operator-tree style evaluation is not supported for functions. 52 public override IOperation Apply(IScope scope) { 53 throw new NotSupportedException(); 44 54 } 45 46 // reference to the 'type' of the function47 private FunctionBase metaObject;48 public IFunction MetaObject {49 get { return metaObject; }50 }51 52 public FunctionBase() {53 metaObject = this; // (FunctionBase)Activator.CreateInstance(this.GetType());54 AddVariableInfo(new VariableInfo("Dataset", "Dataset from which to read samples", typeof(DoubleMatrixData), VariableKind.In));55 AddVariableInfo(new VariableInfo("SampleIndex", "Gives the row index from which to read the sample", typeof(IntData), VariableKind.In));56 AddVariableInfo(new VariableInfo("Result", "The result of the evaluation of the function", typeof(DoubleData), VariableKind.Out));57 58 subFunctions = new List<IFunction>();59 variables = new List<IVariable>();60 }61 62 public FunctionBase(FunctionBase source, IDictionary<Guid, object> clonedObjects)63 : base() {64 this.metaObject = source.metaObject;65 variables = new List<IVariable>();66 subFunctions = new List<IFunction>();67 foreach (IFunction subFunction in source.SubFunctions) {68 subFunctions.Add((IFunction)Auxiliary.Clone(subFunction, clonedObjects));69 }70 foreach (IVariable variable in source.variables) {71 variables.Add((IVariable)Auxiliary.Clone(variable, clonedObjects));72 }73 }74 75 public abstract double Evaluate(Dataset dataset, int sampleIndex);76 77 public override IOperation Apply(IScope scope) {78 DoubleData result = this.GetVariableValue<DoubleData>("Result", scope, false);79 Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);80 IntData sampleIndex = GetVariableValue<IntData>("SampleIndex", scope, true);81 result.Data = Evaluate(dataset, sampleIndex.Data);82 return null;83 }84 85 55 public virtual void Accept(IFunctionVisitor visitor) { 86 56 visitor.Visit(this); 87 57 } 88 58 59 public override IList<IOperator> SubOperators { 60 get { throw new NotSupportedException(); } 61 } 62 89 63 public override void AddSubOperator(IOperator subOperator) { 90 subFunctions.Add((IFunction)subOperator);64 throw new NotSupportedException(); 91 65 } 92 66 93 67 public override bool TryAddSubOperator(IOperator subOperator) { 94 subFunctions.Add((IFunction)subOperator); 95 bool valid = IsValid(); 96 if (!valid) { 97 subFunctions.RemoveAt(subFunctions.Count - 1); 98 } 99 100 return valid; 68 throw new NotSupportedException(); 101 69 } 102 70 103 71 public override bool TryAddSubOperator(IOperator subOperator, int index) { 104 subFunctions.Insert(index, (IFunction)subOperator); 105 bool valid = IsValid(); 106 if (!valid) { 107 subFunctions.RemoveAt(index); 108 } 109 return valid; 72 throw new NotSupportedException(); 110 73 } 111 74 112 75 public override bool TryAddSubOperator(IOperator subOperator, int index, out ICollection<IConstraint> violatedConstraints) { 113 subFunctions.Insert(index, (IFunction)subOperator); 114 bool valid = IsValid(out violatedConstraints); 115 if (!valid) { 116 subFunctions.RemoveAt(index); 117 } 118 return valid; 76 throw new NotSupportedException(); 119 77 } 120 78 121 79 public override bool TryAddSubOperator(IOperator subOperator, out ICollection<IConstraint> violatedConstraints) { 122 subFunctions.Add((IFunction)subOperator); 123 bool valid = IsValid(out violatedConstraints); 124 if (!valid) { 125 subFunctions.RemoveAt(subFunctions.Count - 1); 126 } 127 128 return valid; 80 throw new NotSupportedException(); 129 81 } 130 82 131 83 public override void AddSubOperator(IOperator subOperator, int index) { 132 subFunctions.Insert(index, (IFunction)subOperator);84 throw new NotSupportedException(); 133 85 } 134 86 135 87 public override void RemoveSubOperator(int index) { 136 if (index >= subFunctions.Count) throw new InvalidOperationException(); 137 subFunctions.RemoveAt(index); 88 throw new NotSupportedException(); 138 89 } 139 90 140 public override IList<IOperator> SubOperators {141 get { return subFunctions.ConvertAll(f => (IOperator)f); }142 }143 144 public override ICollection<IVariable> Variables {145 get {146 List<IVariable> mergedVariables = new List<IVariable>(variables);147 if (this == metaObject) {148 foreach (IVariable variable in base.Variables) {149 if (!IsLocalVariable(variable.Name)) {150 mergedVariables.Add(variable);151 }152 }153 } else {154 foreach (IVariable variable in metaObject.Variables) {155 if (!IsLocalVariable(variable.Name)) {156 mergedVariables.Add(variable);157 }158 }159 }160 return mergedVariables;161 }162 }163 164 private bool IsLocalVariable(string name) {165 foreach (IVariable variable in variables) {166 if (variable.Name == name) return true;167 }168 return false;169 }170 171 172 91 public override bool TryRemoveSubOperator(int index) { 173 IFunction removedFunction = subFunctions[index]; 174 subFunctions.RemoveAt(index); 175 bool valid = IsValid(); 176 if (!valid) { 177 subFunctions.Insert(index, removedFunction); 178 } 179 180 return valid; 92 throw new NotSupportedException(); 181 93 } 182 94 183 95 public override bool TryRemoveSubOperator(int index, out ICollection<IConstraint> violatedConstraints) { 184 IFunction removedFunction = subFunctions[index]; 185 subFunctions.RemoveAt(index); 186 bool valid = IsValid(out violatedConstraints); 187 if (!valid) { 188 subFunctions.Insert(index, removedFunction); 189 } 190 191 return valid; 192 } 193 194 public override void AddVariable(IVariable variable) { 195 if (metaObject == this) { 196 base.AddVariable(variable); 197 } else { 198 metaObject.AddVariable(variable); 199 } 200 } 201 202 public override IVariable GetVariable(string name) { 203 foreach (IVariable variable in variables) { 204 if (variable.Name == name) return variable; 205 } 206 if (metaObject == this) { 207 return base.GetVariable(name); 208 } else { 209 return metaObject.GetVariable(name); 210 } 211 } 212 213 public void AddLocalVariable(IVariable variable) { 214 variables.Add(variable); 215 } 216 217 public override void RemoveVariable(string name) { 218 foreach (IVariable variable in variables) { 219 if (variable.Name == name) { 220 variables.Remove(variable); 221 return; 222 } 223 } 224 if (metaObject == this) { 225 base.RemoveVariable(name); 226 } else { 227 metaObject.RemoveVariable(name); 228 } 229 } 230 231 public override IItem GetVariableValue(string formalName, IScope scope, bool recursiveLookup, bool throwOnError) { 232 foreach (IVariable variable in Variables) { 233 if (variable.Name == formalName) { 234 return variable.Value; 235 } 236 } 237 return metaObject.GetVariableValue(formalName, scope, recursiveLookup, throwOnError); 238 } 239 240 public override ICollection<IVariableInfo> VariableInfos { 241 get { 242 if (metaObject == this) { 243 return base.VariableInfos; 244 } else { 245 return metaObject.VariableInfos; 246 } 247 } 248 } 249 250 public override void AddVariableInfo(IVariableInfo variableInfo) { 251 if (metaObject == this) { 252 base.AddVariableInfo(variableInfo); 253 } else { 254 metaObject.AddVariableInfo(variableInfo); 255 } 256 } 257 258 public override void RemoveVariableInfo(string formalName) { 259 if (metaObject == this) { 260 base.RemoveVariableInfo(formalName); 261 } else { 262 metaObject.RemoveVariableInfo(formalName); 263 } 264 } 265 266 public override ICollection<IConstraint> Constraints { 267 get { 268 if (metaObject == this) { 269 return base.Constraints; 270 } else { 271 return metaObject.Constraints; 272 } 273 } 274 } 275 276 public override void AddConstraint(IConstraint constraint) { 277 if (metaObject == this) { 278 base.AddConstraint(constraint); 279 } else { 280 metaObject.AddConstraint(constraint); 281 } 282 } 283 public override void RemoveConstraint(IConstraint constraint) { 284 if (metaObject == this) { 285 base.RemoveConstraint(constraint); 286 } else { 287 metaObject.RemoveConstraint(constraint); 288 } 289 } 290 291 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) { 292 XmlNode node = base.GetXmlNode(name, document, persistedObjects); 293 if (metaObject != this) { 294 XmlNode functionTemplateNode = document.CreateElement("FunctionTemplate"); 295 functionTemplateNode.AppendChild(PersistenceManager.Persist(metaObject, document, persistedObjects)); 296 node.AppendChild(functionTemplateNode); 297 } 298 299 // don't need to persist the sub-functions because OperatorBase.GetXmlNode already persisted the sub-operators 300 301 // persist local variables 302 XmlNode variablesNode = document.CreateNode(XmlNodeType.Element, "LocalVariables", null); 303 foreach (IVariable variable in variables) { 304 variablesNode.AppendChild(PersistenceManager.Persist(variable, document, persistedObjects)); 305 } 306 node.AppendChild(variablesNode); 307 return node; 308 } 309 310 public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) { 311 base.Populate(node, restoredObjects); 312 XmlNode functionTemplateNode = node.SelectSingleNode("FunctionTemplate"); 313 if (functionTemplateNode != null) { 314 metaObject = (FunctionBase)PersistenceManager.Restore(functionTemplateNode.ChildNodes[0], restoredObjects); 315 } else { 316 metaObject = this; 317 } 318 // don't need to explicitly load the sub-functions because that has already been done in OperatorBase.Populate() 319 320 // load local variables 321 XmlNode variablesNode = node.SelectSingleNode("LocalVariables"); 322 323 // remove the variables that have been added in a constructor 324 variables.Clear(); 325 // load the persisted variables 326 foreach (XmlNode variableNode in variablesNode.ChildNodes) 327 variables.Add((IVariable)PersistenceManager.Restore(variableNode, restoredObjects)); 328 } 329 330 public override IView CreateView() { 331 return new FunctionView(this); 96 throw new NotSupportedException(); 332 97 } 333 98 } -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/FunctionView.Designer.cs ¶
r2 r142 22 22 using System; 23 23 namespace HeuristicLab.Functions { 24 partial class Function View {24 partial class FunctionTreeView { 25 25 /// <summary> 26 26 /// Required designer variable. … … 167 167 this.copyToClipboardMenuItem}); 168 168 this.treeNodeContextMenu.Name = "treeNodeContextMenu"; 169 this.treeNodeContextMenu.Size = new System.Drawing.Size(2 59, 26);169 this.treeNodeContextMenu.Size = new System.Drawing.Size(248, 26); 170 170 // 171 171 // copyToClipboardMenuItem 172 172 // 173 173 this.copyToClipboardMenuItem.Name = "copyToClipboardMenuItem"; 174 this.copyToClipboardMenuItem.Size = new System.Drawing.Size(2 58, 22);174 this.copyToClipboardMenuItem.Size = new System.Drawing.Size(247, 22); 175 175 this.copyToClipboardMenuItem.Text = "Copy to clip-board (Model-Analyzer)"; 176 176 this.copyToClipboardMenuItem.Click += new System.EventHandler(this.copyToClipboardMenuItem_Click); -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/FunctionView.cs ¶
r2 r142 32 32 33 33 namespace HeuristicLab.Functions { 34 public partial class Function View : ViewBase {35 private IFunction function;36 37 private IFunction selectedFunction;34 public partial class FunctionTreeView : ViewBase { 35 private IFunctionTree functionTree; 36 37 private IFunctionTree selectedBranch; 38 38 private IVariable selectedVariable; 39 39 40 40 private FunctionNameVisitor functionNameVisitor; 41 public Function View() {41 public FunctionTreeView() { 42 42 InitializeComponent(); 43 43 functionNameVisitor = new FunctionNameVisitor(); 44 44 } 45 45 46 public Function View(IFunction function)46 public FunctionTreeView(IFunctionTree functionTree) 47 47 : this() { 48 this.function = function;48 this.functionTree = functionTree; 49 49 Refresh(); 50 50 } … … 52 52 protected override void UpdateControls() { 53 53 functionTreeView.Nodes.Clear(); 54 function.Accept(functionNameVisitor);54 //functionTree.Accept(functionNameVisitor); 55 55 TreeNode rootNode = new TreeNode(); 56 rootNode.Name = function.Name;57 rootNode.Text = functionNameVisitor.Name;58 rootNode.Tag = function ;56 rootNode.Name = "TASK";// function.Name; 57 rootNode.Text = "TASK"; // functionNameVisitor.Name; 58 rootNode.Tag = functionTree; 59 59 rootNode.ContextMenuStrip = treeNodeContextMenu; 60 60 functionTreeView.Nodes.Add(rootNode); 61 61 62 foreach(IFunction subFunction in function.SubFunctions) {63 CreateTree(rootNode, sub Function);62 foreach(IFunctionTree subTree in functionTree.SubTrees) { 63 CreateTree(rootNode, subTree); 64 64 } 65 65 functionTreeView.ExpandAll(); 66 66 } 67 67 68 private void CreateTree(TreeNode rootNode, IFunction function) {68 private void CreateTree(TreeNode rootNode, IFunctionTree functionTree) { 69 69 TreeNode node = new TreeNode(); 70 function.Accept(functionNameVisitor);71 node. Tag = function;72 node. Name = function.Name;73 node.T ext = functionNameVisitor.Name;70 //functionTree.Accept(functionNameVisitor); 71 node.Name = "TASK"; // function.Name; 72 node.Text = "TASK"; // functionNameVisitor.Name; 73 node.Tag = functionTree; 74 74 node.ContextMenuStrip = treeNodeContextMenu; 75 75 rootNode.Nodes.Add(node); 76 foreach(IFunction subFunction in function.SubFunctions) {77 CreateTree(node, sub Function);76 foreach(IFunctionTree subTree in functionTree.SubTrees) { 77 CreateTree(node, subTree); 78 78 } 79 79 } … … 85 85 editButton.Enabled = false; 86 86 if(functionTreeView.SelectedNode != null && functionTreeView.SelectedNode.Tag != null) { 87 IFunction selectedFunction = (IFunction)functionTreeView.SelectedNode.Tag;88 UpdateVariablesList(selected Function);89 templateTextBox.Text = selected Function.MetaObject.Name;90 this.selected Function = selectedFunction;87 IFunctionTree selectedBranch = (IFunctionTree)functionTreeView.SelectedNode.Tag; 88 UpdateVariablesList(selectedBranch); 89 templateTextBox.Text = selectedBranch.Function.Name; 90 this.selectedBranch = selectedBranch; 91 91 editButton.Enabled = true; 92 92 } 93 93 } 94 94 95 private void UpdateVariablesList(IFunction function) {96 foreach(IVariable variable in function .LocalVariables) {95 private void UpdateVariablesList(IFunctionTree functionTree) { 96 foreach(IVariable variable in functionTree.LocalVariables) { 97 97 variablesListBox.Items.Add(variable.Name); 98 98 } … … 106 106 if(variablesListBox.SelectedItem != null) { 107 107 string selectedVariableName = (string)variablesListBox.SelectedItem; 108 selectedVariable = selected Function.GetVariable(selectedVariableName);108 selectedVariable = selectedBranch.GetLocalVariable(selectedVariableName); 109 109 variablesSplitContainer.Panel2.Controls.Clear(); 110 110 Control editor = (Control)selectedVariable.CreateView(); … … 121 121 if(functionTreeView.SelectedNode != null && functionTreeView.SelectedNode.Tag != null) { 122 122 TreeNode node = functionTreeView.SelectedNode; 123 selectedFunction.Accept(functionNameVisitor); 124 node.Text = functionNameVisitor.Name; 125 } 126 } 127 128 private void editButton_Click(object sender, EventArgs e) { 129 OperatorBaseView operatorView = new OperatorBaseView(selectedFunction.MetaObject); 130 PluginManager.ControlManager.ShowControl(operatorView); 123 // selectedFunctionTree.Accept(functionNameVisitor); 124 node.Text = "TASK"; // functionNameVisitor.Name; 125 } 126 } 127 128 protected virtual void editButton_Click(object sender, EventArgs e) { 129 PluginManager.ControlManager.ShowControl(selectedBranch.Function.CreateView()); 131 130 } 132 131 … … 135 134 if(node == null || node.Tag == null) return; 136 135 137 ModelAnalyzerExportVisitor visitor = new ModelAnalyzerExportVisitor();138 ((IFunction)node.Tag).Accept(visitor);139 Clipboard.SetText(visitor.ModelAnalyzerPrefix);136 // ModelAnalyzerExportVisitor visitor = new ModelAnalyzerExportVisitor(); 137 // ((IFunctionTree)node.Tag).Accept(visitor); 138 // Clipboard.SetText(visitor.ModelAnalyzerPrefix); 140 139 } 141 140 … … 252 251 } 253 252 254 private class ModelAnalyzerExportVisitor : IFunctionVisitor {255 private string prefix;256 private string currentIndend = "";257 public string ModelAnalyzerPrefix {258 get { return prefix; }259 }260 public void Reset() {261 prefix = "";262 }263 264 private void VisitFunction(string name, IFunction f) {265 prefix += currentIndend + "[F]"+name+"(\n";266 currentIndend += " ";267 foreach(IFunction subFunction in f.SubFunctions) {268 subFunction.Accept(this);269 prefix += ";\n";270 }271 prefix = prefix.TrimEnd(';','\n');272 prefix += ")";273 currentIndend = currentIndend.Remove(0, 2);274 }275 276 #region IFunctionVisitor Members277 278 public void Visit(IFunction function) {279 prefix += function.Name;280 }281 282 public void Visit(Addition addition) {283 VisitFunction("Addition[0]", addition);284 }285 286 public void Visit(Constant constant) {287 prefix += currentIndend + "[T]Constant(" + constant.Value.Data.ToString() + ";0;0)";288 }289 290 public void Visit(Cosinus cosinus) {291 VisitFunction("Trigonometrics[1]", cosinus);292 }293 294 public void Visit(Division division) {295 VisitFunction("Division[0]", division);296 }297 298 public void Visit(Exponential exponential) {299 VisitFunction("Exponential[0]", exponential);300 }301 302 public void Visit(Logarithm logarithm) {303 VisitFunction("Logarithm[0]", logarithm);304 }305 306 public void Visit(Multiplication multiplication) {307 VisitFunction("Multiplication[0]", multiplication);308 }309 310 public void Visit(Power power) {311 VisitFunction("Power[0]", power);312 }313 314 public void Visit(Signum signum) {315 VisitFunction("Signum[0]", signum);316 }317 318 public void Visit(Sinus sinus) {319 VisitFunction("Trigonometrics[0]", sinus);320 }321 322 public void Visit(Sqrt sqrt) {323 VisitFunction("Sqrt[0]", sqrt);324 }325 326 public void Visit(Substraction substraction) {327 VisitFunction("Substraction[0]", substraction);328 }329 330 public void Visit(Tangens tangens) {331 VisitFunction("Trigonometrics[2]", tangens);332 }333 334 public void Visit(HeuristicLab.Functions.Variable variable) {335 prefix += currentIndend + "[T]Variable(" + variable.Weight + ";" + variable.VariableIndex + ";" + -variable.SampleOffset + ")";336 }337 338 public void Visit(And and) {339 VisitFunction("Logical[0]", and);340 }341 342 public void Visit(Average average) {343 VisitFunction("N/A (average)", average);344 }345 346 public void Visit(IfThenElse ifThenElse) {347 VisitFunction("Conditional[0]", ifThenElse);348 }349 350 public void Visit(Not not) {351 VisitFunction("Logical[2]", not);352 }353 354 public void Visit(Or or) {355 VisitFunction("Logical[1]", or);356 }357 358 public void Visit(Xor xor) {359 VisitFunction("N/A (xor)", xor);360 }361 362 public void Visit(Equal equal) {363 VisitFunction("Boolean[2]", equal);364 }365 366 public void Visit(LessThan lessThan) {367 VisitFunction("Boolean[0]", lessThan);368 }369 370 #endregion371 }253 //private class ModelAnalyzerExportVisitor : IFunctionVisitor { 254 // private string prefix; 255 // private string currentIndend = ""; 256 // public string ModelAnalyzerPrefix { 257 // get { return prefix; } 258 // } 259 // public void Reset() { 260 // prefix = ""; 261 // } 262 263 // private void VisitFunction(string name, IFunction f) { 264 // prefix += currentIndend + "[F]"+name+"(\n"; 265 // currentIndend += " "; 266 // foreach(IFunction subFunction in f.SubTrees) { 267 // subFunction.Accept(this); 268 // prefix += ";\n"; 269 // } 270 // prefix = prefix.TrimEnd(';','\n'); 271 // prefix += ")"; 272 // currentIndend = currentIndend.Remove(0, 2); 273 // } 274 275 // #region IFunctionVisitor Members 276 277 // public void Visit(IFunction function) { 278 // prefix += function.Name; 279 // } 280 281 // public void Visit(Addition addition) { 282 // VisitFunction("Addition[0]", addition); 283 // } 284 285 // public void Visit(Constant constant) { 286 // prefix += currentIndend + "[T]Constant(" + constant.Value.Data.ToString() + ";0;0)"; 287 // } 288 289 // public void Visit(Cosinus cosinus) { 290 // VisitFunction("Trigonometrics[1]", cosinus); 291 // } 292 293 // public void Visit(Division division) { 294 // VisitFunction("Division[0]", division); 295 // } 296 297 // public void Visit(Exponential exponential) { 298 // VisitFunction("Exponential[0]", exponential); 299 // } 300 301 // public void Visit(Logarithm logarithm) { 302 // VisitFunction("Logarithm[0]", logarithm); 303 // } 304 305 // public void Visit(Multiplication multiplication) { 306 // VisitFunction("Multiplication[0]", multiplication); 307 // } 308 309 // public void Visit(Power power) { 310 // VisitFunction("Power[0]", power); 311 // } 312 313 // public void Visit(Signum signum) { 314 // VisitFunction("Signum[0]", signum); 315 // } 316 317 // public void Visit(Sinus sinus) { 318 // VisitFunction("Trigonometrics[0]", sinus); 319 // } 320 321 // public void Visit(Sqrt sqrt) { 322 // VisitFunction("Sqrt[0]", sqrt); 323 // } 324 325 // public void Visit(Substraction substraction) { 326 // VisitFunction("Substraction[0]", substraction); 327 // } 328 329 // public void Visit(Tangens tangens) { 330 // VisitFunction("Trigonometrics[2]", tangens); 331 // } 332 333 // public void Visit(HeuristicLab.Functions.Variable variable) { 334 // prefix += currentIndend + "[T]Variable(" + variable.Weight + ";" + variable.VariableIndex + ";" + -variable.SampleOffset + ")"; 335 // } 336 337 // public void Visit(And and) { 338 // VisitFunction("Logical[0]", and); 339 // } 340 341 // public void Visit(Average average) { 342 // VisitFunction("N/A (average)", average); 343 // } 344 345 // public void Visit(IfThenElse ifThenElse) { 346 // VisitFunction("Conditional[0]", ifThenElse); 347 // } 348 349 // public void Visit(Not not) { 350 // VisitFunction("Logical[2]", not); 351 // } 352 353 // public void Visit(Or or) { 354 // VisitFunction("Logical[1]", or); 355 // } 356 357 // public void Visit(Xor xor) { 358 // VisitFunction("N/A (xor)", xor); 359 // } 360 361 // public void Visit(Equal equal) { 362 // VisitFunction("Boolean[2]", equal); 363 // } 364 365 // public void Visit(LessThan lessThan) { 366 // VisitFunction("Boolean[0]", lessThan); 367 // } 368 369 // #endregion 370 //} 372 371 373 372 } -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/HeuristicLab.Functions.csproj ¶
r30 r142 52 52 <Compile Include="Average.cs" /> 53 53 <Compile Include="Constant.cs" /> 54 <Compile Include="FunctionTree.cs" /> 55 <Compile Include="IFunctionTree.cs" /> 56 <Compile Include="ProgrammableFunction.cs" /> 54 57 <Compile Include="Equal.cs" /> 55 58 <Compile Include="FunctionView.cs"> … … 99 102 <Name>HeuristicLab.Data</Name> 100 103 </ProjectReference> 104 <ProjectReference Include="..\HeuristicLab.Operators.Programmable\HeuristicLab.Operators.Programmable.csproj"> 105 <Project>{E3CCBFC6-900C-41B6-AFB8-6646DB097435}</Project> 106 <Name>HeuristicLab.Operators.Programmable</Name> 107 </ProjectReference> 101 108 <ProjectReference Include="..\HeuristicLab.PluginInfrastructure\HeuristicLab.PluginInfrastructure.csproj"> 102 109 <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project> -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/IFunction.cs ¶
r2 r142 28 28 namespace HeuristicLab.Functions { 29 29 public interface IFunction : IOperator { 30 IList<IFunction> SubFunctions { get;} 31 ICollection<IVariable> LocalVariables { get; } 32 IFunction MetaObject { get; } 33 double Evaluate(Dataset dataset, int sampleIndex); 30 double Evaluate(Dataset dataset, int sampleIndex, IList<IFunctionTree> subTrees); 31 double Apply(Dataset dataset, int sampleIndex, double[] args); 34 32 void Accept(IFunctionVisitor visitor); 35 33 } -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/IfThenElse.cs ¶
r2 r142 31 31 public override string Description { 32 32 get { 33 return @"Returns the result of the second branch if the first branch evaluates to a value >=0.5 and the result34 of the third branch if the first branch evaluates to <0.5.";33 return @"Returns the result of the second sub-tree if the first sub-tree evaluates to a value < 0.5 and the result 34 of the third sub-tree if the first sub-tree evaluates to >= 0.5."; 35 35 } 36 36 } … … 41 41 } 42 42 43 public IfThenElse(IfThenElse source, IDictionary<Guid, object> clonedObjects) 44 : base(source, clonedObjects) { 45 } 46 47 48 public override double Evaluate(Dataset dataset, int sampleIndex) { 49 double condition = Math.Round(SubFunctions[0].Evaluate(dataset, sampleIndex)); 50 if(condition >= .5) return SubFunctions[2].Evaluate(dataset, sampleIndex); 51 else if(condition < .5) return SubFunctions[1].Evaluate(dataset, sampleIndex); 43 // 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); 52 48 else return double.NaN; 53 49 } 54 50 55 public override object Clone(IDictionary<Guid, object> clonedObjects) { 56 IfThenElse clone = new IfThenElse(this, clonedObjects); 57 clonedObjects.Add(clone.Guid, clone); 58 return clone; 51 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 52 throw new NotImplementedException(); 59 53 } 60 54 -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/LessThan.cs ¶
r2 r142 31 31 public override string Description { 32 32 get { 33 return @"Less-than condition. Returns 1.0 if the value of the first sub- function is less than the value of the second sub-functionand 0.0 otherwise.";33 return @"Less-than condition. Returns 1.0 if the value of the first sub-tree is less than the value of the second sub-tree and 0.0 otherwise."; 34 34 } 35 35 } … … 40 40 } 41 41 42 public LessThan(LessThan source, IDictionary<Guid, object> clonedObjects) 43 : base(source, clonedObjects) { 44 } 45 46 47 public override double Evaluate(Dataset dataset, int sampleIndex) { 48 if(SubFunctions[0].Evaluate(dataset, sampleIndex) < SubFunctions[1].Evaluate(dataset, sampleIndex)) return 1.0; 42 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 43 if(args[0] < args[1]) return 1.0; 49 44 else return 0.0; 50 }51 52 public override object Clone(IDictionary<Guid, object> clonedObjects) {53 LessThan clone = new LessThan(this, clonedObjects);54 clonedObjects.Add(clone.Guid, clone);55 return clone;56 45 } 57 46 -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Logarithm.cs ¶
r2 r142 31 31 public class Logarithm : FunctionBase { 32 32 public override string Description { 33 get { return "Returns the natural (base e) logarithm of the first sub- operator."; }33 get { return "Returns the natural (base e) logarithm of the first sub-tree."; } 34 34 } 35 35 … … 40 40 } 41 41 42 public Logarithm(Logarithm source, IDictionary<Guid, object> clonedObjects) 43 : base(source, clonedObjects) { 44 } 45 46 public override object Clone(IDictionary<Guid, object> clonedObjects) { 47 Logarithm clone = new Logarithm(this, clonedObjects); 48 clonedObjects.Add(clone.Guid, clone); 49 return clone; 50 } 51 52 public override double Evaluate(Dataset dataset, int sampleIndex) { 53 return Math.Log(SubFunctions[0].Evaluate(dataset, sampleIndex)); 42 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 43 return Math.Log(args[0]); 54 44 } 55 45 -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Multiplication.cs ¶
r2 r142 33 33 public override string Description { 34 34 get { 35 return @"Returns the product of the results of all sub- operators.35 return @"Returns the product of the results of all sub-tree. 36 36 (* 3) => 3 37 37 (* 2 3) => 6 … … 46 46 } 47 47 48 public Multiplication(Multiplication source, IDictionary<Guid, object> clonedObjects) 49 : base(source, clonedObjects) { 50 } 51 52 53 public override double Evaluate(Dataset dataset, int sampleIndex) { 48 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 54 49 // (* 3) => 3 55 50 // (* 2 3) => 6 56 51 // (* 3 4 5) => 60 57 52 double result = 1.0; 58 for(int i = SubFunctions.Count - 1; i >= 0; i--) {59 result *= SubFunctions[i].Evaluate(dataset, sampleIndex);53 for(int i = 0; i < args.Length; i++) { 54 result *= args[i]; 60 55 } 61 56 return result; 62 }63 64 public override object Clone(IDictionary<Guid, object> clonedObjects) {65 Multiplication clone = new Multiplication(this, clonedObjects);66 clonedObjects.Add(clone.Guid, clone);67 return clone;68 57 } 69 58 -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Not.cs ¶
r2 r142 31 31 public override string Description { 32 32 get { 33 return @"Logical NOT operation. Only defined for sub- operator-results 0.0 and 1.0.";33 return @"Logical NOT operation. Only defined for sub-tree-results 0.0 and 1.0."; 34 34 } 35 35 } … … 40 40 } 41 41 42 public Not(Not source, IDictionary<Guid, object> clonedObjects) 43 : base(source, clonedObjects) { 44 } 45 46 47 public override double Evaluate(Dataset dataset, int sampleIndex) { 48 double result = Math.Round(SubFunctions[0].Evaluate(dataset, sampleIndex)); 42 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 43 double result = Math.Round(args[0]); 49 44 if(result == 0.0) return 1.0; 50 45 else if(result == 1.0) return 0.0; 51 46 else return double.NaN; 52 }53 54 public override object Clone(IDictionary<Guid, object> clonedObjects) {55 Not clone = new Not(this, clonedObjects);56 clonedObjects.Add(clone.Guid, clone);57 return clone;58 47 } 59 48 -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Or.cs ¶
r2 r142 31 31 public override string Description { 32 32 get { 33 return @"Logical OR operation. Only defined for sub-operator-results 0.0 and 1.0."; 33 return @"Logical OR operation. Only defined for sub-tree-results 0.0 and 1.0. 34 Special form, evaluation stops at first sub-tree that evaluates to 1.0 (true)."; 34 35 } 35 36 } … … 40 41 } 41 42 42 public Or(Or source, IDictionary<Guid, object> clonedObjects) 43 : base(source, clonedObjects) { 44 } 45 46 47 public override double Evaluate(Dataset dataset, int sampleIndex) { 48 foreach(IFunction subFunction in SubFunctions) { 49 double result = Math.Round(subFunction.Evaluate(dataset, sampleIndex)); 50 if(result == 1.0) return 1.0; 43 public override double Evaluate(Dataset dataset, int sampleIndex, IList<IFunctionTree> subTrees) { 44 foreach(IFunctionTree subTree in subTrees) { 45 double result = Math.Round(subTree.Evaluate(dataset, sampleIndex)); 46 if(result == 1.0) return 1.0; // sub-tree evaluates to 1.0 (true) return 1.0 51 47 else if(result != 0.0) return double.NaN; 52 48 } 49 // all sub-trees evaluated to 0.0 (false) return false 53 50 return 0.0; 54 51 } 55 52 56 public override object Clone(IDictionary<Guid, object> clonedObjects) { 57 Or clone = new Or(this, clonedObjects); 58 clonedObjects.Add(clone.Guid, clone); 59 return clone; 53 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 54 throw new NotImplementedException(); 60 55 } 61 62 56 public override void Accept(IFunctionVisitor visitor) { 63 57 visitor.Visit(this); -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Power.cs ¶
r2 r142 31 31 public class Power : FunctionBase { 32 32 public override string Description { 33 get { return "Returns the result of the first sub- operator to the power of the second sub-operator(power(x, y))."; }33 get { return "Returns the result of the first sub-tree to the power of the second sub-tree (power(x, y))."; } 34 34 } 35 35 … … 40 40 } 41 41 42 public Power(Power source, IDictionary<Guid, object> clonedObjects)43 : base(source, clonedObjects) {42 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 43 return Math.Pow(args[0], args[1]); 44 44 } 45 46 47 public override double Evaluate(Dataset dataset, int sampleIndex) {48 return Math.Pow(SubFunctions[0].Evaluate(dataset, sampleIndex), SubFunctions[1].Evaluate(dataset, sampleIndex));49 }50 51 public override object Clone(IDictionary<Guid, object> clonedObjects) {52 Power clone = new Power(this, clonedObjects);53 clonedObjects.Add(clone.Guid, clone);54 return clone;55 }56 57 45 58 46 public override void Accept(IFunctionVisitor visitor) { -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Signum.cs ¶
r2 r142 31 31 public class Signum : FunctionBase { 32 32 public override string Description { 33 get { return "Returns the signum of the first sub- operator."; }33 get { return "Returns the signum of the first sub-tree."; } 34 34 } 35 35 … … 40 40 } 41 41 42 public Signum(Signum source, IDictionary<Guid, object> clonedObjects) 43 : base(source, clonedObjects) { 44 } 45 46 47 public override double Evaluate(Dataset dataset, int sampleIndex) { 48 double value = SubFunctions[0].Evaluate(dataset, sampleIndex); 42 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 43 double value = args[0]; 49 44 if(value < 0) return -1; 50 45 if(value > 0) return 1; 51 46 return 0; 52 47 } 53 54 public override object Clone(IDictionary<Guid, object> clonedObjects) {55 Signum clone = new Signum(this, clonedObjects);56 clonedObjects.Add(clone.Guid, clone);57 return clone;58 }59 60 48 61 49 public override void Accept(IFunctionVisitor visitor) { -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Sinus.cs ¶
r2 r142 31 31 public class Sinus : FunctionBase { 32 32 public override string Description { 33 get { return "Returns the sinus of the first sub- operator."; }33 get { return "Returns the sinus of the first sub-tree."; } 34 34 } 35 35 … … 40 40 } 41 41 42 public Sinus(Sinus source, IDictionary<Guid, object> clonedObjects)43 : base(source, clonedObjects) {42 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 43 return Math.Sin(args[0]); 44 44 } 45 46 47 public override double Evaluate(Dataset dataset, int sampleIndex) {48 return Math.Sin(SubFunctions[0].Evaluate(dataset, sampleIndex));49 }50 51 public override object Clone(IDictionary<Guid, object> clonedObjects) {52 Sinus clone = new Sinus(this, clonedObjects);53 clonedObjects.Add(clone.Guid, clone);54 return clone;55 }56 57 45 58 46 public override void Accept(IFunctionVisitor visitor) { -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Sqrt.cs ¶
r2 r142 31 31 public class Sqrt : FunctionBase { 32 32 public override string Description { 33 get { return "Returns the square root of the first sub- operator."; }33 get { return "Returns the square root of the first sub-tree."; } 34 34 } 35 35 … … 40 40 } 41 41 42 public Sqrt(Sqrt source, IDictionary<Guid, object> clonedObjects) 43 : base(source, clonedObjects) { 42 43 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 44 return Math.Sqrt(args[0]); 44 45 } 45 46 47 public override double Evaluate(Dataset dataset, int sampleIndex) {48 return Math.Sqrt(SubFunctions[0].Evaluate(dataset, sampleIndex));49 }50 51 public override object Clone(IDictionary<Guid, object> clonedObjects) {52 Sqrt clone = new Sqrt(this, clonedObjects);53 clonedObjects.Add(clone.Guid, clone);54 return clone;55 }56 57 46 58 47 public override void Accept(IFunctionVisitor visitor) { -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Substraction.cs ¶
r2 r142 33 33 public override string Description { 34 34 get { 35 return @"Substracts the results of sub- operators 2..n from the result of the first sub-operator.35 return @"Substracts the results of sub-tree 2..n from the result of the first sub-tree. 36 36 (- 3) => -3 37 37 (- 2 3) => -1 … … 46 46 } 47 47 48 public Substraction(Substraction source, IDictionary<Guid, object> clonedObjects)49 : base(source, clonedObjects) {50 }51 48 52 53 public override double Evaluate(Dataset dataset, int sampleIndex) { 54 55 if(SubFunctions.Count == 1) { 56 return -SubFunctions[0].Evaluate(dataset, sampleIndex); 49 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 50 if(args.Length == 1) { 51 return -args[0]; 57 52 } else { 58 double result = SubFunctions[0].Evaluate(dataset, sampleIndex);59 for(int i = 1; i < SubFunctions.Count; i++) {60 result -= SubFunctions[i].Evaluate(dataset, sampleIndex);53 double result = args[0]; 54 for(int i = 1; i < args.Length; i++) { 55 result -= args[i]; 61 56 } 62 57 return result; 63 58 } 64 }65 66 public override object Clone(IDictionary<Guid, object> clonedObjects) {67 Substraction clone = new Substraction(this, clonedObjects);68 clonedObjects.Add(clone.Guid, clone);69 return clone;70 59 } 71 60 -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Tangens.cs ¶
r2 r142 31 31 public class Tangens : FunctionBase { 32 32 public override string Description { 33 get { return "Returns the tangens of the first sub- operator."; }33 get { return "Returns the tangens of the first sub-tree."; } 34 34 } 35 35 … … 40 40 } 41 41 42 public Tangens(Tangens source, IDictionary<Guid, object> clonedObjects)43 : base(source, clonedObjects) {42 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 43 return Math.Tan(args[0]); 44 44 } 45 46 public override double Evaluate(Dataset dataset, int sampleIndex) {47 return Math.Tan(SubFunctions[0].Evaluate(dataset, sampleIndex));48 }49 50 public override object Clone(IDictionary<Guid, object> clonedObjects) {51 Tangens clone = new Tangens(this, clonedObjects);52 clonedObjects.Add(clone.Guid, clone);53 return clone;54 }55 56 45 57 46 public override void Accept(IFunctionVisitor visitor) { -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Variable.cs ¶
r133 r142 64 64 65 65 variable = new ConstrainedIntData(); 66 Add LocalVariable(new HeuristicLab.Core.Variable("Variable", variable));66 AddVariable(new HeuristicLab.Core.Variable("Variable", variable)); 67 67 68 68 weight = new ConstrainedDoubleData(); 69 69 // initialize a totally arbitrary range for the weight = [-20.0, 20.0] 70 70 weight.AddConstraint(new DoubleBoundedConstraint(-20.0, 20.0)); 71 Add LocalVariable(new HeuristicLab.Core.Variable("Weight", weight));71 AddVariable(new HeuristicLab.Core.Variable("Weight", weight)); 72 72 73 73 sampleOffset = new ConstrainedIntData(); 74 74 // initialize a totally arbitrary default range for sampleoffset = [-10, 10] 75 75 sampleOffset.AddConstraint(new IntBoundedConstraint(0, 0)); 76 Add LocalVariable(new HeuristicLab.Core.Variable("SampleOffset", sampleOffset));76 AddVariable(new HeuristicLab.Core.Variable("SampleOffset", sampleOffset)); 77 77 78 78 // samplefeature can't have suboperators … … 80 80 } 81 81 82 public Variable(Variable source, IDictionary<Guid, object> clonedObjects) 83 : base(source, clonedObjects) { 84 82 public override object Clone(IDictionary<Guid, object> clonedObjects) { 83 HeuristicLab.Functions.Variable clone = (HeuristicLab.Functions.Variable)base.Clone(clonedObjects); 84 clone.variable = (ConstrainedIntData)clone.GetVariable("Variable").Value; 85 clone.weight = (ConstrainedDoubleData)clone.GetVariable("Weight").Value; 86 clone.sampleOffset = (ConstrainedIntData)clone.GetVariable("SampleOffset").Value; 87 return clone; 88 } 89 90 public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) { 91 base.Populate(node, restoredObjects); 85 92 variable = (ConstrainedIntData)GetVariable("Variable").Value; 86 93 weight = (ConstrainedDoubleData)GetVariable("Weight").Value; … … 88 95 } 89 96 90 public override object Clone(IDictionary<Guid, object> clonedObjects) { 91 Variable clone = new Variable(this, clonedObjects); 92 clonedObjects.Add(clone.Guid, clone); 93 return clone; 94 } 95 96 public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) { 97 base.Populate(node, restoredObjects); 98 99 variable = (ConstrainedIntData)GetVariable("Variable").Value; 100 weight = (ConstrainedDoubleData)GetVariable("Weight").Value; 101 sampleOffset = (ConstrainedIntData)GetVariable("SampleOffset").Value; 102 } 103 104 105 public override double Evaluate(Dataset dataset, int sampleIndex) { 97 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 106 98 // local variables 107 99 int v = variable.Data; -
TabularUnified branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/Xor.cs ¶
r2 r142 31 31 public override string Description { 32 32 get { 33 return @"Logical XOR operation. Only defined for sub- operator-results 0.0 and 1.0.";33 return @"Logical XOR operation. Only defined for sub-tree-results 0.0 and 1.0."; 34 34 } 35 35 } … … 40 40 } 41 41 42 public Xor(Xor source, IDictionary<Guid, object> clonedObjects) 43 : base(source, clonedObjects) { 44 } 45 46 47 public override double Evaluate(Dataset dataset, int sampleIndex) { 48 double r0 = Math.Round(SubFunctions[0].Evaluate(dataset, sampleIndex)); 49 double r1 = Math.Round(SubFunctions[1].Evaluate(dataset, sampleIndex)); 50 if((r0 == 0.0 && r1 == 0.0) || 51 (r0 == 1.0 && r1 == 1.0)) return 0.0; 52 else if((r0 == 0.0 && r1 == 1.0) || 53 (r0 == 1.0 && r1 == 0.0)) return 1.0; 54 else return double.NaN; 55 } 56 57 public override object Clone(IDictionary<Guid, object> clonedObjects) { 58 Xor clone = new Xor(this, clonedObjects); 59 clonedObjects.Add(clone.Guid, clone); 60 return clone; 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; 61 46 } 62 47
Note: See TracChangeset
for help on using the changeset viewer.