Changeset 2210 for branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3
- Timestamp:
- 07/29/09 18:28:45 (15 years ago)
- Location:
- branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3
- Files:
-
- 5 added
- 1 deleted
- 22 edited
- 3 copied
- 4 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/BaseClasses/BinaryFunction.cs
r2202 r2210 26 26 using HeuristicLab.Constraints; 27 27 28 namespace HeuristicLab.GP .StructureIdentification{28 namespace HeuristicLab.GP { 29 29 public abstract class BinaryFunction : FunctionBase { 30 30 public BinaryFunction() -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/BaseClasses/FunctionBase.cs
r2202 r2210 28 28 using HeuristicLab.Constraints; 29 29 using System.Diagnostics; 30 using HeuristicLab.GP.Interfaces; 30 31 31 32 namespace HeuristicLab.GP { … … 107 108 108 109 public virtual IFunctionTree GetTreeNode() { 109 return new BakedFunctionTree(this); 110 } 111 110 return new FunctionTreeBase(this); 111 } 112 112 113 113 //private List<IConstraint> constraints = new List<IConstraint>(); -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/BaseClasses/Terminal.cs
r2202 r2210 26 26 using HeuristicLab.Constraints; 27 27 28 namespace HeuristicLab.GP .StructureIdentification{28 namespace HeuristicLab.GP { 29 29 public abstract class Terminal : FunctionBase { 30 30 public Terminal() -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/BaseClasses/UnaryFunction.cs
r2202 r2210 26 26 using HeuristicLab.Constraints; 27 27 28 namespace HeuristicLab.GP .StructureIdentification{28 namespace HeuristicLab.GP { 29 29 public abstract class UnaryFunction : FunctionBase { 30 30 public UnaryFunction() -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/DefaultFunctionTreeNameGenerator.cs
r1529 r2210 24 24 using System.Text; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.GP.Interfaces; 26 27 27 28 namespace HeuristicLab.GP { -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/FunView.cs
r2202 r2210 8 8 using System.Windows.Forms; 9 9 using HeuristicLab.Core; 10 using HeuristicLab.GP.Interfaces; 10 11 11 12 namespace HeuristicLab.GP { -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/FunctionLibrary.cs
r2202 r2210 28 28 using HeuristicLab.Data; 29 29 using HeuristicLab.Constraints; 30 using HeuristicLab.GP.Interfaces; 30 31 31 32 namespace HeuristicLab.GP { -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/FunctionLibraryEditor.cs
r2202 r2210 33 33 using HeuristicLab.Operators; 34 34 using HeuristicLab.Random; 35 using HeuristicLab.GP.Interfaces; 35 36 36 37 namespace HeuristicLab.GP { -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/FunctionTreeView.cs
r2202 r2210 31 31 using HeuristicLab.PluginInfrastructure; 32 32 using HeuristicLab.Data; 33 using HeuristicLab.GP.Interfaces; 33 34 34 35 namespace HeuristicLab.GP { … … 136 137 if (funTreeView.SelectedNode != null && funTreeView.SelectedNode.Tag != null) { 137 138 IFunctionTree selectedBranch = (IFunctionTree)funTreeView.SelectedNode.Tag; 138 139 // UpdateVariablesList(selectedBranch); 139 140 templateTextBox.Text = nameGenerator.GetName(selectedBranch); 140 141 this.selectedBranch = selectedBranch; … … 143 144 } 144 145 145 private void UpdateVariablesList(IFunctionTree functionTree) {146 foreach (IVariable variable in functionTree.LocalVariables) {147 variablesListBox.Items.Add(variable.Name);148 }149 }146 //private void UpdateVariablesList(IFunctionTree functionTree) { 147 // foreach (IVariable variable in functionTree.LocalVariables) { 148 // variablesListBox.Items.Add(variable.Name); 149 // } 150 //} 150 151 151 152 private void variablesListBox_SelectedIndexChanged(object sender, EventArgs e) { 152 // in case we had an event-handler registered for a different variable => unregister the event-handler153 if (selectedVariable != null) {154 selectedVariable.Value.Changed -= new EventHandler(selectedVariable_ValueChanged);155 }156 if (variablesListBox.SelectedItem != null) {157 string selectedVariableName = (string)variablesListBox.SelectedItem;158 selectedVariable = selectedBranch.GetLocalVariable(selectedVariableName);159 variablesSplitContainer.Panel2.Controls.Clear();160 Control editor = (Control)selectedVariable.CreateView();161 variablesSplitContainer.Panel2.Controls.Add(editor);162 editor.Dock = DockStyle.Fill;163 // register an event handler that updates the treenode when the value of the variable is changed by the user164 selectedVariable.Value.Changed += new EventHandler(selectedVariable_ValueChanged);165 } else {166 variablesSplitContainer.Panel2.Controls.Clear();167 }153 //// in case we had an event-handler registered for a different variable => unregister the event-handler 154 //if (selectedVariable != null) { 155 // selectedVariable.Value.Changed -= new EventHandler(selectedVariable_ValueChanged); 156 //} 157 //if (variablesListBox.SelectedItem != null) { 158 // string selectedVariableName = (string)variablesListBox.SelectedItem; 159 // selectedVariable = selectedBranch.GetLocalVariable(selectedVariableName); 160 // variablesSplitContainer.Panel2.Controls.Clear(); 161 // Control editor = (Control)selectedVariable.CreateView(); 162 // variablesSplitContainer.Panel2.Controls.Add(editor); 163 // editor.Dock = DockStyle.Fill; 164 // // register an event handler that updates the treenode when the value of the variable is changed by the user 165 // selectedVariable.Value.Changed += new EventHandler(selectedVariable_ValueChanged); 166 //} else { 167 // variablesSplitContainer.Panel2.Controls.Clear(); 168 //} 168 169 } 169 170 -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/HeuristicLab.GP-3.3.csproj
r2202 r2210 82 82 </ItemGroup> 83 83 <ItemGroup> 84 <Compile Include="BakedFunctionTree.cs" /> 84 <Compile Include="BaseClasses\BinaryFunction.cs" /> 85 <Compile Include="BaseClasses\FunctionBase.cs" /> 86 <Compile Include="BaseClasses\FunctionTreeBase.cs" /> 87 <Compile Include="BaseClasses\Terminal.cs" /> 88 <Compile Include="BaseClasses\UnaryFunction.cs" /> 85 89 <Compile Include="FunctionLibrary.cs" /> 86 90 <Compile Include="FunctionLibraryEditor.cs"> … … 102 106 <DependentUpon>FunView.cs</DependentUpon> 103 107 </Compile> 108 <Compile Include="GeneticProgrammingModel.cs" /> 109 <Compile Include="Initialization\ProbabilisticTreeCreator.cs" /> 110 <Compile Include="Initialization\RampedTreeCreator.cs" /> 111 <Compile Include="Manipulation\GPManipulatorBase.cs" /> 104 112 <Compile Include="TrainingWindowSlider.cs" /> 105 113 <Compile Include="DefaultFunctionTreeNameGenerator.cs" /> … … 111 119 <Compile Include="Recombination\UniformCrossover.cs" /> 112 120 <Compile Include="UnknownFunctionException.cs" /> 113 <Compile Include="IFunctionTreeNameGenerator.cs" />114 <Compile Include="IFunctionTreeExporter.cs" />115 <Compile Include="FunctionBase.cs" />116 121 <Compile Include="HeuristicLabGPPlugin.cs" /> 117 <Compile Include="IFunction.cs" />118 <Compile Include="IFunctionTree.cs" />119 122 <Compile Include="Manipulation\ChangeNodeTypeManipulation.cs" /> 120 123 <Compile Include="Manipulation\CutOutNodeManipulation.cs" /> … … 123 126 <Compile Include="Manipulation\OnePointShaker.cs" /> 124 127 <Compile Include="Manipulation\SubstituteSubTreeManipulation.cs" /> 125 <Compile Include="ProbabilisticTreeCreator.cs" />126 128 <Compile Include="Properties\AssemblyInfo.cs" /> 127 <Compile Include="RampedTreeCreator.cs" />128 129 <Compile Include="Recombination\LangdonHomologousCrossOver.cs" /> 129 130 <Compile Include="Recombination\OnePointCrossOver.cs" /> … … 148 149 <Project>{F5614C53-153C-4A37-A608-121E1C087F07}</Project> 149 150 <Name>HeuristicLab.Evolutionary-3.2</Name> 151 </ProjectReference> 152 <ProjectReference Include="..\..\HeuristicLab.GP.Interfaces\3.3\HeuristicLab.GP.Interfaces-3.3.csproj"> 153 <Project>{924B6BEA-9A99-40FE-9334-5C01E8D540EC}</Project> 154 <Name>HeuristicLab.GP.Interfaces-3.3</Name> 150 155 </ProjectReference> 151 156 <ProjectReference Include="..\..\HeuristicLab.Operators\3.2\HeuristicLab.Operators-3.2.csproj"> -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/HeuristicLabGPPlugin.cs
r1529 r2210 28 28 [ClassInfo(Name = "HeuristicLab.GP-3.3")] 29 29 [PluginFile(Filename = "HeuristicLab.GP-3.3.dll", Filetype = PluginFileType.Assembly)] 30 [Dependency(Dependency = "HeuristicLab.GP.Interfaces-3.3")] 30 31 [Dependency(Dependency = "HeuristicLab.Constraints-3.2")] 31 32 [Dependency(Dependency = "HeuristicLab.Core-3.2")] -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Initialization/ProbabilisticTreeCreator.cs
r2202 r2210 27 27 using HeuristicLab.Random; 28 28 using System.Diagnostics; 29 using HeuristicLab.GP.Interfaces; 30 using System.Collections.Generic; 29 31 30 32 namespace HeuristicLab.GP { … … 39 41 : base() { 40 42 AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In)); 41 AddVariableInfo(new VariableInfo(" OperatorLibrary", "The operator library containing all available operators", typeof(FunctionLibrary), VariableKind.In));43 AddVariableInfo(new VariableInfo("FunctionLibrary", "The function library containing all available functions", typeof(FunctionLibrary), VariableKind.In)); 42 44 AddVariableInfo(new VariableInfo("MinTreeSize", "The minimal allowed size of the tree", typeof(IntData), VariableKind.In)); 43 45 AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size of the tree", typeof(IntData), VariableKind.In)); 44 46 AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In)); 45 AddVariableInfo(new VariableInfo("FunctionTree", "The created tree", typeof(IFunctionTree), VariableKind.New | VariableKind.Out)); 46 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New | VariableKind.Out)); 47 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New | VariableKind.Out)); 47 AddVariableInfo(new VariableInfo("FunctionTree", "The created tree", typeof(IGeneticProgrammingModel), VariableKind.New | VariableKind.Out)); 48 48 } 49 49 50 50 public override IOperation Apply(IScope scope) { 51 51 IRandom random = GetVariableValue<IRandom>("Random", scope, true); 52 FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>("OperatorLibrary", scope, true);52 FunctionLibrary funLibrary = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true); 53 53 int minTreeSize = GetVariableValue<IntData>("MinTreeSize", scope, true).Data; 54 54 int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data; 55 55 int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data; 56 56 57 TreeGardener gardener = new TreeGardener(random, opLibrary);58 59 57 int treeSize = random.Next(minTreeSize, maxTreeSize + 1); 60 61 58 IFunctionTree root; 62 int actualTreeSize;63 int actualTreeHeight;64 59 int tries = 0; 60 TreeGardener gardener = new TreeGardener(random, funLibrary); 65 61 do { 66 root = gardener.PTC2(random, treeSize, maxTreeHeight); 67 actualTreeSize = root.Size; 68 actualTreeHeight = root.Height; 62 root = gardener.PTC2(treeSize, maxTreeHeight); 69 63 if (tries++ >= MAX_TRIES) { 70 64 // try a different size … … 72 66 tries = 0; 73 67 } 74 } while ( actualTreeSize > maxTreeSize || actualTreeHeight> maxTreeHeight);68 } while (root.GetSize() > maxTreeSize || root.GetHeight() > maxTreeHeight); 75 69 76 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), root));77 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(actualTreeSize)));78 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(actualTreeHeight)));70 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), new GeneticProgrammingModel(root))); 71 return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(root), scope); 72 } 79 73 80 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(root), scope); 81 } 82 } 74 } 83 75 } -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Initialization/RampedTreeCreator.cs
r2202 r2210 27 27 using HeuristicLab.Random; 28 28 using System.Diagnostics; 29 using HeuristicLab.GP.Interfaces; 29 30 30 31 namespace HeuristicLab.GP { … … 37 38 : base() { 38 39 AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In)); 39 AddVariableInfo(new VariableInfo(" OperatorLibrary", "The operator library containing all available operators", typeof(FunctionLibrary), VariableKind.In));40 AddVariableInfo(new VariableInfo("FunctionLibrary", "The function library containing all available functions", typeof(FunctionLibrary), VariableKind.In)); 40 41 AddVariableInfo(new VariableInfo("MinTreeHeight", "The minimal allowed height of the tree", typeof(IntData), VariableKind.In)); 41 42 AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In)); 42 43 AddVariableInfo(new VariableInfo("BalancedTreesRate", "Determines how many trees should be balanced", typeof(DoubleData), VariableKind.In)); 43 AddVariableInfo(new VariableInfo("FunctionTree", "The created tree", typeof(I FunctionTree), VariableKind.New | VariableKind.Out));44 AddVariableInfo(new VariableInfo("FunctionTree", "The created tree", typeof(IGeneticProgrammingModel), VariableKind.New | VariableKind.Out)); 44 45 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New | VariableKind.Out)); 45 46 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New | VariableKind.Out)); … … 48 49 public override IOperation Apply(IScope scope) { 49 50 IRandom random = GetVariableValue<IRandom>("Random", scope, true); 50 FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>(" OperatorLibrary", scope, true);51 FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true); 51 52 int minTreeHeight = GetVariableValue<IntData>("MinTreeHeight", scope, true).Data; 52 53 int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data; … … 63 64 } 64 65 65 int actualTreeSize = root.Size; 66 int actualTreeHeight = root.Height; 66 Debug.Assert(gardener.IsValidTree(root) && root.GetHeight() <= maxTreeHeight); 67 67 68 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), root)); 69 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(actualTreeSize))); 70 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(actualTreeHeight))); 71 72 Debug.Assert(gardener.IsValidTree(root) && actualTreeHeight <= maxTreeHeight); 73 74 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(root), scope); 68 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), new GeneticProgrammingModel(root))); 69 return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(root), scope); 75 70 } 76 71 } -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Logging/TreeArityAnalyser.cs
r698 r2210 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.GP.Interfaces; 32 33 33 34 namespace HeuristicLab.GP { … … 43 44 public TreeArityAnalyser() 44 45 : base() { 45 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In));46 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to analyse", typeof(IGeneticProgrammingModel), VariableKind.In)); 46 47 AddVariableInfo(new VariableInfo("Histogram", "The histogram of arities over all functions in all functiontrees", typeof(ItemList<ItemList<IntData>>), VariableKind.New | VariableKind.Out)); 47 48 } … … 69 70 70 71 private ItemList<ItemList<IntData>> GetArityHistogram(IScope subScope) { 71 I FunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", subScope, false);72 IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", subScope, false); 72 73 List<int> arities = new List<int>(); 73 Arities( tree, arities);74 Arities(gpModel.FunctionTree, arities); 74 75 var histogram = arities.GroupBy(x => x, (g, xs) => new { Group = g, Count = xs.Count() }).OrderBy(g=>g.Group); // count number of distinct arities 75 76 ItemList<ItemList<IntData>> result = new ItemList<ItemList<IntData>>(); -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/ChangeNodeTypeManipulation.cs
r2202 r2210 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.GP.Interfaces; 32 33 33 34 namespace HeuristicLab.GP { 34 public class ChangeNodeTypeManipulation : OperatorBase {35 public class ChangeNodeTypeManipulation : GPManipulatorBase { 35 36 36 37 public override string Description { … … 44 45 public ChangeNodeTypeManipulation() 45 46 : base() { 46 AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));47 AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(FunctionLibrary), VariableKind.In));48 AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));49 AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));50 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));51 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));52 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));53 47 } 54 48 55 56 public override IOperation Apply(IScope scope) { 57 IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, false); 58 MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true); 59 FunctionLibrary library = GetVariableValue<FunctionLibrary>("OperatorLibrary", scope, true); 60 IntData treeSize = GetVariableValue<IntData>("TreeSize", scope, false); 61 IntData treeHeight = GetVariableValue<IntData>("TreeHeight", scope, false); 62 int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data; 63 int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data; 49 internal override IOperation Manipulate(MersenneTwister random, IGeneticProgrammingModel gpModel, FunctionLibrary library, int maxTreeSize, int maxTreeHeight, IScope scope) { 64 50 TreeGardener gardener = new TreeGardener(random, library); 65 IFunctionTree parent = gardener.GetRandomParentNode( root);51 IFunctionTree parent = gardener.GetRandomParentNode(gpModel.FunctionTree); 66 52 IFunctionTree selectedChild; 67 53 int selectedChildIndex; 68 if (parent == null) {54 if (parent == null) { 69 55 selectedChildIndex = 0; 70 selectedChild = root;56 selectedChild = gpModel.FunctionTree; 71 57 } else { 72 58 selectedChildIndex = random.Next(parent.SubTrees.Count); … … 74 60 } 75 61 76 if (selectedChild.SubTrees.Count == 0) {62 if (selectedChild.SubTrees.Count == 0) { 77 63 IFunctionTree newTerminal = ChangeTerminalType(parent, selectedChild, selectedChildIndex, gardener, random); 78 if (parent == null) {64 if (parent == null) { 79 65 // no parent means the new child is the initial operator 80 // and we have to update the value in the variable 81 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTerminal; 66 gpModel.FunctionTree = newTerminal; 82 67 } else { 83 68 parent.RemoveSubTree(selectedChildIndex); … … 85 70 // updating the variable is not necessary because it stays the same 86 71 } 87 Debug.Assert(gardener.IsValidTree( root));72 Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree)); 88 73 // size and height stays the same when changing a terminal so no need to update the variables 89 74 // schedule an operation to initialize the new terminal 90 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTerminal), scope);75 return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newTerminal), scope); 91 76 } else { 92 77 List<IFunctionTree> uninitializedBranches; … … 95 80 // calculate the height and size difference and 96 81 // check if the size of the new tree is still in the allowed bounds 97 int oldChildSize = selectedChild. Size;98 int oldChildHeight = selectedChild. Height;99 int newChildSize = newFunctionTree. Size;100 int newChildHeight = newFunctionTree. Height;101 if ((treeHeight.Data- oldChildHeight) + newChildHeight > maxTreeHeight ||102 ( treeSize.Data- oldChildSize) + newChildSize > maxTreeSize) {82 int oldChildSize = selectedChild.GetSize(); 83 int oldChildHeight = selectedChild.GetHeight(); 84 int newChildSize = newFunctionTree.GetSize(); 85 int newChildHeight = newFunctionTree.GetHeight(); 86 if ((gpModel.Height - oldChildHeight) + newChildHeight > maxTreeHeight || 87 (gpModel.Size - oldChildSize) + newChildSize > maxTreeSize) { 103 88 // if size-constraints are violated don't change anything 104 89 return null; 105 90 } 106 if (parent == null) {91 if (parent == null) { 107 92 // no parent means the new function is the initial operator 108 // and we have to update the value in the variable 109 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newFunctionTree; 110 root = newFunctionTree; 93 gpModel.FunctionTree = newFunctionTree; 111 94 } else { 112 95 // remove the old child … … 116 99 } 117 100 // update size and height 118 treeSize.Data = (treeSize.Data - oldChildSize)+ newChildSize;119 treeHeight.Data = root.Height; // must recalculate height because we can't know wether the manipulated branch was the deepest branch101 gpModel.Size = gpModel.Size - oldChildSize + newChildSize; 102 gpModel.Height = gpModel.FunctionTree.GetHeight(); // must recalculate height because we can't know wether the manipulated branch was the deepest branch 120 103 // check if whole tree is ok 121 Debug.Assert(gardener.IsValidTree( root));104 Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree)); 122 105 // return a composite operation that initializes all created sub-trees 123 return gardener.CreateInitializationOperation(uninitializedBranches, scope);106 return TreeGardener.CreateInitializationOperation(uninitializedBranches, scope); 124 107 } 125 108 } 126 127 109 128 110 private IFunctionTree ChangeTerminalType(IFunctionTree parent, IFunctionTree child, int childIndex, TreeGardener gardener, MersenneTwister random) { … … 133 115 allowedTerminals = new List<IFunction>(); 134 116 var allAllowedChildren = gardener.GetAllowedSubFunctions(parent.Function, childIndex); 135 foreach (IFunction c in allAllowedChildren) {136 if (gardener.IsTerminal(c)) allowedTerminals.Add(c);117 foreach (IFunction c in allAllowedChildren) { 118 if (TreeGardener.IsTerminal(c)) allowedTerminals.Add(c); 137 119 } 138 120 } … … 159 141 if (actualArity > maxArity) 160 142 actualArity = maxArity; 161 if (actualArity < minArity)143 if (actualArity < minArity) 162 144 actualArity = minArity; 163 145 // create a list that holds old sub-trees that we can reuse in the new tree … … 181 163 // no existing matching tree found => create a new tree of minimal size 182 164 IFunctionTree freshTree = gardener.CreateRandomTree(allowedSubFunctions, 1, 1); 183 freshSubTrees.AddRange( gardener.GetAllSubTrees(freshTree));165 freshSubTrees.AddRange(TreeGardener.GetAllSubTrees(freshTree)); 184 166 newTree.InsertSubTree(i, freshTree); 185 167 } -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/CutOutNodeManipulation.cs
r2202 r2210 28 28 using System; 29 29 using System.Diagnostics; 30 using HeuristicLab.GP.Interfaces; 30 31 31 32 namespace HeuristicLab.GP { 32 public class CutOutNodeManipulation : OperatorBase {33 public class CutOutNodeManipulation : GPManipulatorBase { 33 34 public override string Description { 34 35 get { … … 49 50 public CutOutNodeManipulation() 50 51 : base() { 51 AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));52 AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(FunctionLibrary), VariableKind.In));53 AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));54 AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));55 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));56 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));57 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));58 52 } 59 53 60 54 61 public override IOperation Apply(IScope scope) { 62 IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true); 63 MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true); 64 FunctionLibrary library = GetVariableValue<FunctionLibrary>("OperatorLibrary", scope, true); 65 int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data; 66 int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data; 55 internal override IOperation Manipulate(MersenneTwister random, IGeneticProgrammingModel gpModel, FunctionLibrary library, int maxTreeSize, int maxTreeHeight, IScope scope) { 67 56 TreeGardener gardener = new TreeGardener(random, library); 68 IFunctionTree parent = gardener.GetRandomParentNode( root);57 IFunctionTree parent = gardener.GetRandomParentNode(gpModel.FunctionTree); 69 58 // parent == null means we should cut out the root node 70 59 // => return a random sub-tree of the root 71 60 if (parent == null) { 72 61 // when there are sub-trees then replace the old tree with a random sub-tree 73 if (root.SubTrees.Count > 0) { 74 root = root.SubTrees[random.Next(root.SubTrees.Count)]; 75 GetVariableValue<IntData>("TreeSize", scope, true).Data = root.Size; 76 GetVariableValue<IntData>("TreeHeight", scope, true).Data = root.Height; 77 // update the variable 78 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = root; 79 Debug.Assert(gardener.IsValidTree(root)); 62 if (gpModel.FunctionTree.SubTrees.Count > 0) { 63 gpModel.FunctionTree = gpModel.FunctionTree.SubTrees[random.Next(gpModel.FunctionTree.SubTrees.Count)]; 64 Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree)); 80 65 // we reused a sub-tree so we don't have to schedule initialization operations 81 66 return null; 82 67 } else { 83 68 // we want to cut the root node and there are no sub-trees => create a new random terminal 84 IFunctionTree newTree; 85 newTree = gardener.CreateRandomTree(gardener.Terminals, 1, 1); 86 GetVariableValue<IntData>("TreeSize", scope, true).Data = newTree.Size; 87 GetVariableValue<IntData>("TreeHeight", scope, true).Data = newTree.Height; 88 // update the variable 89 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree; 90 Debug.Assert(gardener.IsValidTree(newTree)); 69 gpModel.FunctionTree = gardener.CreateRandomTree(gardener.Terminals, 1, 1); 70 Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree)); 71 91 72 // schedule an operation to initialize the whole tree 92 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);73 return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(gpModel.FunctionTree), scope); 93 74 } 94 75 } 76 95 77 // select a child to cut away 96 78 int childIndex = random.Next(parent.SubTrees.Count); … … 101 83 if (possibleChilds.Length > 0) { 102 84 // replace child with a random child of that child 103 IFunctionTree selectedChild = possibleChilds[random.Next(possibleChilds.Length)]; 85 IFunctionTree selectedChild = possibleChilds[random.Next(possibleChilds.Length)]; 104 86 parent.RemoveSubTree(childIndex); 105 87 parent.InsertSubTree(childIndex, selectedChild); 106 Debug.Assert(gardener.IsValidTree( root));107 // update the size and height of our tree108 GetVariableValue<IntData>("TreeSize", scope, true).Data = root.Size;109 GetVariableValue<IntData>("TreeHeight", scope, true).Data = root.Height;88 Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree)); 89 // recalculate the size and height of our tree 90 gpModel.Size = gpModel.FunctionTree.GetSize(); 91 gpModel.Height = gpModel.FunctionTree.GetHeight(); 110 92 // don't need to schedule initialization operations 111 93 return null; … … 113 95 // can't reuse an existing branch => create a new tree 114 96 // determine the level of the parent 115 int parentLevel = gardener.GetBranchLevel( root, parent);97 int parentLevel = gardener.GetBranchLevel(gpModel.FunctionTree, parent); 116 98 // first remove the old child (first step essential!) 117 99 parent.RemoveSubTree(childIndex); 118 100 // then determine the number of nodes left over after the child has been removed! 119 int remainingNodes = root.Size;101 int remainingNodes = gpModel.FunctionTree.GetSize(); 120 102 allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex); 121 103 IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel); 122 104 parent.InsertSubTree(childIndex, newFunctionTree); 123 GetVariableValue<IntData>("TreeSize", scope, true).Data = root.Size; 124 GetVariableValue<IntData>("TreeHeight", scope, true).Data = root.Height; 125 Debug.Assert(gardener.IsValidTree(root)); 105 Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree)); 106 // recalculate size and height 107 gpModel.Size = gpModel.FunctionTree.GetSize(); 108 gpModel.Height = gpModel.FunctionTree.GetHeight(); 126 109 // schedule an initialization operation for the new function-tree 127 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newFunctionTree), scope);110 return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newFunctionTree), scope); 128 111 } 129 112 } -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/DeleteSubTreeManipulation.cs
r2202 r2210 27 27 using HeuristicLab.Random; 28 28 using System.Diagnostics; 29 using HeuristicLab.GP.Interfaces; 29 30 30 31 namespace HeuristicLab.GP { 31 public class DeleteSubTreeManipulation : OperatorBase {32 public class DeleteSubTreeManipulation : GPManipulatorBase { 32 33 public override string Description { 33 34 get { … … 39 40 public DeleteSubTreeManipulation() 40 41 : base() { 41 AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));42 AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(FunctionLibrary), VariableKind.In));43 AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));44 AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));45 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));46 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));47 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));48 42 } 49 43 50 51 public override IOperation Apply(IScope scope) { 52 IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true); 53 MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true); 54 FunctionLibrary library = GetVariableValue<FunctionLibrary>("OperatorLibrary", scope, true); 44 internal override IOperation Manipulate(MersenneTwister random, IGeneticProgrammingModel gpModel, FunctionLibrary library, int maxTreeSize, int maxTreeHeight, IScope scope) { 55 45 TreeGardener gardener = new TreeGardener(random, library); 56 IFunctionTree parent = gardener.GetRandomParentNode( root);46 IFunctionTree parent = gardener.GetRandomParentNode(gpModel.FunctionTree); 57 47 58 48 // parent==null means the whole tree should be deleted. 59 49 // => return a new minimal random tree 60 if (parent == null) {50 if (parent == null) { 61 51 IFunctionTree newTree = gardener.CreateBalancedRandomTree(1, 1); 62 52 // check if the tree is ok 63 53 Debug.Assert(gardener.IsValidTree(newTree)); 64 // update sizes to match the new tree 65 GetVariableValue<IntData>("TreeSize", scope, true).Data = newTree.Size; 66 GetVariableValue<IntData>("TreeHeight", scope, true).Data = newTree.Height; 67 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree; 68 54 gpModel.FunctionTree = newTree; 69 55 // schedule an operation to initialize the newly created operator 70 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);56 return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newTree), scope); 71 57 } 72 58 73 59 // select a branch to prune 74 60 int childIndex = random.Next(parent.SubTrees.Count); 75 if (parent.SubTrees.Count > parent.Function.MinArity) {61 if (parent.SubTrees.Count > parent.Function.MinArity) { 76 62 parent.RemoveSubTree(childIndex); 77 63 // actually since the next sub-trees are shifted in the place of the removed branch … … 81 67 // 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) 82 68 83 Debug.Assert(gardener.IsValidTree(root)); 84 GetVariableValue<IntData>("TreeSize", scope, true).Data = root.Size; 85 GetVariableValue<IntData>("TreeHeight", scope, true).Data = root.Height; 69 Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree)); 70 // recalculate size and height 71 gpModel.Size = gpModel.FunctionTree.GetSize(); 72 gpModel.Height = gpModel.FunctionTree.GetHeight(); 86 73 // root hasn't changed so don't need to update 'FunctionTree' variable 87 74 return null; … … 92 79 IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, 1, 1); 93 80 parent.InsertSubTree(childIndex, newFunctionTree); 94 Debug.Assert(gardener.IsValidTree( root));95 GetVariableValue<IntData>("TreeSize", scope, true).Data = root.Size;96 GetVariableValue<IntData>("TreeHeight", scope, true).Data = root.Height;97 // again the root hasn't changed so we don't need to update the 'FunctionTree' variable98 // but we have toreturn an initialization operation for the newly created tree99 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newFunctionTree), scope);81 Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree)); 82 // recalculate size and height 83 gpModel.Size = gpModel.FunctionTree.GetSize(); 84 gpModel.Height = gpModel.FunctionTree.GetHeight(); 85 // return an initialization operation for the newly created tree 86 return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newFunctionTree), scope); 100 87 } 101 88 } -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/FullTreeShaker.cs
r2202 r2210 29 29 using HeuristicLab.Data; 30 30 using HeuristicLab.Selection; 31 using HeuristicLab.GP.Interfaces; 31 32 32 33 namespace HeuristicLab.GP { … … 39 40 : base() { 40 41 AddVariableInfo(new VariableInfo("Random", "A random generator (uniform)", typeof(MersenneTwister), VariableKind.In)); 41 AddVariableInfo(new VariableInfo(" OperatorLibrary", "Operator library that defines operatormutations", typeof(FunctionLibrary), VariableKind.In));42 AddVariableInfo(new VariableInfo("FunctionLibrary", "Function library that defines function mutations", typeof(FunctionLibrary), VariableKind.In)); 42 43 AddVariableInfo(new VariableInfo("ShakingFactor", "Variable that determines the force of the shaking operation", typeof(DoubleData), VariableKind.In)); 43 AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be mutated", typeof(I FunctionTree), VariableKind.In | VariableKind.Out));44 AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be mutated", typeof(IGeneticProgrammingModel), VariableKind.In | VariableKind.Out)); 44 45 } 45 46 46 47 public override IOperation Apply(IScope scope) { 47 FunctionLibrary library = GetVariableValue<FunctionLibrary>(" OperatorLibrary", scope, true);48 I FunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", scope, false);48 FunctionLibrary library = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true); 49 IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, false); 49 50 MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true); 50 51 51 // enqueue all mutation operations as parallel operations 52 // save all existing sub-scopes in a backup scope 53 Scope backupScope = new Scope("backup"); 54 foreach (Scope subScope in scope.SubScopes) { 55 backupScope.AddSubScope(subScope); 56 } 57 58 // create a scope for all shaking operations 59 Scope tempScope = new Scope("Temp. manipulation scope"); 60 scope.AddSubScope(tempScope); // scope containing a subscope for each manipulation 61 scope.AddSubScope(backupScope); // scope containing the old subscopes 62 63 // create a composite operation for all shaking operations 52 64 CompositeOperation next = new CompositeOperation(); 53 65 next.ExecuteInParallel = false; 54 66 55 Scope tempScope = new Scope("Temp. manipulation scope"); 56 57 TreeGardener gardener = new TreeGardener(mt, library); 58 var parametricBranches = gardener.GetAllSubTrees(tree).Where(branch => branch.Manipulator!= null); 59 foreach(IFunctionTree subTree in parametricBranches) { 60 IOperator mutation = subTree.Manipulator; 61 62 // store all local variables into a temporary scope 63 Scope mutationScope = new Scope(); 64 foreach(IVariable variable in subTree.LocalVariables) { 65 mutationScope.AddVariable(variable); 66 } 67 68 tempScope.AddSubScope(mutationScope); 69 next.AddOperation(new AtomicOperation(mutation, mutationScope)); 67 // enqueue all shaking operations 68 foreach (IFunctionTree subTree in TreeGardener.GetAllSubTrees(gpModel.FunctionTree).Where(x=>x.HasLocalParameters)) { 69 IOperation shakingOperation = subTree.CreateShakingOperation(tempScope); 70 next.AddOperation(shakingOperation); 70 71 } 71 72 // save all existing sub-scopes in a backup scope73 Scope backupScope = new Scope("backup");74 foreach(Scope subScope in scope.SubScopes) {75 backupScope.AddSubScope(subScope);76 }77 78 scope.AddSubScope(tempScope); // scope containing a subscope for each manipulation79 scope.AddSubScope(backupScope); // scope containing the old subscopes80 72 81 73 // schedule a reducer operation to delete all temporary scopes and restore -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/OnePointShaker.cs
r2202 r2210 29 29 using HeuristicLab.Data; 30 30 using HeuristicLab.Selection; 31 using HeuristicLab.GP.Interfaces; 31 32 32 33 namespace HeuristicLab.GP { … … 38 39 public OnePointShaker() 39 40 : base() { 40 AddVariableInfo(new VariableInfo(" OperatorLibrary", "Operator library that defines mutation operations for operators", typeof(FunctionLibrary), VariableKind.In));41 AddVariableInfo(new VariableInfo("FunctionLibrary", "Function library that defines mutation operations for functions", typeof(FunctionLibrary), VariableKind.In)); 41 42 AddVariableInfo(new VariableInfo("Random", "A random generator (uniform)", typeof(MersenneTwister), VariableKind.In)); 42 43 AddVariableInfo(new VariableInfo("ShakingFactor", "Factor that determines the force of the shaking operation", typeof(DoubleData), VariableKind.In)); 43 AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be mutated", typeof(I FunctionTree), VariableKind.In | VariableKind.Out));44 AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be mutated", typeof(IGeneticProgrammingModel), VariableKind.In | VariableKind.Out)); 44 45 } 45 46 46 47 public override IOperation Apply(IScope scope) { 47 FunctionLibrary library = GetVariableValue<FunctionLibrary>(" OperatorLibrary", scope, true);48 I FunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", scope, false);48 FunctionLibrary library = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true); 49 IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, false); 49 50 MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true); 50 TreeGardener gardener = new TreeGardener(mt, library);51 51 52 // get all nodes for which a manipulation is defined 53 var parametricBranches = gardener.GetAllSubTrees(tree).Where(branch => branch.Manipulator != null); 52 // get all nodes with local parameters 53 var parametricBranches = TreeGardener.GetAllSubTrees(gpModel.FunctionTree).Where(branch => branch.HasLocalParameters); 54 if (parametricBranches.Count() == 0) return null; // don't manipulate anything if there are no nodes with a manipulation operator 55 IFunctionTree selectedBranch = parametricBranches.ElementAt(mt.Next(parametricBranches.Count())); 54 56 55 if(parametricBranches.Count() == 0) return null; // don't manipulate anything if there are no nodes with a manipulation operator 56 57 IFunctionTree selectedBranch = parametricBranches.ElementAt(mt.Next(parametricBranches.Count())); 58 IOperator mutation = selectedBranch.Manipulator; 59 CompositeOperation next = new CompositeOperation(); 60 61 // store all local variables into a temporary scope 62 Scope tempScope = new Scope("Temp. manipulation scope"); 63 foreach(IVariable variable in selectedBranch.LocalVariables) { 64 tempScope.AddVariable(variable); 65 } 66 67 // save the exising sub-scops in a backup scope 57 // save the exising sub-scopes in a backup scope 68 58 Scope backupScope = new Scope("backup"); 69 59 foreach (Scope subScope in scope.SubScopes) { … … 71 61 } 72 62 63 // create a scope for all shaking operations 64 Scope tempScope = new Scope("Temp. manipulation scope"); 65 73 66 // add the new sub-scopes 74 67 scope.AddSubScope(tempScope); 75 68 scope.AddSubScope(backupScope); 76 69 70 CompositeOperation next = new CompositeOperation(); 71 next.ExecuteInParallel = false; 77 72 // the next operation should first manipulate and then restore the sub-scopes from the backup scope 78 next.AddOperation( new AtomicOperation(mutation,tempScope));73 next.AddOperation(selectedBranch.CreateShakingOperation(tempScope)); 79 74 next.AddOperation(new AtomicOperation(new RightReducer(), scope)); 80 75 -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/SubstituteSubTreeManipulation.cs
r2202 r2210 28 28 using HeuristicLab.Random; 29 29 using System.Diagnostics; 30 using HeuristicLab.GP.Interfaces; 30 31 31 32 namespace HeuristicLab.GP { 32 public class SubstituteSubTreeManipulation : OperatorBase {33 public class SubstituteSubTreeManipulation : GPManipulatorBase { 33 34 34 35 public override string Description { … … 38 39 public SubstituteSubTreeManipulation() 39 40 : base() { 40 AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));41 AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(FunctionLibrary), VariableKind.In));42 AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));43 AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));44 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to manipulate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));45 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));46 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));47 41 } 48 42 49 public override IOperation Apply(IScope scope) { 50 IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true); 51 MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true); 52 FunctionLibrary library = GetVariableValue<FunctionLibrary>("OperatorLibrary", scope, true); 53 int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data; 54 int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data; 55 int treeSize = GetVariableValue<IntData>("TreeSize", scope, true).Data; 56 int treeHeight = GetVariableValue<IntData>("TreeHeight", scope, true).Data; 43 internal override IOperation Manipulate(MersenneTwister random, IGeneticProgrammingModel gpModel, FunctionLibrary library, int maxTreeSize, int maxTreeHeight, IScope scope) { 57 44 TreeGardener gardener = new TreeGardener(random, library); 58 IFunctionTree parent = gardener.GetRandomParentNode( root);59 if (parent == null) {45 IFunctionTree parent = gardener.GetRandomParentNode(gpModel.FunctionTree); 46 if (parent == null) { 60 47 // parent == null means we should subsitute the whole tree 61 48 // => create a new random tree … … 63 50 Debug.Assert(gardener.IsValidTree(newTree)); 64 51 65 // update the variables in the scope with the new values 66 GetVariableValue<IntData>("TreeSize", scope, true).Data = newTree.Size; 67 GetVariableValue<IntData>("TreeHeight", scope, true).Data = newTree.Height; 68 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree; 69 52 gpModel.FunctionTree = newTree; 70 53 // return a CompositeOperation that randomly initializes the new tree 71 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);54 return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newTree), scope); 72 55 } else { 73 56 // determine a random child of the parent to be replaced … … 75 58 // get the list of allowed functions for the new sub-tree 76 59 ICollection<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex); 77 if (allowedFunctions.Count == 0) {60 if (allowedFunctions.Count == 0) { 78 61 // don't change anything 79 62 // this shouldn't happen … … 83 66 // calculate the maximum size and height of the new sub-tree based on the location where 84 67 // it will be inserted 85 int parentLevel = gardener.GetBranchLevel( root, parent);68 int parentLevel = gardener.GetBranchLevel(gpModel.FunctionTree, parent); 86 69 int maxSubTreeHeight = maxTreeHeight - parentLevel; 87 int maxSubTreeSize = maxTreeSize - ( treeSize - parent.SubTrees[childIndex].Size);70 int maxSubTreeSize = maxTreeSize - (gpModel.Size - parent.SubTrees[childIndex].GetSize()); 88 71 89 72 // create a random function tree … … 92 75 parent.InsertSubTree(childIndex, newTree); 93 76 94 Debug.Assert(gardener.IsValidTree( root));77 Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree)); 95 78 // update the values of treeSize and treeHeight 96 GetVariableValue<IntData>("TreeSize", scope, true).Data = root.Size;97 GetVariableValue<IntData>("TreeHeight", scope, true).Data = root.Height;79 gpModel.Size = gpModel.FunctionTree.GetSize(); 80 gpModel.Height = gpModel.FunctionTree.GetHeight(); 98 81 // the root hasn't changed so we don't need to update 99 82 // return a CompositeOperation that randomly initializes all nodes of the new subtree 100 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);83 return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newTree), scope); 101 84 } 102 85 } -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/GPCrossoverBase.cs
r2202 r2210 31 31 using System.Diagnostics; 32 32 using HeuristicLab.Evolutionary; 33 using HeuristicLab.GP.Interfaces; 33 34 34 35 namespace HeuristicLab.GP { … … 36 37 public GPCrossoverBase() 37 38 : base() { 38 AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(FunctionLibrary), VariableKind.In)); 39 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New)); 40 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New)); 41 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New)); 39 AddVariableInfo(new VariableInfo("FunctionLibrary", "The operator library containing all available operators", typeof(FunctionLibrary), VariableKind.In)); 40 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to crossover", typeof(IGeneticProgrammingModel), VariableKind.In | VariableKind.New)); 42 41 } 43 42 … … 45 44 46 45 protected override void Cross(IScope scope, IRandom random) { 47 FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>(" OperatorLibrary", scope, true);46 FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true); 48 47 TreeGardener gardener = new TreeGardener(random, opLibrary); 49 48 … … 51 50 throw new InvalidOperationException("Number of parents must be exactly two."); 52 51 53 I FunctionTree parent0 = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[0], false);54 I FunctionTree parent1 = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[1], false);52 IGeneticProgrammingModel parent0 = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope.SubScopes[0], false); 53 IGeneticProgrammingModel parent1 = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope.SubScopes[1], false); 55 54 56 55 // randomly swap parents to remove a possible bias from selection (e.g. when using gender-specific selection) 57 56 if (random.NextDouble() < 0.5) { 58 I FunctionTreetmp = parent0;57 IGeneticProgrammingModel tmp = parent0; 59 58 parent0 = parent1; 60 59 parent1 = tmp; 61 60 } 62 61 63 IFunctionTree child = Cross(scope, gardener, random, parent0 , parent1);62 IFunctionTree child = Cross(scope, gardener, random, parent0.FunctionTree, parent1.FunctionTree); 64 63 Debug.Assert(gardener.IsValidTree(child)); 65 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), child)); 66 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(child.Size))); 67 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(child.Height))); 64 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), new GeneticProgrammingModel(child))); 68 65 } 69 66 } -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/LangdonHomologousCrossOver.cs
r835 r2210 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.GP.Interfaces; 32 33 33 34 namespace HeuristicLab.GP { -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/OnePointCrossOver.cs
r1286 r2210 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.GP.Interfaces; 32 33 33 34 namespace HeuristicLab.GP { … … 51 52 internal override IFunctionTree Cross(TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) { 52 53 List<CrossoverPoint> allowedCrossOverPoints = new List<CrossoverPoint>(); 53 GetCrossOverPoints(gardener, tree0, tree1, maxTreeSize - tree0. Size, allowedCrossOverPoints);54 GetCrossOverPoints(gardener, tree0, tree1, maxTreeSize - tree0.GetSize(), allowedCrossOverPoints); 54 55 if (allowedCrossOverPoints.Count > 0) { 55 56 CrossoverPoint crossOverPoint = allowedCrossOverPoints[random.Next(allowedCrossOverPoints.Count)]; … … 68 69 // if the current branch can be attached as a sub-tree to branch0 69 70 if (gardener.GetAllowedSubFunctions(branch0.Function, i).Contains(branch1.SubTrees[i].Function) && 70 branch1.SubTrees[i]. Size - branch0.SubTrees[i].Size<= maxNewNodes) {71 branch1.SubTrees[i].GetSize() - branch0.SubTrees[i].GetSize() <= maxNewNodes) { 71 72 CrossoverPoint p = new CrossoverPoint(); 72 73 p.childIndex = i; -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/SizeConstrictedGPCrossoverBase.cs
r1286 r2210 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.GP.Interfaces; 32 33 33 34 namespace HeuristicLab.GP { … … 58 59 newTree = Cross(gardener, random, (IFunctionTree)tree1.Clone(), (IFunctionTree)tree0.Clone(), maxTreeSize, maxTreeHeight); 59 60 } else newTree = tree0; 60 } while (newTree. Size > maxTreeSize || newTree.Height> maxTreeHeight);61 } while (newTree.GetSize() > maxTreeSize || newTree.GetHeight() > maxTreeHeight); 61 62 return newTree; 62 63 } 63 64 64 65 internal abstract IFunctionTree Cross(TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight); 65 66 //private IFunctionTree TakeNextParent(IScope scope) {67 // IFunctionTree parent = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[0], false);68 // scope.RemoveSubScope(scope.SubScopes[0]);69 // return parent;70 //}71 66 } 72 67 } -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/SizeFairCrossOver.cs
r2202 r2210 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.GP.Interfaces; 32 33 33 34 namespace HeuristicLab.GP { … … 69 70 private IFunctionTree GetReplacementBranch(MersenneTwister random, TreeGardener gardener, IFunctionTree intoTree, IFunctionTree parent, int replacedBranchIndex, IFunctionTree fromTree, int maxTreeSize, int maxTreeHeight) { 70 71 IList<IFunction> allowedFunctions = new List<IFunction>(gardener.GetAllowedSubFunctions(parent.Function, replacedBranchIndex)); 71 int removedBranchSize = parent.SubTrees[replacedBranchIndex]. Size;72 int maxBranchSize = maxTreeSize - (intoTree. Size- removedBranchSize);72 int removedBranchSize = parent.SubTrees[replacedBranchIndex].GetSize(); 73 int maxBranchSize = maxTreeSize - (intoTree.GetSize() - removedBranchSize); 73 74 int maxBranchHeight = maxTreeHeight - gardener.GetBranchLevel(intoTree, parent); // returns 1 if intoTree==parent and 2 if parent is a child of intoTree 74 75 List<int> replacedTrail = GetTrail(intoTree, parent); … … 84 85 double pEqualLength = equalLengthBranches.Count > 0 ? 1.0 / removedBranchSize : 0.0; 85 86 double pLonger; 86 if (parent. Size== maxTreeSize) {87 if (parent.GetSize() == maxTreeSize) { 87 88 pLonger = 0.0; 88 89 } else { … … 112 113 private void FindPossibleBranches(IFunctionTree tree, IList<IFunction> allowedFunctions, int maxBranchSize, int maxBranchHeight, int removedBranchSize, 113 114 List<CrossoverPoint> shorterBranches, List<CrossoverPoint> equalLengthBranches, List<CrossoverPoint> longerBranches, List<int> trail) { 114 int treeSize = tree. Size;115 if (allowedFunctions.Contains(tree.Function) && treeSize <= maxBranchSize && tree. Height<= maxBranchHeight) {115 int treeSize = tree.GetSize(); 116 if (allowedFunctions.Contains(tree.Function) && treeSize <= maxBranchSize && tree.GetHeight() <= maxBranchHeight) { 116 117 CrossoverPoint p = new CrossoverPoint(); 117 118 p.branchSize = treeSize; -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/StandardCrossOver.cs
r2202 r2210 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.GP.Interfaces; 32 33 33 34 namespace HeuristicLab.GP { … … 57 58 58 59 // calculate the max size and height that the inserted branch can have 59 int maxInsertedBranchSize = maxTreeSize - (tree0. Size - parent0.SubTrees[replacedChildIndex].Size);60 int maxInsertedBranchSize = maxTreeSize - (tree0.GetSize() - parent0.SubTrees[replacedChildIndex].GetSize()); 60 61 int maxInsertedBranchHeight = maxTreeHeight - gardener.GetBranchLevel(tree0, parent0); // branchlevel is 1 if tree0==parent0 61 62 62 63 IList<IFunction> allowedFunctions = new List<IFunction>(gardener.GetAllowedSubFunctions(parent0.Function, replacedChildIndex)); 63 allowedCrossoverPoints = GetPossibleCrossoverPoints( gardener,tree1, maxInsertedBranchSize, maxInsertedBranchHeight, allowedFunctions);64 allowedCrossoverPoints = GetPossibleCrossoverPoints(tree1, maxInsertedBranchSize, maxInsertedBranchHeight, allowedFunctions); 64 65 } while (allowedCrossoverPoints.Count == 0 && tries++ < MaxRecombinationTries); 65 66 … … 74 75 } 75 76 76 private List<IFunctionTree> GetPossibleCrossoverPoints( TreeGardener gardener,IFunctionTree tree, int maxInsertedBranchSize, int maxInsertedBranchHeight, IList<IFunction> allowedFunctions) {77 private List<IFunctionTree> GetPossibleCrossoverPoints(IFunctionTree tree, int maxInsertedBranchSize, int maxInsertedBranchHeight, IList<IFunction> allowedFunctions) { 77 78 List<IFunctionTree> crossoverPoints = new List<IFunctionTree>(); 78 foreach (IFunctionTree possiblePoint in gardener.GetAllSubTrees(tree)) {79 if (allowedFunctions.Contains(possiblePoint.Function) && possiblePoint. Size <= maxInsertedBranchSize && possiblePoint.Height<= maxInsertedBranchHeight)79 foreach (IFunctionTree possiblePoint in TreeGardener.GetAllSubTrees(tree)) { 80 if (allowedFunctions.Contains(possiblePoint.Function) && possiblePoint.GetSize() <= maxInsertedBranchSize && possiblePoint.GetHeight() <= maxInsertedBranchHeight) 80 81 crossoverPoints.Add(possiblePoint); 81 82 } -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/UniformCrossover.cs
r1286 r2210 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.GP.Interfaces; 32 33 33 34 namespace HeuristicLab.GP { -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/TreeGardener.cs
r2202 r2210 32 32 using System.Collections; 33 33 using HeuristicLab.Selection; 34 using HeuristicLab.GP.Interfaces; 34 35 35 36 namespace HeuristicLab.GP { … … 96 97 } 97 98 98 internal IFunctionTree PTC2(IRandom random,int size, int maxDepth) {99 return PTC2( random,GetRandomRoot(size, maxDepth), size, maxDepth);100 } 101 102 internal IFunctionTree PTC2(IRandom random, IFunction rootF, int size, int maxDepth) {103 IFunctionTree root = rootF .GetTreeNode();104 if (size <= 1 || maxDepth <= 1) return root;99 public IFunctionTree PTC2(int size, int maxDepth) { 100 return PTC2(GetRandomRoot(size, maxDepth), size, maxDepth); 101 } 102 103 private IFunctionTree PTC2(IFunction rootFunction, int size, int maxDepth) { 104 IFunctionTree root = rootFunction.GetTreeNode(); 105 if (size <= 1 || maxDepth <= 1) return root; 105 106 List<object[]> list = new List<object[]>(); 106 107 int currentSize = 1; … … 108 109 int minArity = root.Function.MinArity; 109 110 int maxArity = root.Function.MaxArity; 110 if (maxArity >= size) {111 if (maxArity >= size) { 111 112 maxArity = size; 112 113 } 113 114 int actualArity = random.Next(minArity, maxArity + 1); 114 115 totalListMinSize += root.Function.MinTreeSize - 1; 115 for (int i = 0; i < actualArity; i++) {116 for (int i = 0; i < actualArity; i++) { 116 117 // insert a dummy sub-tree and add the pending extension to the list 117 118 root.AddSubTree(null); … … 119 120 } 120 121 121 while (list.Count > 0 && totalListMinSize + currentSize < size) {122 while (list.Count > 0 && totalListMinSize + currentSize < size) { 122 123 int randomIndex = random.Next(list.Count); 123 124 object[] nextExtension = list[randomIndex]; … … 126 127 int a = (int)nextExtension[1]; 127 128 int d = (int)nextExtension[2]; 128 if (d == maxDepth) {129 if (d == maxDepth) { 129 130 parent.RemoveSubTree(a); 130 131 IFunctionTree branch = CreateRandomTree(GetAllowedSubFunctions(parent.Function, a), 1, 1); 131 132 parent.InsertSubTree(a, branch); // insert a smallest possible tree 132 currentSize += branch. Size;133 totalListMinSize -= branch. Size;133 currentSize += branch.GetSize(); 134 totalListMinSize -= branch.GetSize(); 134 135 } else { 135 IFunction selectedFunction = RandomSelect(GetAllowedSubFunctions(parent.Function, a).Where(136 f => ! IsTerminal(f) && f.MinTreeHeight + (d - 1) <= maxDepth).ToArray());136 IFunction selectedFunction = TreeGardener.RandomSelect(random, GetAllowedSubFunctions(parent.Function, a).Where( 137 f => !TreeGardener.IsTerminal(f) && f.MinTreeHeight + (d - 1) <= maxDepth).ToArray()); 137 138 IFunctionTree newTree = selectedFunction.GetTreeNode(); 138 139 parent.RemoveSubTree(a); … … 143 144 minArity = selectedFunction.MinArity; 144 145 maxArity = selectedFunction.MaxArity; 145 if (maxArity >= size) {146 if (maxArity >= size) { 146 147 maxArity = size; 147 148 } 148 149 actualArity = random.Next(minArity, maxArity + 1); 149 for (int i = 0; i < actualArity; i++) {150 for (int i = 0; i < actualArity; i++) { 150 151 // insert a dummy sub-tree and add the pending extension to the list 151 152 newTree.AddSubTree(null); … … 155 156 } 156 157 } 157 while (list.Count > 0) {158 while (list.Count > 0) { 158 159 int randomIndex = random.Next(list.Count); 159 160 object[] nextExtension = list[randomIndex]; … … 168 169 return root; 169 170 } 170 171 171 172 /// <summary> 172 173 /// selects a random function from allowedFunctions and creates a random (unbalanced) tree with maximal size and height. … … 197 198 // build the tree 198 199 IFunctionTree root; 199 root = PTC2( random,selectedFunction, maxTreeSize, maxTreeHeight);200 root = PTC2(selectedFunction, maxTreeSize, maxTreeHeight); 200 201 return root; 201 202 } 202 203 203 internal CompositeOperation CreateInitializationOperation(ICollection<IFunctionTree> trees, IScope scope) { 204 // needed for the parameter shaking operation 204 internal static CompositeOperation CreateInitializationOperation(ICollection<IFunctionTree> trees, IScope scope) { 205 // create a backup of sub scopes to restore after initialization 206 Scope backupScope = new Scope("backup"); 207 foreach (Scope subScope in scope.SubScopes) { 208 backupScope.AddSubScope(subScope); 209 } 210 205 211 CompositeOperation initializationOperation = new CompositeOperation(); 206 212 Scope tempScope = new Scope("Temp. initialization scope"); 207 213 208 var parametricTrees = trees.Where(t => t. Initializer != null);214 var parametricTrees = trees.Where(t => t.HasLocalParameters); 209 215 foreach(IFunctionTree tree in parametricTrees) { 210 // enqueue an initialization operation for each operator with local variables 211 IOperator initialization = tree.Initializer; 212 Scope initScope = new Scope(); 213 // copy the local variables into a temporary scope used for initialization 214 foreach(IVariable variable in tree.LocalVariables) { 215 initScope.AddVariable(variable); 216 } 217 tempScope.AddSubScope(initScope); 218 initializationOperation.AddOperation(new AtomicOperation(initialization, initScope)); 219 } 220 Scope backupScope = new Scope("backup"); 221 foreach(Scope subScope in scope.SubScopes) { 222 backupScope.AddSubScope(subScope); 216 initializationOperation.AddOperation(tree.CreateInitOperation(tempScope)); 223 217 } 224 218 scope.AddSubScope(tempScope); … … 246 240 } 247 241 248 internal ICollection<IFunctionTree> GetAllSubTrees(IFunctionTree root) {242 internal static ICollection<IFunctionTree> GetAllSubTrees(IFunctionTree root) { 249 243 List<IFunctionTree> allTrees = new List<IFunctionTree>(); 250 244 TreeForEach(root, t => { allTrees.Add(t); }); … … 375 369 return parents; 376 370 } 377 internal bool IsTerminal(IFunction f) {371 internal static bool IsTerminal(IFunction f) { 378 372 return f.MinArity == 0 && f.MaxArity == 0; 379 373 } … … 388 382 389 383 #region private utility methods 390 p rivateIFunction GetRandomRoot(int maxTreeSize, int maxTreeHeight) {384 public IFunction GetRandomRoot(int maxTreeSize, int maxTreeHeight) { 391 385 if(maxTreeHeight == 1 || maxTreeSize == 1) { 392 386 IFunction selectedTerminal = RandomSelect(terminals); … … 450 444 } 451 445 452 private void TreeForEach(IFunctionTree tree, Action<IFunctionTree> action) {446 private static void TreeForEach(IFunctionTree tree, Action<IFunctionTree> action) { 453 447 action(tree); 454 448 foreach(IFunctionTree subTree in tree.SubTrees) { … … 457 451 } 458 452 459 private void GetBranchesAtLevel(IFunctionTree tree, int level, List<IFunctionTree> result) {453 private static void GetBranchesAtLevel(IFunctionTree tree, int level, List<IFunctionTree> result) { 460 454 if(level == 1) result.AddRange(tree.SubTrees); 461 455 foreach(IFunctionTree subTree in tree.SubTrees) { 462 if(subTree. Height>= level - 1)456 if(subTree.GetHeight() >= level - 1) 463 457 GetBranchesAtLevel(subTree, level - 1, result); 464 458 } … … 466 460 467 461 private IFunction RandomSelect(IList<IFunction> functionSet) { 462 return RandomSelect(random, functionSet); 463 } 464 465 public static IFunction RandomSelect(IRandom random, IList<IFunction> functionSet) { 468 466 double[] accumulatedTickets = new double[functionSet.Count]; 469 467 double ticketAccumulator = 0; 470 468 int i = 0; 471 469 // precalculate the slot-sizes 472 foreach (IFunction function in functionSet) {470 foreach (IFunction function in functionSet) { 473 471 ticketAccumulator += function.Tickets; 474 472 accumulatedTickets[i] = ticketAccumulator; … … 478 476 double r = random.NextDouble() * ticketAccumulator; 479 477 // find the slot that has been hit 480 for (i = 0; i < accumulatedTickets.Length; i++) {481 if (r < accumulatedTickets[i]) return functionSet[i];478 for (i = 0; i < accumulatedTickets.Length; i++) { 479 if (r < accumulatedTickets[i]) return functionSet[i]; 482 480 } 483 481 // sanity check
Note: See TracChangeset
for help on using the changeset viewer.