[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System.Linq;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
| 26 | using System;
|
---|
| 27 | using HeuristicLab.Random;
|
---|
[155] | 28 | using HeuristicLab.Functions;
|
---|
[2] | 29 |
|
---|
| 30 | namespace HeuristicLab.StructureIdentification {
|
---|
| 31 | public class RandomTreeCreator : OperatorBase {
|
---|
| 32 | public override string Description {
|
---|
| 33 | get { return @"Generates a new random operator tree."; }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public RandomTreeCreator()
|
---|
| 37 | : base() {
|
---|
| 38 | 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(GPOperatorLibrary), VariableKind.In));
|
---|
| 40 | AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
|
---|
| 41 | AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
|
---|
[23] | 42 | AddVariableInfo(new VariableInfo("BalancedTreesRate", "Determines how many trees should be balanced", typeof(DoubleData), VariableKind.In));
|
---|
[155] | 43 | AddVariableInfo(new VariableInfo("FunctionTree", "The created tree", typeof(IFunctionTree), VariableKind.New | VariableKind.Out));
|
---|
| 44 | AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New | VariableKind.Out));
|
---|
| 45 | AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New | VariableKind.Out));
|
---|
[2] | 46 | }
|
---|
| 47 |
|
---|
| 48 | public override IOperation Apply(IScope scope) {
|
---|
| 49 | IRandom random = GetVariableValue<IRandom>("Random", scope, true);
|
---|
| 50 | GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
|
---|
| 51 | int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
|
---|
| 52 | int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
|
---|
[23] | 53 | double balancedTreesRate = GetVariableValue<DoubleData>("BalancedTreesRate", scope, true).Data;
|
---|
[2] | 54 |
|
---|
| 55 | TreeGardener gardener = new TreeGardener(random, opLibrary);
|
---|
| 56 |
|
---|
| 57 | int treeHeight = random.Next(1, maxTreeHeight + 1);
|
---|
| 58 | int treeSize = random.Next(1, maxTreeSize + 1);
|
---|
[155] | 59 | IFunctionTree root;
|
---|
[23] | 60 | if(random.NextDouble() <= balancedTreesRate) {
|
---|
[155] | 61 | root = gardener.CreateRandomTree(treeSize, treeHeight, true);
|
---|
[23] | 62 | } else {
|
---|
[155] | 63 | root = gardener.CreateRandomTree(treeSize, treeHeight, false);
|
---|
[23] | 64 | }
|
---|
[2] | 65 |
|
---|
[155] | 66 | int actualTreeSize = gardener.GetTreeSize(root);
|
---|
| 67 | int actualTreeHeight = gardener.GetTreeHeight(root);
|
---|
[2] | 68 |
|
---|
[155] | 69 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), root));
|
---|
| 70 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(actualTreeSize)));
|
---|
| 71 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(actualTreeHeight)));
|
---|
[2] | 72 |
|
---|
[155] | 73 | if(!gardener.IsValidTree(root)) { throw new InvalidProgramException(); }
|
---|
[2] | 74 |
|
---|
| 75 | if(actualTreeSize > maxTreeSize ||
|
---|
| 76 | actualTreeHeight > maxTreeHeight) {
|
---|
| 77 | throw new InvalidProgramException();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[155] | 80 | return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(root), scope);
|
---|
[2] | 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|