[11739] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17226] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11739] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[11484] | 24 | using System.Linq;
|
---|
[17366] | 25 | using System.Threading;
|
---|
[16753] | 26 | using HEAL.Attic;
|
---|
[11484] | 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
| 30 | using HeuristicLab.Operators;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 |
|
---|
[11949] | 33 | namespace HeuristicLab.Optimization {
|
---|
[11484] | 34 | [Item("Single-objective Improver", "Improves a solution by calling GetNeighbors and Evaluate of the corresponding problem definition.")]
|
---|
[16723] | 35 | [StorableType("7A917E09-920C-4B47-9599-67371101B35F")]
|
---|
[16753] | 36 | internal sealed class SingleObjectiveImprover<TEncodedSolution> : SingleSuccessorOperator, INeighborBasedOperator<TEncodedSolution>, IImprovementOperator, ISingleObjectiveEvaluationOperator<TEncodedSolution>, IStochasticOperator
|
---|
[16751] | 37 | where TEncodedSolution : class, IEncodedSolution {
|
---|
[11484] | 38 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 39 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[16751] | 42 | public ILookupParameter<IEncoding<TEncodedSolution>> EncodingParameter {
|
---|
| 43 | get { return (ILookupParameter<IEncoding<TEncodedSolution>>)Parameters["Encoding"]; }
|
---|
[11484] | 44 | }
|
---|
| 45 |
|
---|
| 46 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
| 47 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public ILookupParameter<BoolValue> MaximizationParameter {
|
---|
| 51 | get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public IValueLookupParameter<IntValue> ImprovementAttemptsParameter {
|
---|
| 55 | get { return (IValueLookupParameter<IntValue>)Parameters["ImprovementAttempts"]; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public IValueLookupParameter<IntValue> SampleSizeParameter {
|
---|
| 59 | get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public ILookupParameter<IntValue> LocalEvaluatedSolutionsParameter {
|
---|
| 63 | get { return (ILookupParameter<IntValue>)Parameters["LocalEvaluatedSolutions"]; }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[17366] | 66 | public Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, CancellationToken> Evaluate { get; set; }
|
---|
[17363] | 67 | public Func<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>>> GetNeighbors { get; set; }
|
---|
[11739] | 68 |
|
---|
[11484] | 69 | [StorableConstructor]
|
---|
[16723] | 70 | private SingleObjectiveImprover(StorableConstructorFlag _) : base(_) { }
|
---|
[16751] | 71 | private SingleObjectiveImprover(SingleObjectiveImprover<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
|
---|
[11484] | 72 | public SingleObjectiveImprover() {
|
---|
| 73 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
[16751] | 74 | Parameters.Add(new LookupParameter<IEncoding<TEncodedSolution>>("Encoding", "An item that holds the problem's encoding."));
|
---|
[11484] | 75 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
|
---|
| 76 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether the problem should be minimized or maximized."));
|
---|
| 77 | Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
|
---|
| 78 | Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of samples to draw from the neighborhood function at maximum.", new IntValue(300)));
|
---|
| 79 | Parameters.Add(new LookupParameter<IntValue>("LocalEvaluatedSolutions", "The number of solution evaluations that have been performed."));
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[16751] | 83 | return new SingleObjectiveImprover<TEncodedSolution>(this, cloner);
|
---|
[11484] | 84 | }
|
---|
| 85 |
|
---|
| 86 | public override IOperation Apply() {
|
---|
| 87 | var random = RandomParameter.ActualValue;
|
---|
| 88 | var encoding = EncodingParameter.ActualValue;
|
---|
| 89 | var maximize = MaximizationParameter.ActualValue.Value;
|
---|
| 90 | var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value;
|
---|
| 91 | var sampleSize = SampleSizeParameter.ActualValue.Value;
|
---|
[17577] | 92 | var solutionContext = ScopeUtil.CreateSolutionContext(ExecutionContext.Scope, encoding);
|
---|
[11484] | 93 |
|
---|
[17363] | 94 | double quality;
|
---|
| 95 | if (QualityParameter.ActualValue == null) {
|
---|
[17366] | 96 | if (!solutionContext.IsEvaluated) Evaluate(solutionContext, random, CancellationToken.None);
|
---|
[17363] | 97 |
|
---|
| 98 | quality = solutionContext.EvaluationResult.Quality;
|
---|
| 99 | } else quality = QualityParameter.ActualValue.Value;
|
---|
| 100 |
|
---|
[11484] | 101 | var count = 0;
|
---|
| 102 | for (var i = 0; i < maxAttempts; i++) {
|
---|
[17578] | 103 | ISingleObjectiveSolutionContext<TEncodedSolution> best = null;
|
---|
[11484] | 104 | var bestQuality = quality;
|
---|
[17363] | 105 | foreach (var neighbor in GetNeighbors(solutionContext, random).Take(sampleSize)) {
|
---|
[17577] | 106 | if (!neighbor.IsEvaluated)
|
---|
| 107 | Evaluate(neighbor, random, CancellationToken);
|
---|
[17363] | 108 | var q = neighbor.EvaluationResult.Quality;
|
---|
[11484] | 109 | count++;
|
---|
| 110 | if (maximize && bestQuality > q || !maximize && bestQuality < q) continue;
|
---|
[17577] | 111 | best = neighbor;
|
---|
[11484] | 112 | bestQuality = q;
|
---|
| 113 | }
|
---|
| 114 | if (best == null) break;
|
---|
[17577] | 115 | solutionContext = best;
|
---|
[11484] | 116 | quality = bestQuality;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count);
|
---|
[11739] | 120 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
[17577] | 121 | ScopeUtil.CopyEncodedSolutionToScope(ExecutionContext.Scope, encoding, solutionContext.EncodedSolution);
|
---|
| 122 | ScopeUtil.CopyToScope(ExecutionContext.Scope, solutionContext);
|
---|
[11484] | 123 | return base.Apply();
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|