1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.RealVector {
|
---|
29 | public class SelfAdaptiveDiscreteRecombination : DiscreteRecombination {
|
---|
30 | public override string Description {
|
---|
31 | get {
|
---|
32 | return @"Self adaptive Discrete/dominant recombination creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent. It will also use the same strategy to combine the endogenous strategy parameter vector.";
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | public SelfAdaptiveDiscreteRecombination()
|
---|
37 | : base() {
|
---|
38 | AddVariableInfo(new VariableInfo("StrategyVector", "Vector containing the endogenous strategy parameters", typeof(DoubleArrayData), VariableKind.In));
|
---|
39 | }
|
---|
40 |
|
---|
41 | public override IOperation Apply(IScope scope) {
|
---|
42 | int rho = GetVariableValue<IntData>("Rho", scope, true).Data;
|
---|
43 | // with just 1 parent no recombination is necessary/possible
|
---|
44 | if (rho == 1) return null;
|
---|
45 | IRandom random = GetVariableValue<IRandom>("Random", scope, true);
|
---|
46 |
|
---|
47 | if (scope.SubScopes.Count % rho != 0)
|
---|
48 | throw new InvalidOperationException("Number of parents is not a multiple of rho");
|
---|
49 | int lambda = scope.SubScopes.Count / rho;
|
---|
50 | IList<double[]> parents = new List<double[]>(rho);
|
---|
51 | IList<double[]> parentsStrategy = new List<double[]>(rho);
|
---|
52 |
|
---|
53 | for (int i = 0; i < lambda; i++) {
|
---|
54 | IScope childScope = new Scope(i.ToString());
|
---|
55 | double[] childGene = (double[])scope.SubScopes[0].GetVariableValue<DoubleArrayData>("RealVector", false).Data.Clone();
|
---|
56 | double[] strategyParams = (double[])scope.SubScopes[0].GetVariableValue<DoubleArrayData>("StrategyVector", false).Data.Clone();
|
---|
57 | parents.Clear();
|
---|
58 | for (int j = 0; j < rho; j++) {
|
---|
59 | IScope parent = scope.SubScopes[0];
|
---|
60 | parents.Add(parent.GetVariableValue<DoubleArrayData>("RealVector", false).Data);
|
---|
61 | parentsStrategy.Add(parent.GetVariableValue<DoubleArrayData>("StrategyVector", false).Data);
|
---|
62 | scope.RemoveSubScope(parent);
|
---|
63 | }
|
---|
64 | // actual discrete recombination
|
---|
65 | if (childGene.Length != strategyParams.Length)
|
---|
66 | throw new InvalidOperationException("ERROR: strategy vector must be as long as there are dimensions");
|
---|
67 |
|
---|
68 | for (int x = 0; x < childGene.Length; x++) {
|
---|
69 | int nextParent = random.Next(rho);
|
---|
70 | childGene[x] = parents[nextParent][x];
|
---|
71 | strategyParams[x] = parentsStrategy[nextParent][x];
|
---|
72 | }
|
---|
73 | childScope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("RealVector"), new DoubleArrayData(childGene)));
|
---|
74 | childScope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("StrategyVector"), new DoubleArrayData(strategyParams)));
|
---|
75 | scope.AddSubScope(childScope);
|
---|
76 | }
|
---|
77 | return null;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|