#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 HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Random;
using System;
using System.Linq;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using System.Collections.Generic;
namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[StorableClass]
[Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed size")]
public class ProbabilisticTreeCreator : SymbolicExpressionTreeCreator {
public ProbabilisticTreeCreator()
: base() {
}
protected override SymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight) {
return Apply(random, grammar, maxTreeSize.Value, maxTreeHeight.Value);
}
public SymbolicExpressionTree Apply(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight) {
// tree size is limited by the grammar and by the explicit size constraints
int allowedMinSize = grammar.MinimalExpressionLength(grammar.StartSymbol);
int allowedMaxSize = Math.Min(maxTreeSize, grammar.MaximalExpressionLength(grammar.StartSymbol));
// select a target tree size uniformly in the possible range (as determined by explicit limits and limits of the grammar)
int treeSize = random.Next(allowedMinSize, allowedMaxSize);
SymbolicExpressionTree tree = new SymbolicExpressionTree();
do {
try {
tree.Root = PTC2(random, grammar, grammar.StartSymbol, treeSize + 1, maxTreeHeight + 1);
}
catch (ArgumentException) {
// try a different size
treeSize = random.Next(allowedMinSize, allowedMaxSize);
}
} while (tree.Root == null || tree.Size > maxTreeSize || tree.Height > maxTreeHeight);
return tree;
}
private Symbol SelectRandomSymbol(IRandom random, IEnumerable symbols) {
var symbolList = symbols.ToList();
var ticketsSum = symbolList.Select(x => x.Tickets.Value).Sum();
var r = random.NextDouble() * ticketsSum;
double aggregatedTickets = 0;
for (int i = 0; i < symbolList.Count; i++) {
aggregatedTickets += symbolList[i].Tickets.Value;
if (aggregatedTickets >= r) {
return symbolList[i];
}
}
// this should never happen
throw new ArgumentException();
}
private SymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionGrammar grammar, Symbol rootSymbol, int size, int maxDepth) {
SymbolicExpressionTreeNode root = rootSymbol.CreateTreeNode();
if (size <= 1 || maxDepth <= 1) return root;
List