Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection/3.3/OffspringSelector.cs @ 3413

Last change on this file since 3413 was 3413, checked in by abeham, 14 years ago

Updated Gender Specific Selection, Offspring Selector and other changes related to the OSGA #976
Removed old OS plugin, removed SelectedSubScopesLookupParameter

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Selection {
32  [Item("OffspringSelector", "Selects among the offspring population those that are designated successful and discards the unsuccessful offspring, except for some lucky losers. It expects the parent scopes to be below the first sub-scope, and offspring scopes to be below the second sub-scope separated again in two sub-scopes, the first with the failed offspring and the second with successful offspring.")]
33  [StorableClass]
34  public class OffspringSelector : SingleSuccessorOperator {
35
36    public ValueLookupParameter<IntValue> PopulationSizeParameter {
37      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
38    }
39    public ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
40      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
41    }
42    public ValueLookupParameter<DoubleValue> SuccessRatioParameter {
43      get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
44    }
45    public LookupParameter<DoubleValue> SelectionPressureParameter {
46      get { return (ValueLookupParameter<DoubleValue>)Parameters["SelectionPressure"]; }
47    }
48    public LookupParameter<DoubleValue> CurrentSuccessRatioParameter {
49      get { return (LookupParameter<DoubleValue>)Parameters["CurrentSuccessRatio"]; }
50    }
51    public LookupParameter<ItemList<IScope>> WinnersParameter {
52      get { return (LookupParameter<ItemList<IScope>>)Parameters["Winners"]; }
53    }
54    public LookupParameter<ItemList<IScope>> LuckyLosersParameter {
55      get { return (LookupParameter<ItemList<IScope>>)Parameters["LuckyLosers"]; }
56    }
57    public OperatorParameter OffspringCreatorParameter {
58      get { return (OperatorParameter)Parameters["OffspringCreator"]; }
59    }
60
61    public IOperator OffspringCreator {
62      get { return OffspringCreatorParameter.Value; }
63      set { OffspringCreatorParameter.Value = value; }
64    }
65
66    public OffspringSelector()
67      : base() {
68      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The number of offspring to create."));
69      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure which prematurely terminates the offspring selection step."));
70      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful offspring that has to be produced."));
71      Parameters.Add(new ValueLookupParameter<DoubleValue>("SelectionPressure", "The amount of selection pressure currently necessary to fulfill the success ratio."));
72      Parameters.Add(new ValueLookupParameter<DoubleValue>("CurrentSuccessRatio", "The current success ratio indicates how much of the successful offspring have already been generated."));
73      Parameters.Add(new LookupParameter<ItemList<IScope>>("Winners", "Temporary store of the successful offspring."));
74      Parameters.Add(new LookupParameter<ItemList<IScope>>("LuckyLosers", "Temporary store of the lucky losers."));
75      Parameters.Add(new OperatorParameter("OffspringCreator", "The operator used to create new offspring."));
76    }
77
78    public override IOperation Apply() {
79      int populationSize = PopulationSizeParameter.ActualValue.Value;
80      double maxSelPress = MaximumSelectionPressureParameter.ActualValue.Value;
81      double successRatio = SuccessRatioParameter.ActualValue.Value;
82      IScope scope = ExecutionContext.Scope;
83      IScope parents = scope.SubScopes[0];
84      IScope children = scope.SubScopes[1];
85
86      // retrieve actual selection pressure and success ratio
87      DoubleValue selectionPressure = SelectionPressureParameter.ActualValue;
88      if (selectionPressure == null) {
89        selectionPressure = new DoubleValue(0);
90        SelectionPressureParameter.ActualValue = selectionPressure;
91      }
92      DoubleValue currentSuccessRatio = CurrentSuccessRatioParameter.ActualValue;
93      if (currentSuccessRatio == null) {
94        currentSuccessRatio = new DoubleValue(0);
95        CurrentSuccessRatioParameter.ActualValue = currentSuccessRatio;
96      }
97
98      // retrieve winners and lucky losers
99      ItemList<IScope> winners = WinnersParameter.ActualValue;
100      if (winners == null) {
101        winners = new ItemList<IScope>();
102        WinnersParameter.ActualValue = winners;
103        selectionPressure.Value = 0; // initialize selection pressure for this round
104        currentSuccessRatio.Value = 0; // initialize current success ratio for this round
105      }
106      ItemList<IScope> luckyLosers = LuckyLosersParameter.ActualValue;
107      if (luckyLosers == null) {
108        luckyLosers = new ItemList<IScope>();
109        LuckyLosersParameter.ActualValue = luckyLosers;
110      }
111
112      // separate new offspring in winners and lucky losers, the unlucky losers are discarded, sorry guys
113      int winnersCount = 0;
114      int losersCount = 0;
115      ScopeList offspring = children.SubScopes[1].SubScopes; // the winners
116      winnersCount += offspring.Count;
117      winners.AddRange(offspring);
118      offspring = children.SubScopes[0].SubScopes; // the losers
119      while (offspring.Count > 0 && ((1 - successRatio) * populationSize > luckyLosers.Count ||
120            selectionPressure.Value >= maxSelPress)) {
121        luckyLosers.Add(offspring[0]);
122        losersCount++;
123        offspring.RemoveAt(0);
124      }
125      losersCount += offspring.Count;
126      children.SubScopes.Clear();
127
128      // calculate actual selection pressure and success ratio
129      selectionPressure.Value += (winnersCount + losersCount) / ((double)populationSize);
130      currentSuccessRatio.Value = winners.Count / ((double)populationSize);
131
132      // check if enough children have been generated
133      if (((selectionPressure.Value < maxSelPress) && (currentSuccessRatio.Value < successRatio)) ||
134          ((winners.Count + luckyLosers.Count) < populationSize)) {
135        // more children required -> reduce left and start children generation again
136        scope.SubScopes.Remove(parents);
137        scope.SubScopes.Remove(children);
138        while(parents.SubScopes.Count > 0)
139          scope.SubScopes.Add(parents.SubScopes[0]);
140
141        IOperator moreOffspring = OffspringCreatorParameter.ActualValue as IOperator;
142        if (moreOffspring == null) throw new InvalidOperationException(Name + ": More offspring are required, but no operator specified for creating them.");
143        return ExecutionContext.CreateOperation(moreOffspring);
144      } else {
145        // enough children generated
146        while (children.SubScopes.Count < populationSize) {
147          if (winners.Count > 0) {
148            children.SubScopes.Add((IScope)winners[0]);
149            winners.RemoveAt(0);
150          } else {
151            children.SubScopes.Add((IScope)luckyLosers[0]);
152            luckyLosers.RemoveAt(0);
153          }
154        }
155
156        scope.Variables.Remove(WinnersParameter.ActualName);
157        scope.Variables.Remove(LuckyLosersParameter.ActualName);
158        return base.Apply();
159      }
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.