Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2675 for trunk/sources


Ignore:
Timestamp:
01/22/10 16:12:26 (15 years ago)
Author:
gkronber
Message:

Implemented a fix in TreeGardener for #836 (Initial population doesn't contain all functions in combination with SimpleFunctionLibraryInjector). Added test cases for SimpleFunctionLibraryInjector. Changed code for calculation of minTreeHeight and minTreeSize in FunctionBase

Location:
trunk/sources
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/FunctionLibraryInjectors/SimpleFunctionLibraryInjector.cs

    r2566 r2675  
    6767
    6868    protected override FunctionLibrary CreateFunctionLibrary() {
     69      return Create(
     70        GetVariableValue<BoolData>(DIFFERENTIALS_ALLOWED, null, false).Data,
     71        minTimeOffset,
     72        maxTimeOffset);
     73    }
     74
     75    public static FunctionLibrary Create(bool differentialAllowed, int minTimeOffset, int maxTimeOffset) {
    6976      FunctionLibrary functionLibrary = new FunctionLibrary();
    7077
     
    8592
    8693      List<IFunction> valueNodes = new List<IFunction>();
    87       ConditionalAddFunction(DIFFERENTIALS_ALLOWED, differential, valueNodes);
     94      if (differentialAllowed) valueNodes.Add(differential);
    8895      valueNodes.Add(variable);
    8996      valueNodes.Add(constant);
     
    122129      SetAllowedSubOperators(tangens, valueNodes);
    123130
    124       ConditionalAddOperator(DIFFERENTIALS_ALLOWED, functionLibrary, differential);
     131      if (differentialAllowed) functionLibrary.AddFunction(differential);
    125132
    126133      allFunctions.ForEach(x => functionLibrary.AddFunction(x));
     
    131138      return functionLibrary;
    132139    }
    133 
    134     private void ConditionalAddFunction(string condName, IFunction fun, List<IFunction> list) {
    135       if (GetVariableValue<BoolData>(condName, null, false).Data) list.Add(fun);
    136     }
    137 
    138     private void ConditionalAddOperator(string condName, FunctionLibrary functionLib, IFunction op) {
    139       if (GetVariableValue<BoolData>(condName, null, false).Data) functionLib.AddFunction(op);
    140     }
    141140  }
    142141}
  • trunk/sources/HeuristicLab.GP.Tests/HeuristicLab.GP.Tests.csproj

    r2616 r2675  
    6363  </ItemGroup>
    6464  <ItemGroup>
     65    <Compile Include="SimpleFunctionLibraryTest.cs" />
    6566    <Compile Include="NetworkFunctionLibraryTest.cs" />
    6667    <Compile Include="HL3TreeEvaluatorTest.cs" />
  • trunk/sources/HeuristicLab.GP/3.3/BaseClasses/FunctionBase.cs

    r2222 r2675  
    2727using System.Diagnostics;
    2828using HeuristicLab.GP.Interfaces;
     29using System.Linq;
    2930
    3031namespace HeuristicLab.GP {
     
    7273      get {
    7374        if (minTreeSize <= 0) RecalculateMinimalTreeSize();
     75        Debug.Assert(minTreeSize > 0);
    7476        return minTreeSize;
    7577      }
     
    7981      get {
    8082        if (minTreeHeight <= 0) RecalculateMinimalTreeHeight();
     83        Debug.Assert(minTreeHeight > 0);
    8184        return minTreeHeight;
    8285      }
     
    130133
    131134    private void RecalculateMinimalTreeSize() {
    132       minTreeSize = int.MaxValue;
    133       int sum = 1;
    134       int minSize = int.MaxValue;
    135       for (int i = 0; i < MinSubTrees; i++) {
    136         foreach (IFunction subFun in GetAllowedSubFunctions(i)) {
    137           minSize = Math.Min(minSize, subFun.MinTreeSize);
    138         }
    139         sum += minSize;
    140       }
    141       minTreeSize = sum;
     135      if (MinSubTrees == 0) minTreeSize = 1;
     136      else {
     137        minTreeSize = int.MaxValue; // prevent infinite recursion
     138        minTreeSize = 1 + (from slot in Enumerable.Range(0, MinSubTrees)
     139                           let minForSlot = (from function in GetAllowedSubFunctions(slot)
     140                                             where function != this
     141                                             select function.MinTreeSize).Min()
     142                           select minForSlot).Sum();
     143      }
    142144    }
    143145
    144146    private void RecalculateMinimalTreeHeight() {
    145       minTreeHeight = int.MaxValue;
    146       int height = 0;
    147       int minHeight = int.MaxValue;
    148       for (int i = 0; i < MinSubTrees; i++) {
    149         foreach (IFunction subFun in GetAllowedSubFunctions(i)) {
    150           minHeight = Math.Min(minHeight, subFun.MinTreeHeight);
    151         }
    152         height = Math.Max(height, minHeight);
    153       }
    154       minTreeHeight = height + 1;
     147      if (MinSubTrees == 0) minTreeHeight = 1;
     148      else {
     149        minTreeHeight = int.MaxValue;
     150        minTreeHeight = 1 + (from slot in Enumerable.Range(0, MinSubTrees)
     151                             let minForSlot = (from function in GetAllowedSubFunctions(slot)
     152                                               where function != this
     153                                               select function.MinTreeHeight).Min()
     154                             select minForSlot).Max();
     155      }
    155156    }
    156157
  • trunk/sources/HeuristicLab.GP/3.3/TreeGardener.cs

    r2566 r2675  
    120120          int a = (int)nextExtension[1];
    121121          int d = (int)nextExtension[2];
    122           if (d == maxDepth) {
     122          if (d + parent.Function.MinTreeHeight >= maxDepth) {
    123123            parent.RemoveSubTree(a);
    124124            IFunctionTree branch = CreateRandomTree(GetAllowedSubFunctions(parent.Function, a), 1, 1);
     
    127127            totalListMinSize -= branch.GetSize();
    128128          } else {
    129             IFunction selectedFunction = TreeGardener.RandomSelect(random, GetAllowedSubFunctions(parent.Function, a).Where(
    130               f => IsRecursiveExpansionPossible(f) && f.MinTreeHeight + (d - 1) <= maxDepth).ToArray());
     129            var allowedSubFunctions = from f in GetAllowedSubFunctions(parent.Function, a)
     130                                      where f.MinTreeHeight + (d - 1) < maxDepth
     131                                      where IsRecursiveExpansionPossible(f) ||
     132                                            totalListMinSize + currentSize >= size * 0.9 // if the necessary size is almost reached then also allow
     133                                                                                         // terminals or terminal-branches
     134                                      select f;
     135            IFunction selectedFunction = TreeGardener.RandomSelect(random, allowedSubFunctions.ToList());
    131136            IFunctionTree newTree = selectedFunction.GetTreeNode();
    132137            parent.RemoveSubTree(a);
     
    146151              list.Add(new object[] { newTree, i, d + 1 });
    147152            }
    148             totalListMinSize += newTree.Function.MinTreeSize - 1;
     153            totalListMinSize += newTree.Function.MinTreeSize;
    149154          }
    150155        }
     
    164169    }
    165170
    166     private bool IsRecursiveExpansionPossible(IFunction parent) {
    167       return FindCycle(parent, new Stack<IFunction>());
     171    private bool IsRecursiveExpansionPossible(IFunction function) {
     172      return FindCycle(function, new Stack<IFunction>());
    168173    }
    169174
    170175    private Dictionary<IFunction, bool> inCycle = new Dictionary<IFunction, bool>();
    171     private bool FindCycle(IFunction parent, Stack<IFunction> parentChain) {
    172       if (inCycle.ContainsKey(parent)) {
    173         return inCycle[parent];
    174       } else if (IsTerminal(parent)) {
    175         inCycle[parent] = false;
     176    private bool FindCycle(IFunction function, Stack<IFunction> functionChain) {
     177      if (inCycle.ContainsKey(function)) {
     178        return inCycle[function];
     179      } else if (IsTerminal(function)) {
     180        inCycle[function] = false;
    176181        return false;
    177       } else if (parentChain.Contains(parent)) {
    178         inCycle[parent] = true;
     182      } else if (functionChain.Contains(function)) {
     183        inCycle[function] = true;
    179184        return true;
    180185      } else {
    181         parentChain.Push(parent);
     186        functionChain.Push(function);
    182187        bool result = false;
    183188        // all slot indexes
    184         for (int i = 0; i < parent.MaxSubTrees; i++) {
    185           foreach (IFunction subFunction in GetAllowedSubFunctions(parent, i)) {
    186             result |= FindCycle(subFunction, parentChain);
     189        for (int i = 0; i < function.MaxSubTrees; i++) {
     190          foreach (IFunction subFunction in GetAllowedSubFunctions(function, i)) {
     191            result |= FindCycle(subFunction, functionChain);
    187192          }
    188193        }
    189194
    190         parentChain.Pop();
    191         inCycle[parent] = result;
     195        functionChain.Pop();
     196        inCycle[function] = result;
    192197        return result;
    193198      }
Note: See TracChangeset for help on using the changeset viewer.