#region License Information /* HeuristicLab * Copyright (C) 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.Linq; using System.Threading; using HEAL.Attic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Parameters; namespace HeuristicLab.Optimization { [Item("Single-objective Improver", "Improves a solution by calling GetNeighbors and Evaluate of the corresponding problem definition.")] [StorableType("7A917E09-920C-4B47-9599-67371101B35F")] internal sealed class SingleObjectiveImprover : SingleSuccessorOperator, INeighborBasedOperator, IImprovementOperator, ISingleObjectiveEvaluationOperator, IStochasticOperator where TEncodedSolution : class, IEncodedSolution { public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } public ILookupParameter> EncodingParameter { get { return (ILookupParameter>)Parameters["Encoding"]; } } public ILookupParameter QualityParameter { get { return (ILookupParameter)Parameters["Quality"]; } } public ILookupParameter MaximizationParameter { get { return (ILookupParameter)Parameters["Maximization"]; } } public IValueLookupParameter ImprovementAttemptsParameter { get { return (IValueLookupParameter)Parameters["ImprovementAttempts"]; } } public IValueLookupParameter SampleSizeParameter { get { return (IValueLookupParameter)Parameters["SampleSize"]; } } public ILookupParameter LocalEvaluatedSolutionsParameter { get { return (ILookupParameter)Parameters["LocalEvaluatedSolutions"]; } } public Action, IRandom, CancellationToken> Evaluate { get; set; } public Func, IRandom, IEnumerable>> GetNeighbors { get; set; } [StorableConstructor] private SingleObjectiveImprover(StorableConstructorFlag _) : base(_) { } private SingleObjectiveImprover(SingleObjectiveImprover original, Cloner cloner) : base(original, cloner) { } public SingleObjectiveImprover() { Parameters.Add(new LookupParameter("Random", "The random number generator to use.")); Parameters.Add(new LookupParameter>("Encoding", "An item that holds the problem's encoding.")); Parameters.Add(new LookupParameter("Quality", "The quality of the parameter vector.")); Parameters.Add(new LookupParameter("Maximization", "Whether the problem should be minimized or maximized.")); Parameters.Add(new ValueLookupParameter("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100))); Parameters.Add(new ValueLookupParameter("SampleSize", "The number of samples to draw from the neighborhood function at maximum.", new IntValue(300))); Parameters.Add(new LookupParameter("LocalEvaluatedSolutions", "The number of solution evaluations that have been performed.")); } public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveImprover(this, cloner); } public override IOperation Apply() { var random = RandomParameter.ActualValue; var encoding = EncodingParameter.ActualValue; var maximize = MaximizationParameter.ActualValue.Value; var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value; var sampleSize = SampleSizeParameter.ActualValue.Value; var solutionContext = ScopeUtil.CreateSolutionContext(ExecutionContext.Scope, encoding); double quality; if (QualityParameter.ActualValue == null) { if (!solutionContext.IsEvaluated) Evaluate(solutionContext, random, CancellationToken.None); quality = solutionContext.EvaluationResult.Quality; } else quality = QualityParameter.ActualValue.Value; var count = 0; for (var i = 0; i < maxAttempts; i++) { ISingleObjectiveSolutionContext best = default; var bestQuality = quality; foreach (var neighbor in GetNeighbors(solutionContext, random).Take(sampleSize)) { if (!neighbor.IsEvaluated) Evaluate(neighbor, random, CancellationToken); var q = neighbor.EvaluationResult.Quality; count++; if (maximize && bestQuality > q || !maximize && bestQuality < q) continue; best = neighbor; bestQuality = q; } if (best == null) break; solutionContext = best; quality = bestQuality; } LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count); QualityParameter.ActualValue = new DoubleValue(quality); ScopeUtil.CopyEncodedSolutionToScope(ExecutionContext.Scope, encoding, solutionContext.EncodedSolution); ScopeUtil.CopyToScope(ExecutionContext.Scope, solutionContext); return base.Apply(); } } }