[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]
|
---|
[3294] | 37 | public abstract class SymbolicExpressionTreeCrossover : SymbolicExpressionTreeOperator, ICrossover {
|
---|
| 38 | private const string ParentsParameterName = "Parents";
|
---|
| 39 | private const string ChildParameterName = "Child";
|
---|
| 40 | private const string FailedCrossoverEventsParameterName = "FailedCrossoverEvents";
|
---|
[3237] | 41 | public ILookupParameter<ItemArray<SymbolicExpressionTree>> ParentsParameter {
|
---|
[3294] | 42 | get { return (SubScopesLookupParameter<SymbolicExpressionTree>)Parameters[ParentsParameterName]; }
|
---|
[3223] | 43 | }
|
---|
[3237] | 44 | public ILookupParameter<SymbolicExpressionTree> ChildParameter {
|
---|
[3294] | 45 | get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[ChildParameterName]; }
|
---|
[3223] | 46 | }
|
---|
[3294] | 47 | public IValueParameter<IntValue> FailedCrossoverEventsParameter {
|
---|
| 48 | get { return (ValueParameter<IntValue>)Parameters[FailedCrossoverEventsParameterName]; }
|
---|
[3223] | 49 | }
|
---|
[3294] | 50 |
|
---|
| 51 | public IntValue FailedCrossoverEvents {
|
---|
| 52 | get { return FailedCrossoverEventsParameter.Value; }
|
---|
[3237] | 53 | }
|
---|
| 54 | protected SymbolicExpressionTreeCrossover()
|
---|
[3223] | 55 | : base() {
|
---|
[3294] | 56 | Parameters.Add(new SubScopesLookupParameter<SymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed."));
|
---|
| 57 | Parameters.Add(new LookupParameter<SymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover."));
|
---|
| 58 | Parameters.Add(new ValueParameter<IntValue>(FailedCrossoverEventsParameterName, "The number of failed crossover events (child is an exact copy of a parent)", new IntValue()));
|
---|
[3223] | 59 | }
|
---|
| 60 |
|
---|
| 61 | public sealed override IOperation Apply() {
|
---|
[3237] | 62 | if (ParentsParameter.ActualValue.Length != 2)
|
---|
| 63 | throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators.");
|
---|
| 64 |
|
---|
| 65 | SymbolicExpressionTree parent0 = ParentsParameter.ActualValue[0];
|
---|
| 66 | SymbolicExpressionTree parent1 = ParentsParameter.ActualValue[1];
|
---|
| 67 |
|
---|
| 68 | IRandom random = RandomParameter.ActualValue;
|
---|
| 69 | ISymbolicExpressionGrammar grammar = SymbolicExpressionGrammarParameter.ActualValue;
|
---|
| 70 |
|
---|
| 71 | // randomly swap parents to remove a possible bias from selection (e.g. when using gender-specific selection)
|
---|
| 72 | if (random.NextDouble() < 0.5) {
|
---|
| 73 | var tmp = parent0;
|
---|
| 74 | parent0 = parent1;
|
---|
| 75 | parent1 = tmp;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[3294] | 78 | bool success;
|
---|
[3237] | 79 | SymbolicExpressionTree result = Cross(random, grammar, parent0, parent1,
|
---|
[3294] | 80 | MaxTreeSizeParameter.ActualValue, MaxTreeHeightParameter.ActualValue, out success);
|
---|
| 81 |
|
---|
| 82 | if (!success) FailedCrossoverEvents.Value++;
|
---|
| 83 |
|
---|
[3237] | 84 | Debug.Assert(result.Size <= MaxTreeSizeParameter.ActualValue.Value);
|
---|
| 85 | Debug.Assert(result.Height <= MaxTreeHeightParameter.ActualValue.Value);
|
---|
[3294] | 86 | // Debug.Assert(grammar.IsValidExpression(result));
|
---|
[3223] | 87 | ChildParameter.ActualValue = result;
|
---|
| 88 | return base.Apply();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[3237] | 91 | protected abstract SymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionGrammar grammar,
|
---|
| 92 | SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
|
---|
[3294] | 93 | IntValue maxTreeSize, IntValue maxTreeHeight, out bool success);
|
---|
[3223] | 94 | }
|
---|
[3294] | 95 | } |
---|