[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 |
|
---|
[4068] | 22 | using System;
|
---|
[3223] | 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
[4068] | 25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
|
---|
[3223] | 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
[3462] | 29 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers {
|
---|
[3223] | 30 | /// <summary>
|
---|
[3237] | 31 | /// A base class for operators that perform a crossover of symbolic expression trees.
|
---|
[3223] | 32 | /// </summary>
|
---|
[3237] | 33 | [Item("SymbolicExpressionTreeCrossover", "A base class for operators that perform a crossover of symbolic expression trees.")]
|
---|
[3223] | 34 | [StorableClass]
|
---|
[3534] | 35 | public abstract class SymbolicExpressionTreeCrossover : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCrossover {
|
---|
[3294] | 36 | private const string ParentsParameterName = "Parents";
|
---|
| 37 | private const string ChildParameterName = "Child";
|
---|
| 38 | private const string FailedCrossoverEventsParameterName = "FailedCrossoverEvents";
|
---|
[3237] | 39 | public ILookupParameter<ItemArray<SymbolicExpressionTree>> ParentsParameter {
|
---|
[3659] | 40 | get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[ParentsParameterName]; }
|
---|
[3223] | 41 | }
|
---|
[3237] | 42 | public ILookupParameter<SymbolicExpressionTree> ChildParameter {
|
---|
[3294] | 43 | get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[ChildParameterName]; }
|
---|
[3223] | 44 | }
|
---|
[3294] | 45 | public IValueParameter<IntValue> FailedCrossoverEventsParameter {
|
---|
| 46 | get { return (ValueParameter<IntValue>)Parameters[FailedCrossoverEventsParameterName]; }
|
---|
[3223] | 47 | }
|
---|
[3294] | 48 |
|
---|
| 49 | public IntValue FailedCrossoverEvents {
|
---|
| 50 | get { return FailedCrossoverEventsParameter.Value; }
|
---|
[3237] | 51 | }
|
---|
| 52 | protected SymbolicExpressionTreeCrossover()
|
---|
[3223] | 53 | : base() {
|
---|
[3659] | 54 | Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed."));
|
---|
[3294] | 55 | Parameters.Add(new LookupParameter<SymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover."));
|
---|
| 56 | Parameters.Add(new ValueParameter<IntValue>(FailedCrossoverEventsParameterName, "The number of failed crossover events (child is an exact copy of a parent)", new IntValue()));
|
---|
[3223] | 57 | }
|
---|
| 58 |
|
---|
| 59 | public sealed override IOperation Apply() {
|
---|
[3237] | 60 | if (ParentsParameter.ActualValue.Length != 2)
|
---|
| 61 | throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators.");
|
---|
| 62 |
|
---|
| 63 | SymbolicExpressionTree parent0 = ParentsParameter.ActualValue[0];
|
---|
| 64 | SymbolicExpressionTree parent1 = ParentsParameter.ActualValue[1];
|
---|
| 65 |
|
---|
| 66 | IRandom random = RandomParameter.ActualValue;
|
---|
| 67 |
|
---|
[3294] | 68 | bool success;
|
---|
[3338] | 69 | SymbolicExpressionTree result = Cross(random, parent0, parent1,
|
---|
[3294] | 70 | MaxTreeSizeParameter.ActualValue, MaxTreeHeightParameter.ActualValue, out success);
|
---|
[4068] | 71 |
|
---|
[3294] | 72 | if (!success) FailedCrossoverEvents.Value++;
|
---|
| 73 |
|
---|
[3223] | 74 | ChildParameter.ActualValue = result;
|
---|
| 75 | return base.Apply();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[4068] | 78 | protected abstract SymbolicExpressionTree Cross(IRandom random,
|
---|
[3237] | 79 | SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
|
---|
[3294] | 80 | IntValue maxTreeSize, IntValue maxTreeHeight, out bool success);
|
---|
[3223] | 81 | }
|
---|
[3294] | 82 | } |
---|