Changeset 12363 for branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/OffspringSelectors
- Timestamp:
- 04/29/15 16:00:38 (10 years ago)
- Location:
- branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/OffspringSelectors
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/OffspringSelectors/EliteOffspringSelector.cs
r12354 r12363 34 34 35 35 namespace HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm { 36 [Item(" PopDivOffspringSelector", "")]36 [Item("EliteOffspringSelector", "M2")] 37 37 [StorableClass] 38 public class PopDivOffspringSelector : InstrumentedOperator, IOffspringSelector {38 public class EliteOffspringSelector : InstrumentedOperator, IOffspringSelector { 39 39 40 40 private const string SuccessRatioChart = "SuccessRatioChart"; … … 91 91 92 92 [StorableConstructor] 93 protected PopDivOffspringSelector(bool deserializing) : base(deserializing) { }94 protected PopDivOffspringSelector(PopDivOffspringSelector original, Cloner cloner)93 protected EliteOffspringSelector(bool deserializing) : base(deserializing) { } 94 protected EliteOffspringSelector(EliteOffspringSelector original, Cloner cloner) 95 95 : base(original, cloner) { 96 96 } 97 97 public override IDeepCloneable Clone(Cloner cloner) { 98 return new PopDivOffspringSelector(this, cloner);99 } 100 public PopDivOffspringSelector()98 return new EliteOffspringSelector(this, cloner); 99 } 100 public EliteOffspringSelector() 101 101 : base() { 102 102 Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure which prematurely terminates the offspring selection step.")); … … 192 192 int worseOffspringNeeded = (int)((1 - successRatio) * populationSize) - (population.Count - successfulOffspring.Value); 193 193 int successfulOffspringAdded = 0; 194 195 194 double avgPopDiv = DiversityComparisonFactorParameter.Value.Value; 196 bool takeWorseOffspring = false;197 195 198 196 // 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 199 197 string tname = SuccessfulOffspringParameter.TranslatedName; 200 198 double tmpSelPress = selectionPressure.Value, tmpSelPressInc = 1.0 / populationSize; 201 int cnt = 0;202 int stepWidth = offspring.SubScopes.Count / 5;203 199 for (int i = 0; i < offspring.SubScopes.Count; i++) { 204 //calculate population diversity so far205 if (cnt != 0 && cnt % stepWidth == 0) {206 Scope tmpScope = new Scope();207 tmpScope.SubScopes.AddRange(population);208 double popDiversity = SimilarityCalculatorParameter.ActualValue.CalculateSolutionCrowdSimilarity(tmpScope).Average(x => x.Average());209 //this assumes that low-quality solutions are of high diversity210 if (popDiversity > avgPopDiv) {211 takeWorseOffspring = true;212 } else {213 takeWorseOffspring = false;214 }215 }216 cnt++;217 218 200 // fetch value 219 201 IVariable tmpVar; … … 222 204 if (tmp == null) throw new InvalidOperationException(Name + ": The variable that indicates whether an offspring is successful or not must contain a BoolValue."); 223 205 224 if (takeWorseOffspring) { 225 IScope currentOffspring = offspring.SubScopes[i]; 226 Scope tmpScope = new Scope(); 227 tmpScope.SubScopes.AddRange(population); 228 Scope tmpCurrentScope = new Scope(); 229 tmpCurrentScope.SubScopes.Add(currentOffspring); 230 double curDiv = SimilarityCalculatorParameter.ActualValue.CalculateSolutionCrowdSimilarity(tmpCurrentScope, tmpScope)[0].Average(); 231 if (curDiv < avgPopDiv) { 232 offspring.SubScopes.Remove(currentOffspring); 233 i--; 234 population.Add(currentOffspring); 235 worseOffspringNeeded--; 236 } 237 } else if (tmp.Value) { 206 if (!offspring.SubScopes[i].Variables.TryGetValue("ResultImprovement", out tmpVar)) throw new InvalidOperationException(Name + ": Could not find ResultImprovment."); 207 BoolValue betterThanWorseParent = (tmpVar.Value as BoolValue); 208 if (betterThanWorseParent == null) throw new InvalidOperationException(Name + ": Could not find ResultImprovment."); 209 210 //this is an "elite" solution candidate 211 double tmpCurSuccRatio = (successfulOffspring.Value + successfulOffspringAdded) / (double)populationSize; 212 if (tmp.Value && tmpCurSuccRatio <= successRatio) { 213 //this overrides the diversity mechanism; always fill up elites if there is space 238 214 IScope currentOffspring = offspring.SubScopes[i]; 239 215 offspring.SubScopes.Remove(currentOffspring); … … 241 217 population.Add(currentOffspring); 242 218 successfulOffspringAdded++; 243 } else if (worseOffspringNeeded > 0 || tmpSelPress >= maxSelPress) { 244 IScope currentOffspring; 245 if (!fillPopulationWithParents || worseOffspringNeeded > 0) { 246 currentOffspring = offspring.SubScopes[i]; 219 } else if (!tmp.Value && betterThanWorseParent.Value) { 220 //quality is ok, check for diversity 221 IScope currentOffspring = offspring.SubScopes[i]; 222 223 if (!population.Any()) { 247 224 offspring.SubScopes.Remove(currentOffspring); 248 225 i--; 249 226 worseOffspringNeeded--; 227 population.Add(currentOffspring); 250 228 } else { 251 currentOffspring = parents.SubScopes[i]; 229 Scope tmpScope = new Scope(); 230 tmpScope.SubScopes.AddRange(population); 231 Scope tmpCurrentScope = new Scope(); 232 tmpCurrentScope.SubScopes.Add(currentOffspring); 233 double curDiv = SimilarityCalculatorParameter.ActualValue.CalculateSolutionCrowdSimilarity(tmpCurrentScope, tmpScope)[0].Average(); 234 if (curDiv < avgPopDiv) { 235 if (worseOffspringNeeded > 0 || tmpSelPress >= maxSelPress) { 236 if (!fillPopulationWithParents || worseOffspringNeeded > 0) { 237 offspring.SubScopes.Remove(currentOffspring); 238 i--; 239 worseOffspringNeeded--; 240 } else { 241 currentOffspring = parents.SubScopes[i]; 242 } 243 population.Add(currentOffspring); 244 } 245 } 252 246 } 253 population.Add(currentOffspring);254 247 } 248 255 249 tmpSelPress += tmpSelPressInc; 256 250 if (population.Count >= populationSize) break; -
branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/OffspringSelectors/PopDivOffspringSelector.cs
r12352 r12363 34 34 35 35 namespace HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm { 36 [Item("PopDivOffspringSelector", " ")]36 [Item("PopDivOffspringSelector", "M1")] 37 37 [StorableClass] 38 38 public class PopDivOffspringSelector : InstrumentedOperator, IOffspringSelector {
Note: See TracChangeset
for help on using the changeset viewer.