Changeset 143
- Timestamp:
- 04/21/08 15:31:41 (17 years ago)
- Location:
- branches/FunctionsAndStructIdRefactoring
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/IFunctionTree.cs
r142 r143 37 37 void InsertSubTree(int index, IFunctionTree tree); 38 38 void RemoveSubTree(int index); 39 39 40 double Evaluate(Dataset dataset, int sampleIndex); 40 41 } -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Evaluation/MeanSquaredErrorEvaluator.cs
r142 r143 47 47 double targetMean = dataset.GetMean(targetVariable); 48 48 for(int sample = 0; sample < dataset.Rows; sample++) { 49 49 50 double estimated = functionTree.Evaluate(dataset, sample); 50 51 double original = dataset.GetValue(sample, targetVariable); -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/ChangeNodeTypeManipulation.cs
r142 r143 84 84 // size and height stays the same when changing a terminal so no need to update the variables 85 85 // schedule an operation to initialize the new terminal 86 return gardener.CreateInitializationOperation(gardener.GetAll Operators(newTerminal), scope);86 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTerminal), scope); 87 87 } else { 88 88 List<IFunctionTree> uninitializedBranches; … … 147 147 IList<IOperator> allowedSubOperators; 148 148 SubOperatorsConstraintAnalyser analyser = new SubOperatorsConstraintAnalyser(); 149 analyser.AllPossibleOperators = gardener.All Operators;149 analyser.AllPossibleOperators = gardener.AllFunctions; 150 150 if (parent == null) { 151 allowedSubOperators = gardener.All Operators;151 allowedSubOperators = gardener.AllFunctions; 152 152 } else { 153 153 allowedSubOperators = analyser.GetAllowedOperators(parent, childIndex); … … 203 203 freshTree = gardener.CreateRandomTree(allowedOperators, maxSubTreeSize, maxSubTreeHeight, false); 204 204 } 205 freshSubTrees.AddRange(gardener.GetAll Operators(freshTree));205 freshSubTrees.AddRange(gardener.GetAllSubTrees(freshTree)); 206 206 207 207 newTree.InsertSubTree(i, freshTree); -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/CutOutNodeManipulation.cs
r23 r143 27 27 using HeuristicLab.Random; 28 28 using System; 29 using HeuristicLab.Functions; 29 30 30 31 namespace HeuristicLab.StructureIdentification { … … 32 33 public override string Description { 33 34 get { 34 return @"Takes a tree, selects a random node of the tree and then tries to replace a random child35 return @"Takes a tree, selects a random node of the tree and then tries to replace a random sub-tree 35 36 of that node with one of the childs of the selected child. 36 37 … … 53 54 AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In)); 54 55 AddVariableInfo(new VariableInfo("BalancedTreesRate", "Determines how many trees should be balanced", typeof(DoubleData), VariableKind.In)); 55 AddVariableInfo(new VariableInfo(" OperatorTree", "The tree to mutate", typeof(IOperator), VariableKind.In));56 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In ));57 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In ));56 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out)); 57 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out)); 58 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out)); 58 59 } 59 60 60 61 61 62 public override IOperation Apply(IScope scope) { 62 I Operator rootOperator = GetVariableValue<IOperator>("OperatorTree", scope, true);63 IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true); 63 64 MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true); 64 65 GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true); … … 68 69 69 70 TreeGardener gardener = new TreeGardener(random, library); 70 I Operator parent = gardener.GetRandomParentNode(rootOperator);71 IFunctionTree parent = gardener.GetRandomParentNode(root); 71 72 // parent == null means we should cut out the root node 72 // => return a random sub operatorof the root73 // => return a random sub-tree of the root 73 74 if (parent == null) { 74 // when there are sub operators then replace the old operator with a random suboperator75 if (root Operator.SubOperators.Count > 0) {76 root Operator = rootOperator.SubOperators[random.Next(rootOperator.SubOperators.Count)];75 // when there are sub-trees then replace the old tree with a random sub-tree 76 if (root.SubTrees.Count > 0) { 77 root = root.SubTrees[random.Next(root.SubTrees.Count)]; 77 78 78 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root Operator);79 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root Operator);79 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root); 80 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root); 80 81 81 // this is not really necessary (we can leave it in until the operatoris stable)82 if (!gardener.IsValidTree(root Operator)) {82 // this is not really necessary (we can leave it in until the tree is stable) 83 if (!gardener.IsValidTree(root)) { 83 84 throw new InvalidProgramException(); 84 85 } 85 86 86 87 // update the variable 87 scope.GetVariable( "OperatorTree").Value = rootOperator;88 if (!gardener.IsValidTree(root Operator)) {88 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = root; 89 if (!gardener.IsValidTree(root)) { 89 90 throw new InvalidProgramException(); 90 91 } 91 92 93 92 // the tree is already initialized so we don't have to schedule initialization operations 94 93 return null; 95 94 } else { 96 95 // create a new random tree 97 I Operator newOperator;96 IFunctionTree newTree; 98 97 if(random.NextDouble() <= balancedTreesRate) { 99 new Operator = gardener.CreateRandomTree(gardener.AllOperators, maxTreeSize, maxTreeHeight, true);98 newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, true); 100 99 } else { 101 new Operator = gardener.CreateRandomTree(gardener.AllOperators, maxTreeSize, maxTreeHeight, false);100 newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, false); 102 101 } 103 102 104 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(new Operator);105 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(new Operator);103 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newTree); 104 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newTree); 106 105 107 if (!gardener.IsValidTree(newOperator)) { 106 // update the variable 107 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree; 108 109 if (!gardener.IsValidTree(newTree)) { 108 110 throw new InvalidProgramException(); 109 111 } 110 112 111 // update the variable 112 scope.GetVariable("OperatorTree").Value = newOperator; 113 114 if (!gardener.IsValidTree(newOperator)) { 115 throw new InvalidProgramException(); 116 } 117 118 // schedule an operation to initialize the whole operator graph 119 return gardener.CreateInitializationOperation(gardener.GetAllOperators(newOperator), scope); 113 // schedule an operation to initialize the whole tree 114 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope); 120 115 } 121 116 } 122 117 123 int childIndex = random.Next(parent.Sub Operators.Count);124 I Operator child = parent.SubOperators[childIndex];118 int childIndex = random.Next(parent.SubTrees.Count); 119 IFunctionTree child = parent.SubTrees[childIndex]; 125 120 126 // match the suboperators of the child with the allowed suboperators of the parent 127 IOperator[] possibleChilds = gardener.GetAllowedSubOperators(parent, childIndex).SelectMany(allowedOp => child.SubOperators 128 .Where(subOp => ((StringData)subOp.GetVariable(GPOperatorLibrary.TYPE_ID).Value).Data == 129 ((StringData)allowedOp.GetVariable(GPOperatorLibrary.TYPE_ID).Value).Data)).ToArray(); 130 121 // match the sub-trees of the child with the allowed sub-trees of the parent 122 IFunction[] possibleChilds = gardener.GetAllowedSubFunctions(parent.Function, childIndex).SelectMany(allowedFun => child.SubTrees 123 .Where(subTree => allowedFun == subTree.Function)).Select(t=>t.Function).ToArray(); // Actually we should probably use OperatorEqualityComparer here (gkronber 21.04.08) 131 124 132 125 if (possibleChilds.Length > 0) { 133 126 // replace child with a random child of the child 134 // make a clone to simplify removing obsolete operators from the operator-graph135 I Operator selectedChild = (IOperator)possibleChilds[random.Next(possibleChilds.Length)].Clone();136 parent.RemoveSub Operator(childIndex);137 parent. AddSubOperator(selectedChild, childIndex);127 // make a clone to simplify removing obsolete branches from the function-tree 128 IFunction selectedChild = possibleChilds[random.Next(possibleChilds.Length)]; 129 parent.RemoveSubTree(childIndex); 130 parent.InsertSubTree(childIndex, new FunctionTree(selectedChild)); 138 131 139 if (!gardener.IsValidTree(root Operator)) {132 if (!gardener.IsValidTree(root)) { 140 133 throw new InvalidProgramException(); 141 134 } 142 135 143 136 // update the size and height of our tree 144 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root Operator);145 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root Operator);137 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root); 138 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root); 146 139 // don't need to schedule initialization operations 147 140 return null; 148 141 } else { 149 142 // determine the level of the parent 150 int parentLevel = gardener.Get NodeLevel(rootOperator, parent);143 int parentLevel = gardener.GetBranchLevel(root, parent); 151 144 152 145 // first remove the old child (first step essential!) 153 parent.RemoveSub Operator(childIndex);146 parent.RemoveSubTree(childIndex); 154 147 // then determine the number of nodes left over after the child has been removed! 155 int remainingNodes = gardener.GetTreeSize(root Operator);148 int remainingNodes = gardener.GetTreeSize(root); 156 149 157 IList<I Operator> allowedOperators = gardener.GetAllowedSubOperators(parent, childIndex);158 I Operator newOperatorTree = gardener.CreateRandomTree(allowedOperators, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel, true);150 IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex); 151 IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel, true); 159 152 160 parent. AddSubOperator(newOperatorTree, childIndex);153 parent.InsertSubTree(childIndex, newFunctionTree); 161 154 162 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root Operator);163 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root Operator);155 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root); 156 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root); 164 157 165 if (!gardener.IsValidTree(root Operator)) {158 if (!gardener.IsValidTree(root)) { 166 159 throw new InvalidProgramException(); 167 160 } 168 161 169 // schedule an initialization operation for the new operator170 return gardener.CreateInitializationOperation(gardener.GetAll Operators(newOperatorTree), scope);162 // schedule an initialization operation for the new function-tree 163 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newFunctionTree), scope); 171 164 } 172 165 } -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/DeleteSubTreeManipulation.cs
r23 r143 26 26 using HeuristicLab.Operators; 27 27 using HeuristicLab.Random; 28 using HeuristicLab.Functions; 28 29 29 30 namespace HeuristicLab.StructureIdentification { … … 42 43 AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In)); 43 44 AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In)); 44 AddVariableInfo(new VariableInfo(" OperatorTree", "The tree to mutate", typeof(IOperator), VariableKind.In | VariableKind.Out));45 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out)); 45 46 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out)); 46 47 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out)); … … 49 50 50 51 public override IOperation Apply(IScope scope) { 51 I Operator rootOperator = GetVariableValue<IOperator>("OperatorTree", scope, true);52 IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true); 52 53 MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true); 53 54 GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true); 54 55 55 TreeGardener gardener = new TreeGardener(random, library); 56 57 IOperator parent = gardener.GetRandomParentNode(rootOperator); 56 IFunctionTree parent = gardener.GetRandomParentNode(root); 58 57 59 58 // parent==null means the whole tree should be deleted. 60 59 // => return a new minimal random tree 61 60 if(parent == null) { 62 I OperatornewTree = gardener.CreateRandomTree(1, 1, true);61 IFunctionTree newTree = gardener.CreateRandomTree(1, 1, true); 63 62 64 63 // check if the tree is ok … … 70 69 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newTree); 71 70 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newTree); 72 scope.GetVariable( "OperatorTree").Value = newTree;71 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree; 73 72 74 73 // schedule an operation to initialize the newly created operator 75 return gardener.CreateInitializationOperation(gardener.GetAll Operators(newTree), scope);74 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope); 76 75 } 77 76 78 int childIndex = random.Next(parent.Sub Operators.Count);77 int childIndex = random.Next(parent.SubTrees.Count); 79 78 80 79 int min; 81 80 int max; 82 gardener.GetMinMaxArity(parent , out min, out max);83 if(parent.Sub Operators.Count > min) {84 parent.RemoveSub Operator(childIndex);85 // actually since the next sub operators are shifted in the place of the removed operator86 // it might be possible that these sub operators are not allowed in the place of the old operator81 gardener.GetMinMaxArity(parent.Function, out min, out max); 82 if(parent.SubTrees.Count > min) { 83 parent.RemoveSubTree(childIndex); 84 // actually since the next sub-trees are shifted in the place of the removed branch 85 // it might be possible that these sub-trees are not allowed in the place of the old branch 87 86 // we ignore this problem for now. 88 // when this starts to become a problem a possible solution is to go through the shifted operators from the place of the shifted87 // when this starts to become a problem a possible solution is to go through the shifted branches from the place of the shifted 89 88 // and find the first one that doesn't fit. At this position we insert a new randomly initialized subtree of matching type (gkronber 25.12.07) 90 89 91 if(!gardener.IsValidTree(root Operator)) {90 if(!gardener.IsValidTree(root)) { 92 91 throw new InvalidOperationException(); 93 92 } 94 93 95 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(rootOperator); 96 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(rootOperator); 97 // root hasn't changed so don't need to update 'OperatorTree' variable 98 94 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root); 95 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root); 96 // root hasn't changed so don't need to update 'FunctionTree' variable 99 97 return null; 100 98 } else { 101 99 // replace with a minimal random seedling 102 parent.RemoveSub Operator(childIndex);100 parent.RemoveSubTree(childIndex); 103 101 104 IList<I Operator> allowedOperators = gardener.GetAllowedSubOperators(parent, childIndex);105 I Operator newOperatorTree = gardener.CreateRandomTree(allowedOperators, 1, 1, true);102 IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex); 103 IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, 1, 1, true); 106 104 107 parent. AddSubOperator(newOperatorTree, childIndex);105 parent.InsertSubTree(childIndex, newFunctionTree); 108 106 109 if(!gardener.IsValidTree(root Operator)) {107 if(!gardener.IsValidTree(root)) { 110 108 throw new InvalidProgramException(); 111 109 } 112 110 113 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(rootOperator); 114 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(rootOperator); 115 // again the root hasn't changed so we don't need to update the 'OperatorTree' variable 116 117 return gardener.CreateInitializationOperation(gardener.GetAllOperators(newOperatorTree), scope); 111 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root); 112 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root); 113 // again the root hasn't changed so we don't need to update the 'FunctionTree' variable 114 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newFunctionTree), scope); 118 115 } 119 116 } -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/FullTreeShaker.cs
r88 r143 29 29 using HeuristicLab.Data; 30 30 using HeuristicLab.Selection; 31 using HeuristicLab.Functions; 31 32 32 33 namespace HeuristicLab.StructureIdentification { … … 38 39 public FullTreeShaker() 39 40 : base() { 41 AddVariableInfo(new VariableInfo("Random", "A random generator (uniform)", typeof(MersenneTwister), VariableKind.In)); 40 42 AddVariableInfo(new VariableInfo("OperatorLibrary", "Operator library that defines operator mutations", typeof(GPOperatorLibrary), VariableKind.In)); 41 AddVariableInfo(new VariableInfo("OperatorTree", "The operator tree that should be mutated", typeof(IOperator), VariableKind.In)); 42 AddVariableInfo(new VariableInfo("Random", "A random generator (uniform)", typeof(MersenneTwister), VariableKind.In)); 43 AddVariableInfo(new VariableInfo("ShakingFactor", "Variable that determines the force of the shakeing operation", typeof(DoubleData), VariableKind.In)); 43 AddVariableInfo(new VariableInfo("ShakingFactor", "Variable that determines the force of the shaking operation", typeof(DoubleData), VariableKind.In)); 44 AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be mutated", typeof(IFunctionTree), VariableKind.In | VariableKind.Out)); 44 45 } 45 46 46 47 public override IOperation Apply(IScope scope) { 47 48 GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true); 48 I Operator op = GetVariableValue<IOperator>("OperatorTree", scope, false);49 IFunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", scope, false); 49 50 MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true); 50 51 … … 56 57 57 58 TreeGardener gardener = new TreeGardener(mt, library); 58 var parametric Nodes = gardener.GetAllOperators(op).Where(o => o.GetVariable(GPOperatorLibrary.MANIPULATION) != null);59 foreach(I Operator subOperator in parametricNodes) {60 IOperator mutation =(IOperator)sub Operator.GetVariable(GPOperatorLibrary.MANIPULATION).Value;59 var parametricBranches = gardener.GetAllSubTrees(tree).Where(branch => branch.Function.GetVariable(GPOperatorLibrary.MANIPULATION) != null); 60 foreach(IFunctionTree subTree in parametricBranches) { 61 IOperator mutation =(IOperator)subTree.Function.GetVariable(GPOperatorLibrary.MANIPULATION).Value; 61 62 62 63 // store all local variables into a temporary scope 63 64 Scope mutationScope = new Scope(); 64 foreach(IVariableInfo info in subOperator.VariableInfos) { 65 if(info.Local) { 66 mutationScope.AddVariable(subOperator.GetVariable(info.FormalName)); 67 } 65 foreach(IVariable variable in subTree.LocalVariables) { 66 mutationScope.AddVariable(variable); 68 67 } 69 68 -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/OnePointShaker.cs
r88 r143 29 29 using HeuristicLab.Data; 30 30 using HeuristicLab.Selection; 31 using HeuristicLab.Functions; 31 32 32 33 namespace HeuristicLab.StructureIdentification { … … 39 40 : base() { 40 41 AddVariableInfo(new VariableInfo("OperatorLibrary", "Operator library that defines mutation operations for operators", typeof(GPOperatorLibrary), VariableKind.In)); 41 AddVariableInfo(new VariableInfo("OperatorTree", "The operator tree that should be mutated", typeof(IOperator), VariableKind.In));42 42 AddVariableInfo(new VariableInfo("Random", "A random generator (uniform)", typeof(MersenneTwister), VariableKind.In)); 43 43 AddVariableInfo(new VariableInfo("ShakingFactor", "Factor that determines the force of the shaking operation", typeof(DoubleData), VariableKind.In)); 44 AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be mutated", typeof(IFunctionTree), VariableKind.In | VariableKind.Out)); 44 45 } 45 46 46 47 public override IOperation Apply(IScope scope) { 47 48 GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true); 48 I Operator op = GetVariableValue<IOperator>("OperatorTree", scope, false);49 IFunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", scope, false); 49 50 MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true); 50 51 TreeGardener gardener = new TreeGardener(mt, library); 51 52 52 53 // get all nodes for which a manipulation is defined 53 var parametricNodes = gardener.GetAllOperators(op).Where(o => o.GetVariable(GPOperatorLibrary.MANIPULATION) != null); 54 IOperator selectedOp = parametricNodes.ElementAt(mt.Next(parametricNodes.Count())); 55 56 IOperator mutation = (IOperator)selectedOp.GetVariable(GPOperatorLibrary.MANIPULATION).Value; 57 54 var parametricBranches = gardener.GetAllSubTrees(tree).Where(branch => branch.Function.GetVariable(GPOperatorLibrary.MANIPULATION) != null); 55 IFunctionTree selectedBranch = parametricBranches.ElementAt(mt.Next(parametricBranches.Count())); 56 IOperator mutation = (IOperator)selectedBranch.Function.GetVariable(GPOperatorLibrary.MANIPULATION).Value; 58 57 CompositeOperation next = new CompositeOperation(); 59 58 60 59 // store all local variables into a temporary scope 61 60 Scope tempScope = new Scope("Temp. manipulation scope"); 62 // add aliases 63 foreach(IVariableInfo variableInfo in VariableInfos) 64 tempScope.AddAlias(variableInfo.FormalName, variableInfo.ActualName); 65 66 foreach(IVariableInfo info in selectedOp.VariableInfos) { 67 if(info.Local) { 68 tempScope.AddVariable(selectedOp.GetVariable(info.FormalName)); 69 } 61 foreach(IVariable variable in selectedBranch.LocalVariables) { 62 tempScope.AddVariable(variable); 70 63 } 71 64 -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/SubstituteSubTreeManipulation.cs
r23 r143 69 69 IOperator newOperatorTree; 70 70 if(random.NextDouble() <= balancedTreesRate) { 71 newOperatorTree = gardener.CreateRandomTree(gardener.All Operators, maxTreeSize, maxTreeHeight, true);71 newOperatorTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, true); 72 72 } else { 73 newOperatorTree = gardener.CreateRandomTree(gardener.All Operators, maxTreeSize, maxTreeHeight, false);73 newOperatorTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, false); 74 74 } 75 75 … … 84 84 85 85 // return a CompositeOperation that randomly initializes the new operator 86 return gardener.CreateInitializationOperation(gardener.GetAll Operators(newOperatorTree), scope);86 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newOperatorTree), scope); 87 87 } else { 88 88 // determine a random child of the parent to be replaced … … 90 90 91 91 // get the list of allowed suboperators as the new child 92 IList<IOperator> allowedOperators = gardener.GetAllowedSub Operators(parent, childIndex);92 IList<IOperator> allowedOperators = gardener.GetAllowedSubFunctions(parent, childIndex); 93 93 94 94 if(allowedOperators.Count == 0) { … … 100 100 // calculate the maximum size and height of the new sub-tree based on the location where 101 101 // it will be inserted 102 int parentLevel = gardener.Get NodeLevel(rootOperator, parent);102 int parentLevel = gardener.GetBranchLevel(rootOperator, parent); 103 103 104 104 int maxSubTreeHeight = maxTreeHeight - parentLevel; … … 127 127 128 128 // return a CompositeOperation that randomly initializes all nodes of the new subtree 129 return gardener.CreateInitializationOperation(gardener.GetAll Operators(newOperatorTree), scope);129 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newOperatorTree), scope); 130 130 } 131 131 } -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/RandomTreeCreator.cs
r142 r143 78 78 } 79 79 80 return gardener.CreateInitializationOperation(gardener.GetAll Operators(root), scope);80 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(root), scope); 81 81 } 82 82 } -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Recombination/SinglePointCrossOver.cs
r142 r143 81 81 private IOperation Cross(TreeGardener gardener, int maxTreeSize, int maxTreeHeight, 82 82 IScope scope, MersenneTwister random, IScope parent1, IScope parent2, IScope child) { 83 List<I Operator> newOperators;84 I OperatornewTree = Cross(gardener, parent1, parent2,85 random, maxTreeSize, maxTreeHeight, out new Operators);83 List<IFunctionTree> newBranches; 84 IFunctionTree newTree = Cross(gardener, parent1, parent2, 85 random, maxTreeSize, maxTreeHeight, out newBranches); 86 86 87 87 if(!gardener.IsValidTree(newTree)) { … … 91 91 int newTreeSize = gardener.GetTreeSize(newTree); 92 92 int newTreeHeight = gardener.GetTreeHeight(newTree); 93 child.AddVariable(new Variable("FunctionTree", newTree));94 child.AddVariable(new Variable("TreeSize", new IntData(newTreeSize)));95 child.AddVariable(new Variable("TreeHeight", new IntData(newTreeHeight)));93 child.AddVariable(new HeuristicLab.Core.Variable("FunctionTree", newTree)); 94 child.AddVariable(new HeuristicLab.Core.Variable("TreeSize", new IntData(newTreeSize))); 95 child.AddVariable(new HeuristicLab.Core.Variable("TreeHeight", new IntData(newTreeHeight))); 96 96 97 97 // check if the size of the new tree is still in the allowed bounds … … 100 100 throw new InvalidProgramException(); 101 101 } 102 103 104 return gardener.CreateInitializationOperation(newOperators, child); 102 return gardener.CreateInitializationOperation(newBranches, child); 105 103 } 106 104 … … 133 131 int tree0Level = random.Next(tree0Height - 1); // since we checked before that the height of tree0 is > 1 this is OK 134 132 int tree1Level = random.Next(tree1Height); 135 tree0 = gardener.GetRandom Node(tree0, tree0Level);136 tree1 = gardener.GetRandom Node(tree1, tree1Level);133 tree0 = gardener.GetRandomBranch(tree0, tree0Level); 134 tree1 = gardener.GetRandomBranch(tree1, tree1Level); 137 135 138 136 // recalculate the size and height of tree1 (the one that we want to insert) because we need to check constraints later on … … 141 139 142 140 List<int> possibleChildIndices = new List<int>(); 143 TreeGardener. OperatorEqualityComparer comparer = new TreeGardener.OperatorEqualityComparer();141 TreeGardener.FunctionEqualityComparer comparer = new TreeGardener.FunctionEqualityComparer(); 144 142 145 143 // Now tree0 is supposed to take tree1 as one if its children. If this is not possible, … … 148 146 // find the list of allowed indices (regarding allowed sub-operators, maxTreeSize and maxTreeHeight) 149 147 for(int i = 0; i < tree0.SubTrees.Count; i++) { 150 int sub OperatorSize = gardener.GetTreeSize(tree0.SubTrees[i]);151 152 // the index is ok when the operator is allowed as sub-operatorand we don't violate the maxSize and maxHeight constraints153 if(GetAllowedOperators(tree0 , i).Contains(tree1, comparer) &&154 rootSize - sub OperatorSize + tree1Size < maxTreeSize &&148 int subTreeSize = gardener.GetTreeSize(tree0.SubTrees[i]); 149 150 // the index is ok when the function is allowed as sub-tree and we don't violate the maxSize and maxHeight constraints 151 if(GetAllowedOperators(tree0.Function, i).Contains(tree1.Function, comparer) && 152 rootSize - subTreeSize + tree1Size < maxTreeSize && 155 153 tree0Level + tree1Height < maxTreeHeight) { 156 154 possibleChildIndices.Add(i); … … 171 169 // go up in tree0 172 170 tree0Level--; 173 tree0 = gardener.GetRandom Node(root, tree0Level);171 tree0 = gardener.GetRandomBranch(root, tree0Level); 174 172 } else if(tree1.SubTrees.Count > 0) { 175 173 // go down in node2: … … 185 183 possibleChildIndices.Clear(); 186 184 for(int i = 0; i < tree0.SubTrees.Count; i++) { 187 int sub OperatorSize = gardener.GetTreeSize(tree0.SubTrees[i]);188 189 // when the operator is allowed as sub-operatorand we don't violate the maxSize and maxHeight constraints185 int subTreeSize = gardener.GetTreeSize(tree0.SubTrees[i]); 186 187 // when the function is allowed as sub-tree and we don't violate the maxSize and maxHeight constraints 190 188 // the index is ok 191 if(GetAllowedOperators(tree0 , i).Contains(tree1, comparer) &&192 rootSize - sub OperatorSize + tree1Size < maxTreeSize &&189 if(GetAllowedOperators(tree0.Function, i).Contains(tree1.Function, comparer) && 190 rootSize - subTreeSize + tree1Size < maxTreeSize && 193 191 tree0Level + tree1Height < maxTreeHeight) { 194 192 possibleChildIndices.Add(i); … … 205 203 int selectedIndex = possibleChildIndices[random.Next(possibleChildIndices.Count)]; 206 204 tree0.RemoveSubTree(selectedIndex); 207 tree0. AddSubTree(tree1, selectedIndex);205 tree0.InsertSubTree(selectedIndex, tree1); 208 206 209 207 // no new operators where needed … … 213 211 } 214 212 215 private ICollection<I Operator> GetAllowedOperators(IOperator tree0, int i) {216 ItemList slotList = (ItemList) tree0.GetVariable(GPOperatorLibrary.ALLOWED_SUBOPERATORS).Value;217 return ((ItemList)slotList[i]). OfType<IOperator>().ToArray();213 private ICollection<IFunction> GetAllowedOperators(IFunction f, int i) { 214 ItemList slotList = (ItemList)f.GetVariable(GPOperatorLibrary.ALLOWED_SUBOPERATORS).Value; 215 return ((ItemList)slotList[i]).Cast<IFunction>().ToArray(); 218 216 } 219 217 220 218 private IFunctionTree CombineTerminals(TreeGardener gardener, IFunctionTree f, IFunctionTree g, MersenneTwister random, int maxTreeHeight, out List<IFunctionTree> newBranches) { 221 219 newBranches = new List<IFunctionTree>(); 222 ICollection<I Operator> possibleParents = gardener.GetPossibleParents(new List<IOperator>() { f.Function, g.Function });220 ICollection<IFunction> possibleParents = gardener.GetPossibleParents(new List<IFunction>() { f.Function, g.Function }); 223 221 if(possibleParents.Count == 0) throw new InvalidProgramException(); 224 222 225 I Operator parent = (IOperator)possibleParents.ElementAt(random.Next(possibleParents.Count())).Clone();223 IFunctionTree parent = new FunctionTree(possibleParents.ElementAt(random.Next(possibleParents.Count()))); 226 224 227 225 int minArity; 228 226 int maxArity; 229 gardener.GetMinMaxArity(parent , out minArity, out maxArity);227 gardener.GetMinMaxArity(parent.Function, out minArity, out maxArity); 230 228 231 229 int nSlots = Math.Max(2, minArity); 232 230 233 HashSet<I Operator>[] slotSets = new HashSet<IOperator>[nSlots];231 HashSet<IFunction>[] slotSets = new HashSet<IFunction>[nSlots]; 234 232 235 233 SubOperatorsConstraintAnalyser analyser = new SubOperatorsConstraintAnalyser(); 236 234 analyser.AllPossibleOperators = new List<IOperator>() { g.Function, f.Function }; 237 235 for(int slot = 0; slot < nSlots; slot++) { 238 HashSet<I Operator> slotSet = new HashSet<IOperator>(analyser.GetAllowedOperators(parent, slot));236 HashSet<IFunction> slotSet = new HashSet<IFunction>(analyser.GetAllowedOperators(parent.Function, slot).Cast<IFunction>()); 239 237 slotSets[slot] = slotSet; 240 238 } … … 242 240 int[] slotSequence = Enumerable.Range(0, slotSets.Count()).OrderBy(slot => slotSets[slot].Count()).ToArray(); 243 241 244 I Operator[] selectedOperators = new IOperator[nSlots];242 IFunctionTree[] selectedFunctionTrees = new IFunctionTree[nSlots]; 245 243 for(int i = 0; i < slotSequence.Length; i++) { 246 244 int slot = slotSequence[i]; 247 HashSet<I Operator> slotSet = slotSets[slot];245 HashSet<IFunction> slotSet = slotSets[slot]; 248 246 if(slotSet.Count() == 0) { 249 var allowed Operators = GetAllowedOperators(parent, slot);250 selected Operators[slot] = gardener.CreateRandomTree(allowedOperators, 1, 1, true);251 newBranches.AddRange(gardener.GetAll Operators(selectedOperators[slot]));247 var allowedFunctions = GetAllowedOperators(parent.Function, slot); 248 selectedFunctionTrees[slot] = gardener.CreateRandomTree(allowedFunctions, 1, 1, true); 249 newBranches.AddRange(gardener.GetAllSubTrees(selectedFunctionTrees[slot])); 252 250 } else { 253 I Operator selectedOperator= slotSet.ElementAt(random.Next(slotSet.Count()));254 selected Operators[slot] = selectedOperator;251 IFunction selectedFunction = slotSet.ElementAt(random.Next(slotSet.Count())); 252 selectedFunctionTrees[slot] = new FunctionTree(selectedFunction); 255 253 for(int j = i + 1; j < slotSequence.Length; j++) { 256 254 int otherSlot = slotSequence[j]; 257 slotSets[otherSlot].Remove(selected Operator);258 } 259 } 260 } 261 262 for(int i = 0; i < selected Operators.Length; i++) {263 parent. AddSubOperator(selectedOperators[i], i);255 slotSets[otherSlot].Remove(selectedFunction); 256 } 257 } 258 } 259 260 for(int i = 0; i < selectedFunctionTrees.Length; i++) { 261 parent.InsertSubTree(i, selectedFunctionTrees[i]); 264 262 } 265 263 -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/TreeGardener.cs
r142 r143 36 36 internal class TreeGardener { 37 37 private IRandom random; 38 private IOperatorLibrary opLibrary;39 private List<I Operator> functions;40 private List<I Operator> terminals;41 42 internal IList<I Operator> Terminals {38 private IOperatorLibrary funLibrary; 39 private List<IFunction> functions; 40 private List<IFunction> terminals; 41 42 internal IList<IFunction> Terminals { 43 43 get { return terminals.AsReadOnly(); } 44 44 } 45 private List<I Operator> allOperators;46 47 internal IList<I Operator> AllOperators {48 get { return all Operators.AsReadOnly(); }49 } 50 51 internal TreeGardener(IRandom random, IOperatorLibrary opLibrary) {45 private List<IFunction> allFunctions; 46 47 internal IList<IFunction> AllFunctions { 48 get { return allFunctions.AsReadOnly(); } 49 } 50 51 internal TreeGardener(IRandom random, IOperatorLibrary funLibrary) { 52 52 this.random = random; 53 this. opLibrary = opLibrary;54 55 this.all Operators = new List<IOperator>();56 terminals = new List<I Operator>();57 functions = new List<I Operator>();53 this.funLibrary = funLibrary; 54 55 this.allFunctions = new List<IFunction>(); 56 terminals = new List<IFunction>(); 57 functions = new List<IFunction>(); 58 58 59 59 // init functions and terminals based on constraints 60 foreach (I Operator op in opLibrary.Group.Operators) {60 foreach (IFunction fun in funLibrary.Group.Operators) { 61 61 int maxA, minA; 62 GetMinMaxArity( op, out minA, out maxA);62 GetMinMaxArity(fun, out minA, out maxA); 63 63 if (maxA == 0) { 64 terminals.Add( op);64 terminals.Add(fun); 65 65 } else { 66 functions.Add( op);67 } 68 } 69 70 all Operators.AddRange(functions);71 all Operators.AddRange(terminals);66 functions.Add(fun); 67 } 68 } 69 70 allFunctions.AddRange(functions); 71 allFunctions.AddRange(terminals); 72 72 } 73 73 74 74 #region random initialization 75 internal IFunctionTree CreateRandomTree(ICollection<I Operator> allowedOperators, int maxTreeSize, int maxTreeHeight, bool balanceTrees) {76 77 int minTreeHeight = allowed Operators.Select(op => ((IntData)op.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data).Min();75 internal IFunctionTree CreateRandomTree(ICollection<IFunction> allowedFunctions, int maxTreeSize, int maxTreeHeight, bool balanceTrees) { 76 77 int minTreeHeight = allowedFunctions.Select(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data).Min(); 78 78 if (minTreeHeight > maxTreeHeight) 79 79 maxTreeHeight = minTreeHeight; 80 80 81 int minTreeSize = allowed Operators.Select(op => ((IntData)op.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data).Min();81 int minTreeSize = allowedFunctions.Select(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data).Min(); 82 82 if (minTreeSize > maxTreeSize) 83 83 maxTreeSize = minTreeSize; … … 86 86 int treeSize = random.Next(minTreeSize, maxTreeSize + 1); 87 87 88 I Operator[] possibleOperators = allowedOperators.Where(op => ((IntData)op.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data <= treeHeight &&89 ((IntData) op.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data <= treeSize).ToArray();90 I Operator selectedOperator = possibleOperators[random.Next(possibleOperators.Length)];91 92 return CreateRandomTree(selected Operator, treeSize, treeHeight, balanceTrees);88 IFunction[] possibleFunctions = allowedFunctions.Where(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data <= treeHeight && 89 ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data <= treeSize).ToArray(); 90 IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)]; 91 92 return CreateRandomTree(selectedFunction, treeSize, treeHeight, balanceTrees); 93 93 } 94 94 … … 96 96 if (balanceTrees) { 97 97 if (maxTreeHeight == 1 || maxTreeSize==1) { 98 I OperatorselectedTerminal = terminals[random.Next(terminals.Count())];98 IFunction selectedTerminal = terminals[random.Next(terminals.Count())]; 99 99 return new FunctionTree(selectedTerminal); 100 100 } else { 101 I Operator[] possibleFunctions = functions.Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight &&101 IFunction[] possibleFunctions = functions.Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight && 102 102 GetMinimalTreeSize(f) <= maxTreeSize).ToArray(); 103 I OperatorselectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)];103 IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)]; 104 104 FunctionTree root = new FunctionTree(selectedFunction); 105 105 MakeBalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1); … … 108 108 109 109 } else { 110 I Operator[] possibleOperators = allOperators.Where(op => GetMinimalTreeHeight(op) <= maxTreeHeight &&111 GetMinimalTreeSize( op) <= maxTreeSize).ToArray();112 I Operator selectedOperator = possibleOperators[random.Next(possibleOperators.Length)];113 FunctionTree root = new FunctionTree(selected Operator);110 IFunction[] possibleFunctions = allFunctions.Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight && 111 GetMinimalTreeSize(f) <= maxTreeSize).ToArray(); 112 IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)]; 113 FunctionTree root = new FunctionTree(selectedFunction); 114 114 MakeUnbalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1); 115 115 return root; … … 117 117 } 118 118 119 internal IFunctionTree CreateRandomTree(I Operator rootOp, int maxTreeSize, int maxTreeHeight, bool balanceTrees) {120 IFunctionTree root = new FunctionTree(root Op);119 internal IFunctionTree CreateRandomTree(IFunction rootFunction, int maxTreeSize, int maxTreeHeight, bool balanceTrees) { 120 IFunctionTree root = new FunctionTree(rootFunction); 121 121 if (balanceTrees) { 122 122 MakeBalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1); … … 144 144 int maxSubTreeSize = maxTreeSize / actualArity; 145 145 for (int i = 0; i < actualArity; i++) { 146 I Operator[] possibleOperators = GetAllowedSubOperators(parent.Function, i).Where(op => GetMinimalTreeHeight(op) <= maxTreeHeight &&147 GetMinimalTreeSize( op) <= maxSubTreeSize).ToArray();148 I Operator selectedOperator = possibleOperators[random.Next(possibleOperators.Length)];149 FunctionTree newSubTree = new FunctionTree(selected Operator);146 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent.Function, i).Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight && 147 GetMinimalTreeSize(f) <= maxSubTreeSize).ToArray(); 148 IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)]; 149 FunctionTree newSubTree = new FunctionTree(selectedFunction); 150 150 MakeUnbalancedTree(newSubTree, maxSubTreeSize - 1, maxTreeHeight - 1); 151 151 parent.InsertSubTree(i, newSubTree); … … 155 155 156 156 // NOTE: this method doesn't build fully balanced trees because we have constraints on the 157 // types of possible sub operators which can indirectly impose a limit for the depth of a given suboperator157 // types of possible sub-functions which can indirectly impose a limit for the depth of a given sub-tree 158 158 private void MakeBalancedTree(IFunctionTree parent, int maxTreeSize, int maxTreeHeight) { 159 159 if (maxTreeHeight == 0 || maxTreeSize == 0) return; // should never happen anyway … … 169 169 for (int i = 0; i < actualArity; i++) { 170 170 if (maxTreeHeight == 1 || maxSubTreeSize == 1) { 171 I Operator[] possibleTerminals = GetAllowedSubOperators(parent.Function, i).Where(172 op => GetMinimalTreeHeight(op) <= maxTreeHeight &&173 GetMinimalTreeSize( op) <= maxSubTreeSize &&174 IsTerminal( op)).ToArray();175 I OperatorselectedTerminal = possibleTerminals[random.Next(possibleTerminals.Length)];171 IFunction[] possibleTerminals = GetAllowedSubFunctions(parent.Function, i).Where( 172 f => GetMinimalTreeHeight(f) <= maxTreeHeight && 173 GetMinimalTreeSize(f) <= maxSubTreeSize && 174 IsTerminal(f)).ToArray(); 175 IFunction selectedTerminal = possibleTerminals[random.Next(possibleTerminals.Length)]; 176 176 IFunctionTree newTree = new FunctionTree(selectedTerminal); 177 177 parent.InsertSubTree(i, newTree); 178 178 } else { 179 I Operator[] possibleFunctions = GetAllowedSubOperators(parent.Function, i).Where(180 op => GetMinimalTreeHeight(op) <= maxTreeHeight &&181 GetMinimalTreeSize( op) <= maxSubTreeSize &&182 !IsTerminal( op)).ToArray();183 I OperatorselectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)];179 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent.Function, i).Where( 180 f => GetMinimalTreeHeight(f) <= maxTreeHeight && 181 GetMinimalTreeSize(f) <= maxSubTreeSize && 182 !IsTerminal(f)).ToArray(); 183 IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)]; 184 184 FunctionTree newTree = new FunctionTree(selectedFunction); 185 185 parent.InsertSubTree(i, newTree); … … 195 195 Scope tempScope = new Scope("Temp. initialization scope"); 196 196 197 var parametricTrees = trees.Where( o => o.Function.GetVariable(GPOperatorLibrary.INITIALIZATION) != null);197 var parametricTrees = trees.Where(t => t.Function.GetVariable(GPOperatorLibrary.INITIALIZATION) != null); 198 198 199 199 foreach (IFunctionTree tree in parametricTrees) { … … 250 250 } 251 251 252 internal IList<I Operator> GetAllowedSubOperators(IOperator op, int index) {253 if ( op== null) {254 return all Operators;252 internal IList<IFunction> GetAllowedSubFunctions(IFunction f, int index) { 253 if (f == null) { 254 return allFunctions; 255 255 } else { 256 256 257 257 SubOperatorsConstraintAnalyser analyser = new SubOperatorsConstraintAnalyser(); 258 analyser.AllPossibleOperators = all Operators;259 260 return analyser.GetAllowedOperators( op, index);261 } 262 } 263 internal void GetMinMaxArity(I Operator root, out int minArity, out int maxArity) {264 foreach (IConstraint constraint in root.Constraints) {258 analyser.AllPossibleOperators = allFunctions.Cast<IOperator>().ToArray<IOperator>(); 259 260 return analyser.GetAllowedOperators(f, index).Cast<IFunction>().ToList<IFunction>(); 261 } 262 } 263 internal void GetMinMaxArity(IFunction f, out int minArity, out int maxArity) { 264 foreach (IConstraint constraint in f.Constraints) { 265 265 NumberOfSubOperatorsConstraint theConstraint = constraint as NumberOfSubOperatorsConstraint; 266 266 if (theConstraint != null) { … … 274 274 maxArity = 2; 275 275 } 276 internal bool IsTerminal(I Operatorf) {276 internal bool IsTerminal(IFunction f) { 277 277 int minArity; 278 278 int maxArity; … … 281 281 } 282 282 283 internal IList<I Operator> GetAllowedParents(IOperatorchild, int childIndex) {284 List<I Operator> parents = new List<IOperator>();285 foreach (I Operatorfunction in functions) {286 IList<I Operator> allowedSubOperators = GetAllowedSubOperators(function, childIndex);287 if (allowedSub Operators.Contains(child, new OperatorEqualityComparer())) {283 internal IList<IFunction> GetAllowedParents(IFunction child, int childIndex) { 284 List<IFunction> parents = new List<IFunction>(); 285 foreach (IFunction function in functions) { 286 IList<IFunction> allowedSubFunctions = GetAllowedSubFunctions(function, childIndex); 287 if (allowedSubFunctions.Contains(child, new FunctionEqualityComparer())) { 288 288 parents.Add(function); 289 289 } … … 292 292 } 293 293 294 internal ICollection<IFunctionTree> GetAll Operators(IFunctionTree root) {295 List<IFunctionTree> all Ops = new List<IFunctionTree>();296 TreeForEach(root, t => { all Ops.Add(t); });297 return all Ops;294 internal ICollection<IFunctionTree> GetAllSubTrees(IFunctionTree root) { 295 List<IFunctionTree> allTrees = new List<IFunctionTree>(); 296 TreeForEach(root, t => { allTrees.Add(t); }); 297 return allTrees; 298 298 } 299 299 300 300 /// <summary> 301 /// returns the height level of opin the tree302 /// if the op== tree => 1303 /// if op is in the suboperators of tree => 2301 /// returns the height level of branch in the tree 302 /// if the branch == tree => 1 303 /// if branch is in the sub-trees of tree => 2 304 304 /// ... 305 /// if opis not found => -1305 /// if branch is not found => -1 306 306 /// </summary> 307 /// <param name="tree"> operatortree to process</param>308 /// <param name=" op">operaterthat is searched in the tree</param>307 /// <param name="tree">root of the function tree to process</param> 308 /// <param name="branch">branch that is searched in the tree</param> 309 309 /// <returns></returns> 310 internal int GetNodeLevel(IFunctionTree tree, IFunctionTree op) { 311 return GetNodeLevelHelper(tree, op, 1); 312 } 313 314 private int GetNodeLevelHelper(IFunctionTree tree, IFunctionTree op, int level) { 315 if (op == tree) return level; 310 internal int GetBranchLevel(IFunctionTree tree, IFunctionTree branch) { 311 return GetBranchLevelHelper(tree, branch, 1); 312 } 313 314 // 'tail-recursive' helper 315 private int GetBranchLevelHelper(IFunctionTree tree, IFunctionTree branch, int level) { 316 if (branch == tree) return level; 316 317 317 318 foreach (IFunctionTree subTree in tree.SubTrees) { 318 int result = Get NodeLevelHelper(subTree, op, level + 1);319 int result = GetBranchLevelHelper(subTree, branch, level + 1); 319 320 if (result != -1) return result; 320 321 } … … 334 335 } 335 336 336 // returns a random nodefrom the specified level in the tree337 internal IFunctionTree GetRandom Node(IFunctionTree tree, int level) {337 // returns a random branch from the specified level in the tree 338 internal IFunctionTree GetRandomBranch(IFunctionTree tree, int level) { 338 339 if (level == 0) return tree; 339 List<IFunctionTree> nodes = GetOperatorsAtLevel(tree, level);340 return nodes[random.Next(nodes.Count)];340 List<IFunctionTree> branches = GetBranchesAtLevel(tree, level); 341 return branches[random.Next(branches.Count)]; 341 342 } 342 343 #endregion … … 359 360 } 360 361 361 private List<IFunctionTree> Get OperatorsAtLevel(IFunctionTree tree, int level) {362 private List<IFunctionTree> GetBranchesAtLevel(IFunctionTree tree, int level) { 362 363 if (level == 1) return new List<IFunctionTree>(tree.SubTrees); 363 364 364 List<IFunctionTree> result= new List<IFunctionTree>();365 List<IFunctionTree> branches = new List<IFunctionTree>(); 365 366 foreach (IFunctionTree subTree in tree.SubTrees) { 366 result.AddRange(GetOperatorsAtLevel(subTree, level - 1));367 } 368 return result;367 branches.AddRange(GetBranchesAtLevel(subTree, level - 1)); 368 } 369 return branches; 369 370 } 370 371 … … 372 373 #endregion 373 374 374 internal class OperatorEqualityComparer : IEqualityComparer<IOperator> {375 #region IEqualityComparer<I Operator> Members376 public bool Equals(I Operator x, IOperatory) {375 internal class FunctionEqualityComparer : IEqualityComparer<IFunction> { 376 #region IEqualityComparer<IFunction> Members 377 public bool Equals(IFunction x, IFunction y) { 377 378 return x==y || 378 379 ((StringData)x.GetVariable(GPOperatorLibrary.TYPE_ID).Value).Data == … … 380 381 } 381 382 382 public int GetHashCode(I Operatorobj) {383 public int GetHashCode(IFunction obj) { 383 384 return ((StringData)obj.GetVariable(GPOperatorLibrary.TYPE_ID).Value).Data.GetHashCode(); 384 385 } … … 386 387 } 387 388 388 internal ICollection<I Operator> GetPossibleParents(List<IOperator> list) {389 List<I Operator> result = new List<IOperator>();390 foreach (I Operator opin functions) {391 if (IsPossibleParent( op, list)) {392 result.Add( op);389 internal ICollection<IFunction> GetPossibleParents(List<IFunction> list) { 390 List<IFunction> result = new List<IFunction>(); 391 foreach (IFunction f in functions) { 392 if (IsPossibleParent(f, list)) { 393 result.Add(f); 393 394 } 394 395 } … … 396 397 } 397 398 398 private bool IsPossibleParent(I Operator op, List<IOperator> children) {399 private bool IsPossibleParent(IFunction f, List<IFunction> children) { 399 400 int minArity; 400 401 int maxArity; 401 GetMinMaxArity( op, out minArity, out maxArity);402 GetMinMaxArity(f, out minArity, out maxArity); 402 403 403 404 // note: we can't assume that the operators in the children list have different types! … … 411 412 412 413 SubOperatorsConstraintAnalyser analyzer = new SubOperatorsConstraintAnalyser(); 413 analyzer.AllPossibleOperators = children ;414 415 List<HashSet<I Operator>> slotSets = new List<HashSet<IOperator>>();416 417 // we iterate through all slots for sub- operators and calculate the set of418 // allowed sub-operators for this slot.414 analyzer.AllPossibleOperators = children.Cast<IOperator>().ToArray<IOperator>(); 415 416 List<HashSet<IFunction>> slotSets = new List<HashSet<IFunction>>(); 417 418 // we iterate through all slots for sub-trees and calculate the set of 419 // allowed functions for this slot. 419 420 // we only count those slots that can hold at least one of the children that we should combine 420 421 for (int slot = 0; slot < nSlots; slot++) { 421 HashSet<I Operator> operatorSet = new HashSet<IOperator>(analyzer.GetAllowedOperators(op, slot));422 if ( operatorSet.Count() > 0) {423 slotSets.Add( operatorSet);422 HashSet<IFunction> functionSet = new HashSet<IFunction>(analyzer.GetAllowedOperators(f, slot).Cast<IFunction>()); 423 if (functionSet.Count() > 0) { 424 slotSets.Add(functionSet); 424 425 } 425 426 } … … 428 429 // hold one of our children. 429 430 // if the number of slots is smaller than the number of children we can be sure that 430 // we can never combine all children as sub- operators of the operator and thus the operator431 // we can never combine all children as sub-trees of the function and thus the function 431 432 // can't be a parent. 432 433 if (slotSets.Count() < children.Count()) { … … 435 436 436 437 // finally we sort the sets by size and beginning from the first set select one 437 // operator for the slot and thus remove it as possible sub-operatorfrom the remaining sets.438 // when we can successfully assign all available children to a slot the operatoris a valid parent439 // when only a subset of all children can be assigned to slots the operatoris no valid parent438 // function for the slot and thus remove it as possible sub-tree from the remaining sets. 439 // when we can successfully assign all available children to a slot the function is a valid parent 440 // when only a subset of all children can be assigned to slots the function is no valid parent 440 441 slotSets.Sort((p, q) => p.Count() - q.Count()); 441 442 … … 443 444 for (int i = 0; i < slotSets.Count() - 1; i++) { 444 445 if (slotSets[i].Count > 0) { 445 I Operatorselected = slotSets[i].ElementAt(0);446 IFunction selected = slotSets[i].ElementAt(0); 446 447 assignments++; 447 448 for (int j = i + 1; j < slotSets.Count(); j++) { -
branches/FunctionsAndStructIdRefactoring/HeuristicLab/UpdateLocalInstallation.cmd
r142 r143 1 set target= G:\Program Files\HeuristicLab 3.01 set target=C:\Program Files\HeuristicLab 3.0 2 2 3 3 copy "HeuristicLab.exe" "%target%"
Note: See TracChangeset
for help on using the changeset viewer.