Free cookie consent management tool by TermsFeed Policy Generator

Changeset 525 for trunk/sources


Ignore:
Timestamp:
08/20/08 10:48:40 (16 years ago)
Author:
gkronber
Message:

Added caching of min and max arity and the allowed sub-functions in FunctionBase. (#263 List of allowed sub-functions for each function should be cached)

Location:
trunk/sources
Files:
3 edited

Legend:

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

    r429 r525  
    2727using System.Xml;
    2828using HeuristicLab.DataAnalysis;
     29using HeuristicLab.Constraints;
    2930
    3031namespace HeuristicLab.Functions {
     
    3637    public const string INITIALIZATION = "Initialization";
    3738    public const string MANIPULATION = "Manipulation";
    38 
     39    private List<IFunction>[] allowedSubFunctions;
     40    private int minArity = -1;
     41    private int maxArity = -1;
    3942
    4043    public virtual double Apply(Dataset dataset, int sampleIndex, double[] args) {
     
    4851    public virtual IFunctionTree GetTreeNode() {
    4952      return new BakedFunctionTree(this);
     53    }
     54
     55    public int MinArity {
     56      get {
     57        if(minArity < 0) RefreshArity();
     58        return minArity;
     59      }
     60    }
     61
     62    public int MaxArity {
     63      get {
     64        if(maxArity < 0) RefreshArity();
     65        return maxArity;
     66      }
     67    }
     68
     69    private void RefreshArity() {
     70      minArity = 2; maxArity = 2; // default arity is 2
     71      foreach(IConstraint constraint in Constraints) {
     72        NumberOfSubOperatorsConstraint theConstraint = constraint as NumberOfSubOperatorsConstraint;
     73        if(theConstraint != null) {
     74          minArity = theConstraint.MinOperators.Data;
     75          maxArity = theConstraint.MaxOperators.Data;
     76        }
     77      }
     78    }
     79
     80    public IList<IFunction> AllowedSubFunctions(int index) {
     81      if(allowedSubFunctions == null) {
     82        allowedSubFunctions = new List<IFunction>[MaxArity];
     83        for(int i = 0; i < MaxArity; i++) {
     84          foreach(IConstraint constraint in Constraints) {
     85            if(constraint is SubOperatorTypeConstraint) {
     86              SubOperatorTypeConstraint subOpConstraint = constraint as SubOperatorTypeConstraint;
     87              if(subOpConstraint.SubOperatorIndex.Data == index) {
     88                allowedSubFunctions[i] = new List<IFunction>();
     89                foreach(IFunction f in subOpConstraint.AllowedSubOperators) allowedSubFunctions[i].Add(f);
     90                break;
     91              }
     92            } else if(constraint is AllSubOperatorsTypeConstraint) {
     93              AllSubOperatorsTypeConstraint subOpConstraint = constraint as AllSubOperatorsTypeConstraint;
     94              allowedSubFunctions[i] = new List<IFunction>();
     95              foreach(IFunction f in subOpConstraint.AllowedSubOperators) allowedSubFunctions[i].Add(f);
     96              break;
     97            }
     98          }
     99        }
     100      }
     101      return allowedSubFunctions[index];
    50102    }
    51103
  • trunk/sources/HeuristicLab.Functions/IFunction.cs

    r189 r525  
    3131    double Apply(Dataset dataset, int sampleIndex, double[] args);
    3232    void Accept(IFunctionVisitor visitor);
     33    IList<IFunction> AllowedSubFunctions(int index);
     34    int MinArity { get; }
     35    int MaxArity { get; }
    3336  }
    3437}
  • trunk/sources/HeuristicLab.StructureIdentification/TreeGardener.cs

    r452 r525  
    166166        int d = (int)nextExtension[2];
    167167        parent.RemoveSubTree(a);
    168         parent.InsertSubTree(a, 
     168        parent.InsertSubTree(a,
    169169          CreateRandomTree(GetAllowedSubFunctions(parent.Function, a), 1, 1)); // append a tree with minimal possible height
    170170      }
     
    282282
    283283    internal bool IsValidTree(IFunctionTree tree) {
    284       SubOperatorsConstraintAnalyser analyzer = new SubOperatorsConstraintAnalyser();
    285       analyzer.AllPossibleOperators = AllFunctions.Cast<IOperator>().ToArray<IOperator>();
    286284      for(int i = 0; i < tree.SubTrees.Count; i++) {
    287         if(!analyzer.GetAllowedOperators(tree.Function, i).Contains(tree.SubTrees[i].Function)) return false;
    288       }
    289 
    290       foreach(IConstraint constraint in tree.Function.Constraints) {
    291         if(constraint is NumberOfSubOperatorsConstraint) {
    292           int max = ((NumberOfSubOperatorsConstraint)constraint).MaxOperators.Data;
    293           int min = ((NumberOfSubOperatorsConstraint)constraint).MinOperators.Data;
    294           if(tree.SubTrees.Count < min || tree.SubTrees.Count > max)
    295             return false;
    296         }
    297       }
     285        if(!tree.Function.AllowedSubFunctions(i).Contains(tree.SubTrees[i].Function)) return false;
     286      }
     287
     288      if(tree.SubTrees.Count < tree.Function.MinArity || tree.SubTrees.Count > tree.Function.MaxArity)
     289        return false;
    298290      foreach(IFunctionTree subTree in tree.SubTrees) {
    299291        if(!IsValidTree(subTree)) return false;
     
    335327      int nSlots = Math.Max(minArity, children.Count);
    336328
    337       SubOperatorsConstraintAnalyser analyzer = new SubOperatorsConstraintAnalyser();
    338       analyzer.AllPossibleOperators = children.Cast<IOperator>().ToArray<IOperator>();
    339 
    340329      List<HashSet<IFunction>> slotSets = new List<HashSet<IFunction>>();
    341330
     
    344333      // we only count those slots that can hold at least one of the children that we should combine
    345334      for(int slot = 0; slot < nSlots; slot++) {
    346         HashSet<IFunction> functionSet = new HashSet<IFunction>(analyzer.GetAllowedOperators(f, slot).Cast<IFunction>());
     335        HashSet<IFunction> functionSet = new HashSet<IFunction>(f.AllowedSubFunctions(slot));
    347336        if(functionSet.Count() > 0) {
    348337          slotSets.Add(functionSet);
     
    400389        return allFunctions;
    401390      } else {
    402         SubOperatorsConstraintAnalyser analyzer = new SubOperatorsConstraintAnalyser();
    403         analyzer.AllPossibleOperators = AllFunctions.Cast<IOperator>().ToArray<IOperator>();
    404         List<IFunction> result = new List<IFunction>();
    405         foreach(IFunction function in analyzer.GetAllowedOperators(f, index)) {
    406           result.Add(function);
    407         }
    408         return result;
     391        return f.AllowedSubFunctions(index);
    409392      }
    410393    }
    411394    internal void GetMinMaxArity(IFunction f, out int minArity, out int maxArity) {
    412       foreach(IConstraint constraint in f.Constraints) {
    413         NumberOfSubOperatorsConstraint theConstraint = constraint as NumberOfSubOperatorsConstraint;
    414         if(theConstraint != null) {
    415           minArity = theConstraint.MinOperators.Data;
    416           maxArity = theConstraint.MaxOperators.Data;
    417           return;
    418         }
    419       }
    420       // the default arity is 2
    421       minArity = 2;
    422       maxArity = 2;
     395      minArity = f.MinArity;
     396      maxArity = f.MaxArity;
    423397    }
    424398    #endregion
Note: See TracChangeset for help on using the changeset viewer.