Changeset 155 for trunk/sources/HeuristicLab.Functions
- Timestamp:
- 04/22/08 18:05:14 (17 years ago)
- Location:
- trunk/sources/HeuristicLab.Functions
- Files:
-
- 27 edited
- 3 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Functions/Addition.cs
r2 r155 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 -
trunk/sources/HeuristicLab.Functions/And.cs
r2 r155 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) { 49 double result = Math.Round(subFunction.Evaluate(dataset, sampleIndex)); 50 if(result == 0.0) return 0.0; 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 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 -
trunk/sources/HeuristicLab.Functions/Average.cs
r2 r155 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 -
trunk/sources/HeuristicLab.Functions/Constant.cs
r2 r155 31 31 namespace HeuristicLab.Functions { 32 32 public class Constant : FunctionBase { 33 34 private IVariable value; 33 public static readonly string VALUE = "Value"; 35 34 36 35 public override string Description { … … 38 37 } 39 38 40 public ConstrainedDoubleData Value {41 get {42 return (ConstrainedDoubleData)value.Value;43 }44 }45 46 39 public Constant() 47 40 : base() { 48 AddVariableInfo(new VariableInfo( "Value", "The constant value", typeof(ConstrainedDoubleData), VariableKind.None));49 GetVariableInfo( "Value").Local = true;41 AddVariableInfo(new VariableInfo(VALUE, "The constant value", typeof(ConstrainedDoubleData), VariableKind.None)); 42 GetVariableInfo(VALUE).Local = true; 50 43 51 44 ConstrainedDoubleData valueData = new ConstrainedDoubleData(); 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 variable 56 value = new HeuristicLab.Core.Variable("Value", valueData); 57 AddLocalVariable(value); 47 HeuristicLab.Core.Variable value = new HeuristicLab.Core.Variable(VALUE, valueData); 48 AddVariable(value); 58 49 59 50 // constant can't have suboperators … … 61 52 } 62 53 63 public Constant(Constant source, IDictionary<Guid, object> clonedObjects)64 : base(source, clonedObjects) {65 value = GetVariable("Value");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; 66 57 } 67 58 68 public override object Clone(IDictionary<Guid, object> clonedObjects) { 69 Constant clone = new Constant(this, clonedObjects); 70 clonedObjects.Add(clone.Guid, clone); 71 return clone; 72 } 73 74 75 public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) { 76 base.Populate(node, restoredObjects); 77 78 value = GetVariable("Value"); 79 } 80 81 public override double Evaluate(Dataset dataset, int sampleIndex) { 82 return ((ConstrainedDoubleData)value.Value).Data; 59 // can't apply a constant 60 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 61 throw new NotSupportedException(); 83 62 } 84 63 -
trunk/sources/HeuristicLab.Functions/Cosinus.cs
r2 r155 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) { -
trunk/sources/HeuristicLab.Functions/Division.cs
r2 r155 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); -
trunk/sources/HeuristicLab.Functions/Equal.cs
r2 r155 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 -
trunk/sources/HeuristicLab.Functions/Exponential.cs
r2 r155 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) { -
trunk/sources/HeuristicLab.Functions/FunctionBase.cs
r2 r155 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, 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); 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 private static readonly List<IOperator> emptySubOperatorList = new List<IOperator>(); 60 public override IList<IOperator> SubOperators { 61 get { return emptySubOperatorList; } 62 } 63 89 64 public override void AddSubOperator(IOperator subOperator) { 90 subFunctions.Add((IFunction)subOperator);65 throw new NotSupportedException(); 91 66 } 92 67 93 68 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; 69 throw new NotSupportedException(); 101 70 } 102 71 103 72 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; 73 throw new NotSupportedException(); 110 74 } 111 75 112 76 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; 77 throw new NotSupportedException(); 119 78 } 120 79 121 80 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; 81 throw new NotSupportedException(); 129 82 } 130 83 131 84 public override void AddSubOperator(IOperator subOperator, int index) { 132 subFunctions.Insert(index, (IFunction)subOperator);85 throw new NotSupportedException(); 133 86 } 134 87 135 88 public override void RemoveSubOperator(int index) { 136 if (index >= subFunctions.Count) throw new InvalidOperationException(); 137 subFunctions.RemoveAt(index); 89 throw new NotSupportedException(); 138 90 } 139 91 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 92 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; 93 throw new NotSupportedException(); 181 94 } 182 95 183 96 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); 97 throw new NotSupportedException(); 332 98 } 333 99 } -
trunk/sources/HeuristicLab.Functions/FunctionView.Designer.cs
r2 r155 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); -
trunk/sources/HeuristicLab.Functions/FunctionView.cs
r2 r155 30 30 using HeuristicLab.Core; 31 31 using HeuristicLab.PluginInfrastructure; 32 using HeuristicLab.Data; 32 33 33 34 namespace HeuristicLab.Functions { 34 public partial class Function View : ViewBase {35 private IFunction function;36 37 private IFunction selectedFunction;35 public partial class FunctionTreeView : ViewBase { 36 private IFunctionTree functionTree; 37 38 private IFunctionTree selectedBranch; 38 39 private IVariable selectedVariable; 39 40 40 41 private FunctionNameVisitor functionNameVisitor; 41 public Function View() {42 public FunctionTreeView() { 42 43 InitializeComponent(); 43 44 functionNameVisitor = new FunctionNameVisitor(); 44 45 } 45 46 46 public Function View(IFunction function)47 public FunctionTreeView(IFunctionTree functionTree) 47 48 : this() { 48 this.function = function;49 this.functionTree = functionTree; 49 50 Refresh(); 50 51 } … … 52 53 protected override void UpdateControls() { 53 54 functionTreeView.Nodes.Clear(); 54 function .Accept(functionNameVisitor);55 functionNameVisitor.Visit(functionTree); 55 56 TreeNode rootNode = new TreeNode(); 56 rootNode.Name = function .Name;57 rootNode.Name = functionTree.Function.Name; 57 58 rootNode.Text = functionNameVisitor.Name; 58 rootNode.Tag = function ;59 rootNode.Tag = functionTree; 59 60 rootNode.ContextMenuStrip = treeNodeContextMenu; 60 61 functionTreeView.Nodes.Add(rootNode); 61 62 62 foreach(IFunction subFunction in function.SubFunctions) {63 CreateTree(rootNode, sub Function);63 foreach(IFunctionTree subTree in functionTree.SubTrees) { 64 CreateTree(rootNode, subTree); 64 65 } 65 66 functionTreeView.ExpandAll(); 66 67 } 67 68 68 private void CreateTree(TreeNode rootNode, IFunction function) {69 private void CreateTree(TreeNode rootNode, IFunctionTree functionTree) { 69 70 TreeNode node = new TreeNode(); 70 function.Accept(functionNameVisitor); 71 node.Tag = function; 72 node.Name = function.Name; 71 functionNameVisitor.Visit(functionTree); 72 node.Name = functionTree.Function.Name; 73 73 node.Text = functionNameVisitor.Name; 74 node.Tag = functionTree; 74 75 node.ContextMenuStrip = treeNodeContextMenu; 75 76 rootNode.Nodes.Add(node); 76 foreach(IFunction subFunction in function.SubFunctions) {77 CreateTree(node, sub Function);77 foreach(IFunctionTree subTree in functionTree.SubTrees) { 78 CreateTree(node, subTree); 78 79 } 79 80 } … … 85 86 editButton.Enabled = false; 86 87 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;88 IFunctionTree selectedBranch = (IFunctionTree)functionTreeView.SelectedNode.Tag; 89 UpdateVariablesList(selectedBranch); 90 templateTextBox.Text = selectedBranch.Function.Name; 91 this.selectedBranch = selectedBranch; 91 92 editButton.Enabled = true; 92 93 } 93 94 } 94 95 95 private void UpdateVariablesList(IFunction function) {96 foreach(IVariable variable in function .LocalVariables) {96 private void UpdateVariablesList(IFunctionTree functionTree) { 97 foreach(IVariable variable in functionTree.LocalVariables) { 97 98 variablesListBox.Items.Add(variable.Name); 98 99 } … … 106 107 if(variablesListBox.SelectedItem != null) { 107 108 string selectedVariableName = (string)variablesListBox.SelectedItem; 108 selectedVariable = selected Function.GetVariable(selectedVariableName);109 selectedVariable = selectedBranch.GetLocalVariable(selectedVariableName); 109 110 variablesSplitContainer.Panel2.Controls.Clear(); 110 111 Control editor = (Control)selectedVariable.CreateView(); … … 121 122 if(functionTreeView.SelectedNode != null && functionTreeView.SelectedNode.Tag != null) { 122 123 TreeNode node = functionTreeView.SelectedNode; 123 selectedFunction.Accept(functionNameVisitor);124 functionNameVisitor.Visit(selectedBranch); 124 125 node.Text = functionNameVisitor.Name; 125 126 } 126 127 } 127 128 128 private void editButton_Click(object sender, EventArgs e) { 129 OperatorBaseView operatorView = new OperatorBaseView(selectedFunction.MetaObject); 130 PluginManager.ControlManager.ShowControl(operatorView); 129 protected virtual void editButton_Click(object sender, EventArgs e) { 130 PluginManager.ControlManager.ShowControl(selectedBranch.Function.CreateView()); 131 131 } 132 132 … … 136 136 137 137 ModelAnalyzerExportVisitor visitor = new ModelAnalyzerExportVisitor(); 138 ((IFunction)node.Tag).Accept(visitor);138 visitor.Visit((IFunctionTree)node.Tag); 139 139 Clipboard.SetText(visitor.ModelAnalyzerPrefix); 140 140 } … … 142 142 private class FunctionNameVisitor : IFunctionVisitor { 143 143 string name; 144 IFunctionTree currentBranch; 144 145 145 146 public string Name { … … 147 148 } 148 149 150 public void Visit(IFunctionTree tree) { 151 currentBranch = tree; 152 tree.Function.Accept(this); 153 } 154 149 155 #region IFunctionVisitor Members 150 151 156 public void Visit(IFunction function) { 152 157 name = function.Name; … … 158 163 159 164 public void Visit(Constant constant) { 160 name = constant.Value+ "";165 name = ((ConstrainedDoubleData)(currentBranch.GetLocalVariable(HeuristicLab.Functions.Constant.VALUE).Value)).Data + ""; 161 166 } 162 167 … … 207 212 public void Visit(Variable variable) { 208 213 string timeOffset = ""; 209 if(variable.SampleOffset < 0) { 210 timeOffset = "(t" + variable.SampleOffset + ")"; 211 } else if(variable.SampleOffset > 0) { 212 timeOffset = "(t+" + variable.SampleOffset + ")"; 214 int sampleOffset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.OFFSET).Value).Data; 215 int variableIndex = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.INDEX).Value).Data; 216 double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.WEIGHT).Value).Data; 217 if(sampleOffset < 0) { 218 timeOffset = "(t" + sampleOffset + ")"; 219 } else if(sampleOffset > 0) { 220 timeOffset = "(t+" + sampleOffset + ")"; 213 221 } else { 214 222 timeOffset = ""; 215 223 } 216 name = "Var" + variable .VariableIndex + timeOffset + " * " + variable.Weight;224 name = "Var" + variableIndex + timeOffset + " * " + weight; 217 225 } 218 226 … … 255 263 private string prefix; 256 264 private string currentIndend = ""; 265 private IFunctionTree currentBranch; 257 266 public string ModelAnalyzerPrefix { 258 267 get { return prefix; } … … 263 272 264 273 private void VisitFunction(string name, IFunction f) { 265 prefix += currentIndend + "[F]"+name+"(\n"; 274 prefix += currentIndend + "[F]" + name + "(\n"; 275 } 276 277 #region IFunctionVisitor Members 278 279 public void Visit(IFunction function) { 280 prefix += function.Name; 281 } 282 283 public void Visit(Addition addition) { 284 VisitFunction("Addition[0]", addition); 285 } 286 287 public void Visit(Constant constant) { 288 double value = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Constant.VALUE).Value).Data; 289 prefix += currentIndend + "[T]Constant(" + value + ";0;0)"; 290 } 291 292 public void Visit(Cosinus cosinus) { 293 VisitFunction("Trigonometrics[1]", cosinus); 294 } 295 296 public void Visit(Division division) { 297 VisitFunction("Division[0]", division); 298 } 299 300 public void Visit(Exponential exponential) { 301 VisitFunction("Exponential[0]", exponential); 302 } 303 304 public void Visit(Logarithm logarithm) { 305 VisitFunction("Logarithm[0]", logarithm); 306 } 307 308 public void Visit(Multiplication multiplication) { 309 VisitFunction("Multiplication[0]", multiplication); 310 } 311 312 public void Visit(Power power) { 313 VisitFunction("Power[0]", power); 314 } 315 316 public void Visit(Signum signum) { 317 VisitFunction("Signum[0]", signum); 318 } 319 320 public void Visit(Sinus sinus) { 321 VisitFunction("Trigonometrics[0]", sinus); 322 } 323 324 public void Visit(Sqrt sqrt) { 325 VisitFunction("Sqrt[0]", sqrt); 326 } 327 328 public void Visit(Substraction substraction) { 329 VisitFunction("Substraction[0]", substraction); 330 } 331 332 public void Visit(Tangens tangens) { 333 VisitFunction("Trigonometrics[2]", tangens); 334 } 335 336 public void Visit(HeuristicLab.Functions.Variable variable) { 337 double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.WEIGHT).Value).Data; 338 double index = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.INDEX).Value).Data; 339 double offset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.OFFSET).Value).Data; 340 341 prefix += currentIndend + "[T]Variable(" + weight + ";" + index + ";" + -offset + ")"; 342 } 343 344 public void Visit(And and) { 345 VisitFunction("Logical[0]", and); 346 } 347 348 public void Visit(Average average) { 349 VisitFunction("N/A (average)", average); 350 } 351 352 public void Visit(IfThenElse ifThenElse) { 353 VisitFunction("Conditional[0]", ifThenElse); 354 } 355 356 public void Visit(Not not) { 357 VisitFunction("Logical[2]", not); 358 } 359 360 public void Visit(Or or) { 361 VisitFunction("Logical[1]", or); 362 } 363 364 public void Visit(Xor xor) { 365 VisitFunction("N/A (xor)", xor); 366 } 367 368 public void Visit(Equal equal) { 369 VisitFunction("Boolean[2]", equal); 370 } 371 372 public void Visit(LessThan lessThan) { 373 VisitFunction("Boolean[0]", lessThan); 374 } 375 #endregion 376 377 public void Visit(IFunctionTree functionTree) { 378 currentBranch = functionTree; 379 functionTree.Function.Accept(this); 266 380 currentIndend += " "; 267 foreach(IFunction subFunction in f.SubFunctions) {268 subFunction.Accept(this);381 foreach(IFunctionTree subTree in functionTree.SubTrees) { 382 Visit(subTree); 269 383 prefix += ";\n"; 270 384 } 271 prefix = prefix.TrimEnd(';', '\n');385 prefix = prefix.TrimEnd(';', '\n'); 272 386 prefix += ")"; 273 387 currentIndend = currentIndend.Remove(0, 2); 274 388 } 275 276 #region IFunctionVisitor Members 277 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 #endregion 371 } 372 389 } 373 390 } 374 391 } -
trunk/sources/HeuristicLab.Functions/HeuristicLab.Functions.csproj
r30 r155 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> -
trunk/sources/HeuristicLab.Functions/IFunction.cs
r2 r155 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, IFunctionTree tree); 31 double Apply(Dataset dataset, int sampleIndex, double[] args); 34 32 void Accept(IFunctionVisitor visitor); 35 33 } -
trunk/sources/HeuristicLab.Functions/IfThenElse.cs
r2 r155 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, 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); 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 -
trunk/sources/HeuristicLab.Functions/LessThan.cs
r2 r155 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 -
trunk/sources/HeuristicLab.Functions/Logarithm.cs
r2 r155 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 -
trunk/sources/HeuristicLab.Functions/Multiplication.cs
r2 r155 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 -
trunk/sources/HeuristicLab.Functions/Not.cs
r2 r155 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 -
trunk/sources/HeuristicLab.Functions/Or.cs
r2 r155 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, IFunctionTree tree) { 44 foreach(IFunctionTree subTree in tree.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); -
trunk/sources/HeuristicLab.Functions/Power.cs
r2 r155 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) { -
trunk/sources/HeuristicLab.Functions/Signum.cs
r2 r155 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) { -
trunk/sources/HeuristicLab.Functions/Sinus.cs
r2 r155 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) { -
trunk/sources/HeuristicLab.Functions/Sqrt.cs
r2 r155 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) { -
trunk/sources/HeuristicLab.Functions/Substraction.cs
r2 r155 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 -
trunk/sources/HeuristicLab.Functions/Tangens.cs
r2 r155 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) { -
trunk/sources/HeuristicLab.Functions/Variable.cs
r133 r155 32 32 public class Variable : FunctionBase { 33 33 34 private ConstrainedIntData variable; 35 private ConstrainedDoubleData weight; 36 private ConstrainedIntData sampleOffset; 37 38 public double SampleOffset { 39 get { return sampleOffset.Data; } 40 } 41 42 public int VariableIndex { 43 get { return variable.Data; } 44 } 45 46 public double Weight { 47 get { return weight.Data; } 48 } 34 public static readonly string WEIGHT = "Weight"; 35 public static readonly string OFFSET = "SampleOffset"; 36 public static readonly string INDEX = "Variable"; 49 37 50 38 public override string Description { … … 56 44 public Variable() 57 45 : base() { 58 AddVariableInfo(new VariableInfo( "Variable", "Index of the variable in the dataset representing this feature", typeof(ConstrainedIntData), VariableKind.None));59 GetVariableInfo( "Variable").Local = true;60 AddVariableInfo(new VariableInfo( "Weight", "Weight is multiplied to the feature value", typeof(ConstrainedDoubleData), VariableKind.None));61 GetVariableInfo( "Weight").Local = true;62 AddVariableInfo(new VariableInfo( "SampleOffset", "SampleOffset is added to the sample index", typeof(ConstrainedIntData), VariableKind.None));63 GetVariableInfo( "SampleOffset").Local = true;46 AddVariableInfo(new VariableInfo(INDEX, "Index of the variable in the dataset representing this feature", typeof(ConstrainedIntData), VariableKind.None)); 47 GetVariableInfo(INDEX).Local = true; 48 AddVariableInfo(new VariableInfo(WEIGHT, "Weight is multiplied to the feature value", typeof(ConstrainedDoubleData), VariableKind.None)); 49 GetVariableInfo(WEIGHT).Local = true; 50 AddVariableInfo(new VariableInfo(OFFSET, "SampleOffset is added to the sample index", typeof(ConstrainedIntData), VariableKind.None)); 51 GetVariableInfo(OFFSET).Local = true; 64 52 65 variable = new ConstrainedIntData(); 66 AddLocalVariable(new HeuristicLab.Core.Variable("Variable", variable)); 67 68 weight = new ConstrainedDoubleData(); 53 ConstrainedDoubleData weight = new ConstrainedDoubleData(); 69 54 // initialize a totally arbitrary range for the weight = [-20.0, 20.0] 70 55 weight.AddConstraint(new DoubleBoundedConstraint(-20.0, 20.0)); 71 Add LocalVariable(new HeuristicLab.Core.Variable("Weight", weight));56 AddVariable(new HeuristicLab.Core.Variable(WEIGHT, weight)); 72 57 73 sampleOffset = new ConstrainedIntData(); 58 ConstrainedIntData variable = new ConstrainedIntData(); 59 AddVariable(new HeuristicLab.Core.Variable(INDEX, variable)); 60 61 ConstrainedIntData sampleOffset = new ConstrainedIntData(); 74 62 // initialize a totally arbitrary default range for sampleoffset = [-10, 10] 75 63 sampleOffset.AddConstraint(new IntBoundedConstraint(0, 0)); 76 Add LocalVariable(new HeuristicLab.Core.Variable("SampleOffset", sampleOffset));64 AddVariable(new HeuristicLab.Core.Variable(OFFSET, sampleOffset)); 77 65 78 // samplefeature can't have suboperators66 // variable can't have suboperators 79 67 AddConstraint(new NumberOfSubOperatorsConstraint(0, 0)); 80 68 } 81 69 82 public Variable(Variable source, IDictionary<Guid, object> clonedObjects) 83 : base(source, clonedObjects) { 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; 84 76 85 variable = (ConstrainedIntData)GetVariable("Variable").Value; 86 weight = (ConstrainedDoubleData)GetVariable("Weight").Value; 87 sampleOffset = (ConstrainedIntData)GetVariable("SampleOffset").Value; 77 if(sampleIndex + offset < 0 || sampleIndex + offset >= dataset.Rows) return double.NaN; 78 return w * dataset.GetValue(sampleIndex + offset, v); 88 79 } 89 80 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) { 106 // local variables 107 int v = variable.Data; 108 double w = weight.Data; 109 int offset = sampleOffset.Data; 110 111 if(sampleIndex+offset<0 || sampleIndex+offset>=dataset.Rows) return double.NaN; 112 return w * dataset.GetValue(sampleIndex + offset, v); 81 // can't apply a variable 82 public override double Apply(Dataset dataset, int sampleIndex, double[] args) { 83 throw new NotSupportedException(); 113 84 } 114 85 -
trunk/sources/HeuristicLab.Functions/Xor.cs
r2 r155 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.