Changeset 12350
- Timestamp:
- 04/28/15 14:37:20 (10 years ago)
- Location:
- branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm-3.3.csproj
r11884 r12350 180 180 <Compile Include="OffspringCollector.cs" /> 181 181 <Compile Include="OffspringSelectors\IOffspringSelector.cs" /> 182 <Compile Include="OffspringSelectors\PopDivOffspringSelector.cs" /> 182 183 <Compile Include="OffspringSelectors\StandardOffspringSelector.cs" /> 183 184 <Compile Include="ProbabilitiesGenerator.cs" /> -
branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/OffspringSelectors/PopDivOffspringSelector.cs
r12347 r12350 21 21 22 22 using System; 23 using System.Linq; 24 using HeuristicLab.Analysis; 23 25 using HeuristicLab.Common; 24 26 using HeuristicLab.Core; 25 27 using HeuristicLab.Data; 26 28 using HeuristicLab.Operators; 29 using HeuristicLab.Optimization; 30 using HeuristicLab.Optimization.Operators; 27 31 using HeuristicLab.Parameters; 28 32 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 33 using HeuristicLab.PluginInfrastructure; 29 34 30 35 namespace HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm { 31 [Item(" StandardOffspringSelector", "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.")]36 [Item("PopDivOffspringSelector", "")] 32 37 [StorableClass] 33 public class StandardOffspringSelector : InstrumentedOperator, IOffspringSelector { 38 public class PopDivOffspringSelector : InstrumentedOperator, IOffspringSelector { 39 40 private const string SuccessRatioChart = "SuccessRatioChart"; 41 private const string SuccessRatioDataRow = "SuccessRatio"; 42 43 34 44 public ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter { 35 45 get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; } … … 59 69 get { return (ILookupParameter<BoolValue>)Parameters["EnoughChildrenGenerated"]; } 60 70 } 61 71 public IValueLookupParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter { 72 get { return (IValueLookupParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; } 73 } 74 public ValueLookupParameter<ResultCollection> ResultsParameter { 75 get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; } 76 } 77 78 public ValueLookupParameter<DoubleValue> DiversityComparisonFactorParameter { 79 get { return (ValueLookupParameter<DoubleValue>)Parameters["DiversityComparisonFactor"]; } 80 } 81 private ValueLookupParameter<DoubleValue> ComparisonFactorLowerBoundParameter { 82 get { return (ValueLookupParameter<DoubleValue>)Parameters["DiversityComparisonFactorLowerBound"]; } 83 } 84 private ValueLookupParameter<DoubleValue> ComparisonFactorUpperBoundParameter { 85 get { return (ValueLookupParameter<DoubleValue>)Parameters["DiversityComparisonFactorUpperBound"]; } 86 } 87 88 public IConstrainedValueParameter<IDiscreteDoubleValueModifier> ComparisonFactorModifierParameter { 89 get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["ComparisonFactorModifier"]; } 90 } 62 91 63 92 [StorableConstructor] 64 protected StandardOffspringSelector(bool deserializing) : base(deserializing) { } 65 [StorableHook(HookType.AfterDeserialization)] 66 private void AfterDeserialization() { 67 // BackwardsCompatibility3.3 68 #region Backwards compatible code, remove with 3.4 69 if (Parameters.ContainsKey("FillPopulationWithParents") && Parameters["FillPopulationWithParents"] is FixedValueParameter<BoolValue>) 70 Parameters.Remove("FillPopulationWithParents"); 71 if (!Parameters.ContainsKey("FillPopulationWithParents")) 72 Parameters.Add(new ValueLookupParameter<BoolValue>("FillPopulationWithParents", "True if the population should be filled with parent individuals instead of lucky losers.")); 73 #endregion 74 } 75 76 protected StandardOffspringSelector(StandardOffspringSelector original, Cloner cloner) : base(original, cloner) { } 93 protected PopDivOffspringSelector(bool deserializing) : base(deserializing) { } 94 protected PopDivOffspringSelector(PopDivOffspringSelector original, Cloner cloner) 95 : base(original, cloner) { 96 } 77 97 public override IDeepCloneable Clone(Cloner cloner) { 78 return new StandardOffspringSelector(this, cloner);79 } 80 public StandardOffspringSelector()98 return new PopDivOffspringSelector(this, cloner); 99 } 100 public PopDivOffspringSelector() 81 101 : base() { 82 102 Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure which prematurely terminates the offspring selection step.")); … … 89 109 Parameters.Add(new ValueLookupParameter<BoolValue>("FillPopulationWithParents", "True if the population should be filled with parent individual or false if worse children should be used when the maximum selection pressure is exceeded.")); 90 110 Parameters.Add(new LookupParameter<BoolValue>("EnoughChildrenGenerated", "True if enough children have been generated, otherwise false.")); 111 Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity.")); 112 Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the population diversity analysis results should be stored.")); 113 114 Parameters.Add(new ValueLookupParameter<DoubleValue>("DiversityComparisonFactor", "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.", new DoubleValue(0.0))); 115 Parameters.Add(new ValueLookupParameter<DoubleValue>("DiversityComparisonFactorLowerBound", "The lower bound of the comparison factor (start).", new DoubleValue(0.5))); 116 Parameters.Add(new ValueLookupParameter<DoubleValue>("DiversityComparisonFactorUpperBound", "The upper bound of the comparison factor (end).", new DoubleValue(1.0))); 117 Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("ComparisonFactorModifier", "The operator used to modify the comparison factor.", new ItemSet<IDiscreteDoubleValueModifier>(new IDiscreteDoubleValueModifier[] { new LinearDiscreteDoubleValueModifier() }), new LinearDiscreteDoubleValueModifier())); 118 119 120 foreach (IDiscreteDoubleValueModifier modifier in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name)) 121 ComparisonFactorModifierParameter.ValidValues.Add(modifier); 122 IDiscreteDoubleValueModifier linearModifier = ComparisonFactorModifierParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("LinearDiscreteDoubleValueModifier")); 123 if (linearModifier != null) ComparisonFactorModifierParameter.Value = linearModifier; 124 ParameterizeComparisonFactorModifiers(); 125 126 this.AfterExecutionOperators.Clear(); 127 this.AfterExecutionOperators.Add(ComparisonFactorModifierParameter.Value); 128 ComparisonFactorModifierParameter.ValueChanged += ComparisonFactorModifierParameter_ValueChanged; 129 } 130 131 void ComparisonFactorModifierParameter_ValueChanged(object sender, EventArgs e) { 132 this.AfterExecutionOperators.Clear(); 133 this.AfterExecutionOperators.Add(ComparisonFactorModifierParameter.Value); 134 } 135 136 private void ParameterizeComparisonFactorModifiers() { 137 //TODO: does not work if Generations parameter names are changed 138 foreach (IDiscreteDoubleValueModifier modifier in ComparisonFactorModifierParameter.ValidValues) { 139 modifier.IndexParameter.ActualName = "Generations"; 140 modifier.EndIndexParameter.ActualName = "MaximumGenerations"; 141 modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name; 142 modifier.StartIndexParameter.Value = new IntValue(0); 143 modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name; 144 modifier.ValueParameter.ActualName = "DiversityComparisonFactor"; 145 } 91 146 } 92 147 … … 112 167 currentSuccessRatio = new DoubleValue(0); 113 168 CurrentSuccessRatioParameter.ActualValue = currentSuccessRatio; 169 } 170 171 DataTable successRatioDataTable; 172 if (ResultsParameter.ActualValue.ContainsKey(SuccessRatioChart)) { 173 successRatioDataTable = (DataTable)ResultsParameter.ActualValue[SuccessRatioChart].Value; 174 } else { 175 successRatioDataTable = new DataTable(SuccessRatioChart); 176 successRatioDataTable.Rows.Add(new DataRow(SuccessRatioDataRow)); 177 ResultsParameter.ActualValue.Add(new Result(SuccessRatioChart, successRatioDataTable)); 114 178 } 115 179 … … 129 193 int successfulOffspringAdded = 0; 130 194 195 double avgPopDiv = DiversityComparisonFactorParameter.Value.Value; 196 bool takeWorseOffspring = false; 197 131 198 // 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 132 199 string tname = SuccessfulOffspringParameter.TranslatedName; 133 200 double tmpSelPress = selectionPressure.Value, tmpSelPressInc = 1.0 / populationSize; 201 int cnt = 0; 202 int stepWidth = offspring.SubScopes.Count / 5; 134 203 for (int i = 0; i < offspring.SubScopes.Count; i++) { 204 //calculate population diversity so far 205 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 diversity 210 if (popDiversity > avgPopDiv) { 211 takeWorseOffspring = true; 212 } else { 213 takeWorseOffspring = false; 214 } 215 } 216 cnt++; 217 135 218 // fetch value 136 219 IVariable tmpVar; … … 139 222 if (tmp == null) throw new InvalidOperationException(Name + ": The variable that indicates whether an offspring is successful or not must contain a BoolValue."); 140 223 141 // add to population 142 if (tmp.Value) { 224 if (takeWorseOffspring) { 225 IScope currentOffspring = offspring.SubScopes[i]; 226 offspring.SubScopes.Remove(currentOffspring); 227 i--; 228 population.Add(currentOffspring); 229 worseOffspringNeeded--; 230 } else if (tmp.Value) { 143 231 IScope currentOffspring = offspring.SubScopes[i]; 144 232 offspring.SubScopes.Remove(currentOffspring); … … 159 247 } 160 248 tmpSelPress += tmpSelPressInc; 161 if (population.Count == populationSize) break;249 if (population.Count >= populationSize) break; 162 250 } 163 251 successfulOffspring.Value += successfulOffspringAdded; … … 166 254 selectionPressure.Value = tmpSelPress; 167 255 currentSuccessRatio.Value = successfulOffspring.Value / ((double)populationSize); 168 256 successRatioDataTable.Rows[SuccessRatioDataRow].Values.Add(currentSuccessRatio.Value); 169 257 170 258 if (EnoughChildrenGeneratedParameter.ActualValue == null) { … … 192 280 EnoughChildrenGeneratedParameter.ActualValue.Value = true; 193 281 } 282 194 283 return base.InstrumentedApply(); 195 284 }
Note: See TracChangeset
for help on using the changeset viewer.