Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Analysis.FitnessLandscape/3.3/UpDownSelector.cs @ 14871

Last change on this file since 14871 was 13583, checked in by abeham, 9 years ago

#2457:

  • Added stripped-down version of FLA branch
  • Added appropriate calculators
  • Fixed detecting maximization in RLD view
File size: 5.2 KB
RevLine 
[13583]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
[7128]22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
[13583]25using HeuristicLab.Operators;
[7128]26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[13583]29using System.Collections.Generic;
30using System.Linq;
[7128]31
32namespace HeuristicLab.Analysis.FitnessLandscape {
[13583]33  [Item("Up/DownSelector", "A selection operator that moves towards the next local optimum, when found reverses direction and so on.")]
[7128]34  [StorableClass]
[13583]35  public class UpDownSelector : InstrumentedOperator, IStochasticOperator {
[7128]36
37    #region Parameters
[13583]38    public ILookupParameter<BoolValue> MaximizationParameter {
39      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
[7128]40    }
[13583]41    public ILookupParameter<BoolValue> MoveTowardsOptimumParameter {
42      get { return (ILookupParameter<BoolValue>)Parameters["MoveTowardsOptimum"]; }
[7128]43    }
[13583]44    public ILookupParameter<DoubleValue> BaseQualityParameter {
45      get { return (ILookupParameter<DoubleValue>)Parameters["BaseQuality"]; }
[7128]46    }
[13583]47    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
48      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
49    }
50    public ILookupParameter<IRandom> RandomParameter {
51      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
52    }
[7128]53    #endregion
54
55    #region Construction & Cloning
56
57    [StorableConstructor]
[13583]58    protected UpDownSelector(bool deserializing) : base(deserializing) { }
59    protected UpDownSelector(UpDownSelector original, Cloner cloner) : base(original, cloner) { }
60    public UpDownSelector() {
61      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether the problem is a maximization or minimization problem."));
62      Parameters.Add(new LookupParameter<BoolValue>("MoveTowardsOptimum", "Specifies whether the selector should optimize towards or away from the optimum."));
63      Parameters.Add(new LookupParameter<DoubleValue>("BaseQuality", "The base quality value to compare to. This is required to determine wheter a local optimum has been found and the direction should be reversed."));
64      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the solutions."));
[7128]65      Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator"));
66      BaseQualityParameter.ActualName = "Quality";
67    }
68    public override IDeepCloneable Clone(Cloner cloner) {
[13583]69      return new UpDownSelector(this, cloner);
[7128]70    }
71    #endregion
72
[13583]73    public override IOperation InstrumentedApply() {
74      var scopes = ExecutionContext.Scope.SubScopes.ToList();
75      var selected = Select(scopes);
76      scopes.Remove(selected);
77      ExecutionContext.Scope.SubScopes.Clear();
78      ExecutionContext.Scope.SubScopes.Add(new Scope("Remaining"));
79      ExecutionContext.Scope.SubScopes[0].SubScopes.AddRange(scopes);
80      ExecutionContext.Scope.SubScopes.Add(new Scope("Selected"));
81      ExecutionContext.Scope.SubScopes[1].SubScopes.Add(selected);
82      return base.InstrumentedApply();
83    }
84
85    private IScope Select(List<IScope> scopes) {
86      var maximization = MaximizationParameter.ActualValue.Value;
[7128]87      bool optimize = true;
88      if (MoveTowardsOptimumParameter.ActualValue == null) {
89        MoveTowardsOptimumParameter.ActualValue = new BoolValue(true);
90      } else {
91        optimize = MoveTowardsOptimumParameter.ActualValue.Value;
92      }
[13583]93      var qualities = QualityParameter.ActualValue;
94      var list = qualities.Select((x, i) => new { Index = i, Quality = x.Value }).ToList();
95      var baseQuality = BaseQualityParameter.ActualValue.Value;
96      if (double.IsNaN(baseQuality)) {
97        baseQuality = maximization ? list.Max(x => x.Quality) : list.Min(x => x.Quality);
98      }
99      var random = RandomParameter.ActualValue;
[7128]100
101      if (random != null)
102        list.Shuffle(random);
103      if (maximization && optimize || !maximization && !optimize) {
104        list = list.OrderByDescending(x => x.Quality).ToList();
105        if (list.Count > 0 && list[0].Quality <= baseQuality)
106          MoveTowardsOptimumParameter.ActualValue = new BoolValue(!optimize);
107      } else {
108        list = list.OrderBy(x => x.Quality).ToList();
109        if (list.Count > 0 && list[0].Quality >= baseQuality)
110          MoveTowardsOptimumParameter.ActualValue = new BoolValue(!optimize);
111      }
[13583]112      return scopes[list[0].Index];
[7128]113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.