Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8379 was 8377, checked in by jkarder, 12 years ago

#1247:

  • added support for batch offspring creation
  • added population size analyzer
  • improved operator graph
File size: 4.6 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Algorithms.RAPGA {
34  /// <summary>
35  /// An operator that progressively selects offspring by adding it to a scope list.
36  /// </summary>
37  /// <remarks>
38  /// The operator also performs duplication control.
39  /// </remarks>
40  [Item("ProgressiveOffspringPreserver", "An operator that progressively selects offspring by adding it to a scope list. The operator also performs duplication control.")]
41  [StorableClass]
42  public sealed class ProgressiveOffspringPreserver : SingleSuccessorOperator, ISimilarityBasedOperator {
43    #region ISimilarityBasedOperator Members
44    [Storable]
45    public ISolutionSimilarityCalculator SimilarityCalculator { get; set; }
46    #endregion
47
48    #region Parameter Properties
49    public ScopeParameter CurrentScopeParameter {
50      get { return (ScopeParameter)Parameters["CurrentScope"]; }
51    }
52    public ILookupParameter<ScopeList> OffspringListParameter {
53      get { return (ILookupParameter<ScopeList>)Parameters["OffspringList"]; }
54    }
55    public ILookupParameter<IntValue> ElitesParameter {
56      get { return (ILookupParameter<IntValue>)Parameters["Elites"]; }
57    }
58    public ILookupParameter<IntValue> MaximumPopulationSizeParameter {
59      get { return (ILookupParameter<IntValue>)Parameters["MaximumPopulationSize"]; }
60    }
61    #endregion
62
63    #region Properties
64    private IScope CurrentScope {
65      get { return CurrentScopeParameter.ActualValue; }
66    }
67    private ScopeList OffspringList {
68      get { return OffspringListParameter.ActualValue; }
69    }
70    private IntValue Elites {
71      get { return ElitesParameter.ActualValue; }
72    }
73    private IntValue MaximumPopulationSize {
74      get { return MaximumPopulationSizeParameter.ActualValue; }
75    }
76    #endregion
77
78    [StorableConstructor]
79    private ProgressiveOffspringPreserver(bool deserializing) : base(deserializing) { }
80    private ProgressiveOffspringPreserver(ProgressiveOffspringPreserver original, Cloner cloner)
81      : base(original, cloner) {
82      this.SimilarityCalculator = cloner.Clone(original.SimilarityCalculator);
83    }
84    public ProgressiveOffspringPreserver()
85      : base() {
86      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the offspring."));
87      Parameters.Add(new LookupParameter<ScopeList>("OffspringList", "The list that contains the offspring."));
88      Parameters.Add(new LookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
89      Parameters.Add(new LookupParameter<IntValue>("MaximumPopulationSize", "The maximum size of the population of solutions."));
90    }
91
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new ProgressiveOffspringPreserver(this, cloner);
94    }
95
96    public override IOperation Apply() {
97      if (CurrentScope.SubScopes.Any()) {
98        if (OffspringList.Any()) {
99          var currentOffspring = new Scope();
100          currentOffspring.SubScopes.AddRange(OffspringList);
101          var similarityMatrix = SimilarityCalculator.CalculateSolutionCrowdSimilarity(CurrentScope, currentOffspring);
102          for (int i = CurrentScope.SubScopes.Count - 1; i >= 0 && OffspringList.Count < MaximumPopulationSize.Value - Elites.Value; i--)
103            if (similarityMatrix[i].Any(x => x == 1.0)) CurrentScope.SubScopes.RemoveAt(i);
104            else OffspringList.Add(CurrentScope.SubScopes[i]);
105        } else OffspringList.AddRange(CurrentScope.SubScopes);
106      }
107      return base.Apply();
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.