[8185] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8185] | 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 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Encodings.IntegerVectorEncoding {
|
---|
| 31 | [Item("StdDevStrategyVectorCrossover", "Crosses the strategy vector by using intermediate recombination (average crossover).")]
|
---|
| 32 | [StorableClass]
|
---|
| 33 | public class StdDevStrategyVectorCrossover : SingleSuccessorOperator, IStochasticOperator, IIntegerVectorStdDevStrategyParameterCrossover {
|
---|
| 34 | public override bool CanChangeName {
|
---|
| 35 | get { return false; }
|
---|
| 36 | }
|
---|
| 37 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 38 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 39 | }
|
---|
| 40 | public ILookupParameter<ItemArray<DoubleArray>> ParentsParameter {
|
---|
| 41 | get { return (ILookupParameter<ItemArray<DoubleArray>>)Parameters["ParentStrategyParameter"]; }
|
---|
| 42 | }
|
---|
| 43 | public ILookupParameter<DoubleArray> StrategyParameterParameter {
|
---|
| 44 | get { return (ILookupParameter<DoubleArray>)Parameters["StrategyParameter"]; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | [StorableConstructor]
|
---|
| 48 | protected StdDevStrategyVectorCrossover(bool deserializing) : base(deserializing) { }
|
---|
| 49 | protected StdDevStrategyVectorCrossover(StdDevStrategyVectorCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
| 50 | public StdDevStrategyVectorCrossover()
|
---|
| 51 | : base() {
|
---|
| 52 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
| 53 | Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("ParentStrategyParameter", "The strategy parameters to cross."));
|
---|
| 54 | Parameters.Add(new LookupParameter<DoubleArray>("StrategyParameter", "The crossed strategy parameter."));
|
---|
| 55 | ParentsParameter.ActualName = "StrategyParameter";
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 59 | return new StdDevStrategyVectorCrossover(this, cloner);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public override IOperation Apply() {
|
---|
| 63 | StrategyParameterParameter.ActualValue = Average(RandomParameter.ActualValue, ParentsParameter.ActualValue);
|
---|
| 64 | return base.Apply();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | private DoubleArray Average(IRandom random, ItemArray<DoubleArray> parents) {
|
---|
| 68 | int length = parents[0].Length;
|
---|
| 69 | var result = new DoubleArray(length);
|
---|
| 70 | for (int i = 0; i < length; i++) {
|
---|
| 71 | for (int p = 0; p < parents.Length; p++) {
|
---|
| 72 | result[i] += parents[p][i];
|
---|
| 73 | }
|
---|
| 74 | result[i] /= parents.Length;
|
---|
| 75 | }
|
---|
| 76 | return result;
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|