Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/22/08 18:05:14 (17 years ago)
Author:
gkronber
Message:

merged FunctionsAndStructIdRefactoring-branch (r142, r143, r144, r145, r146, r147, r148, r149, r152, r153) back into the trunk (ticket #112)

Location:
trunk/sources/HeuristicLab.Functions
Files:
27 edited
3 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Functions/Addition.cs

    r2 r155  
    3333    public override string Description {
    3434      get {
    35         return @"Returns the sum of all sub-operator results.
     35        return @"Returns the sum of all sub-tree results.
    3636    (+ 3) => 3
    3737    (+ 2 3) => 5
     
    4646    }
    4747
    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) {
    5449      // (+ 3) => 3
    5550      // (+ 2 3) => 5
    5651      // (+ 3 4 5) => 12
    5752      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];
    6055      }
    6156      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;
    6857    }
    6958
  • trunk/sources/HeuristicLab.Functions/And.cs

    r2 r155  
    3131    public override string Description {
    3232      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.
     34AND is a special form, sub-trees are evaluated from the first to the last. Evaluation is
     35stopped as soon as one of the sub-trees evaluates to 0.0 (false).";
    3436      }
    3537    }
     
    4042    }
    4143
    42     public And(And source, IDictionary<Guid, object> clonedObjects)
    43       : base(source, clonedObjects) {
    44     }
    45 
    46 
    47     public override double Evaluate(Dataset dataset, int sampleIndex) {
    48       foreach(IFunction subFunction in SubFunctions) {
    49         double result = Math.Round(subFunction.Evaluate(dataset, sampleIndex));
    50         if(result == 0.0) return 0.0;
     44    // special form
     45    public override double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) {
     46      foreach(IFunctionTree subTree in tree.SubTrees) {
     47        double result = Math.Round(subTree.Evaluate(dataset, sampleIndex));
     48        if(result == 0.0) return 0.0; // one sub-tree is 0.0 (false) => return false
    5149        else if(result != 1.0) return double.NaN;
    5250      }
     51      // all sub-trees evaluated to 1.0 (true) => return 1.0 (true)
    5352      return 1.0;
    5453    }
    5554
    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();
    6057    }
    6158
  • trunk/sources/HeuristicLab.Functions/Average.cs

    r2 r155  
    3131    public override string Description {
    3232      get {
    33         return @"Returns the average (arithmetic mean) of all sub-operator results.";
     33        return @"Returns the average (arithmetic mean) of all sub-tree results.";
    3434      }
    3535    }
     
    4040    }
    4141
    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) {
    4843      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];
    5146      }
    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;
    5948    }
    6049
  • trunk/sources/HeuristicLab.Functions/Constant.cs

    r2 r155  
    3131namespace HeuristicLab.Functions {
    3232  public class Constant : FunctionBase {
    33 
    34     private IVariable value;
     33    public static readonly string VALUE = "Value";
    3534
    3635    public override string Description {
     
    3837    }
    3938
    40     public ConstrainedDoubleData Value {
    41       get {
    42         return (ConstrainedDoubleData)value.Value;
    43       }
    44     }
    45 
    4639    public Constant()
    4740      : base() {
    48       AddVariableInfo(new VariableInfo("Value", "The constant value", typeof(ConstrainedDoubleData), VariableKind.None));
    49       GetVariableInfo("Value").Local = true;
     41      AddVariableInfo(new VariableInfo(VALUE, "The constant value", typeof(ConstrainedDoubleData), VariableKind.None));
     42      GetVariableInfo(VALUE).Local = true;
    5043
    5144      ConstrainedDoubleData valueData = new ConstrainedDoubleData();
    5245      // initialize a default range for the contant value
    5346      valueData.AddConstraint(new DoubleBoundedConstraint(-20.0, 20.0));
    54 
    55       // create the local variable
    56       value = new HeuristicLab.Core.Variable("Value", valueData);
    57       AddLocalVariable(value);
     47      HeuristicLab.Core.Variable value = new HeuristicLab.Core.Variable(VALUE, valueData);
     48      AddVariable(value);
    5849
    5950      // constant can't have suboperators
     
    6152    }
    6253
    63     public Constant(Constant source, IDictionary<Guid, object> clonedObjects)
    64       : base(source, clonedObjects) {
    65       value = GetVariable("Value");
     54    // constant is evaluated directly. Evaluation reads the local variable value from the tree and returns
     55    public override double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) {
     56      return ((ConstrainedDoubleData)tree.GetLocalVariable(VALUE).Value).Data;
    6657    }
    6758
    68     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    69       Constant clone = new Constant(this, clonedObjects);
    70       clonedObjects.Add(clone.Guid, clone);
    71       return clone;
    72     }
    73 
    74 
    75     public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
    76       base.Populate(node, restoredObjects);
    77 
    78       value = GetVariable("Value");
    79     }
    80 
    81     public override double Evaluate(Dataset dataset, int sampleIndex) {
    82       return ((ConstrainedDoubleData)value.Value).Data;
     59    // can't apply a constant
     60    public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
     61      throw new NotSupportedException();
    8362    }
    8463
  • trunk/sources/HeuristicLab.Functions/Cosinus.cs

    r2 r155  
    3131  public class Cosinus : FunctionBase {
    3232    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."; }
    3434    }
    3535
    3636    public Cosinus()
    3737      : base() {
    38 
    3938      // must have exactly one subfunction
    4039      AddConstraint(new NumberOfSubOperatorsConstraint(1, 1));
    4140    }
    4241
    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]);
    4544    }
    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 
    5745
    5846    public override void Accept(IFunctionVisitor visitor) {
  • trunk/sources/HeuristicLab.Functions/Division.cs

    r2 r155  
    3636      get {
    3737        return @"Protected division
    38 Divides the result of the first sub-operator by the results of the following sub-operators.
     38Divides the result of the first sub-tree by the results of the following sub-tree.
    3939In case one of the divisors is 0 returns 0.
    4040    (/ 3) => 1/3
     
    4848    public Division()
    4949      : base() {
    50 
    5150      // 2 - 3 seems like an reasonable defaut (used for +,-,*,/) (discussion with swinkler and maffenze)
    5251      AddConstraint(new NumberOfSubOperatorsConstraint(2, 3));
    5352    }
    5453
    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) {
    6055      // (/ 3) => 1/3
    6156      // (/ 2 3) => 2/3
    6257      // (/ 3 4 5) => 3/20
    6358
    64       if(SubFunctions.Count == 1) {
    65         double divisor = SubFunctions[0].Evaluate(dataset, sampleIndex);
     59      if(args.Length == 1) {
     60        double divisor = args[0];
    6661        if(Math.Abs(divisor) < EPSILON) return 0;
    6762        else return 1.0 / divisor;
    6863      } 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];
    7267          if(Math.Abs(divisor) < EPSILON) return 0.0;
    7368          result /= divisor;
     
    7772    }
    7873
    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 
    8574    public override void Accept(IFunctionVisitor visitor) {
    8675      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/Equal.cs

    r2 r155  
    3131    public override string Description {
    3232      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.";
    3434      }
    3535    }
     
    4040    }
    4141
    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;
    4944      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;
    5645    }
    5746
  • trunk/sources/HeuristicLab.Functions/Exponential.cs

    r2 r155  
    3131  public class Exponential : FunctionBase {
    3232    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))."; }
    3434    }
    3535
     
    3939      AddConstraint(new NumberOfSubOperatorsConstraint(1, 1));
    4040    }
    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]);
    4344    }
    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 
    5645
    5746    public override void Accept(IFunctionVisitor visitor) {
  • trunk/sources/HeuristicLab.Functions/FunctionBase.cs

    r2 r155  
    2929
    3030namespace 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>
    3135  public abstract class FunctionBase : OperatorBase, IFunction {
    32     private List<IFunction> subFunctions;
    33     // instance subfunctions
    34     public IList<IFunction> SubFunctions {
    35       get {
    36         return subFunctions;
     36   
     37    public virtual double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) {
     38      if(tree.SubTrees.Count > 0) {
     39        double[] evaluationResults = new double[tree.SubTrees.Count];
     40        for(int i = 0; i < evaluationResults.Length; i++) {
     41          evaluationResults[i] = tree.SubTrees[i].Evaluate(dataset, sampleIndex);
     42        }
     43        return Apply(dataset, sampleIndex, evaluationResults);
     44      } else {
     45        return Apply(dataset, sampleIndex, null);
    3746      }
    3847    }
    3948
    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();
    4454    }
    45 
    46     // reference to the 'type' of the function
    47     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 
    8555    public virtual void Accept(IFunctionVisitor visitor) {
    8656      visitor.Visit(this);
    8757    }
    8858
     59    private static readonly List<IOperator> emptySubOperatorList = new List<IOperator>();
     60    public override IList<IOperator> SubOperators {
     61      get { return emptySubOperatorList; }
     62    }
     63
    8964    public override void AddSubOperator(IOperator subOperator) {
    90       subFunctions.Add((IFunction)subOperator);
     65      throw new NotSupportedException();
    9166    }
    9267
    9368    public override bool TryAddSubOperator(IOperator subOperator) {
    94       subFunctions.Add((IFunction)subOperator);
    95       bool valid = IsValid();
    96       if (!valid) {
    97         subFunctions.RemoveAt(subFunctions.Count - 1);
    98       }
    99 
    100       return valid;
     69      throw new NotSupportedException();
    10170    }
    10271
    10372    public override bool TryAddSubOperator(IOperator subOperator, int index) {
    104       subFunctions.Insert(index, (IFunction)subOperator);
    105       bool valid = IsValid();
    106       if (!valid) {
    107         subFunctions.RemoveAt(index);
    108       }
    109       return valid;
     73      throw new NotSupportedException();
    11074    }
    11175
    11276    public override bool TryAddSubOperator(IOperator subOperator, int index, out ICollection<IConstraint> violatedConstraints) {
    113       subFunctions.Insert(index, (IFunction)subOperator);
    114       bool valid = IsValid(out violatedConstraints);
    115       if (!valid) {
    116         subFunctions.RemoveAt(index);
    117       }
    118       return valid;
     77      throw new NotSupportedException();
    11978    }
    12079
    12180    public override bool TryAddSubOperator(IOperator subOperator, out ICollection<IConstraint> violatedConstraints) {
    122       subFunctions.Add((IFunction)subOperator);
    123       bool valid = IsValid(out violatedConstraints);
    124       if (!valid) {
    125         subFunctions.RemoveAt(subFunctions.Count - 1);
    126       }
    127 
    128       return valid;
     81      throw new NotSupportedException();
    12982    }
    13083
    13184    public override void AddSubOperator(IOperator subOperator, int index) {
    132       subFunctions.Insert(index, (IFunction)subOperator);
     85      throw new NotSupportedException();
    13386    }
    13487
    13588    public override void RemoveSubOperator(int index) {
    136       if (index >= subFunctions.Count) throw new InvalidOperationException();
    137       subFunctions.RemoveAt(index);
     89      throw new NotSupportedException();
    13890    }
    13991
    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 
    17292    public override bool TryRemoveSubOperator(int index) {
    173       IFunction removedFunction = subFunctions[index];
    174       subFunctions.RemoveAt(index);
    175       bool valid = IsValid();
    176       if (!valid) {
    177         subFunctions.Insert(index, removedFunction);
    178       }
    179 
    180       return valid;
     93      throw new NotSupportedException();
    18194    }
    18295
    18396    public override bool TryRemoveSubOperator(int index, out ICollection<IConstraint> violatedConstraints) {
    184       IFunction removedFunction = subFunctions[index];
    185       subFunctions.RemoveAt(index);
    186       bool valid = IsValid(out violatedConstraints);
    187       if (!valid) {
    188         subFunctions.Insert(index, removedFunction);
    189       }
    190 
    191       return valid;
    192     }
    193 
    194     public override void AddVariable(IVariable variable) {
    195       if (metaObject == this) {
    196         base.AddVariable(variable);
    197       } else {
    198         metaObject.AddVariable(variable);
    199       }
    200     }
    201 
    202     public override IVariable GetVariable(string name) {
    203       foreach (IVariable variable in variables) {
    204         if (variable.Name == name) return variable;
    205       }
    206       if (metaObject == this) {
    207         return base.GetVariable(name);
    208       } else {
    209         return metaObject.GetVariable(name);
    210       }
    211     }
    212 
    213     public void AddLocalVariable(IVariable variable) {
    214       variables.Add(variable);
    215     }
    216 
    217     public override void RemoveVariable(string name) {
    218       foreach (IVariable variable in variables) {
    219         if (variable.Name == name) {
    220           variables.Remove(variable);
    221           return;
    222         }
    223       }
    224       if (metaObject == this) {
    225         base.RemoveVariable(name);
    226       } else {
    227         metaObject.RemoveVariable(name);
    228       }
    229     }
    230 
    231     public override IItem GetVariableValue(string formalName, IScope scope, bool recursiveLookup, bool throwOnError) {
    232       foreach (IVariable variable in Variables) {
    233         if (variable.Name == formalName) {
    234           return variable.Value;
    235         }
    236       }
    237       return metaObject.GetVariableValue(formalName, scope, recursiveLookup, throwOnError);
    238     }
    239 
    240     public override ICollection<IVariableInfo> VariableInfos {
    241       get {
    242         if (metaObject == this) {
    243           return base.VariableInfos;
    244         } else {
    245           return metaObject.VariableInfos;
    246         }
    247       }
    248     }
    249 
    250     public override void AddVariableInfo(IVariableInfo variableInfo) {
    251       if (metaObject == this) {
    252         base.AddVariableInfo(variableInfo);
    253       } else {
    254         metaObject.AddVariableInfo(variableInfo);
    255       }
    256     }
    257 
    258     public override void RemoveVariableInfo(string formalName) {
    259       if (metaObject == this) {
    260         base.RemoveVariableInfo(formalName);
    261       } else {
    262         metaObject.RemoveVariableInfo(formalName);
    263       }
    264     }
    265 
    266     public override ICollection<IConstraint> Constraints {
    267       get {
    268         if (metaObject == this) {
    269           return base.Constraints;
    270         } else {
    271           return metaObject.Constraints;
    272         }
    273       }
    274     }
    275 
    276     public override void AddConstraint(IConstraint constraint) {
    277       if (metaObject == this) {
    278         base.AddConstraint(constraint);
    279       } else {
    280         metaObject.AddConstraint(constraint);
    281       }
    282     }
    283     public override void RemoveConstraint(IConstraint constraint) {
    284       if (metaObject == this) {
    285         base.RemoveConstraint(constraint);
    286       } else {
    287         metaObject.RemoveConstraint(constraint);
    288       }
    289     }
    290 
    291     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    292       XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    293       if (metaObject != this) {
    294         XmlNode functionTemplateNode = document.CreateElement("FunctionTemplate");
    295         functionTemplateNode.AppendChild(PersistenceManager.Persist(metaObject, document, persistedObjects));
    296         node.AppendChild(functionTemplateNode);
    297       }
    298 
    299       // don't need to persist the sub-functions because OperatorBase.GetXmlNode already persisted the sub-operators
    300 
    301       // persist local variables
    302       XmlNode variablesNode = document.CreateNode(XmlNodeType.Element, "LocalVariables", null);
    303       foreach (IVariable variable in variables) {
    304         variablesNode.AppendChild(PersistenceManager.Persist(variable, document, persistedObjects));
    305       }
    306       node.AppendChild(variablesNode);
    307       return node;
    308     }
    309 
    310     public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    311       base.Populate(node, restoredObjects);
    312       XmlNode functionTemplateNode = node.SelectSingleNode("FunctionTemplate");
    313       if (functionTemplateNode != null) {
    314         metaObject = (FunctionBase)PersistenceManager.Restore(functionTemplateNode.ChildNodes[0], restoredObjects);
    315       } else {
    316         metaObject = this;
    317       }
    318       // don't need to explicitly load the sub-functions because that has already been done in OperatorBase.Populate()
    319 
    320       // load local variables
    321       XmlNode variablesNode = node.SelectSingleNode("LocalVariables");
    322      
    323       // remove the variables that have been added in a constructor
    324       variables.Clear();
    325       // load the persisted variables
    326       foreach (XmlNode variableNode in variablesNode.ChildNodes)
    327         variables.Add((IVariable)PersistenceManager.Restore(variableNode, restoredObjects));
    328     }
    329 
    330     public override IView CreateView() {
    331       return new FunctionView(this);
     97      throw new NotSupportedException();
    33298    }
    33399  }
  • trunk/sources/HeuristicLab.Functions/FunctionView.Designer.cs

    r2 r155  
    2222using System;
    2323namespace HeuristicLab.Functions {
    24   partial class FunctionView {
     24  partial class FunctionTreeView {
    2525    /// <summary>
    2626    /// Required designer variable.
     
    167167            this.copyToClipboardMenuItem});
    168168      this.treeNodeContextMenu.Name = "treeNodeContextMenu";
    169       this.treeNodeContextMenu.Size = new System.Drawing.Size(259, 26);
     169      this.treeNodeContextMenu.Size = new System.Drawing.Size(248, 26);
    170170      //
    171171      // copyToClipboardMenuItem
    172172      //
    173173      this.copyToClipboardMenuItem.Name = "copyToClipboardMenuItem";
    174       this.copyToClipboardMenuItem.Size = new System.Drawing.Size(258, 22);
     174      this.copyToClipboardMenuItem.Size = new System.Drawing.Size(247, 22);
    175175      this.copyToClipboardMenuItem.Text = "Copy to clip-board (Model-Analyzer)";
    176176      this.copyToClipboardMenuItem.Click += new System.EventHandler(this.copyToClipboardMenuItem_Click);
  • trunk/sources/HeuristicLab.Functions/FunctionView.cs

    r2 r155  
    3030using HeuristicLab.Core;
    3131using HeuristicLab.PluginInfrastructure;
     32using HeuristicLab.Data;
    3233
    3334namespace HeuristicLab.Functions {
    34   public partial class FunctionView : ViewBase {
    35     private IFunction function;
    36 
    37     private IFunction selectedFunction;
     35  public partial class FunctionTreeView : ViewBase {
     36    private IFunctionTree functionTree;
     37
     38    private IFunctionTree selectedBranch;
    3839    private IVariable selectedVariable;
    3940
    4041    private FunctionNameVisitor functionNameVisitor;
    41     public FunctionView() {
     42    public FunctionTreeView() {
    4243      InitializeComponent();
    4344      functionNameVisitor = new FunctionNameVisitor();
    4445    }
    4546
    46     public FunctionView(IFunction function)
     47    public FunctionTreeView(IFunctionTree functionTree)
    4748      : this() {
    48       this.function = function;
     49      this.functionTree = functionTree;
    4950      Refresh();
    5051    }
     
    5253    protected override void UpdateControls() {
    5354      functionTreeView.Nodes.Clear();
    54       function.Accept(functionNameVisitor);
     55      functionNameVisitor.Visit(functionTree);
    5556      TreeNode rootNode = new TreeNode();
    56       rootNode.Name = function.Name;
     57      rootNode.Name = functionTree.Function.Name;
    5758      rootNode.Text = functionNameVisitor.Name;
    58       rootNode.Tag = function;
     59      rootNode.Tag = functionTree;
    5960      rootNode.ContextMenuStrip = treeNodeContextMenu;
    6061      functionTreeView.Nodes.Add(rootNode);
    6162
    62       foreach(IFunction subFunction in function.SubFunctions) {
    63         CreateTree(rootNode, subFunction);
     63      foreach(IFunctionTree subTree in functionTree.SubTrees) {
     64        CreateTree(rootNode, subTree);
    6465      }
    6566      functionTreeView.ExpandAll();
    6667    }
    6768
    68     private void CreateTree(TreeNode rootNode, IFunction function) {
     69    private void CreateTree(TreeNode rootNode, IFunctionTree functionTree) {
    6970      TreeNode node = new TreeNode();
    70       function.Accept(functionNameVisitor);
    71       node.Tag = function;
    72       node.Name = function.Name;
     71      functionNameVisitor.Visit(functionTree);
     72      node.Name = functionTree.Function.Name;
    7373      node.Text = functionNameVisitor.Name;
     74      node.Tag = functionTree;
    7475      node.ContextMenuStrip = treeNodeContextMenu;
    7576      rootNode.Nodes.Add(node);
    76       foreach(IFunction subFunction in function.SubFunctions) {
    77         CreateTree(node, subFunction);
     77      foreach(IFunctionTree subTree in functionTree.SubTrees) {
     78        CreateTree(node, subTree);
    7879      }
    7980    }
     
    8586      editButton.Enabled = false;
    8687      if(functionTreeView.SelectedNode != null && functionTreeView.SelectedNode.Tag != null) {
    87         IFunction selectedFunction = (IFunction)functionTreeView.SelectedNode.Tag;
    88         UpdateVariablesList(selectedFunction);
    89         templateTextBox.Text = selectedFunction.MetaObject.Name;
    90         this.selectedFunction = selectedFunction;
     88        IFunctionTree selectedBranch = (IFunctionTree)functionTreeView.SelectedNode.Tag;
     89        UpdateVariablesList(selectedBranch);
     90        templateTextBox.Text = selectedBranch.Function.Name;
     91        this.selectedBranch = selectedBranch;
    9192        editButton.Enabled = true;
    9293      }
    9394    }
    9495
    95     private void UpdateVariablesList(IFunction function) {
    96       foreach(IVariable variable in function.LocalVariables) {
     96    private void UpdateVariablesList(IFunctionTree functionTree) {
     97      foreach(IVariable variable in functionTree.LocalVariables) {
    9798        variablesListBox.Items.Add(variable.Name);
    9899      }
     
    106107      if(variablesListBox.SelectedItem != null) {
    107108        string selectedVariableName = (string)variablesListBox.SelectedItem;
    108         selectedVariable = selectedFunction.GetVariable(selectedVariableName);
     109        selectedVariable = selectedBranch.GetLocalVariable(selectedVariableName);
    109110        variablesSplitContainer.Panel2.Controls.Clear();
    110111        Control editor = (Control)selectedVariable.CreateView();
     
    121122      if(functionTreeView.SelectedNode != null && functionTreeView.SelectedNode.Tag != null) {
    122123        TreeNode node = functionTreeView.SelectedNode;
    123         selectedFunction.Accept(functionNameVisitor);
     124        functionNameVisitor.Visit(selectedBranch);
    124125        node.Text = functionNameVisitor.Name;
    125126      }
    126127    }
    127128
    128     private void editButton_Click(object sender, EventArgs e) {
    129       OperatorBaseView operatorView = new OperatorBaseView(selectedFunction.MetaObject);
    130       PluginManager.ControlManager.ShowControl(operatorView);
     129    protected virtual void editButton_Click(object sender, EventArgs e) {
     130      PluginManager.ControlManager.ShowControl(selectedBranch.Function.CreateView());
    131131    }
    132132
     
    136136
    137137      ModelAnalyzerExportVisitor visitor = new ModelAnalyzerExportVisitor();
    138       ((IFunction)node.Tag).Accept(visitor);
     138      visitor.Visit((IFunctionTree)node.Tag);
    139139      Clipboard.SetText(visitor.ModelAnalyzerPrefix);
    140140    }
     
    142142    private class FunctionNameVisitor : IFunctionVisitor {
    143143      string name;
     144      IFunctionTree currentBranch;
    144145
    145146      public string Name {
     
    147148      }
    148149
     150      public void Visit(IFunctionTree tree) {
     151        currentBranch = tree;
     152        tree.Function.Accept(this);
     153      }
     154
    149155      #region IFunctionVisitor Members
    150 
    151156      public void Visit(IFunction function) {
    152157        name = function.Name;
     
    158163
    159164      public void Visit(Constant constant) {
    160         name = constant.Value + "";
     165        name = ((ConstrainedDoubleData)(currentBranch.GetLocalVariable(HeuristicLab.Functions.Constant.VALUE).Value)).Data + "";
    161166      }
    162167
     
    207212      public void Visit(Variable variable) {
    208213        string timeOffset = "";
    209         if(variable.SampleOffset < 0) {
    210           timeOffset = "(t" + variable.SampleOffset + ")";
    211         } else if(variable.SampleOffset > 0) {
    212           timeOffset = "(t+" + variable.SampleOffset + ")";
     214        int sampleOffset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.OFFSET).Value).Data;
     215        int variableIndex = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.INDEX).Value).Data;
     216        double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.WEIGHT).Value).Data;
     217        if(sampleOffset < 0) {
     218          timeOffset = "(t" + sampleOffset + ")";
     219        } else if(sampleOffset > 0) {
     220          timeOffset = "(t+" + sampleOffset + ")";
    213221        } else {
    214222          timeOffset = "";
    215223        }
    216         name = "Var" + variable.VariableIndex + timeOffset + " * " + variable.Weight;
     224        name = "Var" + variableIndex + timeOffset + " * " + weight;
    217225      }
    218226
     
    255263      private string prefix;
    256264      private string currentIndend = "";
     265      private IFunctionTree currentBranch;
    257266      public string ModelAnalyzerPrefix {
    258267        get { return prefix; }
     
    263272
    264273      private void VisitFunction(string name, IFunction f) {
    265         prefix += currentIndend + "[F]"+name+"(\n";
     274        prefix += currentIndend + "[F]" + name + "(\n";
     275      }
     276
     277      #region IFunctionVisitor Members
     278
     279      public void Visit(IFunction function) {
     280        prefix += function.Name;
     281      }
     282
     283      public void Visit(Addition addition) {
     284        VisitFunction("Addition[0]", addition);
     285      }
     286
     287      public void Visit(Constant constant) {
     288        double value = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Constant.VALUE).Value).Data;
     289        prefix += currentIndend + "[T]Constant(" + value + ";0;0)";
     290      }
     291
     292      public void Visit(Cosinus cosinus) {
     293        VisitFunction("Trigonometrics[1]", cosinus);
     294      }
     295
     296      public void Visit(Division division) {
     297        VisitFunction("Division[0]", division);
     298      }
     299
     300      public void Visit(Exponential exponential) {
     301        VisitFunction("Exponential[0]", exponential);
     302      }
     303
     304      public void Visit(Logarithm logarithm) {
     305        VisitFunction("Logarithm[0]", logarithm);
     306      }
     307
     308      public void Visit(Multiplication multiplication) {
     309        VisitFunction("Multiplication[0]", multiplication);
     310      }
     311
     312      public void Visit(Power power) {
     313        VisitFunction("Power[0]", power);
     314      }
     315
     316      public void Visit(Signum signum) {
     317        VisitFunction("Signum[0]", signum);
     318      }
     319
     320      public void Visit(Sinus sinus) {
     321        VisitFunction("Trigonometrics[0]", sinus);
     322      }
     323
     324      public void Visit(Sqrt sqrt) {
     325        VisitFunction("Sqrt[0]", sqrt);
     326      }
     327
     328      public void Visit(Substraction substraction) {
     329        VisitFunction("Substraction[0]", substraction);
     330      }
     331
     332      public void Visit(Tangens tangens) {
     333        VisitFunction("Trigonometrics[2]", tangens);
     334      }
     335
     336      public void Visit(HeuristicLab.Functions.Variable variable) {
     337        double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.WEIGHT).Value).Data;
     338        double index = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.INDEX).Value).Data;
     339        double offset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.OFFSET).Value).Data;
     340
     341        prefix += currentIndend + "[T]Variable(" + weight + ";" + index + ";" + -offset + ")";
     342      }
     343
     344      public void Visit(And and) {
     345        VisitFunction("Logical[0]", and);
     346      }
     347
     348      public void Visit(Average average) {
     349        VisitFunction("N/A (average)", average);
     350      }
     351
     352      public void Visit(IfThenElse ifThenElse) {
     353        VisitFunction("Conditional[0]", ifThenElse);
     354      }
     355
     356      public void Visit(Not not) {
     357        VisitFunction("Logical[2]", not);
     358      }
     359
     360      public void Visit(Or or) {
     361        VisitFunction("Logical[1]", or);
     362      }
     363
     364      public void Visit(Xor xor) {
     365        VisitFunction("N/A (xor)", xor);
     366      }
     367
     368      public void Visit(Equal equal) {
     369        VisitFunction("Boolean[2]", equal);
     370      }
     371
     372      public void Visit(LessThan lessThan) {
     373        VisitFunction("Boolean[0]", lessThan);
     374      }
     375      #endregion
     376
     377      public void Visit(IFunctionTree functionTree) {
     378        currentBranch = functionTree;
     379        functionTree.Function.Accept(this);
    266380        currentIndend += "  ";
    267         foreach(IFunction subFunction in f.SubFunctions) {
    268           subFunction.Accept(this);
     381        foreach(IFunctionTree subTree in functionTree.SubTrees) {
     382          Visit(subTree);
    269383          prefix += ";\n";
    270384        }
    271         prefix = prefix.TrimEnd(';','\n');
     385        prefix = prefix.TrimEnd(';', '\n');
    272386        prefix += ")";
    273387        currentIndend = currentIndend.Remove(0, 2);
    274388      }
    275 
    276       #region IFunctionVisitor Members
    277 
    278       public void Visit(IFunction function) {
    279         prefix += function.Name;
    280       }
    281 
    282       public void Visit(Addition addition) {
    283         VisitFunction("Addition[0]", addition);
    284       }
    285 
    286       public void Visit(Constant constant) {
    287         prefix += currentIndend + "[T]Constant(" + constant.Value.Data.ToString() + ";0;0)";
    288       }
    289 
    290       public void Visit(Cosinus cosinus) {
    291         VisitFunction("Trigonometrics[1]", cosinus);
    292       }
    293 
    294       public void Visit(Division division) {
    295         VisitFunction("Division[0]", division);
    296       }
    297 
    298       public void Visit(Exponential exponential) {
    299         VisitFunction("Exponential[0]", exponential);
    300       }
    301 
    302       public void Visit(Logarithm logarithm) {
    303         VisitFunction("Logarithm[0]", logarithm);
    304       }
    305 
    306       public void Visit(Multiplication multiplication) {
    307         VisitFunction("Multiplication[0]", multiplication);
    308       }
    309 
    310       public void Visit(Power power) {
    311         VisitFunction("Power[0]", power);
    312       }
    313 
    314       public void Visit(Signum signum) {
    315         VisitFunction("Signum[0]", signum);
    316       }
    317 
    318       public void Visit(Sinus sinus) {
    319         VisitFunction("Trigonometrics[0]", sinus);
    320       }
    321 
    322       public void Visit(Sqrt sqrt) {
    323         VisitFunction("Sqrt[0]", sqrt);
    324       }
    325 
    326       public void Visit(Substraction substraction) {
    327         VisitFunction("Substraction[0]", substraction);
    328       }
    329 
    330       public void Visit(Tangens tangens) {
    331         VisitFunction("Trigonometrics[2]", tangens);
    332       }
    333 
    334       public void Visit(HeuristicLab.Functions.Variable variable) {
    335         prefix += currentIndend + "[T]Variable(" + variable.Weight + ";" + variable.VariableIndex + ";" + -variable.SampleOffset + ")";
    336       }
    337 
    338       public void Visit(And and) {
    339         VisitFunction("Logical[0]", and);
    340       }
    341 
    342       public void Visit(Average average) {
    343         VisitFunction("N/A (average)", average);
    344       }
    345 
    346       public void Visit(IfThenElse ifThenElse) {
    347         VisitFunction("Conditional[0]", ifThenElse);
    348       }
    349 
    350       public void Visit(Not not) {
    351         VisitFunction("Logical[2]", not);
    352       }
    353 
    354       public void Visit(Or or) {
    355         VisitFunction("Logical[1]", or);
    356       }
    357 
    358       public void Visit(Xor xor) {
    359         VisitFunction("N/A (xor)", xor);
    360       }
    361 
    362       public void Visit(Equal equal) {
    363         VisitFunction("Boolean[2]", equal);
    364       }
    365 
    366       public void Visit(LessThan lessThan) {
    367         VisitFunction("Boolean[0]", lessThan);
    368       }
    369 
    370       #endregion
    371     }
    372 
     389    }
    373390  }
    374391}
  • trunk/sources/HeuristicLab.Functions/HeuristicLab.Functions.csproj

    r30 r155  
    5252    <Compile Include="Average.cs" />
    5353    <Compile Include="Constant.cs" />
     54    <Compile Include="FunctionTree.cs" />
     55    <Compile Include="IFunctionTree.cs" />
     56    <Compile Include="ProgrammableFunction.cs" />
    5457    <Compile Include="Equal.cs" />
    5558    <Compile Include="FunctionView.cs">
     
    99102      <Name>HeuristicLab.Data</Name>
    100103    </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>
    101108    <ProjectReference Include="..\HeuristicLab.PluginInfrastructure\HeuristicLab.PluginInfrastructure.csproj">
    102109      <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project>
  • trunk/sources/HeuristicLab.Functions/IFunction.cs

    r2 r155  
    2828namespace HeuristicLab.Functions {
    2929  public interface IFunction : IOperator {
    30     IList<IFunction> SubFunctions { get;}
    31     ICollection<IVariable> LocalVariables { get; }
    32     IFunction MetaObject { get; }
    33     double Evaluate(Dataset dataset, int sampleIndex);
     30    double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree);
     31    double Apply(Dataset dataset, int sampleIndex, double[] args);
    3432    void Accept(IFunctionVisitor visitor);
    3533  }
  • trunk/sources/HeuristicLab.Functions/IfThenElse.cs

    r2 r155  
    3131    public override string Description {
    3232      get {
    33         return @"Returns the result of the second branch if the first branch evaluates to a value >= 0.5 and the result
    34 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
     34of the third sub-tree if the first sub-tree evaluates to >= 0.5.";
    3535      }
    3636    }
     
    4141    }
    4242
    43     public IfThenElse(IfThenElse source, IDictionary<Guid, object> clonedObjects)
    44       : base(source, clonedObjects) {
    45     }
    46 
    47 
    48     public override double Evaluate(Dataset dataset, int sampleIndex) {
    49       double condition = Math.Round(SubFunctions[0].Evaluate(dataset, sampleIndex));
    50       if(condition >= .5) return SubFunctions[2].Evaluate(dataset, sampleIndex);
    51       else if(condition < .5) return SubFunctions[1].Evaluate(dataset, sampleIndex);
     43    // special form
     44    public override double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) {
     45      double condition = Math.Round(tree.SubTrees[0].Evaluate(dataset, sampleIndex));
     46      if(condition < .5) return tree.SubTrees[1].Evaluate(dataset, sampleIndex);
     47      else if(condition >= .5) return tree.SubTrees[2].Evaluate(dataset, sampleIndex);
    5248      else return double.NaN;
    5349    }
    5450
    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();
    5953    }
    6054
  • trunk/sources/HeuristicLab.Functions/LessThan.cs

    r2 r155  
    3131    public override string Description {
    3232      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-function and 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.";
    3434      }
    3535    }
     
    4040    }
    4141
    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;
    4944      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;
    5645    }
    5746
  • trunk/sources/HeuristicLab.Functions/Logarithm.cs

    r2 r155  
    3131  public class Logarithm : FunctionBase {
    3232    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."; }
    3434    }
    3535
     
    4040    }
    4141
    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]);
    5444    }
    5545
  • trunk/sources/HeuristicLab.Functions/Multiplication.cs

    r2 r155  
    3333    public override string Description {
    3434      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.
    3636  (* 3) => 3
    3737  (* 2 3) => 6
     
    4646    }
    4747
    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) {
    5449      // (* 3) => 3
    5550      // (* 2 3) => 6
    5651      // (* 3 4 5) => 60
    5752      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];
    6055      }
    6156      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;
    6857    }
    6958
  • trunk/sources/HeuristicLab.Functions/Not.cs

    r2 r155  
    3131    public override string Description {
    3232      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.";
    3434      }
    3535    }
     
    4040    }
    4141
    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]);
    4944      if(result == 0.0) return 1.0;
    5045      else if(result == 1.0) return 0.0;
    5146      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;
    5847    }
    5948
  • trunk/sources/HeuristicLab.Functions/Or.cs

    r2 r155  
    3131    public override string Description {
    3232      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.
     34Special form, evaluation stops at first sub-tree that evaluates to 1.0 (true).";
    3435      }
    3536    }
     
    4041    }
    4142
    42     public Or(Or source, IDictionary<Guid, object> clonedObjects)
    43       : base(source, clonedObjects) {
    44     }
    45 
    46 
    47     public override double Evaluate(Dataset dataset, int sampleIndex) {
    48       foreach(IFunction subFunction in SubFunctions) {
    49         double result = Math.Round(subFunction.Evaluate(dataset, sampleIndex));
    50         if(result == 1.0) return 1.0;
     43    public override double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) {
     44      foreach(IFunctionTree subTree in tree.SubTrees) {
     45        double result = Math.Round(subTree.Evaluate(dataset, sampleIndex));
     46        if(result == 1.0) return 1.0; // sub-tree evaluates to 1.0 (true) return 1.0
    5147        else if(result != 0.0) return double.NaN;
    5248      }
     49      // all sub-trees evaluated to 0.0 (false) return false
    5350      return 0.0;
    5451    }
    5552
    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();
    6055    }
    61 
    6256    public override void Accept(IFunctionVisitor visitor) {
    6357      visitor.Visit(this);
  • trunk/sources/HeuristicLab.Functions/Power.cs

    r2 r155  
    3131  public class Power : FunctionBase {
    3232    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))."; }
    3434    }
    3535
     
    4040    }
    4141
    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]);
    4444    }
    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 
    5745
    5846    public override void Accept(IFunctionVisitor visitor) {
  • trunk/sources/HeuristicLab.Functions/Signum.cs

    r2 r155  
    3131  public class Signum : FunctionBase {
    3232    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."; }
    3434    }
    3535
     
    4040    }
    4141
    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];
    4944      if(value < 0) return -1;
    5045      if(value > 0) return 1;
    5146      return 0;
    5247    }
    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 
    6048
    6149    public override void Accept(IFunctionVisitor visitor) {
  • trunk/sources/HeuristicLab.Functions/Sinus.cs

    r2 r155  
    3131  public class Sinus : FunctionBase {
    3232    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."; }
    3434    }
    3535
     
    4040    }
    4141
    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]);
    4444    }
    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 
    5745
    5846    public override void Accept(IFunctionVisitor visitor) {
  • trunk/sources/HeuristicLab.Functions/Sqrt.cs

    r2 r155  
    3131  public class Sqrt : FunctionBase {
    3232    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."; }
    3434    }
    3535
     
    4040    }
    4141
    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]);
    4445    }
    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 
    5746
    5847    public override void Accept(IFunctionVisitor visitor) {
  • trunk/sources/HeuristicLab.Functions/Substraction.cs

    r2 r155  
    3333    public override string Description {
    3434      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.
    3636    (- 3) => -3
    3737    (- 2 3) => -1
     
    4646    }
    4747
    48     public Substraction(Substraction source, IDictionary<Guid, object> clonedObjects)
    49       : base(source, clonedObjects) {
    50     }
    5148
    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];
    5752      } 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];
    6156        }
    6257        return result;
    6358      }
    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;
    7059    }
    7160
  • trunk/sources/HeuristicLab.Functions/Tangens.cs

    r2 r155  
    3131  public class Tangens : FunctionBase {
    3232    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."; }
    3434    }
    3535
     
    4040    }
    4141
    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]);
    4444    }
    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 
    5645
    5746    public override void Accept(IFunctionVisitor visitor) {
  • trunk/sources/HeuristicLab.Functions/Variable.cs

    r133 r155  
    3232  public class Variable : FunctionBase {
    3333
    34     private ConstrainedIntData variable;
    35     private ConstrainedDoubleData weight;
    36     private ConstrainedIntData sampleOffset;
    37 
    38     public double SampleOffset {
    39       get { return sampleOffset.Data; }
    40     }
    41 
    42     public int VariableIndex {
    43       get { return variable.Data; }
    44     }
    45 
    46     public double Weight {
    47       get { return weight.Data; }
    48     }
     34    public static readonly string WEIGHT = "Weight";
     35    public static readonly string OFFSET = "SampleOffset";
     36    public static readonly string INDEX = "Variable";
    4937
    5038    public override string Description {
     
    5644    public Variable()
    5745      : base() {
    58       AddVariableInfo(new VariableInfo("Variable", "Index of the variable in the dataset representing this feature", typeof(ConstrainedIntData), VariableKind.None));
    59       GetVariableInfo("Variable").Local = true;
    60       AddVariableInfo(new VariableInfo("Weight", "Weight is multiplied to the feature value", typeof(ConstrainedDoubleData), VariableKind.None));
    61       GetVariableInfo("Weight").Local = true;
    62       AddVariableInfo(new VariableInfo("SampleOffset", "SampleOffset is added to the sample index", typeof(ConstrainedIntData), VariableKind.None));
    63       GetVariableInfo("SampleOffset").Local = true;
     46      AddVariableInfo(new VariableInfo(INDEX, "Index of the variable in the dataset representing this feature", typeof(ConstrainedIntData), VariableKind.None));
     47      GetVariableInfo(INDEX).Local = true;
     48      AddVariableInfo(new VariableInfo(WEIGHT, "Weight is multiplied to the feature value", typeof(ConstrainedDoubleData), VariableKind.None));
     49      GetVariableInfo(WEIGHT).Local = true;
     50      AddVariableInfo(new VariableInfo(OFFSET, "SampleOffset is added to the sample index", typeof(ConstrainedIntData), VariableKind.None));
     51      GetVariableInfo(OFFSET).Local = true;
    6452
    65       variable = new ConstrainedIntData();
    66       AddLocalVariable(new HeuristicLab.Core.Variable("Variable", variable));
    67 
    68       weight = new ConstrainedDoubleData();
     53      ConstrainedDoubleData weight = new ConstrainedDoubleData();
    6954      // initialize a totally arbitrary range for the weight = [-20.0, 20.0]
    7055      weight.AddConstraint(new DoubleBoundedConstraint(-20.0, 20.0));
    71       AddLocalVariable(new HeuristicLab.Core.Variable("Weight", weight));
     56      AddVariable(new HeuristicLab.Core.Variable(WEIGHT, weight));
    7257
    73       sampleOffset = new ConstrainedIntData();
     58      ConstrainedIntData variable = new ConstrainedIntData();
     59      AddVariable(new HeuristicLab.Core.Variable(INDEX, variable));
     60
     61      ConstrainedIntData sampleOffset = new ConstrainedIntData();
    7462      // initialize a totally arbitrary default range for sampleoffset = [-10, 10]
    7563      sampleOffset.AddConstraint(new IntBoundedConstraint(0, 0));
    76       AddLocalVariable(new HeuristicLab.Core.Variable("SampleOffset", sampleOffset));
     64      AddVariable(new HeuristicLab.Core.Variable(OFFSET, sampleOffset));
    7765
    78       // samplefeature can't have suboperators
     66      // variable can't have suboperators
    7967      AddConstraint(new NumberOfSubOperatorsConstraint(0, 0));
    8068    }
    8169
    82     public Variable(Variable source, IDictionary<Guid, object> clonedObjects)
    83       : base(source, clonedObjects) {
     70    // variable can be evaluated directly
     71    // evaluation reads local variables weight, index, offset from function-tree and returns the variable-value * weight
     72    public override double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) {
     73      double w = ((ConstrainedDoubleData)tree.GetLocalVariable(WEIGHT).Value).Data;
     74      int v = ((ConstrainedIntData)tree.GetLocalVariable(INDEX).Value).Data;
     75      int offset = ((ConstrainedIntData)tree.GetLocalVariable(OFFSET).Value).Data;
    8476
    85       variable = (ConstrainedIntData)GetVariable("Variable").Value;
    86       weight = (ConstrainedDoubleData)GetVariable("Weight").Value;
    87       sampleOffset = (ConstrainedIntData)GetVariable("SampleOffset").Value;
     77      if(sampleIndex + offset < 0 || sampleIndex + offset >= dataset.Rows) return double.NaN;
     78      return w * dataset.GetValue(sampleIndex + offset, v);
    8879    }
    8980
    90     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    91       Variable clone = new Variable(this, clonedObjects);
    92       clonedObjects.Add(clone.Guid, clone);
    93       return clone;
    94     }
    95 
    96     public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    97       base.Populate(node, restoredObjects);
    98 
    99       variable = (ConstrainedIntData)GetVariable("Variable").Value;
    100       weight = (ConstrainedDoubleData)GetVariable("Weight").Value;
    101       sampleOffset = (ConstrainedIntData)GetVariable("SampleOffset").Value;
    102     }
    103 
    104 
    105     public override double Evaluate(Dataset dataset, int sampleIndex) {
    106       // local variables
    107       int v = variable.Data;
    108       double w = weight.Data;
    109       int offset = sampleOffset.Data;
    110 
    111       if(sampleIndex+offset<0 || sampleIndex+offset>=dataset.Rows) return double.NaN;
    112       return w * dataset.GetValue(sampleIndex + offset, v);
     81    // can't apply a variable
     82    public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
     83      throw new NotSupportedException();
    11384    }
    11485
  • trunk/sources/HeuristicLab.Functions/Xor.cs

    r2 r155  
    3131    public override string Description {
    3232      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.";
    3434      }
    3535    }
     
    4040    }
    4141
    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;
    6146    }
    6247
Note: See TracChangeset for help on using the changeset viewer.