Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SymbolicExpressionTreeCrossover.cs @ 3462

Last change on this file since 3462 was 3462, checked in by gkronber, 14 years ago

Refactored symbolic expression tree encoding and problem classes for symbolic regression. #937 , #938

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