1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Threading;
|
---|
26 | using HEAL.Attic;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Operators;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Optimization {
|
---|
34 | [Item("Single-objective Improver", "Improves a solution by calling GetNeighbors and Evaluate of the corresponding problem definition.")]
|
---|
35 | [StorableType("7A917E09-920C-4B47-9599-67371101B35F")]
|
---|
36 | internal sealed class SingleObjectiveImprover<TEncodedSolution> : SingleSuccessorOperator, INeighborBasedOperator<TEncodedSolution>, IImprovementOperator, ISingleObjectiveEvaluationOperator<TEncodedSolution>, IStochasticOperator
|
---|
37 | where TEncodedSolution : class, IEncodedSolution {
|
---|
38 | public ILookupParameter<IRandom> RandomParameter {
|
---|
39 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public ILookupParameter<IEncoding> EncodingParameter {
|
---|
43 | get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
|
---|
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 |
|
---|
66 | public Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, CancellationToken> Evaluate { get; set; }
|
---|
67 | public Func<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>>> GetNeighbors { get; set; }
|
---|
68 |
|
---|
69 | [StorableConstructor]
|
---|
70 | private SingleObjectiveImprover(StorableConstructorFlag _) : base(_) { }
|
---|
71 | private SingleObjectiveImprover(SingleObjectiveImprover<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
|
---|
72 | public SingleObjectiveImprover() {
|
---|
73 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
74 | Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
|
---|
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) {
|
---|
83 | return new SingleObjectiveImprover<TEncodedSolution>(this, cloner);
|
---|
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;
|
---|
92 | var solutionContext = ScopeUtil.CreateSolutionContext<TEncodedSolution>(ExecutionContext.Scope, encoding);
|
---|
93 |
|
---|
94 | double quality;
|
---|
95 | if (QualityParameter.ActualValue == null) {
|
---|
96 | if (!solutionContext.IsEvaluated) Evaluate(solutionContext, random, CancellationToken.None);
|
---|
97 |
|
---|
98 | quality = solutionContext.EvaluationResult.Quality;
|
---|
99 | } else quality = QualityParameter.ActualValue.Value;
|
---|
100 |
|
---|
101 | var count = 0;
|
---|
102 | for (var i = 0; i < maxAttempts; i++) {
|
---|
103 | ISingleObjectiveSolutionContext<TEncodedSolution> best = null;
|
---|
104 | var bestQuality = quality;
|
---|
105 | foreach (var neighbor in GetNeighbors(solutionContext, random).Take(sampleSize)) {
|
---|
106 | if (!neighbor.IsEvaluated)
|
---|
107 | Evaluate(neighbor, random, CancellationToken);
|
---|
108 | var q = neighbor.EvaluationResult.Quality;
|
---|
109 | count++;
|
---|
110 | if (maximize && bestQuality > q || !maximize && bestQuality < q) continue;
|
---|
111 | best = neighbor;
|
---|
112 | bestQuality = q;
|
---|
113 | }
|
---|
114 | if (best == null) break;
|
---|
115 | solutionContext = best;
|
---|
116 | quality = bestQuality;
|
---|
117 | }
|
---|
118 |
|
---|
119 | LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count);
|
---|
120 | ScopeUtil.CopyEncodedSolutionToScope(ExecutionContext.Scope, encoding, solutionContext.EncodedSolution);
|
---|
121 | ScopeUtil.CopyToScope(ExecutionContext.Scope, solutionContext);
|
---|
122 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
123 | return base.Apply();
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|