Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6307 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 4.2 KB
RevLine 
[3223]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3223]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]22using System;
[4722]23using HeuristicLab.Common;
[3223]24using HeuristicLab.Core;
25using HeuristicLab.Data;
[4068]26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
[3223]27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
[3462]30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers {
[3223]31  /// <summary>
[3237]32  /// A base class for operators that perform a crossover of symbolic expression trees.
[3223]33  /// </summary>
[3237]34  [Item("SymbolicExpressionTreeCrossover", "A base class for operators that perform a crossover of symbolic expression trees.")]
[3223]35  [StorableClass]
[3534]36  public abstract class SymbolicExpressionTreeCrossover : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCrossover {
[3294]37    private const string ParentsParameterName = "Parents";
38    private const string ChildParameterName = "Child";
39    private const string FailedCrossoverEventsParameterName = "FailedCrossoverEvents";
[3237]40    public ILookupParameter<ItemArray<SymbolicExpressionTree>> ParentsParameter {
[3659]41      get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[ParentsParameterName]; }
[3223]42    }
[3237]43    public ILookupParameter<SymbolicExpressionTree> ChildParameter {
[3294]44      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[ChildParameterName]; }
[3223]45    }
[3294]46    public IValueParameter<IntValue> FailedCrossoverEventsParameter {
47      get { return (ValueParameter<IntValue>)Parameters[FailedCrossoverEventsParameterName]; }
[3223]48    }
[3294]49
50    public IntValue FailedCrossoverEvents {
51      get { return FailedCrossoverEventsParameter.Value; }
[3237]52    }
[4722]53    [StorableConstructor]
54    protected SymbolicExpressionTreeCrossover(bool deserializing) : base(deserializing) { }
55    protected SymbolicExpressionTreeCrossover(SymbolicExpressionTreeCrossover original, Cloner cloner) : base(original, cloner) { }
[3237]56    protected SymbolicExpressionTreeCrossover()
[3223]57      : base() {
[3659]58      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed."));
[3294]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()));
[3223]61    }
62
63    public sealed override IOperation Apply() {
[3237]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
[3294]72      bool success;
[3338]73      SymbolicExpressionTree result = Cross(random, parent0, parent1,
[3294]74        MaxTreeSizeParameter.ActualValue, MaxTreeHeightParameter.ActualValue, out success);
[4068]75
[3294]76      if (!success) FailedCrossoverEvents.Value++;
77
[3223]78      ChildParameter.ActualValue = result;
79      return base.Apply();
80    }
81
[4068]82    protected abstract SymbolicExpressionTree Cross(IRandom random,
[3237]83      SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
[3294]84      IntValue maxTreeSize, IntValue maxTreeHeight, out bool success);
[3223]85  }
[3294]86}
Note: See TracBrowser for help on using the repository browser.