Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Analysis.FitnessLandscape/3.3/UpDownSelector.cs @ 17504

Last change on this file since 17504 was 16728, checked in by abeham, 6 years ago

#1614: updated to new persistence and .NET 4.6.1

File size: 5.2 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.Collections.Generic;
23using System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31
32namespace HeuristicLab.Analysis.FitnessLandscape {
33  [Item("Up/DownSelector", "A selection operator that moves towards the next local optimum, when found reverses direction and so on.")]
34  [StorableType("67964036-8CE4-4987-9495-8174D1B218A5")]
35  public class UpDownSelector : InstrumentedOperator, IStochasticOperator {
36
37    #region Parameters
38    public ILookupParameter<BoolValue> MaximizationParameter {
39      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
40    }
41    public ILookupParameter<BoolValue> MoveTowardsOptimumParameter {
42      get { return (ILookupParameter<BoolValue>)Parameters["MoveTowardsOptimum"]; }
43    }
44    public ILookupParameter<DoubleValue> BaseQualityParameter {
45      get { return (ILookupParameter<DoubleValue>)Parameters["BaseQuality"]; }
46    }
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    }
53    #endregion
54
55    #region Construction & Cloning
56
57    [StorableConstructor]
58    protected UpDownSelector(StorableConstructorFlag _) : base(_) { }
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."));
65      Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator"));
66      BaseQualityParameter.ActualName = "Quality";
67    }
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new UpDownSelector(this, cloner);
70    }
71    #endregion
72
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;
87      bool optimize = true;
88      if (MoveTowardsOptimumParameter.ActualValue == null) {
89        MoveTowardsOptimumParameter.ActualValue = new BoolValue(true);
90      } else {
91        optimize = MoveTowardsOptimumParameter.ActualValue.Value;
92      }
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;
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      }
112      return scopes[list[0].Index];
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.