[2900] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2900] | 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 |
|
---|
[5434] | 22 | using System;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[2900] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
[3053] | 31 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
[2900] | 32 | /// <summary>
|
---|
| 33 | /// A base class for operators that perform a crossover of real-valued vectors.
|
---|
| 34 | /// </summary>
|
---|
| 35 | [Item("RealVectorCrossover", "A base class for operators that perform a crossover of real-valued vectors.")]
|
---|
[3017] | 36 | [StorableClass]
|
---|
[2900] | 37 | public abstract class RealVectorCrossover : SingleSuccessorOperator, IRealVectorCrossover, IStochasticOperator {
|
---|
[3034] | 38 | public override bool CanChangeName {
|
---|
| 39 | get { return false; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[2900] | 42 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 43 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 44 | }
|
---|
[3060] | 45 | public ILookupParameter<ItemArray<RealVector>> ParentsParameter {
|
---|
[3659] | 46 | get { return (ScopeTreeLookupParameter<RealVector>)Parameters["Parents"]; }
|
---|
[2900] | 47 | }
|
---|
[3060] | 48 | public ILookupParameter<RealVector> ChildParameter {
|
---|
| 49 | get { return (ILookupParameter<RealVector>)Parameters["Child"]; }
|
---|
[2900] | 50 | }
|
---|
[3182] | 51 | public IValueLookupParameter<DoubleMatrix> BoundsParameter {
|
---|
| 52 | get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
| 53 | }
|
---|
[5434] | 54 | protected OptionalValueParameter<IRealVectorBoundsChecker> BoundsCheckerParameter {
|
---|
[5381] | 55 | get { return (OptionalValueParameter<IRealVectorBoundsChecker>)Parameters["BoundsChecker"]; }
|
---|
| 56 | }
|
---|
[5434] | 57 | public IRealVectorBoundsChecker BoundsChecker {
|
---|
| 58 | get { return BoundsCheckerParameter.Value; }
|
---|
| 59 | set { BoundsCheckerParameter.Value = value; }
|
---|
| 60 | }
|
---|
[2900] | 61 |
|
---|
[4722] | 62 | [StorableConstructor]
|
---|
| 63 | protected RealVectorCrossover(bool deserializing) : base(deserializing) { }
|
---|
[5434] | 64 | protected RealVectorCrossover(RealVectorCrossover original, Cloner cloner)
|
---|
| 65 | : base(original, cloner) {
|
---|
| 66 | RegisterEventHandlers();
|
---|
| 67 | }
|
---|
[2900] | 68 | protected RealVectorCrossover()
|
---|
| 69 | : base() {
|
---|
| 70 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
|
---|
[3659] | 71 | Parameters.Add(new ScopeTreeLookupParameter<RealVector>("Parents", "The parent vectors which should be crossed."));
|
---|
[3910] | 72 | ParentsParameter.ActualName = "RealVector";
|
---|
[3060] | 73 | Parameters.Add(new LookupParameter<RealVector>("Child", "The child vector resulting from the crossover."));
|
---|
[3910] | 74 | ChildParameter.ActualName = "RealVector";
|
---|
[3182] | 75 | Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds of the real vector."));
|
---|
[5381] | 76 | Parameters.Add(new OptionalValueParameter<IRealVectorBoundsChecker>("BoundsChecker", "The bounds checker that ensures that the values stay within the bounds.", new BoundsChecker()));
|
---|
[5434] | 77 |
|
---|
| 78 | RegisterEventHandlers();
|
---|
| 79 | ParameterizeBoundsChecker();
|
---|
[2900] | 80 | }
|
---|
| 81 |
|
---|
[5381] | 82 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 83 | private void AfterDeserialization() {
|
---|
[5434] | 84 | // BackwardsCompatibility3.3
|
---|
| 85 | #region Backwards compatible code (remove with 3.4)
|
---|
| 86 | if (!Parameters.ContainsKey("BoundsChecker")) {
|
---|
[5381] | 87 | Parameters.Add(new OptionalValueParameter<IRealVectorBoundsChecker>("BoundsChecker", "The bounds checker that ensures that the values stay within the bounds.", new BoundsChecker()));
|
---|
[5434] | 88 | ParameterizeBoundsChecker();
|
---|
| 89 | }
|
---|
| 90 | #endregion
|
---|
| 91 | RegisterEventHandlers();
|
---|
[5381] | 92 | }
|
---|
| 93 |
|
---|
[5434] | 94 | protected virtual void RegisterEventHandlers() {
|
---|
| 95 | BoundsCheckerParameter.ValueChanged += new System.EventHandler(BoundsCheckerParameter_ValueChanged);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[2900] | 98 | public sealed override IOperation Apply() {
|
---|
[3182] | 99 | RealVector result = Cross(RandomParameter.ActualValue, ParentsParameter.ActualValue);
|
---|
[5382] | 100 | ChildParameter.ActualValue = result;
|
---|
[5381] | 101 |
|
---|
| 102 | IOperation successor = base.Apply();
|
---|
[5434] | 103 | if (BoundsChecker != null) {
|
---|
| 104 | IOperation checkerOperation = ExecutionContext.CreateChildOperation(BoundsChecker);
|
---|
[5381] | 105 | if (successor == null) return checkerOperation;
|
---|
| 106 | else return new OperationCollection(checkerOperation, successor);
|
---|
| 107 | } else return successor;
|
---|
[2900] | 108 | }
|
---|
| 109 |
|
---|
[3060] | 110 | protected abstract RealVector Cross(IRandom random, ItemArray<RealVector> parents);
|
---|
[5434] | 111 |
|
---|
| 112 | protected virtual void BoundsCheckerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 113 | ParameterizeBoundsChecker();
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | protected virtual void ParameterizeBoundsChecker() {
|
---|
| 117 | if (BoundsChecker != null) {
|
---|
| 118 | BoundsChecker.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
| 119 | BoundsChecker.RealVectorParameter.ActualName = ChildParameter.Name;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
[2900] | 122 | }
|
---|
| 123 | }
|
---|