Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Algorithms.RAPGA/3.3/ProgressiveOffspringPreserver.cs @ 11245

Last change on this file since 11245 was 8989, checked in by jkarder, 12 years ago

#1890: refactored DuplicatesSelector and ProgressiveOffspringPreserver

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.RAPGA {
32  /// <summary>
33  /// An operator that progressively selects offspring by adding it to a scope list.
34  /// </summary>
35  /// <remarks>
36  /// The operator also performs duplication control.
37  /// </remarks>
38  [Item("ProgressiveOffspringPreserver", "An operator that progressively selects offspring by adding it to a scope list. The operator also performs duplication control.")]
39  [StorableClass]
40  public sealed class ProgressiveOffspringPreserver : SingleSuccessorOperator, ISimilarityBasedOperator {
41    #region ISimilarityBasedOperator Members
42    [Storable]
43    public ISolutionSimilarityCalculator SimilarityCalculator { get; set; }
44    #endregion
45
46    #region Parameter Properties
47    public ScopeParameter CurrentScopeParameter {
48      get { return (ScopeParameter)Parameters["CurrentScope"]; }
49    }
50    public ILookupParameter<ScopeList> OffspringListParameter {
51      get { return (ILookupParameter<ScopeList>)Parameters["OffspringList"]; }
52    }
53    public ILookupParameter<IntValue> ElitesParameter {
54      get { return (ILookupParameter<IntValue>)Parameters["Elites"]; }
55    }
56    public ILookupParameter<IntValue> MaximumPopulationSizeParameter {
57      get { return (ILookupParameter<IntValue>)Parameters["MaximumPopulationSize"]; }
58    }
59    #endregion
60
61    #region Properties
62    private IScope CurrentScope {
63      get { return CurrentScopeParameter.ActualValue; }
64    }
65    private ScopeList OffspringList {
66      get { return OffspringListParameter.ActualValue; }
67    }
68    private IntValue Elites {
69      get { return ElitesParameter.ActualValue; }
70    }
71    private IntValue MaximumPopulationSize {
72      get { return MaximumPopulationSizeParameter.ActualValue; }
73    }
74    #endregion
75
76    [StorableConstructor]
77    private ProgressiveOffspringPreserver(bool deserializing) : base(deserializing) { }
78    private ProgressiveOffspringPreserver(ProgressiveOffspringPreserver original, Cloner cloner)
79      : base(original, cloner) {
80      this.SimilarityCalculator = cloner.Clone(original.SimilarityCalculator);
81    }
82    public ProgressiveOffspringPreserver()
83      : base() {
84      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the offspring."));
85      Parameters.Add(new LookupParameter<ScopeList>("OffspringList", "The list that contains the offspring."));
86      Parameters.Add(new LookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
87      Parameters.Add(new LookupParameter<IntValue>("MaximumPopulationSize", "The maximum size of the population of solutions."));
88    }
89
90    public override IDeepCloneable Clone(Cloner cloner) {
91      return new ProgressiveOffspringPreserver(this, cloner);
92    }
93
94    public override IOperation Apply() {
95      if (CurrentScope.SubScopes.Any()) { // offspring created
96        if (!OffspringList.Any()) OffspringList.AddRange(CurrentScope.SubScopes);
97        else { // stored offspring exists
98          var storedOffspringScope = new Scope();
99          storedOffspringScope.SubScopes.AddRange(OffspringList);
100          var similarityMatrix = SimilarityCalculator.CalculateSolutionCrowdSimilarity(CurrentScope, storedOffspringScope);
101
102          var createdOffspring = CurrentScope.SubScopes.ToArray();
103
104          int i = 0;
105          // as long as offspring is available and not enough offspring has been preserved
106          while (i < createdOffspring.Length && OffspringList.Count < MaximumPopulationSize.Value - Elites.Value) {
107            if (similarityMatrix[i].Any(x => x.IsAlmost(1.0))) createdOffspring[i] = null; // discard duplicates
108            else OffspringList.Add(createdOffspring[i]);
109            i++;
110          }
111
112          // discard remaining offspring
113          while (i < createdOffspring.Length) createdOffspring[i++] = null;
114
115          // clean current scope
116          CurrentScope.SubScopes.Replace(createdOffspring.Where(x => x != null));
117        }
118      }
119      return base.Apply();
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.