Changeset 13261
- Timestamp:
- 11/18/15 19:27:22 (9 years ago)
- Location:
- trunk/sources
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization.Operators/3.3/ChildrenCopyCreator.cs
r13164 r13261 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 48 49 public ChildrenCopyCreator() 49 50 : base() { 50 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose sub-scopes represent the parents."));51 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose sub-scopes should be copied.")); 51 52 } 52 53 … … 56 57 57 58 public override IOperation Apply() { 58 int parents= CurrentScope.SubScopes.Count;59 int nChildren = CurrentScope.SubScopes.Count; 59 60 60 for (int i = 0; i < parents; i++) {61 IScope parent= CurrentScope.SubScopes[i];62 parent.SubScopes.Clear();61 for (int i = 0; i < nChildren; i++) { 62 IScope child = CurrentScope.SubScopes[i]; 63 if (child.SubScopes.Count > 0) throw new ArgumentException("The sub-scope that should be cloned has further sub-scopes."); 63 64 64 //copy parent65 IScope child = new Scope(i.ToString());66 foreach (IVariable var in parent.Variables)67 child .Variables.Add((IVariable)var.Clone());65 IScope childCopy = new Scope(i.ToString()); 66 var cloner = new Cloner(); 67 foreach (IVariable var in child.Variables) 68 childCopy.Variables.Add(cloner.Clone(var)); 68 69 69 parent.SubScopes.Add(child);70 child.SubScopes.Add(childCopy); 70 71 } 71 72 return base.Apply(); -
trunk/sources/HeuristicLab.Selection/3.3/EvolutionStrategyOffspringSelector.cs
r13164 r13261 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Runtime.InteropServices; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; … … 34 35 public class EvolutionStrategyOffspringSelector : SingleSuccessorOperator { 35 36 36 private class QualityComparer : IComparer<IScope> {37 private class FitnessComparer : IComparer<IScope> { 37 38 38 39 #region IComparer<IScope> Member 39 40 40 41 private String qualityParameterName; 41 42 public QualityComparer(String qualityParamName) { 42 private bool maximization; 43 44 public FitnessComparer(String qualityParamName, bool maximization) { 43 45 this.qualityParameterName = qualityParamName; 44 } 45 46 this.maximization = maximization; 47 } 48 49 // less than zero if x is-better-than y 50 // larger than zero if y is-better-than x 51 // zero if both are the same 46 52 public int Compare(IScope x, IScope y) { 47 IVariable quality1, quality2; 48 49 if (x.Variables.TryGetValue(qualityParameterName, out quality1) 50 && y.Variables.TryGetValue(qualityParameterName, out quality2)) { 51 DoubleValue dblVal = quality1.Value as DoubleValue; 52 DoubleValue dblVal2 = quality2.Value as DoubleValue; 53 return dblVal.CompareTo(dblVal2); 54 } 55 else 56 throw new Exception("ERROR!!! Quality Param: "+qualityParameterName); 53 IVariable quality1, quality2; 54 55 if (x.Variables.TryGetValue(qualityParameterName, out quality1) 56 && y.Variables.TryGetValue(qualityParameterName, out quality2)) { 57 DoubleValue left = quality1.Value as DoubleValue; 58 DoubleValue right = quality2.Value as DoubleValue; 59 var res = left.CompareTo(right); 60 if (maximization) return -res; // in the maximization case the largest value should preceed all others in the sort order 61 else return res; 62 } else 63 throw new ArgumentException("Quality variable " + qualityParameterName + " not found."); 57 64 } 58 65 … … 95 102 public ILookupParameter<DoubleValue> QualityParameter { 96 103 get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; } 104 } 105 public ILookupParameter<BoolValue> MaximizationParameter { 106 get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; } 97 107 } 98 108 … … 119 129 Parameters.Add(new ScopeTreeLookupParameter<BoolValue>("SuccessfulOffspring", "True if the offspring was more successful than its parents.", 2)); 120 130 Parameters.Add(new OperatorParameter("OffspringCreator", "The operator used to create new offspring.")); 121 Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));131 Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of solution evaluations.")); 122 132 Parameters.Add(new LookupParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions (approximately).")); 123 133 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of a child")); 134 Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Flag that indicates if the problem is a maximization problem")); 124 135 } 125 136 … … 165 176 // 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 166 177 string tname = SuccessfulOffspringParameter.TranslatedName; 167 double tmpSelPress = selectionPressure.Value, tmpSelPressInc = 1.0 / populationSize; 178 double tmpSelPress = selectionPressure.Value; 179 double tmpSelPressInc = 1.0 / populationSize; 168 180 for (int i = 0; i < offspring.SubScopes.Count; i++) { 169 181 // fetch value … … 176 188 if (tmp.Value) { 177 189 IScope currentOffspring = offspring.SubScopes[i]; 178 offspring.SubScopes.Remove (currentOffspring);179 i--; 190 offspring.SubScopes.RemoveAt(i); 191 i--; // next loop should continue with the subscope at index i which replaced currentOffspring 180 192 population.Add(currentOffspring); 181 193 successfulOffspringAdded++; 182 } 183 else { 194 } else { 184 195 IScope currentOffspring = offspring.SubScopes[i]; 185 offspring.SubScopes.Remove (currentOffspring);196 offspring.SubScopes.RemoveAt(i); 186 197 i--; 187 virtual_population.Add(currentOffspring); 198 virtual_population.Add(currentOffspring); // add to losers pool 188 199 } 189 200 tmpSelPress += tmpSelPressInc; 190 201 191 double tmpSuccessRatio = (successfulOffspring.Value +successfulOffspringAdded) / ((double)populationSize);202 double tmpSuccessRatio = (successfulOffspring.Value + successfulOffspringAdded) / ((double)populationSize); 192 203 if (tmpSuccessRatio >= successRatio && (population.Count + virtual_population.Count) >= populationSize) 193 204 break; … … 200 211 201 212 // check if enough children have been generated (or limit of selection pressure or evaluted solutions is reached) 202 if (((EvaluatedSolutionsParameter.ActualValue.Value < MaximumEvaluatedSolutionsParameter.ActualValue.Value) 203 && (selectionPressure.Value < maxSelPress) 204 && (currentSuccessRatio.Value < successRatio)) 213 if (((EvaluatedSolutionsParameter.ActualValue.Value < MaximumEvaluatedSolutionsParameter.ActualValue.Value) 214 && (selectionPressure.Value < maxSelPress) 215 && (currentSuccessRatio.Value < successRatio)) 205 216 || ((population.Count + virtual_population.Count) < populationSize)) { 206 217 // more children required -> reduce left and start children generation again … … 209 220 while (parents.SubScopes.Count > 0) { 210 221 IScope parent = parents.SubScopes[0]; 211 parents.SubScopes.RemoveAt(0); 212 scope.SubScopes.Add(parent); 213 } 214 215 IOperator moreOffspring= OffspringCreatorParameter.ActualValue as IOperator;216 if ( moreOffspring== null) throw new InvalidOperationException(Name + ": More offspring are required, but no operator specified for creating them.");217 return ExecutionContext.CreateOperation( moreOffspring);222 parents.SubScopes.RemoveAt(0); // TODO: repeated call of RemoveAt(0) is inefficient? 223 scope.SubScopes.Add(parent); // order of subscopes is reversed 224 } 225 226 IOperator offspringCreator = OffspringCreatorParameter.ActualValue as IOperator; 227 if (offspringCreator == null) throw new InvalidOperationException(Name + ": More offspring are required, but no operator specified for creating them."); 228 return ExecutionContext.CreateOperation(offspringCreator); // this assumes that this operator will be called again indirectly or directly 218 229 } else { 219 230 // enough children generated 220 QualityComparer qualityComparer = new QualityComparer(QualityParameter.TranslatedName);221 population.Sort( qualityComparer);222 223 // only keep minimum best successful children in population231 var fitnessComparer = new FitnessComparer(QualityParameter.TranslatedName, MaximizationParameter.ActualValue.Value); 232 population.Sort(fitnessComparer); // sort individuals by descending fitness 233 234 // keeps only the best successRatio * populationSize solutions in the population (the remaining ones are added to the virtual population) 224 235 int removed = 0; 225 236 for (int i = 0; i < population.Count; i++) { 226 237 double tmpSuccessRatio = i / (double)populationSize; 227 238 if (tmpSuccessRatio > successRatio) { 228 229 239 virtual_population.Add(population[i]); 240 removed++; 230 241 } 231 242 } … … 233 244 234 245 //fill up population with best remaining children (successful or unsuccessful) 235 virtual_population.Sort( qualityComparer);246 virtual_population.Sort(fitnessComparer); 236 247 int offspringNeeded = populationSize - population.Count; 237 248 for (int i = 0; i < offspringNeeded && i < virtual_population.Count; i++) { 238 population.Add(virtual_population[i]); 249 population.Add(virtual_population[i]); // children are sorted by descending fitness 239 250 } 240 251
Note: See TracChangeset
for help on using the changeset viewer.