source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveImprover.cs @ 11739

Last change on this file since 11739 was 11739, checked in by mkommend, 8 years ago

#2174: Worked on operators and programmable problem base classes and scripts.

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.Programmable {
34  [Item("Single-objective Improver", "Improves a solution by calling GetNeighbors and Evaluate of the corresponding problem definition.")]
35  [StorableClass]
36  public sealed class SingleObjectiveImprover : SingleSuccessorOperator, INeighborBasedOperator, ISingleObjectiveEvaluationOperator, IStochasticOperator {
37    public ILookupParameter<IRandom> RandomParameter {
38      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
39    }
40
41    public ILookupParameter<IEncoding> EncodingParameter {
42      get { return (ILookupParameter<IEncoding>)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<Individual, IRandom, double> EvaluateFunc { get; set; }
66    public Func<Individual, IRandom, IEnumerable<Individual>> GetNeighborsFunc { get; set; }
67
68    [StorableConstructor]
69    private SingleObjectiveImprover(bool deserializing) : base(deserializing) { }
70    private SingleObjectiveImprover(SingleObjectiveImprover 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>("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(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 individual = encoding.GetIndividual(ExecutionContext.Scope);
92      var quality = QualityParameter.ActualValue == null ? EvaluateFunc(individual, random) : QualityParameter.ActualValue.Value;
93
94      var count = 0;
95      for (var i = 0; i < maxAttempts; i++) {
96        Individual best = null;
97        var bestQuality = quality;
98        foreach (var neighbor in GetNeighborsFunc(individual, 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        individual = best;
107        quality = bestQuality;
108      }
109
110      LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count);
111      QualityParameter.ActualValue = new DoubleValue(quality);
112      individual.Copy(ExecutionContext.Scope);
113      return base.Apply();
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.