#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
///
/// A base class for operators that perform a crossover of symbolic expression trees.
///
[Item("SymbolicExpressionTreeCrossover", "A base class for operators that perform a crossover of symbolic expression trees.")]
[StorableClass]
public abstract class SymbolicExpressionTreeCrossover : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCrossover {
private const string ParentsParameterName = "Parents";
#region Parameter Properties
public ILookupParameter> ParentsParameter {
get { return (ScopeTreeLookupParameter)Parameters[ParentsParameterName]; }
}
#endregion
#region Properties
private ItemArray Parents {
get { return ParentsParameter.ActualValue; }
}
private ISymbolicExpressionTree Child {
get { return SymbolicExpressionTreeParameter.ActualValue; }
set { SymbolicExpressionTreeParameter.ActualValue = value; }
}
#endregion
[StorableConstructor]
protected SymbolicExpressionTreeCrossover(bool deserializing) : base(deserializing) { }
protected SymbolicExpressionTreeCrossover(SymbolicExpressionTreeCrossover original, Cloner cloner) : base(original, cloner) { }
protected SymbolicExpressionTreeCrossover()
: base() {
Parameters.Add(new ScopeTreeLookupParameter(ParentsParameterName, "The parent symbolic expression trees which should be crossed."));
ParentsParameter.ActualName = "SymbolicExpressionTree";
}
public sealed override IOperation InstrumentedApply() {
if (Parents.Length != 2)
throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators.");
ISymbolicExpressionTree result = Crossover(RandomParameter.ActualValue, Parents[0], Parents[1]);
Child = result;
return base.InstrumentedApply();
}
public abstract ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1);
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
// BackwardsCompatibility3.4
#region Backwards compatible code, remove with 3.5
if (Parameters.ContainsKey("Child")) {
var oldChildParameter = (ILookupParameter)Parameters["Child"];
Parameters.Remove("Child");
SymbolicExpressionTreeParameter.ActualName = oldChildParameter.ActualName;
}
#endregion
}
}
}