Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/22/10 16:12:26 (14 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.