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