Changeset 2675
- Timestamp:
- 01/22/10 16:12:26 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/FunctionLibraryInjectors/SimpleFunctionLibraryInjector.cs
r2566 r2675 67 67 68 68 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) { 69 76 FunctionLibrary functionLibrary = new FunctionLibrary(); 70 77 … … 85 92 86 93 List<IFunction> valueNodes = new List<IFunction>(); 87 ConditionalAddFunction(DIFFERENTIALS_ALLOWED, differential, valueNodes);94 if (differentialAllowed) valueNodes.Add(differential); 88 95 valueNodes.Add(variable); 89 96 valueNodes.Add(constant); … … 122 129 SetAllowedSubOperators(tangens, valueNodes); 123 130 124 ConditionalAddOperator(DIFFERENTIALS_ALLOWED, functionLibrary,differential);131 if (differentialAllowed) functionLibrary.AddFunction(differential); 125 132 126 133 allFunctions.ForEach(x => functionLibrary.AddFunction(x)); … … 131 138 return functionLibrary; 132 139 } 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 }141 140 } 142 141 } -
trunk/sources/HeuristicLab.GP.Tests/HeuristicLab.GP.Tests.csproj
r2616 r2675 63 63 </ItemGroup> 64 64 <ItemGroup> 65 <Compile Include="SimpleFunctionLibraryTest.cs" /> 65 66 <Compile Include="NetworkFunctionLibraryTest.cs" /> 66 67 <Compile Include="HL3TreeEvaluatorTest.cs" /> -
trunk/sources/HeuristicLab.GP/3.3/BaseClasses/FunctionBase.cs
r2222 r2675 27 27 using System.Diagnostics; 28 28 using HeuristicLab.GP.Interfaces; 29 using System.Linq; 29 30 30 31 namespace HeuristicLab.GP { … … 72 73 get { 73 74 if (minTreeSize <= 0) RecalculateMinimalTreeSize(); 75 Debug.Assert(minTreeSize > 0); 74 76 return minTreeSize; 75 77 } … … 79 81 get { 80 82 if (minTreeHeight <= 0) RecalculateMinimalTreeHeight(); 83 Debug.Assert(minTreeHeight > 0); 81 84 return minTreeHeight; 82 85 } … … 130 133 131 134 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 } 142 144 } 143 145 144 146 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 } 155 156 } 156 157 -
trunk/sources/HeuristicLab.GP/3.3/TreeGardener.cs
r2566 r2675 120 120 int a = (int)nextExtension[1]; 121 121 int d = (int)nextExtension[2]; 122 if (d == maxDepth) {122 if (d + parent.Function.MinTreeHeight >= maxDepth) { 123 123 parent.RemoveSubTree(a); 124 124 IFunctionTree branch = CreateRandomTree(GetAllowedSubFunctions(parent.Function, a), 1, 1); … … 127 127 totalListMinSize -= branch.GetSize(); 128 128 } 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()); 131 136 IFunctionTree newTree = selectedFunction.GetTreeNode(); 132 137 parent.RemoveSubTree(a); … … 146 151 list.Add(new object[] { newTree, i, d + 1 }); 147 152 } 148 totalListMinSize += newTree.Function.MinTreeSize - 1;153 totalListMinSize += newTree.Function.MinTreeSize; 149 154 } 150 155 } … … 164 169 } 165 170 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>()); 168 173 } 169 174 170 175 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; 176 181 return false; 177 } else if ( parentChain.Contains(parent)) {178 inCycle[ parent] = true;182 } else if (functionChain.Contains(function)) { 183 inCycle[function] = true; 179 184 return true; 180 185 } else { 181 parentChain.Push(parent);186 functionChain.Push(function); 182 187 bool result = false; 183 188 // 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); 187 192 } 188 193 } 189 194 190 parentChain.Pop();191 inCycle[ parent] = result;195 functionChain.Pop(); 196 inCycle[function] = result; 192 197 return result; 193 198 }
Note: See TracChangeset
for help on using the changeset viewer.