#region License Information /* HeuristicLab * Copyright (C) 2002-2016 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.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Persistence; using HeuristicLab.Selection; namespace HeuristicLab.Algorithms.ALPS { [Item("EldersSelector", "Select all individuals which are too old for their current layer.")] [StorableType("5b879fbd-1ccf-4ea5-9f32-93a6d70d8c88")] public sealed class EldersSelector : Selector { public IScopeTreeLookupParameter AgeParameter { get { return (IScopeTreeLookupParameter)Parameters["Age"]; } } public ILookupParameter AgeLimitsParameter { get { return (ILookupParameter)Parameters["AgeLimits"]; } } public ILookupParameter NumberOfLayersParameter { get { return (ILookupParameter)Parameters["NumberOfLayers"]; } } public ILookupParameter LayerParameter { get { return (ILookupParameter)Parameters["Layer"]; } } [StorableConstructor] private EldersSelector(StorableConstructorFlag deserializing) : base(deserializing) { } private EldersSelector(EldersSelector original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new EldersSelector(this, cloner); } public EldersSelector() : base() { Parameters.Add(new ScopeTreeLookupParameter("Age", "The age of individuals.")); Parameters.Add(new LookupParameter("AgeLimits", "The maximum age an individual is allowed to reach in a certain layer.")); Parameters.Add(new LookupParameter("NumberOfLayers", "The number of layers.")); Parameters.Add(new LookupParameter("Layer", "The number of the current layer.")); } protected override IScope[] Select(List scopes) { var ages = AgeParameter.ActualValue; var ageLimits = AgeLimitsParameter.ActualValue; int numberOfLayers = NumberOfLayersParameter.ActualValue.Value; int layer = LayerParameter.ActualValue.Value; if (layer >= numberOfLayers - 1) // is max layer? return new IScope[0]; int limit = ageLimits[layer]; var elders = ages .Select((x, index) => new { index, age = x.Value }) .Where(x => x.age > limit) .ToList(); IScope[] selected = new IScope[elders.Count]; for (int i = 0; i < elders.Count; i++) { selected[i] = scopes[elders[i].index]; scopes[elders[i].index] = null; } scopes.RemoveAll(x => x == null); return selected; } } }