Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1247:

  • added similarity calculators
  • adjusted event handling
  • reformatted code
File size: 4.6 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()) {
96        if (OffspringList.Any()) {
97          var currentOffspring = new Scope();
98          currentOffspring.SubScopes.AddRange(OffspringList);
99          var similarityMatrix = SimilarityCalculator.CalculateSolutionCrowdSimilarity(CurrentScope, currentOffspring);
100          for (int i = CurrentScope.SubScopes.Count - 1; i >= 0 && OffspringList.Count < MaximumPopulationSize.Value - Elites.Value; i--)
101            if (similarityMatrix[i].Any(x => x == 1.0)) CurrentScope.SubScopes.RemoveAt(i);
102            else OffspringList.Add(CurrentScope.SubScopes[i]);
103        } else OffspringList.AddRange(CurrentScope.SubScopes);
104      }
105      return base.Apply();
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.