Changeset 142
- Timestamp:
- 04/21/08 00:17:54 (17 years ago)
- Location:
- branches/FunctionsAndStructIdRefactoring
- Files:
-
- 3 added
- 1 deleted
- 38 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
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 -
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 -
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 -
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 -
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) { -
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); -
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 -
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) { -
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 } -
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); -
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 } -
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> -
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 } -
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 -
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 -
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 -
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 -
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 -
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); -
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) { -
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) { -
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) { -
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) { -
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 -
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) { -
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; -
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 -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Evaluation/CoefficientOfDeterminationEvaluator.cs
r128 r142 34 34 public override string Description { 35 35 get { 36 return @" Applies 'OperatorTree' toall samples of 'Dataset' and calculates36 return @"Evaluates 'FunctionTree' for all samples of 'Dataset' and calculates 37 37 the 'coefficient of determination' of estimated values vs. real values of 'TargetVariable'."; 38 38 } … … 43 43 } 44 44 45 public override double Evaluate(IScope scope, IFunction function, int targetVariable, Dataset dataset) {45 public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) { 46 46 double errorsSquaredSum = 0.0; 47 47 double originalDeviationTotalSumOfSquares = 0.0; 48 48 double targetMean = dataset.GetMean(targetVariable); 49 49 for(int sample = 0; sample < dataset.Rows; sample++) { 50 double estimated = function .Evaluate(dataset, sample);50 double estimated = functionTree.Evaluate(dataset, sample); 51 51 double original = dataset.GetValue(sample, targetVariable); 52 52 if(!double.IsNaN(original) && !double.IsInfinity(original)) { -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Evaluation/EarlyStoppingMeanSquaredErrorEvaluator.cs
r136 r142 34 34 public override string Description { 35 35 get { 36 return @"Evaluates ' OperatorTree' for all samples of the dataset and calculates the mean-squared-error36 return @"Evaluates 'FunctionTree' for all samples of the dataset and calculates the mean-squared-error 37 37 for the estimated values vs. the real values of 'TargetVariable'. 38 38 This operator stops the computation as soon as an upper limit for the mean-squared-error is reached."; … … 45 45 } 46 46 47 public override double Evaluate(IScope scope, IFunction function, int targetVariable, Dataset dataset) {47 public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) { 48 48 double qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, false).Data; 49 49 double errorsSquaredSum = 0; 50 50 double targetMean = dataset.GetMean(targetVariable); 51 51 for(int sample = 0; sample < dataset.Rows; sample++) { 52 double estimated = function .Evaluate(dataset, sample);52 double estimated = functionTree.Evaluate(dataset, sample); 53 53 double original = dataset.GetValue(sample, targetVariable); 54 54 if(double.IsNaN(estimated) || double.IsInfinity(estimated)) { -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Evaluation/GPEvaluatorBase.cs
r135 r142 36 36 public GPEvaluatorBase() 37 37 : base() { 38 AddVariableInfo(new VariableInfo(" OperatorTree", "The function tree that should be evaluated", typeof(IFunction), VariableKind.In));38 AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be evaluated", typeof(IFunctionTree), VariableKind.In)); 39 39 AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In)); 40 40 AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In)); … … 46 46 int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data; 47 47 Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true); 48 IFunction function = GetVariableValue<IFunction>("OperatorTree", scope, true);48 IFunctionTree functionTree = GetVariableValue<IFunctionTree>("FunctionTree", scope, true); 49 49 this.maximumPunishment = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data * dataset.GetRange(targetVariable); 50 50 51 double result = Evaluate(scope, function , targetVariable, dataset);51 double result = Evaluate(scope, functionTree, targetVariable, dataset); 52 52 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Quality"), new DoubleData(result))); 53 53 return null; 54 54 } 55 55 56 public abstract double Evaluate(IScope scope, IFunction function, int targetVariable, Dataset dataset);56 public abstract double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset); 57 57 } 58 58 } -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Evaluation/MeanSquaredErrorEvaluator.cs
r128 r142 34 34 public override string Description { 35 35 get { 36 return @"Evaluates ' OperatorTree' for all samples of 'DataSet' and calculates the mean-squared-error36 return @"Evaluates 'FunctionTree' for all samples of 'DataSet' and calculates the mean-squared-error 37 37 for the estimated values vs. the real values of 'TargetVariable'."; 38 38 } … … 43 43 } 44 44 45 public override double Evaluate(IScope scope, IFunction function, int targetVariable, Dataset dataset) {45 public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) { 46 46 double errorsSquaredSum = 0; 47 47 double targetMean = dataset.GetMean(targetVariable); 48 48 for(int sample = 0; sample < dataset.Rows; sample++) { 49 double estimated = function .Evaluate(dataset, sample);49 double estimated = functionTree.Evaluate(dataset, sample); 50 50 double original = dataset.GetValue(sample, targetVariable); 51 51 if(double.IsNaN(estimated) || double.IsInfinity(estimated)) { -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Evaluation/VarianceAccountedForEvaluator.cs
r128 r142 34 34 public override string Description { 35 35 get { 36 return @"Evaluates ' OperatorTree' for all samples of 'DataSet' and calculates36 return @"Evaluates 'FunctionTree' for all samples of 'DataSet' and calculates 37 37 the variance-accounted-for quality measure for the estimated values vs. the real values of 'TargetVariable'. 38 38 … … 53 53 54 54 55 public override double Evaluate(IScope scope, IFunction function, int targetVariable, Dataset dataset) {55 public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) { 56 56 double[] errors = new double[dataset.Rows]; 57 57 double[] originalTargetVariableValues = new double[dataset.Rows]; 58 58 double targetMean = dataset.GetMean(targetVariable); 59 59 for(int sample = 0; sample < dataset.Rows; sample++) { 60 double estimated = function .Evaluate(dataset, sample);60 double estimated = functionTree.Evaluate(dataset, sample); 61 61 double original = dataset.GetValue(sample, targetVariable); 62 62 if(!double.IsNaN(original) && !double.IsInfinity(original)) { -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/HeuristicLab.StructureIdentification.csproj
r128 r142 67 67 <Compile Include="Manipulation\OnePointShaker.cs" /> 68 68 <Compile Include="Manipulation\SubstituteSubTreeManipulation.cs" /> 69 <Compile Include="PopulationAnalyser.cs" />70 69 <Compile Include="HeuristicLabStructureIdentificationPlugin.cs" /> 71 70 <Compile Include="Properties\AssemblyInfo.cs" /> -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/ChangeNodeTypeManipulation.cs
r23 r142 29 29 using HeuristicLab.Data; 30 30 using HeuristicLab.Constraints; 31 using HeuristicLab.Functions; 31 32 32 33 namespace HeuristicLab.StructureIdentification { … … 39 40 AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In)); 40 41 AddVariableInfo(new VariableInfo("BalancedTreesRate", "Determines how many trees should be balanced", typeof(DoubleData), VariableKind.In)); 41 AddVariableInfo(new VariableInfo(" OperatorTree", "The tree to mutate", typeof(IOperator), VariableKind.In));42 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In ));43 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In ));42 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out)); 43 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out)); 44 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out)); 44 45 } 45 46 46 47 47 48 public override IOperation Apply(IScope scope) { 48 I Operator rootOperator = GetVariableValue<IOperator>("OperatorTree", scope, false);49 IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, false); 49 50 MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true); 50 51 GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true); … … 56 57 57 58 TreeGardener gardener = new TreeGardener(random, library); 58 59 IOperator parent = gardener.GetRandomParentNode(rootOperator); 60 61 IOperator selectedChild; 59 IFunctionTree parent = gardener.GetRandomParentNode(root); 60 61 IFunctionTree selectedChild; 62 62 int selectedChildIndex; 63 63 if (parent == null) { 64 64 selectedChildIndex = 0; 65 selectedChild = root Operator;66 } else { 67 selectedChildIndex = random.Next(parent.Sub Operators.Count);68 selectedChild = parent.Sub Operators[selectedChildIndex];69 } 70 71 if (selectedChild.Sub Operators.Count == 0) {72 I OperatornewTerminal = ChangeTerminalType(parent, selectedChild, selectedChildIndex, gardener, random);65 selectedChild = root; 66 } else { 67 selectedChildIndex = random.Next(parent.SubTrees.Count); 68 selectedChild = parent.SubTrees [selectedChildIndex]; 69 } 70 71 if (selectedChild.SubTrees.Count == 0) { 72 IFunctionTree newTerminal = ChangeTerminalType(parent, selectedChild, selectedChildIndex, gardener, random); 73 73 74 74 if (parent == null) { 75 75 // no parent means the new child is the initial operator 76 76 // and we have to update the value in the variable 77 scope.GetVariable(" OperatorTree").Value = newTerminal;77 scope.GetVariable("FunctionTree").Value = newTerminal; 78 78 } else { 79 parent.RemoveSub Operator(selectedChildIndex);80 parent. AddSubOperator(newTerminal, selectedChildIndex);79 parent.RemoveSubTree(selectedChildIndex); 80 parent.InsertSubTree(selectedChildIndex, newTerminal); 81 81 // updating the variable is not necessary because it stays the same 82 82 } … … 86 86 return gardener.CreateInitializationOperation(gardener.GetAllOperators(newTerminal), scope); 87 87 } else { 88 List<I Operator> uninitializedOperators;89 I Operator newFunction = ChangeFunctionType(parent, selectedChild, selectedChildIndex, gardener, random, balancedTreesRate, out uninitializedOperators);88 List<IFunctionTree> uninitializedBranches; 89 IFunctionTree newFunction = ChangeFunctionType(parent, selectedChild, selectedChildIndex, gardener, random, balancedTreesRate, out uninitializedBranches); 90 90 91 91 if (parent == null) { 92 92 // no parent means the new function is the initial operator 93 93 // and we have to update the value in the variable 94 scope.GetVariable(" OperatorTree").Value = newFunction;95 root Operator= newFunction;94 scope.GetVariable("FunctionTree").Value = newFunction; 95 root = newFunction; 96 96 } else { 97 97 // remove the old child 98 parent.RemoveSub Operator(selectedChildIndex);98 parent.RemoveSubTree(selectedChildIndex); 99 99 // add the new child as sub-tree of parent 100 parent. AddSubOperator(newFunction, selectedChildIndex);100 parent.InsertSubTree(selectedChildIndex, newFunction); 101 101 } 102 102 103 103 // recalculate size and height 104 treeSize.Data = gardener.GetTreeSize(root Operator);105 treeHeight.Data = gardener.GetTreeHeight(root Operator);104 treeSize.Data = gardener.GetTreeSize(root); 105 treeHeight.Data = gardener.GetTreeHeight(root); 106 106 107 107 // check if the size of the new tree is still in the allowed bounds … … 112 112 113 113 // check if whole tree is ok 114 if (!gardener.IsValidTree(root Operator)) {114 if (!gardener.IsValidTree(root)) { 115 115 throw new InvalidProgramException(); 116 116 } 117 117 118 118 // return a composite operation that initializes all created sub-trees 119 return gardener.CreateInitializationOperation(uninitialized Operators, scope);120 } 121 } 122 123 124 private I Operator ChangeTerminalType(IOperator parent, IOperatorchild, int childIndex, TreeGardener gardener, MersenneTwister random) {119 return gardener.CreateInitializationOperation(uninitializedBranches, scope); 120 } 121 } 122 123 124 private IFunctionTree ChangeTerminalType(IFunctionTree parent, IFunctionTree child, int childIndex, TreeGardener gardener, MersenneTwister random) { 125 125 126 126 IList<IOperator> allowedChildren; … … 138 138 } 139 139 140 private I Operator ChangeFunctionType(IOperator parent, IOperatorchild, int childIndex, TreeGardener gardener, MersenneTwister random,141 double balancedTreesRate, out List<I Operator> uninitializedOperators) {142 // since there are sub operators, we have to check which143 // and how many of the existing sub operators we can reuse140 private IFunctionTree ChangeFunctionType(IFunctionTree parent, IFunctionTree child, int childIndex, TreeGardener gardener, MersenneTwister random, 141 double balancedTreesRate, out List<IFunctionTree> uninitializedBranches) { 142 // since there are subtrees, we have to check which 143 // and how many of the existing subtrees we can reuse 144 144 145 145 // let's choose the operator we want to use instead of the old child. For this we have to determine the … … 155 155 156 156 // try to make a tree with the same arity as the old child. 157 int actualArity = child.Sub Operators.Count;157 int actualArity = child.SubTrees.Count; 158 158 // arity of the selected operator 159 159 int minArity; … … 165 165 }).ToList(); 166 166 167 I Operator newOperator = (IOperator)allowedSubOperators[random.Next(allowedSubOperators.Count)].Clone();167 IFunctionTree newTree = new FunctionTree((IOperator)allowedSubOperators[random.Next(allowedSubOperators.Count)]); 168 168 169 169 gardener.GetMinMaxArity(newOperator, out minArity, out maxArity); … … 175 175 // use the size of the smallest subtree as the maximal allowed size for new subtrees to 176 176 // prevent that we ever create trees over the MaxTreeSize limit 177 int maxSubTreeSize = child.Sub Operators.Select(subOp => gardener.GetTreeSize(subOp)).Min();177 int maxSubTreeSize = child.SubTrees.Select(subTree => gardener.GetTreeSize(subTree)).Min(); 178 178 int maxSubTreeHeight = gardener.GetTreeHeight(child) - 1; 179 179 180 180 // create a list that holds old sub-trees that we can reuse in the new tree 181 List<I Operator> availableSubOperators = new List<IOperator>(child.SubOperators);182 List<I Operator> freshSubTrees = new List<IOperator>() { newOperator};181 List<IFunctionTree> availableSubTrees = new List<IFunctionTree>(child.SubTrees); 182 List<IFunctionTree> freshSubTrees = new List<IFunctionTree>() { new FunctionTree(newOperator) }; 183 183 184 184 // randomly select the suboperators that we keep … … 189 189 // that fit in the given slot then create a new random tree and use it for the slot 190 190 IList<IOperator> allowedOperators = analyser.GetAllowedOperators(newOperator, i); 191 var matching Operators = availableSubOperators.Where(subOp => allowedOperators.Contains(subOp, new TreeGardener.OperatorEqualityComparer()));192 193 if (matching Operators.Count() > 0) {194 I Operator selectedSubOperator = matchingOperators.ElementAt(random.Next(matchingOperators.Count()));191 var matchingSubTrees = availableSubTrees.Where(subTree => allowedOperators.Contains(subTree.Function)); 192 193 if (matchingSubTrees.Count() > 0) { 194 IFunctionTree selectedSubTree = matchingSubTrees.ElementAt(random.Next(matchingTrees.Count())); 195 195 // we can just add it as suboperator 196 new Operator.AddSubOperator(selectedSubOperator, i);197 availableSub Operators.Remove(selectedSubOperator); // the operatorshouldn't be available for the following slots196 newTree.InsertSubTree(i, selectedSubTree); 197 availableSubTrees.Remove(selectedSubTree); // the branch shouldn't be available for the following slots 198 198 } else { 199 I Operator freshOperatorTree;199 IFunctionTree freshTree; 200 200 if(random.NextDouble() <= balancedTreesRate) { 201 fresh OperatorTree = gardener.CreateRandomTree(allowedOperators, maxSubTreeSize, maxSubTreeHeight, true);201 freshTree = gardener.CreateRandomTree(allowedOperators, maxSubTreeSize, maxSubTreeHeight, true); 202 202 } else { 203 fresh OperatorTree = gardener.CreateRandomTree(allowedOperators, maxSubTreeSize, maxSubTreeHeight, false);203 freshTree = gardener.CreateRandomTree(allowedOperators, maxSubTreeSize, maxSubTreeHeight, false); 204 204 } 205 freshSubTrees.AddRange(gardener.GetAllOperators(fresh OperatorTree));206 207 new Operator.AddSubOperator(freshOperatorTree, i);208 } 209 } 210 211 uninitialized Operators = freshSubTrees;212 return new Operator;205 freshSubTrees.AddRange(gardener.GetAllOperators(freshTree)); 206 207 newTree.InsertSubTree(i, freshTree); 208 } 209 } 210 211 uninitializedBranches = freshSubTrees; 212 return newTree; 213 213 } 214 214 } -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/RandomTreeCreator.cs
r23 r142 26 26 using System; 27 27 using HeuristicLab.Random; 28 using HeuristicLab.Functions; 28 29 29 30 namespace HeuristicLab.StructureIdentification { … … 40 41 AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In)); 41 42 AddVariableInfo(new VariableInfo("BalancedTreesRate", "Determines how many trees should be balanced", typeof(DoubleData), VariableKind.In)); 42 AddVariableInfo(new VariableInfo("OperatorTree", "The tree to mutate", typeof(I Operator), VariableKind.In));43 AddVariableInfo(new VariableInfo("OperatorTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In)); 43 44 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In)); 44 45 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In)); … … 56 57 int treeHeight = random.Next(1, maxTreeHeight + 1); 57 58 int treeSize = random.Next(1, maxTreeSize + 1); 58 I Operator rootOperator;59 IFunctionTree root; 59 60 if(random.NextDouble() <= balancedTreesRate) { 60 root Operator= gardener.CreateRandomTree(treeSize, treeHeight, true);61 root = gardener.CreateRandomTree(treeSize, treeHeight, true); 61 62 } else { 62 root Operator= gardener.CreateRandomTree(treeSize, treeHeight, false);63 root = gardener.CreateRandomTree(treeSize, treeHeight, false); 63 64 } 64 65 65 int actualTreeSize = gardener.GetTreeSize(root Operator);66 int actualTreeHeight = gardener.GetTreeHeight(root Operator);66 int actualTreeSize = gardener.GetTreeSize(root); 67 int actualTreeHeight = gardener.GetTreeHeight(root); 67 68 68 scope.AddVariable(new Variable("OperatorTree", rootOperator));69 scope.AddVariable(new Variable("TreeSize", new IntData(actualTreeSize)));70 scope.AddVariable(new Variable("TreeHeight", new IntData(actualTreeHeight)));69 scope.AddVariable(new HeuristicLab.Core.Variable("OperatorTree", root)); 70 scope.AddVariable(new HeuristicLab.Core.Variable("TreeSize", new IntData(actualTreeSize))); 71 scope.AddVariable(new HeuristicLab.Core.Variable("TreeHeight", new IntData(actualTreeHeight))); 71 72 72 if(!gardener.IsValidTree(root Operator)) { throw new InvalidProgramException(); }73 if(!gardener.IsValidTree(root)) { throw new InvalidProgramException(); } 73 74 74 75 if(actualTreeSize > maxTreeSize || … … 77 78 } 78 79 79 return gardener.CreateInitializationOperation(gardener.GetAllOperators(root Operator), scope);80 return gardener.CreateInitializationOperation(gardener.GetAllOperators(root), scope); 80 81 } 81 82 } -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Recombination/SinglePointCrossOver.cs
r23 r142 29 29 using HeuristicLab.Data; 30 30 using HeuristicLab.Constraints; 31 using HeuristicLab.Functions; 31 32 32 33 namespace HeuristicLab.StructureIdentification { … … 44 45 AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In)); 45 46 AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In)); 46 AddVariableInfo(new VariableInfo(" OperatorTree", "The tree to mutate", typeof(IOperator), VariableKind.In));47 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind. In));48 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind. In));47 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New)); 48 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New)); 49 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New)); 49 50 } 50 51 … … 90 91 int newTreeSize = gardener.GetTreeSize(newTree); 91 92 int newTreeHeight = gardener.GetTreeHeight(newTree); 92 child.AddVariable(new Variable(" OperatorTree", newTree));93 child.AddVariable(new Variable("FunctionTree", newTree)); 93 94 child.AddVariable(new Variable("TreeSize", new IntData(newTreeSize))); 94 95 child.AddVariable(new Variable("TreeHeight", new IntData(newTreeHeight))); … … 105 106 106 107 107 private I Operator Cross(TreeGardener gardener, IScope f, IScope g, MersenneTwister random, int maxTreeSize, int maxTreeHeight, out List<IOperator> newOperators) {108 I Operator tree0 = f.GetVariableValue<IOperator>("OperatorTree", false);108 private IFunctionTree Cross(TreeGardener gardener, IScope f, IScope g, MersenneTwister random, int maxTreeSize, int maxTreeHeight, out List<IFunctionTree> newBranches) { 109 IFunctionTree tree0 = f.GetVariableValue<IFunctionTree>("FunctionTree", false); 109 110 int tree0Height = f.GetVariableValue<IntData>("TreeHeight", false).Data; 110 111 int tree0Size = f.GetVariableValue<IntData>("TreeSize", false).Data; 111 112 112 I Operator tree1 = g.GetVariableValue<IOperator>("OperatorTree", false);113 IFunctionTree tree1 = g.GetVariableValue<IFunctionTree>("FunctionTree", false); 113 114 int tree1Height = g.GetVariableValue<IntData>("TreeHeight", false).Data; 114 115 int tree1Size = g.GetVariableValue<IntData>("TreeSize", false).Data; 115 116 116 117 if(tree0Size == 1 && tree1Size == 1) { 117 return CombineTerminals(gardener, tree0, tree1, random, maxTreeHeight, out new Operators);118 return CombineTerminals(gardener, tree0, tree1, random, maxTreeHeight, out newBranches); 118 119 } else { 119 120 // we are going to insert tree1 into tree0 at a random place so we have to make sure that tree0 is not a terminal 120 121 // in case both trees are higher than 1 we swap the trees with probability 50% 121 122 if(tree0Height == 1 || (tree1Height > 1 && random.Next(2) == 0)) { 122 I Operatortmp = tree0; tree0 = tree1; tree1 = tmp;123 IFunctionTree tmp = tree0; tree0 = tree1; tree1 = tmp; 123 124 int tmpHeight = tree0Height; tree0Height = tree1Height; tree1Height = tmpHeight; 124 125 int tmpSize = tree0Size; tree0Size = tree1Size; tree1Size = tmpSize; … … 126 127 127 128 // save the root because later on we change tree0 and tree1 while searching a valid tree configuration 128 I Operatorroot = tree0;129 IFunctionTree root = tree0; 129 130 int rootSize = tree0Size; 130 131 … … 146 147 // to merge the trees then throw an exception 147 148 // find the list of allowed indices (regarding allowed sub-operators, maxTreeSize and maxTreeHeight) 148 for(int i = 0; i < tree0.Sub Operators.Count; i++) {149 int subOperatorSize = gardener.GetTreeSize(tree0.Sub Operators[i]);149 for(int i = 0; i < tree0.SubTrees.Count; i++) { 150 int subOperatorSize = gardener.GetTreeSize(tree0.SubTrees[i]); 150 151 151 152 // the index is ok when the operator is allowed as sub-operator and we don't violate the maxSize and maxHeight constraints … … 160 161 // ok we couln't find a possible configuration given the current tree0 and tree1 161 162 // possible reasons for this are: 162 // - tree1 is not allowed as sub- operatorof tree0163 // - tree1 is not allowed as sub-tree of tree0 163 164 // - appending tree1 as child of tree0 would create a tree that exceedes the maxTreeHeight 164 165 // - replacing any child of tree0 with tree1 woulde create a tree that exceedes the maxTeeSize … … 166 167 // - go up in tree0 => the insert position allows larger trees 167 168 // - go down in tree1 => the tree that is inserted becomes smaller 168 // - however we have to get lucky to solve the 'allowed sub- operators' problem169 // - however we have to get lucky to solve the 'allowed sub-trees' problem 169 170 if(tree1Height == 1 || (tree0Level>0 && random.Next(2) == 0)) { 170 171 // go up in tree0 171 172 tree0Level--; 172 173 tree0 = gardener.GetRandomNode(root, tree0Level); 173 } else if(tree1.Sub Operators.Count > 0) {174 } else if(tree1.SubTrees.Count > 0) { 174 175 // go down in node2: 175 tree1 = tree1.Sub Operators[random.Next(tree1.SubOperators.Count)];176 tree1 = tree1.SubTrees[random.Next(tree1.SubTrees.Count)]; 176 177 tree1Size = gardener.GetTreeSize(tree1); 177 178 tree1Height = gardener.GetTreeHeight(tree1); … … 183 184 // recalculate the list of possible indices 184 185 possibleChildIndices.Clear(); 185 for(int i = 0; i < tree0.Sub Operators.Count; i++) {186 int subOperatorSize = gardener.GetTreeSize(tree0.Sub Operators[i]);186 for(int i = 0; i < tree0.SubTrees.Count; i++) { 187 int subOperatorSize = gardener.GetTreeSize(tree0.SubTrees[i]); 187 188 188 189 // when the operator is allowed as sub-operator and we don't violate the maxSize and maxHeight constraints … … 203 204 // replace the existing sub-tree at a random index in tree0 with tree1 204 205 int selectedIndex = possibleChildIndices[random.Next(possibleChildIndices.Count)]; 205 tree0.RemoveSub Operator(selectedIndex);206 tree0.AddSub Operator(tree1, selectedIndex);206 tree0.RemoveSubTree(selectedIndex); 207 tree0.AddSubTree(tree1, selectedIndex); 207 208 208 209 // no new operators where needed 209 new Operators = new List<IOperator>();210 newBranches = new List<IFunctionTree>(); 210 211 return root; 211 212 } … … 217 218 } 218 219 219 private I Operator CombineTerminals(TreeGardener gardener, IOperator f, IOperator g, MersenneTwister random, int maxTreeHeight, out List<IOperator> newOperators) {220 new Operators = new List<IOperator>();221 ICollection<IOperator> possibleParents = gardener.GetPossibleParents(new List<IOperator>() { f , g});220 private IFunctionTree CombineTerminals(TreeGardener gardener, IFunctionTree f, IFunctionTree g, MersenneTwister random, int maxTreeHeight, out List<IFunctionTree> newBranches) { 221 newBranches = new List<IFunctionTree>(); 222 ICollection<IOperator> possibleParents = gardener.GetPossibleParents(new List<IOperator>() { f.Function, g.Function }); 222 223 if(possibleParents.Count == 0) throw new InvalidProgramException(); 223 224 … … 233 234 234 235 SubOperatorsConstraintAnalyser analyser = new SubOperatorsConstraintAnalyser(); 235 analyser.AllPossibleOperators = new List<IOperator>() { g , f};236 analyser.AllPossibleOperators = new List<IOperator>() { g.Function, f.Function }; 236 237 for(int slot = 0; slot < nSlots; slot++) { 237 238 HashSet<IOperator> slotSet = new HashSet<IOperator>(analyser.GetAllowedOperators(parent, slot)); … … 248 249 var allowedOperators = GetAllowedOperators(parent, slot); 249 250 selectedOperators[slot] = gardener.CreateRandomTree(allowedOperators, 1, 1, true); 250 new Operators.AddRange(gardener.GetAllOperators(selectedOperators[slot]));251 newBranches.AddRange(gardener.GetAllOperators(selectedOperators[slot])); 251 252 } else { 252 253 IOperator selectedOperator = slotSet.ElementAt(random.Next(slotSet.Count())); -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/TreeGardener.cs
r23 r142 31 31 using HeuristicLab.Operators; 32 32 using HeuristicLab.Selection; 33 using HeuristicLab.Functions; 33 34 34 35 namespace HeuristicLab.StructureIdentification { … … 72 73 73 74 #region random initialization 74 internal I OperatorCreateRandomTree(ICollection<IOperator> allowedOperators, int maxTreeSize, int maxTreeHeight, bool balanceTrees) {75 internal IFunctionTree CreateRandomTree(ICollection<IOperator> allowedOperators, int maxTreeSize, int maxTreeHeight, bool balanceTrees) { 75 76 76 77 int minTreeHeight = allowedOperators.Select(op => ((IntData)op.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data).Min(); … … 87 88 IOperator[] possibleOperators = allowedOperators.Where(op => ((IntData)op.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data <= treeHeight && 88 89 ((IntData)op.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data <= treeSize).ToArray(); 89 IOperator selectedOperator = (IOperator)possibleOperators[random.Next(possibleOperators.Length)].Clone(); 90 91 IOperator rootOperator = CreateRandomTree(selectedOperator, treeSize, treeHeight, balanceTrees); 92 93 return rootOperator; 94 } 95 96 internal IOperator CreateRandomTree(int maxTreeSize, int maxTreeHeight, bool balanceTrees) { 90 IOperator selectedOperator = possibleOperators[random.Next(possibleOperators.Length)]; 91 92 return CreateRandomTree(selectedOperator, treeSize, treeHeight, balanceTrees); 93 } 94 95 internal IFunctionTree CreateRandomTree(int maxTreeSize, int maxTreeHeight, bool balanceTrees) { 97 96 if (balanceTrees) { 98 97 if (maxTreeHeight == 1 || maxTreeSize==1) { 99 IOperator selectedTerminal = (IOperator)terminals[random.Next(terminals.Count())].Clone();100 return selectedTerminal;98 IOperator selectedTerminal = terminals[random.Next(terminals.Count())]; 99 return new FunctionTree(selectedTerminal); 101 100 } else { 102 101 IOperator[] possibleFunctions = functions.Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight && 103 102 GetMinimalTreeSize(f) <= maxTreeSize).ToArray(); 104 IOperator selectedFunction = (IOperator)possibleFunctions[random.Next(possibleFunctions.Length)].Clone(); 105 MakeBalancedTree(selectedFunction, maxTreeSize - 1, maxTreeHeight - 1); 106 return selectedFunction; 103 IOperator selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)]; 104 FunctionTree root = new FunctionTree(selectedFunction); 105 MakeBalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1); 106 return root; 107 107 } 108 108 … … 110 110 IOperator[] possibleOperators = allOperators.Where(op => GetMinimalTreeHeight(op) <= maxTreeHeight && 111 111 GetMinimalTreeSize(op) <= maxTreeSize).ToArray(); 112 IOperator selectedOperator = (IOperator)possibleOperators[random.Next(possibleOperators.Length)].Clone(); 113 MakeUnbalancedTree(selectedOperator, maxTreeSize - 1, maxTreeHeight - 1); 114 return selectedOperator; 115 } 116 } 117 118 internal IOperator CreateRandomTree(IOperator root, int maxTreeSize, int maxTreeHeight, bool balanceTrees) { 112 IOperator selectedOperator = possibleOperators[random.Next(possibleOperators.Length)]; 113 FunctionTree root = new FunctionTree(selectedOperator); 114 MakeUnbalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1); 115 return root; 116 } 117 } 118 119 internal IFunctionTree CreateRandomTree(IOperator rootOp, int maxTreeSize, int maxTreeHeight, bool balanceTrees) { 120 IFunctionTree root = new FunctionTree(rootOp); 119 121 if (balanceTrees) { 120 122 MakeBalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1); … … 130 132 131 133 132 private void MakeUnbalancedTree(I Operatorparent, int maxTreeSize, int maxTreeHeight) {134 private void MakeUnbalancedTree(IFunctionTree parent, int maxTreeSize, int maxTreeHeight) { 133 135 if (maxTreeHeight == 0 || maxTreeSize == 0) return; 134 136 int minArity; 135 137 int maxArity; 136 GetMinMaxArity(parent , out minArity, out maxArity);138 GetMinMaxArity(parent.Function, out minArity, out maxArity); 137 139 if (maxArity >= maxTreeSize) { 138 140 maxArity = maxTreeSize; … … 142 144 int maxSubTreeSize = maxTreeSize / actualArity; 143 145 for (int i = 0; i < actualArity; i++) { 144 IOperator[] possibleOperators = GetAllowedSubOperators(parent , i).Where(op => GetMinimalTreeHeight(op) <= maxTreeHeight &&146 IOperator[] possibleOperators = GetAllowedSubOperators(parent.Function, i).Where(op => GetMinimalTreeHeight(op) <= maxTreeHeight && 145 147 GetMinimalTreeSize(op) <= maxSubTreeSize).ToArray(); 146 IOperator selectedOperator = (IOperator)possibleOperators[random.Next(possibleOperators.Length)].Clone(); 147 parent.AddSubOperator(selectedOperator, i); 148 MakeUnbalancedTree(selectedOperator, maxSubTreeSize - 1, maxTreeHeight - 1); 148 IOperator selectedOperator = possibleOperators[random.Next(possibleOperators.Length)]; 149 FunctionTree newSubTree = new FunctionTree(selectedOperator); 150 MakeUnbalancedTree(newSubTree, maxSubTreeSize - 1, maxTreeHeight - 1); 151 parent.InsertSubTree(i, newSubTree); 149 152 } 150 153 } … … 153 156 // NOTE: this method doesn't build fully balanced trees because we have constraints on the 154 157 // types of possible suboperators which can indirectly impose a limit for the depth of a given suboperator 155 private void MakeBalancedTree(I Operatorparent, int maxTreeSize, int maxTreeHeight) {158 private void MakeBalancedTree(IFunctionTree parent, int maxTreeSize, int maxTreeHeight) { 156 159 if (maxTreeHeight == 0 || maxTreeSize == 0) return; // should never happen anyway 157 160 int minArity; 158 161 int maxArity; 159 GetMinMaxArity(parent , out minArity, out maxArity);162 GetMinMaxArity(parent.Function, out minArity, out maxArity); 160 163 if (maxArity >= maxTreeSize) { 161 164 maxArity = maxTreeSize; … … 166 169 for (int i = 0; i < actualArity; i++) { 167 170 if (maxTreeHeight == 1 || maxSubTreeSize == 1) { 168 IOperator[] possibleTerminals = GetAllowedSubOperators(parent , i).Where(171 IOperator[] possibleTerminals = GetAllowedSubOperators(parent.Function, i).Where( 169 172 op => GetMinimalTreeHeight(op) <= maxTreeHeight && 170 173 GetMinimalTreeSize(op) <= maxSubTreeSize && 171 174 IsTerminal(op)).ToArray(); 172 IOperator selectedTerminal = (IOperator)possibleTerminals[random.Next(possibleTerminals.Length)].Clone(); 173 parent.AddSubOperator(selectedTerminal, i); 175 IOperator selectedTerminal = possibleTerminals[random.Next(possibleTerminals.Length)]; 176 IFunctionTree newTree = new FunctionTree(selectedTerminal); 177 parent.InsertSubTree(i, newTree); 174 178 } else { 175 IOperator[] possibleFunctions = GetAllowedSubOperators(parent , i).Where(179 IOperator[] possibleFunctions = GetAllowedSubOperators(parent.Function, i).Where( 176 180 op => GetMinimalTreeHeight(op) <= maxTreeHeight && 177 181 GetMinimalTreeSize(op) <= maxSubTreeSize && 178 182 !IsTerminal(op)).ToArray(); 179 IOperator selectedFunction = (IOperator)possibleFunctions[random.Next(possibleFunctions.Length)].Clone(); 180 parent.AddSubOperator(selectedFunction, i); 181 MakeBalancedTree(selectedFunction, maxSubTreeSize - 1, maxTreeHeight - 1); 183 IOperator selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)]; 184 FunctionTree newTree = new FunctionTree(selectedFunction); 185 parent.InsertSubTree(i, newTree); 186 MakeBalancedTree(newTree, maxSubTreeSize - 1, maxTreeHeight - 1); 182 187 } 183 188 } … … 185 190 } 186 191 187 internal CompositeOperation CreateInitializationOperation(ICollection<I Operator> operators, IScope scope) {192 internal CompositeOperation CreateInitializationOperation(ICollection<IFunctionTree> trees, IScope scope) { 188 193 // needed for the parameter shaking operation 189 194 CompositeOperation initializationOperation = new CompositeOperation(); 190 195 Scope tempScope = new Scope("Temp. initialization scope"); 191 196 192 var parametric Operators = operators.Where(o => o.GetVariable(GPOperatorLibrary.INITIALIZATION) != null);193 194 foreach (I Operator op in parametricOperators) {197 var parametricTrees = trees.Where(o => o.Function.GetVariable(GPOperatorLibrary.INITIALIZATION) != null); 198 199 foreach (IFunctionTree tree in parametricTrees) { 195 200 // enqueue an initialization operation for each operator with local variables 196 IOperator initialization = (IOperator) op.GetVariable(GPOperatorLibrary.INITIALIZATION).Value;201 IOperator initialization = (IOperator)tree.Function.GetVariable(GPOperatorLibrary.INITIALIZATION).Value; 197 202 Scope initScope = new Scope(); 198 203 199 204 // copy the local variables into a temporary scope used for initialization 200 foreach (VariableInfo info in op.VariableInfos) { 201 if (info.Local) { 202 initScope.AddVariable(op.GetVariable(info.FormalName)); 203 } 205 foreach (IVariable variable in tree.LocalVariables) { 206 initScope.AddVariable(variable); 204 207 } 205 208 … … 223 226 224 227 #region tree information gathering 225 internal int GetTreeSize(I Operatortree) {226 return 1 + tree.Sub Operators.Sum(f => GetTreeSize(f));227 } 228 229 internal int GetTreeHeight(I Operatortree) {230 if (tree.Sub Operators.Count == 0) return 1;231 return 1 + tree.Sub Operators.Max(f => GetTreeHeight(f));232 } 233 234 internal I Operator GetRandomParentNode(IOperatortree) {235 List<I Operator> parentNodes = new List<IOperator>();228 internal int GetTreeSize(IFunctionTree tree) { 229 return 1 + tree.SubTrees.Sum(f => GetTreeSize(f)); 230 } 231 232 internal int GetTreeHeight(IFunctionTree tree) { 233 if (tree.SubTrees.Count == 0) return 1; 234 return 1 + tree.SubTrees.Max(f => GetTreeHeight(f)); 235 } 236 237 internal IFunctionTree GetRandomParentNode(IFunctionTree tree) { 238 List<IFunctionTree> parentNodes = new List<IFunctionTree>(); 236 239 237 240 // add null for the parent of the root node 238 241 parentNodes.Add(null); 239 242 240 TreeForEach(tree, delegate(I Operator op) {241 if ( op.SubOperators.Count > 0) {242 parentNodes.Add( op);243 TreeForEach(tree, delegate(IFunctionTree possibleParentNode) { 244 if (possibleParentNode.SubTrees.Count > 0) { 245 parentNodes.Add(possibleParentNode); 243 246 } 244 247 }); … … 289 292 } 290 293 291 internal ICollection<I Operator> GetAllOperators(IOperatorroot) {292 List<I Operator> allOps = new List<IOperator>();294 internal ICollection<IFunctionTree> GetAllOperators(IFunctionTree root) { 295 List<IFunctionTree> allOps = new List<IFunctionTree>(); 293 296 TreeForEach(root, t => { allOps.Add(t); }); 294 297 return allOps; … … 305 308 /// <param name="op">operater that is searched in the tree</param> 306 309 /// <returns></returns> 307 internal int GetNodeLevel(I Operator tree, IOperatorop) {310 internal int GetNodeLevel(IFunctionTree tree, IFunctionTree op) { 308 311 return GetNodeLevelHelper(tree, op, 1); 309 312 } 310 313 311 private int GetNodeLevelHelper(I Operator tree, IOperatorop, int level) {314 private int GetNodeLevelHelper(IFunctionTree tree, IFunctionTree op, int level) { 312 315 if (op == tree) return level; 313 316 314 foreach (I Operator subTree in tree.SubOperators) {317 foreach (IFunctionTree subTree in tree.SubTrees) { 315 318 int result = GetNodeLevelHelper(subTree, op, level + 1); 316 319 if (result != -1) return result; … … 320 323 } 321 324 322 internal bool IsValidTree(I Operatortree) {323 if (!tree. IsValid())325 internal bool IsValidTree(IFunctionTree tree) { 326 if (!tree.Function.IsValid()) 324 327 return false; 325 foreach (I Operator subTree in tree.SubOperators) {326 if (!subTree. IsValid())328 foreach (IFunctionTree subTree in tree.SubTrees) { 329 if (!subTree.Function.IsValid()) 327 330 return false; 328 331 } … … 332 335 333 336 // returns a random node from the specified level in the tree 334 internal I Operator GetRandomNode(IOperatortree, int level) {337 internal IFunctionTree GetRandomNode(IFunctionTree tree, int level) { 335 338 if (level == 0) return tree; 336 List<I Operator> nodes = GetOperatorsAtLevel(tree, level);339 List<IFunctionTree> nodes = GetOperatorsAtLevel(tree, level); 337 340 return nodes[random.Next(nodes.Count)]; 338 341 } … … 349 352 } 350 353 351 private void TreeForEach(I Operator tree, Action<IOperator> action) {354 private void TreeForEach(IFunctionTree tree, Action<IFunctionTree> action) { 352 355 action(tree); 353 foreach (I Operator child in tree.SubOperators) {354 TreeForEach( child, action);355 } 356 } 357 358 private List<I Operator> GetOperatorsAtLevel(IOperatortree, int level) {359 if (level == 1) return new List<I Operator>(tree.SubOperators);360 361 List<I Operator> result = new List<IOperator>();362 foreach (I Operator subOperator in tree.SubOperators) {363 result.AddRange(GetOperatorsAtLevel(sub Operator, level - 1));356 foreach (IFunctionTree subTree in tree.SubTrees) { 357 TreeForEach(subTree, action); 358 } 359 } 360 361 private List<IFunctionTree> GetOperatorsAtLevel(IFunctionTree tree, int level) { 362 if (level == 1) return new List<IFunctionTree>(tree.SubTrees); 363 364 List<IFunctionTree> result = new List<IFunctionTree>(); 365 foreach (IFunctionTree subTree in tree.SubTrees) { 366 result.AddRange(GetOperatorsAtLevel(subTree, level - 1)); 364 367 } 365 368 return result; … … 371 374 internal class OperatorEqualityComparer : IEqualityComparer<IOperator> { 372 375 #region IEqualityComparer<IOperator> Members 373 374 376 public bool Equals(IOperator x, IOperator y) { 375 return ((StringData)x.GetVariable(GPOperatorLibrary.TYPE_ID).Value).Data == 377 return x==y || 378 ((StringData)x.GetVariable(GPOperatorLibrary.TYPE_ID).Value).Data == 376 379 ((StringData)y.GetVariable(GPOperatorLibrary.TYPE_ID).Value).Data; 377 380 } … … 380 383 return ((StringData)obj.GetVariable(GPOperatorLibrary.TYPE_ID).Value).Data.GetHashCode(); 381 384 } 382 383 385 #endregion 384 386 } -
branches/FunctionsAndStructIdRefactoring/HeuristicLab/UpdateLocalInstallation.cmd
r45 r142 1 set target= C:\Program Files\HeuristicLab 3.01 set target=G:\Program Files\HeuristicLab 3.0 2 2 3 3 copy "HeuristicLab.exe" "%target%"
Note: See TracChangeset
for help on using the changeset viewer.