Changeset 12363
- Timestamp:
- 04/29/15 16:00:38 (10 years ago)
- Location:
- branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm
- Files:
-
- 2 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/Comparators/EliteWeightedParentsQualityComparator.cs
r12354 r12363 31 31 32 32 namespace HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm { 33 [Item(" WeightedParentsQualityComparator", "Compares the quality against that of its parents (assumes the parents are subscopes to the child scope). This operator works with any number of subscopes > 0.")]33 [Item("EliteWeightedParentsQualityComparator", "M2")] 34 34 [StorableClass] 35 public class WeightedParentsQualityComparator : SingleSuccessorOperator, ISubScopesQualityComparatorOperator {35 public class EliteWeightedParentsQualityComparator : SingleSuccessorOperator, ISubScopesQualityComparatorOperator { 36 36 public IValueLookupParameter<BoolValue> MaximizationParameter { 37 37 get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; } … … 46 46 get { return (ILookupParameter<BoolValue>)Parameters["Result"]; } 47 47 } 48 public ILookupParameter<BoolValue> ResultImprovementParameter { 49 get { return (ILookupParameter<BoolValue>)Parameters["ResultImprovement"]; } 50 } 48 51 public ValueLookupParameter<DoubleValue> ComparisonFactorParameter { 49 52 get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; } … … 51 54 52 55 [StorableConstructor] 53 protected WeightedParentsQualityComparator(bool deserializing) : base(deserializing) { }54 protected WeightedParentsQualityComparator(WeightedParentsQualityComparator original, Cloner cloner) : base(original, cloner) { }55 public WeightedParentsQualityComparator()56 protected EliteWeightedParentsQualityComparator(bool deserializing) : base(deserializing) { } 57 protected EliteWeightedParentsQualityComparator(EliteWeightedParentsQualityComparator original, Cloner cloner) : base(original, cloner) { } 58 public EliteWeightedParentsQualityComparator() 56 59 : base() { 57 60 Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise")); … … 59 62 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSide", "The qualities of the parents.")); 60 63 Parameters.Add(new LookupParameter<BoolValue>("Result", "The result of the comparison: True means Quality is better, False means it is worse than parents.")); 64 Parameters.Add(new LookupParameter<BoolValue>("ResultImprovement", "A solution has improved if it is better than the worse parent.")); 61 65 Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactor", "Determines if the quality should be compared to the better parent (1.0), to the worse (0.0) or to any linearly interpolated value between them.")); 62 66 } 63 67 64 68 public override IDeepCloneable Clone(Cloner cloner) { 65 return new WeightedParentsQualityComparator(this, cloner);69 return new EliteWeightedParentsQualityComparator(this, cloner); 66 70 } 67 71 … … 122 126 } 123 127 128 bool resultImprovement = maximization && leftQuality > rightQualities.Min(x => x.Value) || 129 !maximization && leftQuality < rightQualities.Max(x => x.Value); 130 BoolValue resultImprovementValue = ResultImprovementParameter.ActualValue; 131 if (resultImprovementValue == null) { 132 ResultImprovementParameter.ActualValue = new BoolValue(resultImprovement); 133 } else { 134 resultImprovementValue.Value = resultImprovement; 135 } 136 124 137 return base.Apply(); 125 138 } -
branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm-3.3.csproj
r12350 r12363 176 176 <Compile Include="Comparators\UnwantedMutationsComparator.cs" /> 177 177 <Compile Include="Comparators\WeightedParentsDiversityComparator.cs" /> 178 <Compile Include="Comparators\EliteWeightedParentsQualityComparator.cs" /> 178 179 <Compile Include="Comparators\WeightedParentsQualityComparator.cs" /> 179 180 <Compile Include="ISubScopesQualityComparatorOperator.cs" /> 180 181 <Compile Include="OffspringCollector.cs" /> 181 182 <Compile Include="OffspringSelectors\IOffspringSelector.cs" /> 183 <Compile Include="OffspringSelectors\EliteOffspringSelector.cs" /> 182 184 <Compile Include="OffspringSelectors\PopDivOffspringSelector.cs" /> 183 185 <Compile Include="OffspringSelectors\StandardOffspringSelector.cs" /> -
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.