#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 HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using System.Collections.Generic; using System.Linq; namespace HeuristicLab.Analysis.FitnessLandscape { [Item("Up/DownSelector", "A selection operator that moves towards the next local optimum, when found reverses direction and so on.")] [StorableClass] public class UpDownSelector : InstrumentedOperator, IStochasticOperator { #region Parameters public ILookupParameter MaximizationParameter { get { return (ILookupParameter)Parameters["Maximization"]; } } public ILookupParameter MoveTowardsOptimumParameter { get { return (ILookupParameter)Parameters["MoveTowardsOptimum"]; } } public ILookupParameter BaseQualityParameter { get { return (ILookupParameter)Parameters["BaseQuality"]; } } public IScopeTreeLookupParameter QualityParameter { get { return (IScopeTreeLookupParameter)Parameters["Quality"]; } } public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } #endregion #region Construction & Cloning [StorableConstructor] protected UpDownSelector(bool deserializing) : base(deserializing) { } protected UpDownSelector(UpDownSelector original, Cloner cloner) : base(original, cloner) { } public UpDownSelector() { Parameters.Add(new LookupParameter("Maximization", "Whether the problem is a maximization or minimization problem.")); Parameters.Add(new LookupParameter("MoveTowardsOptimum", "Specifies whether the selector should optimize towards or away from the optimum.")); Parameters.Add(new LookupParameter("BaseQuality", "The base quality value to compare to. This is required to determine wheter a local optimum has been found and the direction should be reversed.")); Parameters.Add(new ScopeTreeLookupParameter("Quality", "The qualities of the solutions.")); Parameters.Add(new LookupParameter("Random", "Random number generator")); BaseQualityParameter.ActualName = "Quality"; } public override IDeepCloneable Clone(Cloner cloner) { return new UpDownSelector(this, cloner); } #endregion public override IOperation InstrumentedApply() { var scopes = ExecutionContext.Scope.SubScopes.ToList(); var selected = Select(scopes); scopes.Remove(selected); ExecutionContext.Scope.SubScopes.Clear(); ExecutionContext.Scope.SubScopes.Add(new Scope("Remaining")); ExecutionContext.Scope.SubScopes[0].SubScopes.AddRange(scopes); ExecutionContext.Scope.SubScopes.Add(new Scope("Selected")); ExecutionContext.Scope.SubScopes[1].SubScopes.Add(selected); return base.InstrumentedApply(); } private IScope Select(List scopes) { var maximization = MaximizationParameter.ActualValue.Value; bool optimize = true; if (MoveTowardsOptimumParameter.ActualValue == null) { MoveTowardsOptimumParameter.ActualValue = new BoolValue(true); } else { optimize = MoveTowardsOptimumParameter.ActualValue.Value; } var qualities = QualityParameter.ActualValue; var list = qualities.Select((x, i) => new { Index = i, Quality = x.Value }).ToList(); var baseQuality = BaseQualityParameter.ActualValue.Value; if (double.IsNaN(baseQuality)) { baseQuality = maximization ? list.Max(x => x.Quality) : list.Min(x => x.Quality); } var random = RandomParameter.ActualValue; if (random != null) list.Shuffle(random); if (maximization && optimize || !maximization && !optimize) { list = list.OrderByDescending(x => x.Quality).ToList(); if (list.Count > 0 && list[0].Quality <= baseQuality) MoveTowardsOptimumParameter.ActualValue = new BoolValue(!optimize); } else { list = list.OrderBy(x => x.Quality).ToList(); if (list.Count > 0 && list[0].Quality >= baseQuality) MoveTowardsOptimumParameter.ActualValue = new BoolValue(!optimize); } return scopes[list[0].Index]; } } }