Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SymbolicExpressionTreeCrossover.cs @ 18242

Last change on this file since 18242 was 17209, checked in by gkronber, 5 years ago

#2994: merged r17132:17198 from trunk to branch

File size: 3.6 KB
RevLine 
[3223]1#region License Information
2/* HeuristicLab
[17209]3 * Copyright (C) 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.Parameters;
[16565]26using HEAL.Attic;
[3223]27
[5499]28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[3223]29  /// <summary>
[3237]30  /// A base class for operators that perform a crossover of symbolic expression trees.
[3223]31  /// </summary>
[3237]32  [Item("SymbolicExpressionTreeCrossover", "A base class for operators that perform a crossover of symbolic expression trees.")]
[16565]33  [StorableType("AB6004AE-B6ED-41D7-824E-87ECDA5B0AAF")]
[3534]34  public abstract class SymbolicExpressionTreeCrossover : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCrossover {
[3294]35    private const string ParentsParameterName = "Parents";
[5499]36    #region Parameter Properties
[5510]37    public ILookupParameter<ItemArray<ISymbolicExpressionTree>> ParentsParameter {
38      get { return (ScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[ParentsParameterName]; }
[3223]39    }
[5499]40    #endregion
41    #region Properties
[12422]42    private ItemArray<ISymbolicExpressionTree> Parents {
[5499]43      get { return ParentsParameter.ActualValue; }
[3223]44    }
[12422]45    private ISymbolicExpressionTree Child {
46      get { return SymbolicExpressionTreeParameter.ActualValue; }
47      set { SymbolicExpressionTreeParameter.ActualValue = value; }
[3237]48    }
[5499]49    #endregion
[4722]50    [StorableConstructor]
[16565]51    protected SymbolicExpressionTreeCrossover(StorableConstructorFlag _) : base(_) { }
[4722]52    protected SymbolicExpressionTreeCrossover(SymbolicExpressionTreeCrossover original, Cloner cloner) : base(original, cloner) { }
[3237]53    protected SymbolicExpressionTreeCrossover()
[3223]54      : base() {
[5510]55      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed."));
[8344]56      ParentsParameter.ActualName = "SymbolicExpressionTree";
[3223]57    }
58
[10291]59    public sealed override IOperation InstrumentedApply() {
[5499]60      if (Parents.Length != 2)
[3237]61        throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators.");
62
[12422]63      ISymbolicExpressionTree result = Crossover(RandomParameter.ActualValue, Parents[0], Parents[1]);
[3237]64
[5499]65      Child = result;
[10291]66      return base.InstrumentedApply();
[3223]67    }
68
[7506]69    public abstract ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1);
[12480]70
71
72    [StorableHook(HookType.AfterDeserialization)]
73    private void AfterDeserialization() {
74      // BackwardsCompatibility3.4
75      #region Backwards compatible code, remove with 3.5
76      if (Parameters.ContainsKey("Child")) {
77        var oldChildParameter = (ILookupParameter<ISymbolicExpressionTree>)Parameters["Child"];
78        Parameters.Remove("Child");
79        SymbolicExpressionTreeParameter.ActualName = oldChildParameter.ActualName;
80      }
81      #endregion
82    }
[3223]83  }
[3294]84}
Note: See TracBrowser for help on using the repository browser.