[3223] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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.Operators;
|
---|
| 25 | using HeuristicLab.Optimization;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3237] | 28 | using System;
|
---|
| 29 | using System.Diagnostics;
|
---|
[3223] | 30 |
|
---|
| 31 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
| 32 | /// <summary>
|
---|
[3237] | 33 | /// A base class for operators that perform a crossover of symbolic expression trees.
|
---|
[3223] | 34 | /// </summary>
|
---|
[3237] | 35 | [Item("SymbolicExpressionTreeCrossover", "A base class for operators that perform a crossover of symbolic expression trees.")]
|
---|
[3223] | 36 | [StorableClass]
|
---|
[3239] | 37 | public abstract class SymbolicExpressionTreeCrossover : SingleSuccessorOperator, ICrossover, IStochasticOperator, ISymbolicExpressionTreeOperator {
|
---|
[3223] | 38 | public override bool CanChangeName {
|
---|
| 39 | get { return false; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 43 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 44 | }
|
---|
[3237] | 45 | public ILookupParameter<ItemArray<SymbolicExpressionTree>> ParentsParameter {
|
---|
| 46 | get { return (SubScopesLookupParameter<SymbolicExpressionTree>)Parameters["Parents"]; }
|
---|
[3223] | 47 | }
|
---|
[3237] | 48 | public ILookupParameter<SymbolicExpressionTree> ChildParameter {
|
---|
| 49 | get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["Child"]; }
|
---|
[3223] | 50 | }
|
---|
[3237] | 51 | public IValueLookupParameter<IntValue> MaxTreeSizeParameter {
|
---|
| 52 | get { return (IValueLookupParameter<IntValue>)Parameters["MaxTreeSize"]; }
|
---|
[3223] | 53 | }
|
---|
[3237] | 54 | public IValueLookupParameter<IntValue> MaxTreeHeightParameter {
|
---|
| 55 | get { return (IValueLookupParameter<IntValue>)Parameters["MaxTreeHeight"]; }
|
---|
| 56 | }
|
---|
| 57 | public ILookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionGrammarParameter {
|
---|
| 58 | get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters["SymbolicExpressionGrammar"]; }
|
---|
| 59 | }
|
---|
[3223] | 60 |
|
---|
[3237] | 61 | protected SymbolicExpressionTreeCrossover()
|
---|
[3223] | 62 | : base() {
|
---|
| 63 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
|
---|
[3237] | 64 | Parameters.Add(new SubScopesLookupParameter<SymbolicExpressionTree>("Parents", "The parent symbolic expression trees which should be crossed."));
|
---|
| 65 | Parameters.Add(new ValueLookupParameter<IntValue>("MaxTreeSize", "The maximal size (number of nodes) of the symbolic expression tree that should be initialized."));
|
---|
| 66 | Parameters.Add(new ValueLookupParameter<IntValue>("MaxTreeHeight", "The maximal height of the symbolic expression tree that should be initialized (a tree with one node has height = 0)."));
|
---|
| 67 | Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>("SymbolicExpressionGrammar", "The grammar that defines the allowed symbols and syntax of the symbolic expression trees."));
|
---|
| 68 | Parameters.Add(new LookupParameter<SymbolicExpressionTree>("Child", "The child symbolic expression tree resulting from the crossover."));
|
---|
[3223] | 69 | }
|
---|
| 70 |
|
---|
| 71 | public sealed override IOperation Apply() {
|
---|
[3237] | 72 | if (ParentsParameter.ActualValue.Length != 2)
|
---|
| 73 | throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators.");
|
---|
| 74 |
|
---|
| 75 | SymbolicExpressionTree parent0 = ParentsParameter.ActualValue[0];
|
---|
| 76 | SymbolicExpressionTree parent1 = ParentsParameter.ActualValue[1];
|
---|
| 77 |
|
---|
| 78 | IRandom random = RandomParameter.ActualValue;
|
---|
| 79 | ISymbolicExpressionGrammar grammar = SymbolicExpressionGrammarParameter.ActualValue;
|
---|
| 80 |
|
---|
| 81 | // randomly swap parents to remove a possible bias from selection (e.g. when using gender-specific selection)
|
---|
| 82 | if (random.NextDouble() < 0.5) {
|
---|
| 83 | var tmp = parent0;
|
---|
| 84 | parent0 = parent1;
|
---|
| 85 | parent1 = tmp;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | SymbolicExpressionTree result = Cross(random, grammar, parent0, parent1,
|
---|
| 89 | MaxTreeSizeParameter.ActualValue, MaxTreeHeightParameter.ActualValue);
|
---|
| 90 | Debug.Assert(result.Size <= MaxTreeSizeParameter.ActualValue.Value);
|
---|
| 91 | Debug.Assert(result.Height <= MaxTreeHeightParameter.ActualValue.Value);
|
---|
| 92 | Debug.Assert(grammar.IsValidExpression(result));
|
---|
[3223] | 93 | ChildParameter.ActualValue = result;
|
---|
| 94 | return base.Apply();
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[3237] | 97 | protected abstract SymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionGrammar grammar,
|
---|
| 98 | SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
|
---|
| 99 | IntValue maxTreeSize, IntValue maxTreeHeight);
|
---|
[3223] | 100 | }
|
---|
| 101 | }
|
---|