source: branches/2936_GQAPIntegration/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/StrategyParameters/StdDevStrategyVectorCrossover.cs @ 16710

Last change on this file since 16710 was 16710, checked in by gkronber, 3 months ago

#2936: merged r15682 & r16117:16658 from trunk/HeuristicLab.Encodings.IntegerVectorEncoding to branch/HeuristicLab.Encodings.IntegerVectorEncoding

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HEAL.Attic;
29
30namespace HeuristicLab.Encodings.IntegerVectorEncoding {
31  [Item("StdDevStrategyVectorCrossover", "Crosses the strategy vector by using intermediate recombination (average crossover).")]
32  [StorableType("27583F67-4C80-4059-B4F9-80587C0DFB13")]
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(StorableConstructorFlag _) : base(_) { }
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}
Note: See TracBrowser for help on using the repository browser.