Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11484 was 11484, checked in by abeham, 10 years ago

#2174: Major refactoring

  • Removed ProblemDefinitionHosts
  • Renamed ParameterVector to Individual
  • Renamed Configuration to Encoding
  • Changed handling of existing operators that they will not be removed and recreated, but only rewired
File size: 4.9 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Operators;
7using HeuristicLab.Optimization;
8using HeuristicLab.Parameters;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10
11namespace HeuristicLab.Problems.Programmable {
12  [Item("Single-objective Improver", "Improves a solution by calling GetNeighbors and Evaluate of the corresponding problem definition.")]
13  [StorableClass]
14  public sealed class SingleObjectiveImprover : SingleSuccessorOperator, ISingleObjectiveImprovementOperator, IStochasticOperator {
15    public IValueLookupParameter<IItem> SolutionParameter {
16      get { return new ValueLookupParameter<IItem>("notused"); }
17    }
18
19    public ILookupParameter<IRandom> RandomParameter {
20      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
21    }
22
23    public ILookupParameter<ISingleObjectiveProblemDefinition> ProblemDefinitionParameter {
24      get { return (ILookupParameter<ISingleObjectiveProblemDefinition>)Parameters["ProblemDefinition"]; }
25    }
26
27    public ILookupParameter<Encoding> EncodingParameter {
28      get { return (ILookupParameter<Encoding>)Parameters["Encoding"]; }
29    }
30
31    public ILookupParameter<DoubleValue> QualityParameter {
32      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
33    }
34
35    public ILookupParameter<BoolValue> MaximizationParameter {
36      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
37    }
38
39    public IValueLookupParameter<IntValue> ImprovementAttemptsParameter {
40      get { return (IValueLookupParameter<IntValue>)Parameters["ImprovementAttempts"]; }
41    }
42
43    public IValueLookupParameter<IntValue> SampleSizeParameter {
44      get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
45    }
46
47    public ILookupParameter<IntValue> LocalEvaluatedSolutionsParameter {
48      get { return (ILookupParameter<IntValue>)Parameters["LocalEvaluatedSolutions"]; }
49    }
50
51    [StorableConstructor]
52    private SingleObjectiveImprover(bool deserializing) : base(deserializing) { }
53    private SingleObjectiveImprover(SingleObjectiveImprover original, Cloner cloner) : base(original, cloner) { }
54    public SingleObjectiveImprover() {
55      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
56      Parameters.Add(new LookupParameter<ISingleObjectiveProblemDefinition>("ProblemDefinition", "The host that holds the problem definition."));
57      Parameters.Add(new LookupParameter<Encoding>("Encoding", "An item that holds the problem's encoding."));
58      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
59      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether the problem should be minimized or maximized."));
60      Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
61      Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of samples to draw from the neighborhood function at maximum.", new IntValue(300)));
62      Parameters.Add(new LookupParameter<IntValue>("LocalEvaluatedSolutions", "The number of solution evaluations that have been performed."));
63    }
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new SingleObjectiveImprover(this, cloner);
67    }
68
69    public override IOperation Apply() {
70      var random = RandomParameter.ActualValue;
71      var definition = ProblemDefinitionParameter.ActualValue;
72      if (definition == null) throw new InvalidOperationException("Problem definition is null.");
73      var encoding = EncodingParameter.ActualValue;
74      var maximize = MaximizationParameter.ActualValue.Value;
75      var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value;
76      var sampleSize = SampleSizeParameter.ActualValue.Value;
77      var vector = Helper.Extract(ExecutionContext.Scope, encoding);
78      var quality = QualityParameter.ActualValue == null ? definition.Evaluate(random, vector) : QualityParameter.ActualValue.Value;
79
80      var count = 0;
81      for (var i = 0; i < maxAttempts; i++) {
82        Individual best = null;
83        var bestQuality = quality;
84        foreach (var neighbor in definition.GetNeighbors(random, vector).Take(sampleSize)) {
85          var q = definition.Evaluate(random, neighbor);
86          count++;
87          if (maximize && bestQuality > q || !maximize && bestQuality < q) continue;
88          best = neighbor;
89          bestQuality = q;
90        }
91        if (best == null) break;
92        vector = best;
93        quality = bestQuality;
94      }
95
96      LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count);
97      Helper.Write(ExecutionContext.Scope, vector);
98      return base.Apply();
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.