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 |
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace 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 | } |
---|