Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Enhanced OSALPS/HeuristicLab.Algorithms.ALPS/3.3/ContinuousMatingPoolCreator.cs @ 15432

Last change on this file since 15432 was 15432, checked in by pfleck, 7 years ago

#2849: AlpsOs2 with a ContinuousMatingPoolCreator

File size: 4.6 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.Algorithms.ALPS {
33  [Item("ContinuousMatingPoolCreator", "An operator which creates mating pools based on a set of sub-populations. For each sub-population, the individuals from the previous sub-population are copied into the current sub-population.")]
34  [StorableClass]
35  public sealed class ContinuousMatingPoolCreator : SingleSuccessorOperator, IMatingPoolCreator {
36    public IValueLookupParameter<DoubleArray> RangesParameter {
37      get { return (IValueLookupParameter<DoubleArray>)Parameters["Ranges"]; }
38    }
39
40    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
41      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
42    }
43    public IValueLookupParameter<BoolValue> MaximizationParameter {
44      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
45    }
46
47
48    #region Constructor, Cloning & Persistence
49    public ContinuousMatingPoolCreator()
50      : base() {
51      Parameters.Add(new ValueLookupParameter<DoubleArray>("Ranges", "The range of sub-populations used for creating a mating pool. (1 = current + previous sub-population)"));
52      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality value contained in each sub-scope which is used for selection."));
53      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the current problem is a maximization problem, otherwise false."));
54    }
55
56    private ContinuousMatingPoolCreator(ContinuousMatingPoolCreator original, Cloner cloner)
57      : base(original, cloner) { }
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new ContinuousMatingPoolCreator(this, cloner);
60    }
61
62    [StorableConstructor]
63    private ContinuousMatingPoolCreator(bool deserializing)
64      : base(deserializing) { }
65    #endregion
66
67    /// <summary>
68    /// Copies the subscopes of the n previous scopes into the current scope. (default n = 1)
69    /// <pre>
70    ///          __ scope __              __ scope __
71    ///         /     |     \            /     |     \
72    ///       Pop1   Pop2   Pop3  =>   Pop1   Pop2   Pop3
73    ///       /|\    /|\    /|\         /|\   /|\    /|\
74    ///       ABC    DEF    GHI         ABC   DEF    GHI
75    ///                                       ABC    DEF
76    /// </pre>
77    /// </summary>
78    /// <returns>The next operation.</returns>
79    public override IOperation Apply() {
80      var subScopes = ExecutionContext.Scope.SubScopes;
81      var ranges = RangesParameter.ActualValue;
82      var qualityName = QualityParameter.TranslatedName;
83      bool maximization = MaximizationParameter.ActualValue.Value;
84
85      for (int targetIndex = subScopes.Count - 1; targetIndex > 0; targetIndex--) {
86        var targetScope = subScopes[targetIndex];
87        var range = ranges.Length == 1 ? ranges[0] : ranges[targetIndex];
88        for (int n = 1; (n <= (range + 1)) && (targetIndex - n >= 0); n++) {
89          var prevScope = subScopes[targetIndex - n];
90          IEnumerable<IScope> individuals = prevScope.SubScopes
91            .OrderBy(i => ((DoubleValue)i.Variables[qualityName].Value).Value);
92          if (maximization) individuals = individuals.Reverse();
93          individuals = individuals.Take((int)(Math.Min(range + 1 - n, 1.0) * prevScope.SubScopes.Count));
94          foreach (var individual in individuals) {
95            targetScope.SubScopes.Add((IScope)individual.Clone());
96          }
97        }
98      }
99
100      return base.Apply();
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.