Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveImprover.cs @ 16751

Last change on this file since 16751 was 16751, checked in by mkommend, 5 years ago

#2521: Renamed Solution to EncodedSolution.

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HEAL.Attic;
31
32namespace HeuristicLab.Optimization {
33  [Item("Single-objective Improver", "Improves a solution by calling GetNeighbors and Evaluate of the corresponding problem definition.")]
34  [StorableType("7A917E09-920C-4B47-9599-67371101B35F")]
35  public sealed class SingleObjectiveImprover<TEncodedSolution> : SingleSuccessorOperator, INeighborBasedOperator<TEncodedSolution>, IImprovementOperator, ISingleObjectiveEvaluationOperator<TEncodedSolution>, IStochasticOperator
36    where TEncodedSolution : class, IEncodedSolution {
37    public ILookupParameter<IRandom> RandomParameter {
38      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
39    }
40
41    public ILookupParameter<IEncoding<TEncodedSolution>> EncodingParameter {
42      get { return (ILookupParameter<IEncoding<TEncodedSolution>>)Parameters["Encoding"]; }
43    }
44
45    public ILookupParameter<DoubleValue> QualityParameter {
46      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
47    }
48
49    public ILookupParameter<BoolValue> MaximizationParameter {
50      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
51    }
52
53    public IValueLookupParameter<IntValue> ImprovementAttemptsParameter {
54      get { return (IValueLookupParameter<IntValue>)Parameters["ImprovementAttempts"]; }
55    }
56
57    public IValueLookupParameter<IntValue> SampleSizeParameter {
58      get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
59    }
60
61    public ILookupParameter<IntValue> LocalEvaluatedSolutionsParameter {
62      get { return (ILookupParameter<IntValue>)Parameters["LocalEvaluatedSolutions"]; }
63    }
64
65    public Func<TEncodedSolution, IRandom, double> EvaluateFunc { get; set; }
66    public Func<TEncodedSolution, IRandom, IEnumerable<TEncodedSolution>> GetNeighborsFunc { get; set; }
67
68    [StorableConstructor]
69    private SingleObjectiveImprover(StorableConstructorFlag _) : base(_) { }
70    private SingleObjectiveImprover(SingleObjectiveImprover<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
71    public SingleObjectiveImprover() {
72      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
73      Parameters.Add(new LookupParameter<IEncoding<TEncodedSolution>>("Encoding", "An item that holds the problem's encoding."));
74      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
75      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether the problem should be minimized or maximized."));
76      Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
77      Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of samples to draw from the neighborhood function at maximum.", new IntValue(300)));
78      Parameters.Add(new LookupParameter<IntValue>("LocalEvaluatedSolutions", "The number of solution evaluations that have been performed."));
79    }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new SingleObjectiveImprover<TEncodedSolution>(this, cloner);
83    }
84
85    public override IOperation Apply() {
86      var random = RandomParameter.ActualValue;
87      var encoding = EncodingParameter.ActualValue;
88      var maximize = MaximizationParameter.ActualValue.Value;
89      var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value;
90      var sampleSize = SampleSizeParameter.ActualValue.Value;
91      var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding);
92      var quality = QualityParameter.ActualValue == null ? EvaluateFunc(solution, random) : QualityParameter.ActualValue.Value;
93
94      var count = 0;
95      for (var i = 0; i < maxAttempts; i++) {
96        TEncodedSolution best = default(TEncodedSolution);
97        var bestQuality = quality;
98        foreach (var neighbor in GetNeighborsFunc(solution, random).Take(sampleSize)) {
99          var q = EvaluateFunc(neighbor, random);
100          count++;
101          if (maximize && bestQuality > q || !maximize && bestQuality < q) continue;
102          best = neighbor;
103          bestQuality = q;
104        }
105        if (best == null) break;
106        solution = best;
107        quality = bestQuality;
108      }
109
110      LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count);
111      QualityParameter.ActualValue = new DoubleValue(quality);
112
113      ScopeUtil.CopyEncodedSolutionToScope(ExecutionContext.Scope, encoding, solution);
114      return base.Apply();
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.