Changeset 5646 for trunk/sources/HeuristicLab.Selection
- Timestamp:
- 03/09/11 20:58:30 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Selection/3.3/NoSameMatesSelector.cs
r5596 r5646 19 19 private const string QualityDifferencePercentageParameterName = "QualityDifferencePercentage"; 20 20 private const string QualityDifferenceMaxAttemptsParameterName = "QualityDifferenceMaxAttempts"; 21 private const string QualityDifferenceUseRangeParameterName = "QualityDifferenceUseRange"; 21 22 22 23 #region Parameters … … 30 31 get { return (IValueParameter<IntValue>)Parameters[QualityDifferenceMaxAttemptsParameterName]; } 31 32 } 33 public IValueParameter<BoolValue> QualityDifferenceUseRangeParameter { 34 get { return (IValueParameter<BoolValue>)Parameters[QualityDifferenceUseRangeParameterName]; } 35 } 32 36 #endregion 33 37 … … 45 49 public IntValue QualityDifferenceMaxAttempts { 46 50 get { return QualityDifferenceMaxAttemptsParameter.Value; } 51 } 52 public BoolValue QualityDifferenceUseRange { 53 get { return QualityDifferenceUseRangeParameter.Value; } 47 54 } 48 55 #endregion … … 60 67 Parameters.Add(new ValueParameter<PercentValue>(QualityDifferencePercentageParameterName, "The minimum quality difference from parent1 to parent2 to accept the selection.", new PercentValue(0.05))); 61 68 Parameters.Add(new ValueParameter<IntValue>(QualityDifferenceMaxAttemptsParameterName, "The maximum number of attempts to find parents which differ in quality.", new IntValue(5))); 69 Parameters.Add(new ValueParameter<BoolValue>(QualityDifferenceUseRangeParameterName, "Use the range from minimum to maximum quality as basis for QualityDifferencePercentage.", new BoolValue(false))); 62 70 #endregion 63 71 … … 67 75 [StorableHook(HookType.AfterDeserialization)] 68 76 private void AfterDeserialization() { 77 if (!Parameters.ContainsKey(QualityDifferenceUseRangeParameterName)) 78 Parameters.Add(new ValueParameter<BoolValue>(QualityDifferenceUseRangeParameterName, "Use the range from minimum to maximum quality as basis for QualityDifferencePercentage.", new BoolValue(false))); 69 79 Initialize(); 70 80 } … … 73 83 int count = NumberOfSelectedSubScopes.Value; 74 84 if (count % 2 > 0) throw new InvalidOperationException(Name + ": There must be an equal number of sub-scopes to be selected."); 85 int limit = count - 1; 75 86 IScope[] selected = new IScope[count]; 76 87 77 88 double qualityDifferencePercentage = QualityDifferencePercentage.Value; 78 89 int qualityDifferenceMaxAttempts = QualityDifferenceMaxAttempts.Value; 79 90 bool qualityDifferenceUseRange = QualityDifferenceUseRange.Value; 91 string qualityName = QualityParameter.ActualName; 92 93 // get minimum and maximum quality, calculate quality offsets 94 double minQualityOffset = 0; 95 double maxQualityOffset = 0; 96 if (qualityDifferenceUseRange) { 97 ItemArray<DoubleValue> qualities = QualityParameter.ActualValue; 98 double minQuality = double.MaxValue, maxQuality = double.MinValue; 99 for (int l = 0; l < qualities.Length; l++) { 100 if (qualities[l].Value < minQuality) minQuality = qualities[l].Value; 101 if (qualities[l].Value > maxQuality) maxQuality = qualities[l].Value; 102 } // maximization flag is not needed because only the range is relevant 103 minQualityOffset = (maxQuality - minQuality) * qualityDifferencePercentage; 104 } else { 105 maxQualityOffset = 1.0 + qualityDifferencePercentage; 106 minQualityOffset = 1.0 - qualityDifferencePercentage; 107 } 108 109 ScopeList parents, remaining; 110 double qualityParent1, qualityParent2; 111 bool parentsDifferent; 80 112 int attempts = 1; 81 int j= 0;82 while ( j < count - 1) { // repeat until enough parents are selected113 int i, j, k = 0; 114 while (k < limit) { // repeat until enough parents are selected 83 115 ApplyInnerSelector(); 84 ScopeList parents = CurrentScope.SubScopes[1].SubScopes; 85 86 for (int i = 0; j < count - 1 && i < parents.Count / 2; i++) { 87 double qualityParent1 = ((DoubleValue)parents[i * 2].Variables[QualityParameter.ActualName].Value).Value; 88 double qualityParent2 = ((DoubleValue)parents[i * 2 + 1].Variables[QualityParameter.ActualName].Value).Value; 89 90 bool parentsDifferent = (qualityParent2 > qualityParent1 * (1.0 + qualityDifferencePercentage) || 91 qualityParent2 < qualityParent1 * (1.0 - qualityDifferencePercentage)); 116 parents = CurrentScope.SubScopes[1].SubScopes; 117 118 for (i = 0; k < limit && i < parents.Count - 1; i += 2) { 119 j = i + 1; 120 qualityParent1 = ((DoubleValue)parents[i].Variables[qualityName].Value).Value; 121 qualityParent2 = ((DoubleValue)parents[j].Variables[qualityName].Value).Value; 122 123 if (qualityDifferenceUseRange) { 124 parentsDifferent = (qualityParent2 > qualityParent1 - minQualityOffset || 125 qualityParent2 < qualityParent1 + minQualityOffset); 126 } else { 127 parentsDifferent = (qualityParent2 > qualityParent1 * maxQualityOffset || 128 qualityParent2 < qualityParent1 * minQualityOffset); 129 } 130 92 131 // parents meet difference criterion or max attempts reached 93 132 if (attempts >= qualityDifferenceMaxAttempts || parentsDifferent) { 94 95 133 // inner selector already copied scopes 96 selected[ j++] = parents[i * 2];97 selected[ j++] = parents[i * 2 + 1];134 selected[k++] = parents[i]; 135 selected[k++] = parents[j]; 98 136 if (!CopySelected.Value) { 99 scopes.Remove(parents[i * 2]);100 scopes.Remove(parents[ i * 2 + 1]);137 scopes.Remove(parents[i]); 138 scopes.Remove(parents[j]); 101 139 } 102 140 attempts = 1; … … 106 144 } 107 145 // modify scopes 108 ScopeListremaining = CurrentScope.SubScopes[0].SubScopes;146 remaining = CurrentScope.SubScopes[0].SubScopes; 109 147 CurrentScope.SubScopes.Clear(); 110 148 CurrentScope.SubScopes.AddRange(remaining);
Note: See TracChangeset
for help on using the changeset viewer.