Free cookie consent management tool by TermsFeed Policy Generator

Changeset 189


Ignore:
Timestamp:
04/25/08 12:13:34 (16 years ago)
Author:
gkronber
Message:

fixed #131

Location:
trunk/sources
Files:
12 edited

Legend:

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

    r155 r189  
    4242    }
    4343
    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
    49         else if(result != 1.0) return double.NaN;
    50       }
    51       // all sub-trees evaluated to 1.0 (true) => return 1.0 (true)
    52       return 1.0;
     44    public override IFunctionTree GetTreeNode() {
     45      return new AndFunctionTree(this);
    5346    }
    5447
     48    // special form
    5549    public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    5650      throw new NotImplementedException();
     
    6155    }
    6256  }
     57
     58  class AndFunctionTree : FunctionTree {
     59    public AndFunctionTree() : base() { }
     60    public AndFunctionTree(And and) : base(and) { }
     61
     62    public override double Evaluate(Dataset dataset, int sampleIndex) {
     63      foreach(IFunctionTree subTree in SubTrees) {
     64        double result = Math.Round(subTree.Evaluate(dataset, sampleIndex));
     65        if(result == 0.0) return 0.0; // one sub-tree is 0.0 (false) => return false
     66        else if(result != 1.0) return double.NaN;
     67      }
     68      // all sub-trees evaluated to 1.0 (true) => return 1.0 (true)
     69      return 1.0;     
     70    }
     71  }
    6372}
  • trunk/sources/HeuristicLab.Functions/Constant.cs

    r177 r189  
    5252    }
    5353
    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;
     54    public override IFunctionTree GetTreeNode() {
     55      return new ConstantFunctionTree(this);
    5756    }
    5857
     
    6665    }
    6766  }
     67
     68  class ConstantFunctionTree : FunctionTree {
     69    private ConstrainedDoubleData value;
     70    public ConstantFunctionTree() : base() { }
     71    public ConstantFunctionTree(Constant constant) : base(constant) {
     72      UpdateCachedValues();
     73    }
     74
     75    private void UpdateCachedValues() {
     76      value = (ConstrainedDoubleData)GetLocalVariable(Constant.VALUE).Value;
     77    }
     78
     79    public override double Evaluate(Dataset dataset, int sampleIndex) {
     80      return value.Data;
     81    }
     82
     83    public override object Clone(IDictionary<Guid, object> clonedObjects) {
     84      ConstantFunctionTree clone = (ConstantFunctionTree)base.Clone(clonedObjects);
     85      clone.UpdateCachedValues();
     86      return clone;
     87    }
     88
     89    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
     90      base.Populate(node, restoredObjects);
     91      UpdateCachedValues();
     92    }
     93  }
    6894}
  • trunk/sources/HeuristicLab.Functions/FunctionBase.cs

    r165 r189  
    3535  public abstract class FunctionBase : OperatorBase, IFunction {
    3636   
    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);
    46       }
    47     }
    48 
    4937    public abstract double Apply(Dataset dataset, int sampleIndex, double[] args);
    5038
    5139    public virtual void Accept(IFunctionVisitor visitor) {
    5240      visitor.Visit(this);
     41    }
     42
     43    public virtual IFunctionTree GetTreeNode() {
     44      return new FunctionTree(this);
    5345    }
    5446
  • trunk/sources/HeuristicLab.Functions/FunctionTree.cs

    r157 r189  
    3535    private IFunction function;
    3636
    37     public FunctionTree() : base() {
     37    public FunctionTree()
     38      : base() {
    3839      subTrees = new List<IFunctionTree>();
    3940      localVariables = new List<IVariable>();
    4041    }
    4142
    42     public FunctionTree(IFunction function) : this() {
     43    internal FunctionTree(IFunction function)
     44      : this() {
    4345      this.function = function;
    4446      // create and store clones of all local variables of the function
     
    9193    }
    9294
    93     public double Evaluate(Dataset dataset, int sampleIndex) {
    94       return function.Evaluate(dataset, sampleIndex, this);
     95    public virtual double Evaluate(Dataset dataset, int sampleIndex) {
     96      double[] evaluationResults = new double[SubTrees.Count];
     97      for(int i = 0; i < evaluationResults.Length; i++) {
     98        evaluationResults[i] = SubTrees[i].Evaluate(dataset, sampleIndex);
     99      }
     100      return function.Apply(dataset, sampleIndex, evaluationResults);
    95101    }
    96102    #endregion
  • trunk/sources/HeuristicLab.Functions/IFunction.cs

    r155 r189  
    2828namespace HeuristicLab.Functions {
    2929  public interface IFunction : IOperator {
    30     double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree);
     30    IFunctionTree GetTreeNode();
    3131    double Apply(Dataset dataset, int sampleIndex, double[] args);
    3232    void Accept(IFunctionVisitor visitor);
  • trunk/sources/HeuristicLab.Functions/IfThenElse.cs

    r155 r189  
    4141    }
    4242
    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);
    48       else return double.NaN;
     43    public override IFunctionTree GetTreeNode() {
     44      return new IfThenElseFunctionTree(this);
    4945    }
    5046
     47    // special form
    5148    public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    5249      throw new NotImplementedException();
     
    5754    }
    5855  }
     56
     57  class IfThenElseFunctionTree : FunctionTree {
     58    public IfThenElseFunctionTree() : base() { }
     59    public IfThenElseFunctionTree(IfThenElse ifte) : base(ifte) { }
     60
     61    public override double Evaluate(Dataset dataset, int sampleIndex) {
     62      double condition = Math.Round(SubTrees[0].Evaluate(dataset, sampleIndex));
     63      if(condition < .5) return SubTrees[1].Evaluate(dataset, sampleIndex);
     64      else if(condition >= .5) return SubTrees[2].Evaluate(dataset, sampleIndex);
     65      else return double.NaN;
     66    }
     67  }
    5968}
  • trunk/sources/HeuristicLab.Functions/Or.cs

    r155 r189  
    4141    }
    4242
    43     public override double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) {
    44       foreach(IFunctionTree subTree in tree.SubTrees) {
     43    public override IFunctionTree GetTreeNode() {
     44      return new OrFunctionTree(this);
     45    }
     46    // or is a special form and can't be applied
     47    public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
     48      throw new NotImplementedException();
     49    }
     50    public override void Accept(IFunctionVisitor visitor) {
     51      visitor.Visit(this);
     52    }
     53  }
     54
     55  class OrFunctionTree : FunctionTree {
     56    public OrFunctionTree() : base() { }
     57    public OrFunctionTree(Or or) : base(or) { }
     58
     59    public override double Evaluate(Dataset dataset, int sampleIndex) {
     60      foreach(IFunctionTree subTree in SubTrees) {
    4561        double result = Math.Round(subTree.Evaluate(dataset, sampleIndex));
    4662        if(result == 1.0) return 1.0; // sub-tree evaluates to 1.0 (true) return 1.0
     
    5066      return 0.0;
    5167    }
    52 
    53     public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
    54       throw new NotImplementedException();
    55     }
    56     public override void Accept(IFunctionVisitor visitor) {
    57       visitor.Visit(this);
    58     }
    5968  }
    6069}
  • trunk/sources/HeuristicLab.Functions/ProgrammableFunction.cs

    r165 r189  
    119119    }
    120120
    121     public double Evaluate(Dataset dataset, int sampleIndex, IFunctionTree tree) {
    122       // evaluate sub-trees
    123       double[] evaluationResults = new double[tree.SubTrees.Count];
    124       for(int subTree=0; subTree < tree.SubTrees.Count; subTree++) {
    125         evaluationResults[subTree] = tree.SubTrees[subTree].Evaluate(dataset, sampleIndex);
    126       }
     121    public IFunctionTree GetTreeNode() {
     122      return new ProgrammableFunctionTree(this);
     123    }
     124
     125    // application of programmable-function is not possible
     126    public double Apply(Dataset dataset, int sampleIndex, double[] args) {
     127      throw new NotSupportedException();
     128    }
     129
     130    internal double Call(object[] parameters) {
    127131      // lazy activation of the user-programmed code
    128132      if(applyMethod == null) {
    129133        Compile();
    130134      }
     135      return (double)applyMethod.Invoke(null, parameters);
     136    }
     137
     138    #endregion
     139
     140    #region disabled operator functionality
     141    // operator-tree style evaluation is not supported for functions.
     142    public override IOperation Apply(IScope scope) {
     143      throw new NotSupportedException();
     144    }
     145
     146    private static readonly List<IOperator> emptySubOperatorList = new List<IOperator>();
     147    public override IList<IOperator> SubOperators {
     148      get { return emptySubOperatorList; }
     149    }
     150
     151    public override void AddSubOperator(IOperator subOperator) {
     152      throw new NotSupportedException();
     153    }
     154
     155    public override bool TryAddSubOperator(IOperator subOperator) {
     156      throw new NotSupportedException();
     157    }
     158
     159    public override bool TryAddSubOperator(IOperator subOperator, int index) {
     160      throw new NotSupportedException();
     161    }
     162
     163    public override bool TryAddSubOperator(IOperator subOperator, int index, out ICollection<IConstraint> violatedConstraints) {
     164      throw new NotSupportedException();
     165    }
     166
     167    public override bool TryAddSubOperator(IOperator subOperator, out ICollection<IConstraint> violatedConstraints) {
     168      throw new NotSupportedException();
     169    }
     170
     171    public override void AddSubOperator(IOperator subOperator, int index) {
     172      throw new NotSupportedException();
     173    }
     174
     175    public override void RemoveSubOperator(int index) {
     176      throw new NotSupportedException();
     177    }
     178
     179    public override bool TryRemoveSubOperator(int index) {
     180      throw new NotSupportedException();
     181    }
     182
     183    public override bool TryRemoveSubOperator(int index, out ICollection<IConstraint> violatedConstraints) {
     184      throw new NotSupportedException();
     185    }
     186    #endregion
     187  }
     188
     189  class ProgrammableFunctionTree : FunctionTree {
     190    private ProgrammableFunction progFun;
     191    public ProgrammableFunctionTree() : base() { }
     192    public ProgrammableFunctionTree(ProgrammableFunction progFun) : base(progFun) {
     193      this.progFun = progFun;
     194    }
     195    public override double Evaluate(Dataset dataset, int sampleIndex) {
     196      // evaluate sub-trees
     197      double[] evaluationResults = new double[SubTrees.Count];
     198      for(int subTree = 0; subTree < SubTrees.Count; subTree++) {
     199        evaluationResults[subTree] = SubTrees[subTree].Evaluate(dataset, sampleIndex);
     200      }
    131201
    132202      // collect parameters
    133       object[] parameters = new object[VariableInfos.Count + 3];
     203      object[] parameters = new object[LocalVariables.Count + 3];
    134204      parameters[0] = dataset;
    135205      parameters[1] = sampleIndex;
    136206      int i = 2;
    137207      // all local variables are available in the custom function
    138       foreach(IVariable variable in tree.LocalVariables) {
     208      foreach(IVariable variable in LocalVariables) {
    139209        parameters[i] = variable;
    140210        i++;
    141211      }
    142212      parameters[i] = evaluationResults;
    143       return (double)applyMethod.Invoke(null, parameters);
    144     }
    145 
    146     // application of programmable-function is not possible
    147     public double Apply(Dataset dataset, int sampleIndex, double[] args) {
    148       throw new NotSupportedException();
    149     }
    150 
    151     #endregion
    152 
    153     #region disabled operator functionality
    154     // operator-tree style evaluation is not supported for functions.
    155     public override IOperation Apply(IScope scope) {
    156       throw new NotSupportedException();
    157     }
    158 
    159     private static readonly List<IOperator> emptySubOperatorList = new List<IOperator>();
    160     public override IList<IOperator> SubOperators {
    161       get { return emptySubOperatorList; }
    162     }
    163 
    164     public override void AddSubOperator(IOperator subOperator) {
    165       throw new NotSupportedException();
    166     }
    167 
    168     public override bool TryAddSubOperator(IOperator subOperator) {
    169       throw new NotSupportedException();
    170     }
    171 
    172     public override bool TryAddSubOperator(IOperator subOperator, int index) {
    173       throw new NotSupportedException();
    174     }
    175 
    176     public override bool TryAddSubOperator(IOperator subOperator, int index, out ICollection<IConstraint> violatedConstraints) {
    177       throw new NotSupportedException();
    178     }
    179 
    180     public override bool TryAddSubOperator(IOperator subOperator, out ICollection<IConstraint> violatedConstraints) {
    181       throw new NotSupportedException();
    182     }
    183 
    184     public override void AddSubOperator(IOperator subOperator, int index) {
    185       throw new NotSupportedException();
    186     }
    187 
    188     public override void RemoveSubOperator(int index) {
    189       throw new NotSupportedException();
    190     }
    191 
    192     public override bool TryRemoveSubOperator(int index) {
    193       throw new NotSupportedException();
    194     }
    195 
    196     public override bool TryRemoveSubOperator(int index, out ICollection<IConstraint> violatedConstraints) {
    197       throw new NotSupportedException();
    198     }
    199     #endregion
     213      return progFun.Call(parameters);
     214    }
    200215  }
    201216}
  • trunk/sources/HeuristicLab.Functions/Variable.cs

    r177 r189  
    6868    }
    6969
    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;
    76 
    77       if(sampleIndex + offset < 0 || sampleIndex + offset >= dataset.Rows) return double.NaN;
    78       return w * dataset.GetValue(sampleIndex + offset, v);
     70    public override IFunctionTree GetTreeNode() {
     71      return new VariableFunctionTree(this);
    7972    }
    8073
     
    8881    }
    8982  }
     83  class VariableFunctionTree : FunctionTree {
     84    private ConstrainedDoubleData weight;
     85    private ConstrainedIntData index;
     86    private ConstrainedIntData offset;
     87
     88    public VariableFunctionTree() : base() { }
     89    public VariableFunctionTree(Variable variable) : base(variable) {
     90      UpdateCachedValues();
     91    }
     92
     93    protected void UpdateCachedValues() {
     94      weight = (ConstrainedDoubleData)GetLocalVariable(Variable.WEIGHT).Value;
     95      index = (ConstrainedIntData)GetLocalVariable(Variable.INDEX).Value;
     96      offset = (ConstrainedIntData)GetLocalVariable(Variable.OFFSET).Value;
     97    }
     98
     99    public override double Evaluate(Dataset dataset, int sampleIndex) {
     100      return weight.Data * dataset.GetValue(sampleIndex + offset.Data, index.Data);
     101    }
     102
     103    public override object Clone(IDictionary<Guid, object> clonedObjects) {
     104      VariableFunctionTree clone = (VariableFunctionTree)base.Clone(clonedObjects);
     105      clone.UpdateCachedValues();
     106      return clone;
     107    }
     108
     109    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
     110      base.Populate(node, restoredObjects);
     111      UpdateCachedValues();
     112    }
     113  }
    90114}
  • trunk/sources/HeuristicLab.StructureIdentification/Manipulation/ChangeNodeTypeManipulation.cs

    r180 r189  
    156156      int maxArity;
    157157      // create a new tree-node for a randomly selected function
    158       IFunctionTree newTree = new FunctionTree(allowedFunctions[random.Next(allowedFunctions.Count)]);
     158      IFunctionTree newTree = allowedFunctions[random.Next(allowedFunctions.Count)].GetTreeNode();
    159159      gardener.GetMinMaxArity(newTree.Function, out minArity, out maxArity);
    160160      // if the old child had too many sub-trees then the new child should keep as many sub-trees as possible
  • trunk/sources/HeuristicLab.StructureIdentification/Recombination/SizeFairCrossOver.cs

    r161 r189  
    223223      if(possibleParents.Count == 0) throw new InvalidProgramException();
    224224      // and select a random one
    225       IFunctionTree parent = new FunctionTree(possibleParents.ElementAt(random.Next(possibleParents.Count())));
     225      IFunctionTree parent = possibleParents.ElementAt(random.Next(possibleParents.Count())).GetTreeNode();
    226226
    227227      int minArity;
  • trunk/sources/HeuristicLab.StructureIdentification/TreeGardener.cs

    r182 r189  
    141141
    142142      // build the tree
    143       IFunctionTree root = new FunctionTree(selectedFunction);
     143      IFunctionTree root = selectedFunction.GetTreeNode();
    144144      if(balanceTrees) {
    145145        MakeBalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1);
     
    376376      if(maxTreeHeight == 1 || maxTreeSize == 1) {
    377377        IFunction selectedTerminal = RandomSelect(terminals);
    378         return new FunctionTree(selectedTerminal);
     378        return selectedTerminal.GetTreeNode();
    379379      } else {
    380380        IFunction[] possibleFunctions = allFunctions.Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight &&
    381381          GetMinimalTreeSize(f) <= maxTreeSize).ToArray();
    382382        IFunction selectedFunction = RandomSelect(possibleFunctions);
    383         return new FunctionTree(selectedFunction);
     383        return selectedFunction.GetTreeNode();
    384384      }
    385385    }
     
    400400            GetMinimalTreeSize(f) <= maxSubTreeSize).ToArray();
    401401          IFunction selectedFunction = RandomSelect(possibleFunctions);
    402           FunctionTree newSubTree = new FunctionTree(selectedFunction);
     402          IFunctionTree newSubTree = selectedFunction.GetTreeNode();
    403403          MakeUnbalancedTree(newSubTree, maxSubTreeSize - 1, maxTreeHeight - 1);
    404404          parent.InsertSubTree(i, newSubTree);
     
    424424            IFunction[] possibleTerminals = GetAllowedSubFunctions(parent.Function, i).Where(f => IsTerminal(f)).ToArray();
    425425            IFunction selectedTerminal = RandomSelect(possibleTerminals);
    426             IFunctionTree newTree = new FunctionTree(selectedTerminal);
     426            IFunctionTree newTree = selectedTerminal.GetTreeNode();
    427427            parent.InsertSubTree(i, newTree);
    428428          } else {
     
    432432              !IsTerminal(f)).ToArray();
    433433            IFunction selectedFunction = RandomSelect(possibleFunctions);
    434             FunctionTree newTree = new FunctionTree(selectedFunction);
     434            IFunctionTree newTree = selectedFunction.GetTreeNode();
    435435            parent.InsertSubTree(i, newTree);
    436436            MakeBalancedTree(newTree, maxSubTreeSize - 1, maxTreeHeight - 1);
Note: See TracChangeset for help on using the changeset viewer.