[645] | 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 HeuristicLab.Core;
|
---|
| 23 | using HeuristicLab.Data;
|
---|
| 24 | using HeuristicLab.Random;
|
---|
[2210] | 25 | using HeuristicLab.GP.Interfaces;
|
---|
[645] | 26 |
|
---|
[2212] | 27 | namespace HeuristicLab.GP.Operators {
|
---|
[645] | 28 | public class ProbabilisticTreeCreator : OperatorBase {
|
---|
[2447] | 29 | private static int MAX_TRIES { get { return 100; } }
|
---|
[1198] | 30 |
|
---|
[645] | 31 | public override string Description {
|
---|
| 32 | get { return @"Generates a new random operator tree."; }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public ProbabilisticTreeCreator()
|
---|
| 36 | : base() {
|
---|
| 37 | AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));
|
---|
[2210] | 38 | AddVariableInfo(new VariableInfo("FunctionLibrary", "The function library containing all available functions", typeof(FunctionLibrary), VariableKind.In));
|
---|
[645] | 39 | AddVariableInfo(new VariableInfo("MinTreeSize", "The minimal allowed size of the tree", typeof(IntData), VariableKind.In));
|
---|
| 40 | AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size of the tree", typeof(IntData), VariableKind.In));
|
---|
| 41 | AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
|
---|
[2210] | 42 | AddVariableInfo(new VariableInfo("FunctionTree", "The created tree", typeof(IGeneticProgrammingModel), VariableKind.New | VariableKind.Out));
|
---|
[645] | 43 | }
|
---|
| 44 |
|
---|
| 45 | public override IOperation Apply(IScope scope) {
|
---|
| 46 | IRandom random = GetVariableValue<IRandom>("Random", scope, true);
|
---|
[2210] | 47 | FunctionLibrary funLibrary = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true);
|
---|
[645] | 48 | int minTreeSize = GetVariableValue<IntData>("MinTreeSize", scope, true).Data;
|
---|
| 49 | int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
|
---|
| 50 | int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
|
---|
| 51 |
|
---|
[2447] | 52 | IFunctionTree root = Create(random, funLibrary, minTreeSize, maxTreeSize, maxTreeHeight);
|
---|
| 53 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), new GeneticProgrammingModel(root)));
|
---|
| 54 | return Util.CreateInitializationOperation(TreeGardener.GetAllSubTrees(root), scope);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | public static IFunctionTree Create(IRandom random, FunctionLibrary funLib, int minSize, int maxSize, int maxHeight) {
|
---|
| 59 | int treeSize = random.Next(minSize, maxSize + 1);
|
---|
[1198] | 60 | IFunctionTree root;
|
---|
| 61 | int tries = 0;
|
---|
[2447] | 62 | TreeGardener gardener = new TreeGardener(random, funLib);
|
---|
[1198] | 63 | do {
|
---|
[2447] | 64 | root = gardener.PTC2(treeSize, maxHeight);
|
---|
[1198] | 65 | if (tries++ >= MAX_TRIES) {
|
---|
| 66 | // try a different size
|
---|
[2447] | 67 | treeSize = random.Next(minSize, maxSize + 1);
|
---|
[1198] | 68 | tries = 0;
|
---|
| 69 | }
|
---|
[2447] | 70 | } while (root.GetSize() > maxHeight || root.GetHeight() > maxHeight);
|
---|
| 71 | return root;
|
---|
[2210] | 72 | }
|
---|
[2447] | 73 | }
|
---|
[645] | 74 | } |
---|