Changeset 3740 for trunk/sources/HeuristicLab.Selection
- Timestamp:
- 05/10/10 15:10:36 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Selection/3.3/OffspringSelector.cs
r3489 r3740 46 46 get { return (LookupParameter<DoubleValue>)Parameters["CurrentSuccessRatio"]; } 47 47 } 48 public LookupParameter<ItemList<IScope>> WinnersParameter {49 get { return (LookupParameter<ItemList<IScope>>)Parameters[" Winners"]; }48 public LookupParameter<ItemList<IScope>> OffspringPopulationParameter { 49 get { return (LookupParameter<ItemList<IScope>>)Parameters["OffspringPopulation"]; } 50 50 } 51 public LookupParameter<ItemList<IScope>> LuckyLosersParameter { 52 get { return (LookupParameter<ItemList<IScope>>)Parameters["LuckyLosers"]; } 51 public LookupParameter<IntValue> OffspringPopulationWinnersParameter { 52 get { return (LookupParameter<IntValue>)Parameters["OffspringPopulationWinners"]; } 53 } 54 public ScopeTreeLookupParameter<BoolValue> SuccessfulOffspringParameter { 55 get { return (ScopeTreeLookupParameter<BoolValue>)Parameters["SuccessfulOffspring"]; } 53 56 } 54 57 public OperatorParameter OffspringCreatorParameter { … … 67 70 Parameters.Add(new ValueLookupParameter<DoubleValue>("SelectionPressure", "The amount of selection pressure currently necessary to fulfill the success ratio.")); 68 71 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.")); 72 Parameters.Add(new LookupParameter<ItemList<IScope>>("OffspringPopulation", "Temporary store of the offspring population.")); 73 Parameters.Add(new LookupParameter<IntValue>("OffspringPopulationWinners", "Temporary store the number of successful offspring in the offspring population.")); 74 Parameters.Add(new ScopeTreeLookupParameter<BoolValue>("SuccessfulOffspring", "True if the offspring was more successful than its parents.", 2)); 71 75 Parameters.Add(new OperatorParameter("OffspringCreator", "The operator used to create new offspring.")); 72 76 } … … 77 81 IScope scope = ExecutionContext.Scope; 78 82 IScope parents = scope.SubScopes[0]; 79 IScope children= scope.SubScopes[1];83 IScope offspring = scope.SubScopes[1]; 80 84 int populationSize = parents.SubScopes.Count; 85 int offspringSize = offspring.SubScopes.Count; 81 86 82 87 // retrieve actual selection pressure and success ratio … … 92 97 } 93 98 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 // retrieve next population 100 ItemList<IScope> population = OffspringPopulationParameter.ActualValue; 101 IntValue successfulOffspring; 102 if (population == null) { 103 population = new ItemList<IScope>(); 104 OffspringPopulationParameter.ActualValue = population; 99 105 selectionPressure.Value = 0; // initialize selection pressure for this round 100 106 currentSuccessRatio.Value = 0; // initialize current success ratio for this round 107 successfulOffspring = new IntValue(0); 108 OffspringPopulationWinnersParameter.ActualValue = successfulOffspring; 109 } else successfulOffspring = OffspringPopulationWinnersParameter.ActualValue; 110 111 int worseOffspringNeeded = (int)((1 - successRatio) * populationSize) - (population.Count - successfulOffspring.Value); 112 int successfulOffspringAdded = 0; 113 114 // implement the ActualValue fetch here - otherwise the parent scope would also be included, given that there may be 1000 or more parents, this is quite unnecessary 115 string tname = SuccessfulOffspringParameter.TranslatedName; 116 double tmpSelPress = selectionPressure.Value, tmpSelPressInc = 1.0 / populationSize; 117 for (int i = 0; i < offspringSize; i++) { 118 // fetch value 119 IVariable tmpVar; 120 if (!offspring.SubScopes[i].Variables.TryGetValue(tname, out tmpVar)) throw new InvalidOperationException(Name + ": Could not determine if an offspring was successful or not."); 121 BoolValue tmp = (tmpVar.Value as BoolValue); 122 if (tmp == null) throw new InvalidOperationException(Name + ": The variable that indicates whether an offspring is successful or not must contain a BoolValue."); 123 124 // add to population 125 if (tmp.Value) { 126 population.Add(offspring.SubScopes[i]); 127 successfulOffspringAdded++; 128 } else if (worseOffspringNeeded > 0 || tmpSelPress >= maxSelPress) { 129 population.Add(offspring.SubScopes[i]); 130 worseOffspringNeeded--; 131 } 132 tmpSelPress += tmpSelPressInc; 133 if (population.Count == populationSize) break; 101 134 } 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 } 135 successfulOffspring.Value += successfulOffspringAdded; 121 136 122 137 // calculate actual selection pressure and success ratio 123 selectionPressure.Value += (winnersCount + losersCount) / ((double)populationSize);124 currentSuccessRatio.Value = winners.Count/ ((double)populationSize);138 selectionPressure.Value += offspringSize / (double)populationSize; 139 currentSuccessRatio.Value = successfulOffspring.Value / ((double)populationSize); 125 140 126 141 // check if enough children have been generated 127 142 if (((selectionPressure.Value < maxSelPress) && (currentSuccessRatio.Value < successRatio)) || 128 ( (winners.Count + luckyLosers.Count)< populationSize)) {143 (population.Count < populationSize)) { 129 144 // more children required -> reduce left and start children generation again 130 145 scope.SubScopes.Remove(parents); 131 scope.SubScopes.Remove( children);146 scope.SubScopes.Remove(offspring); 132 147 while(parents.SubScopes.Count > 0) 133 148 scope.SubScopes.Add(parents.SubScopes[0]); … … 138 153 } else { 139 154 // 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 } 155 offspring.SubScopes.Clear(); 156 offspring.SubScopes.AddRange(population); 150 157 151 scope.Variables.Remove( WinnersParameter.ActualName);152 scope.Variables.Remove( LuckyLosersParameter.ActualName);158 scope.Variables.Remove(OffspringPopulationParameter.ActualName); 159 scope.Variables.Remove(OffspringPopulationWinnersParameter.ActualName); 153 160 return base.Apply(); 154 161 }
Note: See TracChangeset
for help on using the changeset viewer.