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