Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/09/11 20:58:30 (14 years ago)
Author:
cfischer
Message:

#1364 implemented QualityDifferenceUseRange option

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Selection/3.3/NoSameMatesSelector.cs

    r5596 r5646  
    1919    private const string QualityDifferencePercentageParameterName = "QualityDifferencePercentage";
    2020    private const string QualityDifferenceMaxAttemptsParameterName = "QualityDifferenceMaxAttempts";
     21    private const string QualityDifferenceUseRangeParameterName = "QualityDifferenceUseRange";
    2122
    2223    #region Parameters
     
    3031      get { return (IValueParameter<IntValue>)Parameters[QualityDifferenceMaxAttemptsParameterName]; }
    3132    }
     33    public IValueParameter<BoolValue> QualityDifferenceUseRangeParameter {
     34      get { return (IValueParameter<BoolValue>)Parameters[QualityDifferenceUseRangeParameterName]; }
     35    }
    3236    #endregion
    3337
     
    4549    public IntValue QualityDifferenceMaxAttempts {
    4650      get { return QualityDifferenceMaxAttemptsParameter.Value; }
     51    }
     52    public BoolValue QualityDifferenceUseRange {
     53      get { return QualityDifferenceUseRangeParameter.Value; }
    4754    }
    4855    #endregion
     
    6067      Parameters.Add(new ValueParameter<PercentValue>(QualityDifferencePercentageParameterName, "The minimum quality difference from parent1 to parent2 to accept the selection.", new PercentValue(0.05)));
    6168      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)));
    6270      #endregion
    6371
     
    6775    [StorableHook(HookType.AfterDeserialization)]
    6876    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)));
    6979      Initialize();
    7080    }
     
    7383      int count = NumberOfSelectedSubScopes.Value;
    7484      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;
    7586      IScope[] selected = new IScope[count];
    7687
    7788      double qualityDifferencePercentage = QualityDifferencePercentage.Value;
    7889      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;
    80112      int attempts = 1;
    81       int j = 0;
    82       while (j < count - 1) { // repeat until enough parents are selected
     113      int i, j, k = 0;
     114      while (k < limit) { // repeat until enough parents are selected
    83115        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
    92131          // parents meet difference criterion or max attempts reached
    93132          if (attempts >= qualityDifferenceMaxAttempts || parentsDifferent) {
    94      
    95133            // 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];
    98136            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]);
    101139            }
    102140            attempts = 1;
     
    106144        }
    107145        // modify scopes
    108         ScopeList remaining = CurrentScope.SubScopes[0].SubScopes;
     146        remaining = CurrentScope.SubScopes[0].SubScopes;
    109147        CurrentScope.SubScopes.Clear();
    110148        CurrentScope.SubScopes.AddRange(remaining);
Note: See TracChangeset for help on using the changeset viewer.