Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/29/09 18:28:45 (15 years ago)
Author:
gkronber
Message:

GP Refactoring #713

  • introduced a plugin for GP interfaces
  • created a new interface IGeneticProgrammingModel which represents GP models in HL scopes instead of IFunctionTree
  • changed interfaces IFunction and IFunctionTree
  • moved some files to new directories (general housekeeping)
  • changed all GP operators and engines to work with IGeneticProgrammingModels
  • removed parameters TreeSize and TreeHeight in all GP operators
  • changed parameter OperatorLibrary to FunctionLibrary in all GP operators
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  
    2626using HeuristicLab.Constraints;
    2727
    28 namespace HeuristicLab.GP.StructureIdentification {
     28namespace HeuristicLab.GP {
    2929  public abstract class BinaryFunction : FunctionBase {
    3030    public BinaryFunction()
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/BaseClasses/FunctionBase.cs

    r2202 r2210  
    2828using HeuristicLab.Constraints;
    2929using System.Diagnostics;
     30using HeuristicLab.GP.Interfaces;
    3031
    3132namespace HeuristicLab.GP {
     
    107108
    108109    public virtual IFunctionTree GetTreeNode() {
    109       return new BakedFunctionTree(this);
    110     }
    111 
     110      return new FunctionTreeBase(this);
     111    }
    112112
    113113    //private List<IConstraint> constraints = new List<IConstraint>();
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/BaseClasses/Terminal.cs

    r2202 r2210  
    2626using HeuristicLab.Constraints;
    2727
    28 namespace HeuristicLab.GP.StructureIdentification {
     28namespace HeuristicLab.GP {
    2929  public abstract class Terminal : FunctionBase {
    3030    public Terminal()
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/BaseClasses/UnaryFunction.cs

    r2202 r2210  
    2626using HeuristicLab.Constraints;
    2727
    28 namespace HeuristicLab.GP.StructureIdentification {
     28namespace HeuristicLab.GP {
    2929  public abstract class UnaryFunction : FunctionBase {
    3030    public UnaryFunction()
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/DefaultFunctionTreeNameGenerator.cs

    r1529 r2210  
    2424using System.Text;
    2525using HeuristicLab.Core;
     26using HeuristicLab.GP.Interfaces;
    2627
    2728namespace HeuristicLab.GP {
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/FunView.cs

    r2202 r2210  
    88using System.Windows.Forms;
    99using HeuristicLab.Core;
     10using HeuristicLab.GP.Interfaces;
    1011
    1112namespace HeuristicLab.GP {
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/FunctionLibrary.cs

    r2202 r2210  
    2828using HeuristicLab.Data;
    2929using HeuristicLab.Constraints;
     30using HeuristicLab.GP.Interfaces;
    3031
    3132namespace HeuristicLab.GP {
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/FunctionLibraryEditor.cs

    r2202 r2210  
    3333using HeuristicLab.Operators;
    3434using HeuristicLab.Random;
     35using HeuristicLab.GP.Interfaces;
    3536
    3637namespace HeuristicLab.GP {
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/FunctionTreeView.cs

    r2202 r2210  
    3131using HeuristicLab.PluginInfrastructure;
    3232using HeuristicLab.Data;
     33using HeuristicLab.GP.Interfaces;
    3334
    3435namespace HeuristicLab.GP {
     
    136137      if (funTreeView.SelectedNode != null && funTreeView.SelectedNode.Tag != null) {
    137138        IFunctionTree selectedBranch = (IFunctionTree)funTreeView.SelectedNode.Tag;
    138         UpdateVariablesList(selectedBranch);
     139//      UpdateVariablesList(selectedBranch);
    139140        templateTextBox.Text = nameGenerator.GetName(selectedBranch);
    140141        this.selectedBranch = selectedBranch;
     
    143144    }
    144145
    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    //}
    150151
    151152    private void variablesListBox_SelectedIndexChanged(object sender, EventArgs e) {
    152       // in case we had an event-handler registered for a different variable => unregister the event-handler
    153       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 user
    164         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      //}
    168169    }
    169170
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/HeuristicLab.GP-3.3.csproj

    r2202 r2210  
    8282  </ItemGroup>
    8383  <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" />
    8589    <Compile Include="FunctionLibrary.cs" />
    8690    <Compile Include="FunctionLibraryEditor.cs">
     
    102106      <DependentUpon>FunView.cs</DependentUpon>
    103107    </Compile>
     108    <Compile Include="GeneticProgrammingModel.cs" />
     109    <Compile Include="Initialization\ProbabilisticTreeCreator.cs" />
     110    <Compile Include="Initialization\RampedTreeCreator.cs" />
     111    <Compile Include="Manipulation\GPManipulatorBase.cs" />
    104112    <Compile Include="TrainingWindowSlider.cs" />
    105113    <Compile Include="DefaultFunctionTreeNameGenerator.cs" />
     
    111119    <Compile Include="Recombination\UniformCrossover.cs" />
    112120    <Compile Include="UnknownFunctionException.cs" />
    113     <Compile Include="IFunctionTreeNameGenerator.cs" />
    114     <Compile Include="IFunctionTreeExporter.cs" />
    115     <Compile Include="FunctionBase.cs" />
    116121    <Compile Include="HeuristicLabGPPlugin.cs" />
    117     <Compile Include="IFunction.cs" />
    118     <Compile Include="IFunctionTree.cs" />
    119122    <Compile Include="Manipulation\ChangeNodeTypeManipulation.cs" />
    120123    <Compile Include="Manipulation\CutOutNodeManipulation.cs" />
     
    123126    <Compile Include="Manipulation\OnePointShaker.cs" />
    124127    <Compile Include="Manipulation\SubstituteSubTreeManipulation.cs" />
    125     <Compile Include="ProbabilisticTreeCreator.cs" />
    126128    <Compile Include="Properties\AssemblyInfo.cs" />
    127     <Compile Include="RampedTreeCreator.cs" />
    128129    <Compile Include="Recombination\LangdonHomologousCrossOver.cs" />
    129130    <Compile Include="Recombination\OnePointCrossOver.cs" />
     
    148149      <Project>{F5614C53-153C-4A37-A608-121E1C087F07}</Project>
    149150      <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>
    150155    </ProjectReference>
    151156    <ProjectReference Include="..\..\HeuristicLab.Operators\3.2\HeuristicLab.Operators-3.2.csproj">
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/HeuristicLabGPPlugin.cs

    r1529 r2210  
    2828  [ClassInfo(Name = "HeuristicLab.GP-3.3")]
    2929  [PluginFile(Filename = "HeuristicLab.GP-3.3.dll", Filetype = PluginFileType.Assembly)]
     30  [Dependency(Dependency = "HeuristicLab.GP.Interfaces-3.3")]
    3031  [Dependency(Dependency = "HeuristicLab.Constraints-3.2")]
    3132  [Dependency(Dependency = "HeuristicLab.Core-3.2")]
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Initialization/ProbabilisticTreeCreator.cs

    r2202 r2210  
    2727using HeuristicLab.Random;
    2828using System.Diagnostics;
     29using HeuristicLab.GP.Interfaces;
     30using System.Collections.Generic;
    2931
    3032namespace HeuristicLab.GP {
     
    3941      : base() {
    4042      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));
    4244      AddVariableInfo(new VariableInfo("MinTreeSize", "The minimal allowed size of the tree", typeof(IntData), VariableKind.In));
    4345      AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size of the tree", typeof(IntData), VariableKind.In));
    4446      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));
    4848    }
    4949
    5050    public override IOperation Apply(IScope scope) {
    5151      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
    52       FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>("OperatorLibrary", scope, true);
     52      FunctionLibrary funLibrary = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true);
    5353      int minTreeSize = GetVariableValue<IntData>("MinTreeSize", scope, true).Data;
    5454      int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
    5555      int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
    5656
    57       TreeGardener gardener = new TreeGardener(random, opLibrary);
    58 
    5957      int treeSize = random.Next(minTreeSize, maxTreeSize + 1);
    60 
    6158      IFunctionTree root;
    62       int actualTreeSize;
    63       int actualTreeHeight;
    6459      int tries = 0;
     60      TreeGardener gardener = new TreeGardener(random, funLibrary);
    6561      do {
    66         root = gardener.PTC2(random, treeSize, maxTreeHeight);
    67         actualTreeSize = root.Size;
    68         actualTreeHeight = root.Height;
     62        root = gardener.PTC2(treeSize, maxTreeHeight);
    6963        if (tries++ >= MAX_TRIES) {
    7064          // try a different size
     
    7266          tries = 0;
    7367        }
    74       } while (actualTreeSize > maxTreeSize || actualTreeHeight > maxTreeHeight);
     68      } while (root.GetSize() > maxTreeSize || root.GetHeight() > maxTreeHeight);
    7569
    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    }
    7973
    80       return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(root), scope);
    81     }
    82   }
     74 }
    8375}
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Initialization/RampedTreeCreator.cs

    r2202 r2210  
    2727using HeuristicLab.Random;
    2828using System.Diagnostics;
     29using HeuristicLab.GP.Interfaces;
    2930
    3031namespace HeuristicLab.GP {
     
    3738      : base() {
    3839      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));
    4041      AddVariableInfo(new VariableInfo("MinTreeHeight", "The minimal allowed height of the tree", typeof(IntData), VariableKind.In));
    4142      AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
    4243      AddVariableInfo(new VariableInfo("BalancedTreesRate", "Determines how many trees should be balanced", typeof(DoubleData), VariableKind.In));
    43       AddVariableInfo(new VariableInfo("FunctionTree", "The created tree", typeof(IFunctionTree), VariableKind.New | VariableKind.Out));
     44      AddVariableInfo(new VariableInfo("FunctionTree", "The created tree", typeof(IGeneticProgrammingModel), VariableKind.New | VariableKind.Out));
    4445      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New | VariableKind.Out));
    4546      AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New | VariableKind.Out));
     
    4849    public override IOperation Apply(IScope scope) {
    4950      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
    50       FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>("OperatorLibrary", scope, true);
     51      FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true);
    5152      int minTreeHeight = GetVariableValue<IntData>("MinTreeHeight", scope, true).Data;
    5253      int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
     
    6364      }
    6465
    65       int actualTreeSize = root.Size;
    66       int actualTreeHeight = root.Height;
     66      Debug.Assert(gardener.IsValidTree(root) && root.GetHeight() <= maxTreeHeight);
    6767
    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);
    7570    }
    7671  }
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Logging/TreeArityAnalyser.cs

    r698 r2210  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.GP.Interfaces;
    3233
    3334namespace HeuristicLab.GP {
     
    4344    public TreeArityAnalyser()
    4445      : 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));
    4647      AddVariableInfo(new VariableInfo("Histogram", "The histogram of arities over all functions in all functiontrees", typeof(ItemList<ItemList<IntData>>), VariableKind.New | VariableKind.Out));
    4748    }
     
    6970
    7071    private ItemList<ItemList<IntData>> GetArityHistogram(IScope subScope) {
    71       IFunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", subScope, false);
     72      IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", subScope, false);
    7273      List<int> arities = new List<int>();
    73       Arities(tree, arities);
     74      Arities(gpModel.FunctionTree, arities);
    7475      var histogram = arities.GroupBy(x => x, (g, xs) => new { Group = g, Count = xs.Count() }).OrderBy(g=>g.Group); // count number of distinct arities
    7576      ItemList<ItemList<IntData>> result = new ItemList<ItemList<IntData>>();
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/ChangeNodeTypeManipulation.cs

    r2202 r2210  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.GP.Interfaces;
    3233
    3334namespace HeuristicLab.GP {
    34   public class ChangeNodeTypeManipulation : OperatorBase {
     35  public class ChangeNodeTypeManipulation : GPManipulatorBase {
    3536
    3637    public override string Description {
     
    4445    public ChangeNodeTypeManipulation()
    4546      : 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));
    5347    }
    5448
    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) {
    6450      TreeGardener gardener = new TreeGardener(random, library);
    65       IFunctionTree parent = gardener.GetRandomParentNode(root);
     51      IFunctionTree parent = gardener.GetRandomParentNode(gpModel.FunctionTree);
    6652      IFunctionTree selectedChild;
    6753      int selectedChildIndex;
    68       if(parent == null) {
     54      if (parent == null) {
    6955        selectedChildIndex = 0;
    70         selectedChild = root;
     56        selectedChild = gpModel.FunctionTree;
    7157      } else {
    7258        selectedChildIndex = random.Next(parent.SubTrees.Count);
     
    7460      }
    7561
    76       if(selectedChild.SubTrees.Count == 0) {
     62      if (selectedChild.SubTrees.Count == 0) {
    7763        IFunctionTree newTerminal = ChangeTerminalType(parent, selectedChild, selectedChildIndex, gardener, random);
    78         if(parent == null) {
     64        if (parent == null) {
    7965          // 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;
    8267        } else {
    8368          parent.RemoveSubTree(selectedChildIndex);
     
    8570          // updating the variable is not necessary because it stays the same
    8671        }
    87         Debug.Assert(gardener.IsValidTree(root));
     72        Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
    8873        // size and height stays the same when changing a terminal so no need to update the variables
    8974        // schedule an operation to initialize the new terminal
    90         return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTerminal), scope);
     75        return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newTerminal), scope);
    9176      } else {
    9277        List<IFunctionTree> uninitializedBranches;
     
    9580        // calculate the height and size difference and
    9681        // 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) {
    10388          // if size-constraints are violated don't change anything
    10489          return null;
    10590        }
    106         if(parent == null) {
     91        if (parent == null) {
    10792          // 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;
    11194        } else {
    11295          // remove the old child
     
    11699        }
    117100        // 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 branch
     101        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
    120103        // check if whole tree is ok
    121         Debug.Assert(gardener.IsValidTree(root));
     104        Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
    122105        // return a composite operation that initializes all created sub-trees
    123         return gardener.CreateInitializationOperation(uninitializedBranches, scope);
     106        return TreeGardener.CreateInitializationOperation(uninitializedBranches, scope);
    124107      }
    125108    }
    126  
    127109
    128110    private IFunctionTree ChangeTerminalType(IFunctionTree parent, IFunctionTree child, int childIndex, TreeGardener gardener, MersenneTwister random) {
     
    133115        allowedTerminals = new List<IFunction>();
    134116        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);
    137119        }
    138120      }
     
    159141      if (actualArity > maxArity)
    160142        actualArity = maxArity;
    161       if(actualArity < minArity)
     143      if (actualArity < minArity)
    162144        actualArity = minArity;
    163145      // create a list that holds old sub-trees that we can reuse in the new tree
     
    181163          // no existing matching tree found => create a new tree of minimal size
    182164          IFunctionTree freshTree = gardener.CreateRandomTree(allowedSubFunctions, 1, 1);
    183           freshSubTrees.AddRange(gardener.GetAllSubTrees(freshTree));
     165          freshSubTrees.AddRange(TreeGardener.GetAllSubTrees(freshTree));
    184166          newTree.InsertSubTree(i, freshTree);
    185167        }
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/CutOutNodeManipulation.cs

    r2202 r2210  
    2828using System;
    2929using System.Diagnostics;
     30using HeuristicLab.GP.Interfaces;
    3031
    3132namespace HeuristicLab.GP {
    32   public class CutOutNodeManipulation : OperatorBase {
     33  public class CutOutNodeManipulation : GPManipulatorBase {
    3334    public override string Description {
    3435      get {
     
    4950    public CutOutNodeManipulation()
    5051      : 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));
    5852    }
    5953
    6054
    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) {
    6756      TreeGardener gardener = new TreeGardener(random, library);
    68       IFunctionTree parent = gardener.GetRandomParentNode(root);
     57      IFunctionTree parent = gardener.GetRandomParentNode(gpModel.FunctionTree);
    6958      // parent == null means we should cut out the root node
    7059      // => return a random sub-tree of the root
    7160      if (parent == null) {
    7261        // 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));
    8065          // we reused a sub-tree so we don't have to schedule initialization operations
    8166          return null;
    8267        } else {
    8368          // 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
    9172          // 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);
    9374        }
    9475      }
     76
    9577      // select a child to cut away
    9678      int childIndex = random.Next(parent.SubTrees.Count);
     
    10183      if (possibleChilds.Length > 0) {
    10284        // 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)];
    10486        parent.RemoveSubTree(childIndex);
    10587        parent.InsertSubTree(childIndex, selectedChild);
    106         Debug.Assert(gardener.IsValidTree(root));
    107         // update the size and height of our tree
    108         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();
    11092        // don't need to schedule initialization operations
    11193        return null;
     
    11395        // can't reuse an existing branch => create a new tree
    11496        // determine the level of the parent
    115         int parentLevel = gardener.GetBranchLevel(root, parent);
     97        int parentLevel = gardener.GetBranchLevel(gpModel.FunctionTree, parent);
    11698        // first remove the old child (first step essential!)
    11799        parent.RemoveSubTree(childIndex);
    118100        // 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();
    120102        allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
    121103        IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel);
    122104        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();
    126109        // 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);
    128111      }
    129112    }
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/DeleteSubTreeManipulation.cs

    r2202 r2210  
    2727using HeuristicLab.Random;
    2828using System.Diagnostics;
     29using HeuristicLab.GP.Interfaces;
    2930
    3031namespace HeuristicLab.GP {
    31   public class DeleteSubTreeManipulation : OperatorBase {
     32  public class DeleteSubTreeManipulation : GPManipulatorBase {
    3233    public override string Description {
    3334      get {
     
    3940    public DeleteSubTreeManipulation()
    4041      : 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));
    4842    }
    4943
    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) {
    5545      TreeGardener gardener = new TreeGardener(random, library);
    56       IFunctionTree parent = gardener.GetRandomParentNode(root);
     46      IFunctionTree parent = gardener.GetRandomParentNode(gpModel.FunctionTree);
    5747
    5848      // parent==null means the whole tree should be deleted.
    5949      // => return a new minimal random tree
    60       if(parent == null) {
     50      if (parent == null) {
    6151        IFunctionTree newTree = gardener.CreateBalancedRandomTree(1, 1);
    6252        // check if the tree is ok
    6353        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;
    6955        // 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);
    7157      }
    7258
    7359      // select a branch to prune
    7460      int childIndex = random.Next(parent.SubTrees.Count);
    75       if(parent.SubTrees.Count > parent.Function.MinArity) {
     61      if (parent.SubTrees.Count > parent.Function.MinArity) {
    7662        parent.RemoveSubTree(childIndex);
    7763        // actually since the next sub-trees are shifted in the place of the removed branch
     
    8167        // 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)
    8268
    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();
    8673        // root hasn't changed so don't need to update 'FunctionTree' variable
    8774        return null;
     
    9279        IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, 1, 1);
    9380        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' variable
    98         // but we have to return an initialization operation for the newly created tree
    99         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);
    10087      }
    10188    }
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/FullTreeShaker.cs

    r2202 r2210  
    2929using HeuristicLab.Data;
    3030using HeuristicLab.Selection;
     31using HeuristicLab.GP.Interfaces;
    3132
    3233namespace HeuristicLab.GP {
     
    3940      : base() {
    4041      AddVariableInfo(new VariableInfo("Random", "A random generator (uniform)", typeof(MersenneTwister), VariableKind.In));
    41       AddVariableInfo(new VariableInfo("OperatorLibrary", "Operator library that defines operator mutations", typeof(FunctionLibrary), VariableKind.In));
     42      AddVariableInfo(new VariableInfo("FunctionLibrary", "Function library that defines function mutations", typeof(FunctionLibrary), VariableKind.In));
    4243      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(IFunctionTree), VariableKind.In | VariableKind.Out));
     44      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be mutated", typeof(IGeneticProgrammingModel), VariableKind.In | VariableKind.Out));
    4445    }
    4546
    4647    public override IOperation Apply(IScope scope) {
    47       FunctionLibrary library = GetVariableValue<FunctionLibrary>("OperatorLibrary", scope, true);
    48       IFunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", scope, false);
     48      FunctionLibrary library = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true);
     49      IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, false);
    4950      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
    5051
    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
    5264      CompositeOperation next = new CompositeOperation();
    5365      next.ExecuteInParallel = false;
    5466
    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);
    7071      }
    71 
    72       // save all existing sub-scopes in a backup scope
    73       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 manipulation
    79       scope.AddSubScope(backupScope); // scope containing the old subscopes
    8072
    8173      // 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  
    2929using HeuristicLab.Data;
    3030using HeuristicLab.Selection;
     31using HeuristicLab.GP.Interfaces;
    3132
    3233namespace HeuristicLab.GP {
     
    3839    public OnePointShaker()
    3940      : 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));
    4142      AddVariableInfo(new VariableInfo("Random", "A random generator (uniform)", typeof(MersenneTwister), VariableKind.In));
    4243      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(IFunctionTree), VariableKind.In | VariableKind.Out));
     44      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be mutated", typeof(IGeneticProgrammingModel), VariableKind.In | VariableKind.Out));
    4445    }
    4546
    4647    public override IOperation Apply(IScope scope) {
    47       FunctionLibrary library = GetVariableValue<FunctionLibrary>("OperatorLibrary", scope, true);
    48       IFunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", scope, false);
     48      FunctionLibrary library = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true);
     49      IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, false);
    4950      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
    50       TreeGardener gardener = new TreeGardener(mt, library);
    5151
    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()));
    5456
    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
    6858      Scope backupScope = new Scope("backup");
    6959      foreach (Scope subScope in scope.SubScopes) {
     
    7161      }
    7262
     63      // create a scope for all shaking operations
     64      Scope tempScope = new Scope("Temp. manipulation scope");
     65     
    7366      // add the new sub-scopes
    7467      scope.AddSubScope(tempScope);
    7568      scope.AddSubScope(backupScope);
    7669
     70      CompositeOperation next = new CompositeOperation();
     71      next.ExecuteInParallel = false;
    7772      // 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));
    7974      next.AddOperation(new AtomicOperation(new RightReducer(), scope));
    8075
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/SubstituteSubTreeManipulation.cs

    r2202 r2210  
    2828using HeuristicLab.Random;
    2929using System.Diagnostics;
     30using HeuristicLab.GP.Interfaces;
    3031
    3132namespace HeuristicLab.GP {
    32   public class SubstituteSubTreeManipulation : OperatorBase {
     33  public class SubstituteSubTreeManipulation : GPManipulatorBase {
    3334
    3435    public override string Description {
     
    3839    public SubstituteSubTreeManipulation()
    3940      : 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));
    4741    }
    4842
    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) {
    5744      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) {
    6047        // parent == null means we should subsitute the whole tree
    6148        // => create a new random tree
     
    6350        Debug.Assert(gardener.IsValidTree(newTree));
    6451
    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;
    7053        // 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);
    7255      } else {
    7356        // determine a random child of the parent to be replaced
     
    7558        // get the list of allowed functions for the new sub-tree
    7659        ICollection<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
    77         if(allowedFunctions.Count == 0) {
     60        if (allowedFunctions.Count == 0) {
    7861          // don't change anything
    7962          // this shouldn't happen
     
    8366        // calculate the maximum size and height of the new sub-tree based on the location where
    8467        // it will be inserted
    85         int parentLevel = gardener.GetBranchLevel(root, parent);
     68        int parentLevel = gardener.GetBranchLevel(gpModel.FunctionTree, parent);
    8669        int maxSubTreeHeight = maxTreeHeight - parentLevel;
    87         int maxSubTreeSize = maxTreeSize - (treeSize - parent.SubTrees[childIndex].Size);
     70        int maxSubTreeSize = maxTreeSize - (gpModel.Size - parent.SubTrees[childIndex].GetSize());
    8871
    8972        // create a random function tree
     
    9275        parent.InsertSubTree(childIndex, newTree);
    9376
    94         Debug.Assert(gardener.IsValidTree(root));
     77        Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
    9578        // 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();
    9881        // the root hasn't changed so we don't need to update
    9982        // 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);
    10184      }
    10285    }
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/GPCrossoverBase.cs

    r2202 r2210  
    3131using System.Diagnostics;
    3232using HeuristicLab.Evolutionary;
     33using HeuristicLab.GP.Interfaces;
    3334
    3435namespace HeuristicLab.GP {
     
    3637    public GPCrossoverBase()
    3738      : 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));
    4241    }
    4342
     
    4544
    4645    protected override void Cross(IScope scope, IRandom random) {
    47       FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>("OperatorLibrary", scope, true);
     46      FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true);
    4847      TreeGardener gardener = new TreeGardener(random, opLibrary);
    4948
     
    5150        throw new InvalidOperationException("Number of parents must be exactly two.");
    5251
    53       IFunctionTree parent0 = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[0], false);
    54       IFunctionTree 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);
    5554
    5655      // randomly swap parents to remove a possible bias from selection (e.g. when using gender-specific selection)
    5756      if (random.NextDouble() < 0.5) {
    58         IFunctionTree tmp = parent0;
     57        IGeneticProgrammingModel tmp = parent0;
    5958        parent0 = parent1;
    6059        parent1 = tmp;
    6160      }
    6261
    63       IFunctionTree child = Cross(scope, gardener, random, parent0, parent1);
     62      IFunctionTree child = Cross(scope, gardener, random, parent0.FunctionTree, parent1.FunctionTree);
    6463      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)));
    6865    }
    6966  }
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/LangdonHomologousCrossOver.cs

    r835 r2210  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.GP.Interfaces;
    3233
    3334namespace HeuristicLab.GP {
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/OnePointCrossOver.cs

    r1286 r2210  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.GP.Interfaces;
    3233
    3334namespace HeuristicLab.GP {
     
    5152    internal override IFunctionTree Cross(TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) {
    5253      List<CrossoverPoint> allowedCrossOverPoints = new List<CrossoverPoint>();
    53       GetCrossOverPoints(gardener, tree0, tree1, maxTreeSize - tree0.Size, allowedCrossOverPoints);
     54      GetCrossOverPoints(gardener, tree0, tree1, maxTreeSize - tree0.GetSize(), allowedCrossOverPoints);
    5455      if (allowedCrossOverPoints.Count > 0) {
    5556        CrossoverPoint crossOverPoint = allowedCrossOverPoints[random.Next(allowedCrossOverPoints.Count)];
     
    6869        // if the current branch can be attached as a sub-tree to branch0
    6970        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) {
    7172          CrossoverPoint p = new CrossoverPoint();
    7273          p.childIndex = i;
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/SizeConstrictedGPCrossoverBase.cs

    r1286 r2210  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.GP.Interfaces;
    3233
    3334namespace HeuristicLab.GP {
     
    5859          newTree = Cross(gardener, random, (IFunctionTree)tree1.Clone(), (IFunctionTree)tree0.Clone(), maxTreeSize, maxTreeHeight);
    5960        } else newTree = tree0;
    60       } while (newTree.Size > maxTreeSize || newTree.Height > maxTreeHeight);
     61      } while (newTree.GetSize() > maxTreeSize || newTree.GetHeight() > maxTreeHeight);
    6162      return newTree;
    6263    }
    6364
    6465    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     //}
    7166  }
    7267}
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/SizeFairCrossOver.cs

    r2202 r2210  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.GP.Interfaces;
    3233
    3334namespace HeuristicLab.GP {
     
    6970    private IFunctionTree GetReplacementBranch(MersenneTwister random, TreeGardener gardener, IFunctionTree intoTree, IFunctionTree parent, int replacedBranchIndex, IFunctionTree fromTree, int maxTreeSize, int maxTreeHeight) {
    7071      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);
    7374      int maxBranchHeight = maxTreeHeight - gardener.GetBranchLevel(intoTree, parent);  // returns 1 if intoTree==parent and 2 if parent is a child of intoTree
    7475      List<int> replacedTrail = GetTrail(intoTree, parent);
     
    8485        double pEqualLength = equalLengthBranches.Count > 0 ? 1.0 / removedBranchSize : 0.0;
    8586        double pLonger;
    86         if (parent.Size == maxTreeSize) {
     87        if (parent.GetSize() == maxTreeSize) {
    8788          pLonger = 0.0;
    8889        } else {
     
    112113    private void FindPossibleBranches(IFunctionTree tree, IList<IFunction> allowedFunctions, int maxBranchSize, int maxBranchHeight, int removedBranchSize,
    113114      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) {
    116117        CrossoverPoint p = new CrossoverPoint();
    117118        p.branchSize = treeSize;
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/StandardCrossOver.cs

    r2202 r2210  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.GP.Interfaces;
    3233
    3334namespace HeuristicLab.GP {
     
    5758
    5859        // 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());
    6061        int maxInsertedBranchHeight = maxTreeHeight - gardener.GetBranchLevel(tree0, parent0); // branchlevel is 1 if tree0==parent0
    6162
    6263        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);
    6465      } while (allowedCrossoverPoints.Count == 0 && tries++ < MaxRecombinationTries);
    6566
     
    7475    }
    7576
    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) {
    7778      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)
    8081          crossoverPoints.Add(possiblePoint);
    8182      }
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/UniformCrossover.cs

    r1286 r2210  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.GP.Interfaces;
    3233
    3334namespace HeuristicLab.GP {
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/TreeGardener.cs

    r2202 r2210  
    3232using System.Collections;
    3333using HeuristicLab.Selection;
     34using HeuristicLab.GP.Interfaces;
    3435
    3536namespace HeuristicLab.GP {
     
    9697    }
    9798
    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;
    105106      List<object[]> list = new List<object[]>();
    106107      int currentSize = 1;
     
    108109      int minArity = root.Function.MinArity;
    109110      int maxArity = root.Function.MaxArity;
    110       if(maxArity >= size) {
     111      if (maxArity >= size) {
    111112        maxArity = size;
    112113      }
    113114      int actualArity = random.Next(minArity, maxArity + 1);
    114115      totalListMinSize += root.Function.MinTreeSize - 1;
    115       for(int i = 0; i < actualArity; i++) {
     116      for (int i = 0; i < actualArity; i++) {
    116117        // insert a dummy sub-tree and add the pending extension to the list
    117118        root.AddSubTree(null);
     
    119120      }
    120121
    121       while(list.Count > 0 && totalListMinSize + currentSize < size) {
     122      while (list.Count > 0 && totalListMinSize + currentSize < size) {
    122123        int randomIndex = random.Next(list.Count);
    123124        object[] nextExtension = list[randomIndex];
     
    126127        int a = (int)nextExtension[1];
    127128        int d = (int)nextExtension[2];
    128         if(d == maxDepth) {
     129        if (d == maxDepth) {
    129130          parent.RemoveSubTree(a);
    130131          IFunctionTree branch = CreateRandomTree(GetAllowedSubFunctions(parent.Function, a), 1, 1);
    131132          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();
    134135        } 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());
    137138          IFunctionTree newTree = selectedFunction.GetTreeNode();
    138139          parent.RemoveSubTree(a);
     
    143144          minArity = selectedFunction.MinArity;
    144145          maxArity = selectedFunction.MaxArity;
    145           if(maxArity >= size) {
     146          if (maxArity >= size) {
    146147            maxArity = size;
    147148          }
    148149          actualArity = random.Next(minArity, maxArity + 1);
    149           for(int i = 0; i < actualArity; i++) {
     150          for (int i = 0; i < actualArity; i++) {
    150151            // insert a dummy sub-tree and add the pending extension to the list
    151152            newTree.AddSubTree(null);
     
    155156        }
    156157      }
    157       while(list.Count > 0) {
     158      while (list.Count > 0) {
    158159        int randomIndex = random.Next(list.Count);
    159160        object[] nextExtension = list[randomIndex];
     
    168169      return root;
    169170    }
    170 
     171 
    171172    /// <summary>
    172173    /// selects a random function from allowedFunctions and creates a random (unbalanced) tree with maximal size and height.
     
    197198      // build the tree
    198199      IFunctionTree root;
    199       root = PTC2(random, selectedFunction, maxTreeSize, maxTreeHeight);
     200      root = PTC2(selectedFunction, maxTreeSize, maxTreeHeight);
    200201      return root;
    201202    }
    202203
    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
    205211      CompositeOperation initializationOperation = new CompositeOperation();
    206212      Scope tempScope = new Scope("Temp. initialization scope");
    207213
    208       var parametricTrees = trees.Where(t => t.Initializer != null);
     214      var parametricTrees = trees.Where(t => t.HasLocalParameters);
    209215      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));
    223217      }
    224218      scope.AddSubScope(tempScope);
     
    246240    }
    247241
    248     internal ICollection<IFunctionTree> GetAllSubTrees(IFunctionTree root) {
     242    internal static ICollection<IFunctionTree> GetAllSubTrees(IFunctionTree root) {
    249243      List<IFunctionTree> allTrees = new List<IFunctionTree>();
    250244      TreeForEach(root, t => { allTrees.Add(t); });
     
    375369      return parents;
    376370    }
    377     internal bool IsTerminal(IFunction f) {
     371    internal static bool IsTerminal(IFunction f) {
    378372      return f.MinArity == 0 && f.MaxArity == 0;
    379373    }
     
    388382
    389383    #region private utility methods
    390     private IFunction GetRandomRoot(int maxTreeSize, int maxTreeHeight) {
     384    public IFunction GetRandomRoot(int maxTreeSize, int maxTreeHeight) {
    391385      if(maxTreeHeight == 1 || maxTreeSize == 1) {
    392386        IFunction selectedTerminal = RandomSelect(terminals);
     
    450444    }
    451445
    452     private void TreeForEach(IFunctionTree tree, Action<IFunctionTree> action) {
     446    private static void TreeForEach(IFunctionTree tree, Action<IFunctionTree> action) {
    453447      action(tree);
    454448      foreach(IFunctionTree subTree in tree.SubTrees) {
     
    457451    }
    458452
    459     private void GetBranchesAtLevel(IFunctionTree tree, int level, List<IFunctionTree> result) {
     453    private static void GetBranchesAtLevel(IFunctionTree tree, int level, List<IFunctionTree> result) {
    460454      if(level == 1) result.AddRange(tree.SubTrees);
    461455      foreach(IFunctionTree subTree in tree.SubTrees) {
    462         if(subTree.Height >= level - 1)
     456        if(subTree.GetHeight() >= level - 1)
    463457          GetBranchesAtLevel(subTree, level - 1, result);
    464458      }
     
    466460
    467461    private IFunction RandomSelect(IList<IFunction> functionSet) {
     462      return RandomSelect(random, functionSet);
     463    }
     464
     465    public static IFunction RandomSelect(IRandom random, IList<IFunction> functionSet) {
    468466      double[] accumulatedTickets = new double[functionSet.Count];
    469467      double ticketAccumulator = 0;
    470468      int i = 0;
    471469      // precalculate the slot-sizes
    472       foreach(IFunction function in functionSet) {
     470      foreach (IFunction function in functionSet) {
    473471        ticketAccumulator += function.Tickets;
    474472        accumulatedTickets[i] = ticketAccumulator;
     
    478476      double r = random.NextDouble() * ticketAccumulator;
    479477      // 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];
    482480      }
    483481      // sanity check
Note: See TracChangeset for help on using the changeset viewer.