#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 solution pool. /// [Item("SolutionPoolUpdateMethod", "An operator that updates the solution pool.")] [StorableClass] public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator { #region Parameter properties private ScopeParameter CurrentScopeParameter { get { return (ScopeParameter)Parameters["CurrentScope"]; } } private ValueLookupParameter NewSolutionsParameter { get { return (ValueLookupParameter)Parameters["NewSolutions"]; } } #endregion #region Properties public IScope CurrentScope { get { return CurrentScopeParameter.ActualValue; } } public BoolValue NewSolutions { get { return NewSolutionsParameter.ActualValue; } } #endregion [StorableConstructor] private SolutionPoolUpdateMethod(bool deserializing) : base(deserializing) { } private SolutionPoolUpdateMethod(SolutionPoolUpdateMethod original, Cloner cloner) : base(original, cloner) { } public SolutionPoolUpdateMethod() : base() { Initialize(); } public override IDeepCloneable Clone(Cloner cloner) { return new SolutionPoolUpdateMethod(this, cloner); } private void Initialize() { #region Create parameters Parameters.Add(new ValueLookupParameter("NewSolutions", "Indicates if new solutions have been found.")); Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes.")); #endregion #region Create operators #endregion #region Create operator graph #endregion } public override IOperation Apply() { IScope parents = new Scope("Parents"); IScope offspring = new Scope("Offspring"); foreach (var scope in CurrentScope.SubScopes) { parents.SubScopes.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1)); offspring.SubScopes.AddRange(scope.SubScopes.Last().SubScopes); } CurrentScope.SubScopes.Clear(); CurrentScope.SubScopes.AddRange(parents.SubScopes.OrderByDescending(o => o.Variables["Quality"].Value)); if ((offspring.SubScopes.OrderByDescending(o => o.Variables["Quality"].Value).First().Variables["Quality"].Value as DoubleValue).Value > (parents.SubScopes.OrderByDescending(o => o.Variables["Quality"].Value).First().Variables["Quality"].Value as DoubleValue).Value) { CurrentScope.SubScopes.Replace(CurrentScope.SubScopes.Union(offspring.SubScopes).OrderByDescending(o => o.Variables["Quality"].Value).Take(10)); NewSolutions.Value = true; } return base.Apply(); } } }