#region License Information /* HeuristicLab * Copyright (C) 2002-2008 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 System.Collections.Generic; using System.Text; using HeuristicLab.Core; using HeuristicLab.Data; namespace HeuristicLab.RealVector { public class SelfAdaptiveDiscreteRecombination : DiscreteRecombination { public override string Description { get { 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."; } } public SelfAdaptiveDiscreteRecombination() : base() { AddVariableInfo(new VariableInfo("StrategyVector", "Vector containing the endogenous strategy parameters", typeof(DoubleArrayData), VariableKind.In)); } public override IOperation Apply(IScope scope) { int rho = GetVariableValue("Rho", scope, true).Data; // with just 1 parent no recombination is necessary/possible if (rho == 1) return null; IRandom random = GetVariableValue("Random", scope, true); if (scope.SubScopes.Count % rho != 0) throw new InvalidOperationException("Number of parents is not a multiple of rho"); int lambda = scope.SubScopes.Count / rho; IList parents = new List(rho); IList parentsStrategy = new List(rho); for (int i = 0; i < lambda; i++) { IScope childScope = new Scope(i.ToString()); double[] childGene = (double[])scope.SubScopes[0].GetVariableValue("RealVector", false).Data.Clone(); double[] strategyParams = (double[])scope.SubScopes[0].GetVariableValue("StrategyVector", false).Data.Clone(); parents.Clear(); for (int j = 0; j < rho; j++) { IScope parent = scope.SubScopes[0]; parents.Add(parent.GetVariableValue("RealVector", false).Data); parentsStrategy.Add(parent.GetVariableValue("StrategyVector", false).Data); scope.RemoveSubScope(parent); } // actual discrete recombination if (childGene.Length != strategyParams.Length) throw new InvalidOperationException("ERROR: strategy vector must be as long as there are dimensions"); for (int x = 0; x < childGene.Length; x++) { int nextParent = random.Next(rho); childGene[x] = parents[nextParent][x]; strategyParams[x] = parentsStrategy[nextParent][x]; } childScope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("RealVector"), new DoubleArrayData(childGene))); childScope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("StrategyVector"), new DoubleArrayData(strategyParams))); scope.AddSubScope(childScope); } return null; } } }