#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Linq;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using System;
using HeuristicLab.Random;
using System.Diagnostics;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.GP {
public class ProbabilisticTreeCreator : OperatorBase {
private int MAX_TRIES { get { return 100; } }
public override string Description {
get { return @"Generates a new random operator tree."; }
}
public ProbabilisticTreeCreator()
: base() {
AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));
AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));
AddVariableInfo(new VariableInfo("MinTreeSize", "The minimal allowed size of the tree", typeof(IntData), VariableKind.In));
AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size of the tree", typeof(IntData), VariableKind.In));
AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
AddVariableInfo(new VariableInfo("FunctionTree", "The created tree", typeof(IFunctionTree), VariableKind.New | VariableKind.Out));
AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New | VariableKind.Out));
AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New | VariableKind.Out));
}
public override IOperation Apply(IScope scope) {
IRandom random = GetVariableValue("Random", scope, true);
GPOperatorLibrary opLibrary = GetVariableValue("OperatorLibrary", scope, true);
int minTreeSize = GetVariableValue("MinTreeSize", scope, true).Data;
int maxTreeSize = GetVariableValue("MaxTreeSize", scope, true).Data;
int maxTreeHeight = GetVariableValue("MaxTreeHeight", scope, true).Data;
TreeGardener gardener = new TreeGardener(random, opLibrary);
int treeSize = random.Next(minTreeSize, maxTreeSize + 1);
IFunctionTree root;
int actualTreeSize;
int actualTreeHeight;
int tries = 0;
do {
root = gardener.PTC2(random, treeSize, maxTreeHeight);
actualTreeSize = root.Size;
actualTreeHeight = root.Height;
if (tries++ >= MAX_TRIES) {
// try a different size
treeSize = random.Next(minTreeSize, maxTreeSize + 1);
tries = 0;
}
} while (actualTreeSize > maxTreeSize || actualTreeHeight > maxTreeHeight);
scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), root));
scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(actualTreeSize)));
scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(actualTreeHeight)));
return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(root), scope);
}
}
}