#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Algorithms.ScatterSearch { /// /// An operator that updates the reference set and rebuilds the population. /// [Item("PopulationRebuildMethod", "An operator that updates the reference set and rebuilds the population.")] [StorableClass] public sealed class PopulationRebuildMethod : SingleSuccessorOperator { #region Parameter properties public ScopeParameter CurrentScopeParameter { get { return (ScopeParameter)Parameters["CurrentScope"]; } } public IValueLookupParameter NumberOfHighQualitySolutionsParameter { get { return (IValueLookupParameter)Parameters["NumberOfHighQualitySolutions"]; } } public IValueLookupParameter ReferenceSetSizeParameter { get { return (IValueLookupParameter)Parameters["ReferenceSetSize"]; } } public IValueLookupParameter QualityParameter { get { return (IValueLookupParameter)Parameters["Quality"]; } } #endregion #region Properties private IScope CurrentScope { get { return CurrentScopeParameter.ActualValue; } } private IntValue NumberOfHighQualitySolutions { get { return NumberOfHighQualitySolutionsParameter.ActualValue; } set { NumberOfHighQualitySolutionsParameter.ActualValue = value; } } private IntValue ReferenceSetSize { get { return ReferenceSetSizeParameter.ActualValue; } set { ReferenceSetSizeParameter.ActualValue = value; } } private IItem Quality { get { return QualityParameter.ActualValue; } } #endregion [StorableConstructor] private PopulationRebuildMethod(bool deserializing) : base(deserializing) { } private PopulationRebuildMethod(PopulationRebuildMethod original, Cloner cloner) : base(original, cloner) { } public PopulationRebuildMethod() : base() { Initialize(); } public override IDeepCloneable Clone(Cloner cloner) { return new PopulationRebuildMethod(this, cloner); } private void Initialize() { #region Create parameters Parameters.Add(new ScopeParameter("CurrentScope")); Parameters.Add(new ValueLookupParameter("NumberOfHighQualitySolutions")); Parameters.Add(new ValueLookupParameter("ReferenceSetSize")); Parameters.Add(new ValueLookupParameter("Quality")); #endregion } public override IOperation Apply() { var population = CurrentScope.SubScopes[0]; var refSet = CurrentScope.SubScopes[1]; refSet.SubScopes.Replace(refSet.SubScopes.OrderByDescending(r => r.Variables[QualityParameter.ActualName].Value) .Take(NumberOfHighQualitySolutions.Value).ToList()); population.SubScopes.Clear(); return base.Apply(); } } }