Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveImprover.cs @ 15287

Last change on this file since 15287 was 15281, checked in by abeham, 7 years ago

#2258: merged r14001:15280 from trunk into branch

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