Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/22/12 11:11:38 (12 years ago)
Author:
jkarder
Message:

#1331:

  • synced branch with trunk
  • added custom interface (ISimilarityBasedOperator) to mark operators that conduct similarity calculation
  • similarity calculators are now parameterized by the algorithm
  • deleted SolutionPool2TierUpdateMethod
  • deleted KnapsackMultipleGuidesPathRelinker
  • moved IImprovementOperator, IPathRelinker and ISimilarityCalculator to HeuristicLab.Optimization
  • added parameter descriptions
  • fixed plugin references
  • fixed count of EvaluatedSolutions
  • fixed check for duplicate solutions
  • minor code improvements
Location:
branches/ScatterSearch (trunk integration)
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/ScatterSearch (trunk integration)

  • branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs

    r7789 r8086  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.Operators;
     29using HeuristicLab.Optimization;
    2930using HeuristicLab.Optimization.Operators;
    3031using HeuristicLab.Parameters;
     
    3738  [Item("SolutionPoolUpdateMethod", "An operator that updates the solution pool.")]
    3839  [StorableClass]
    39   public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator, IScatterSearchOperator {
     40  public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator, IScatterSearchOperator, ISimilarityBasedOperator {
     41    #region ISimilarityBasedOperator Members
     42    public ISimilarityCalculator SimilarityCalculator { get; set; }
     43    #endregion
     44
    4045    #region Parameter properties
    4146    public ScopeParameter CurrentScopeParameter {
     
    5358    public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
    5459      get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
    55     }
    56     public IValueLookupParameter<SimilarityCalculator> SimilarityCalculatorParameter {
    57       get { return (IValueLookupParameter<SimilarityCalculator>)Parameters["SimilarityCalculator"]; }
    58     }
    59     public IValueLookupParameter<IItem> TargetParameter {
    60       get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
    6160    }
    6261    #endregion
     
    8079      set { ReferenceSetSizeParameter.ActualValue = value; }
    8180    }
    82     private SimilarityCalculator SimilarityCalculator {
    83       get { return SimilarityCalculatorParameter.ActualValue; }
    84     }
    85     private IItem Target {
    86       get { return TargetParameter.ActualValue; }
    87     }
    8881    #endregion
    8982
     
    9992    private void Initialize() {
    10093      #region Create parameters
    101       Parameters.Add(new ScopeParameter("CurrentScope"));
    102       Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization"));
    103       Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions"));
    104       Parameters.Add(new ValueLookupParameter<IItem>("Quality"));
    105       Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));
    106       Parameters.Add(new ValueLookupParameter<SimilarityCalculator>("SimilarityCalculator"));
    107       Parameters.Add(new ValueLookupParameter<IItem>("Target"));
     94      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that is the reference set."));
     95      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
     96      Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions", "True if new solutions have been found, otherwise false."));
     97      Parameters.Add(new ValueLookupParameter<IItem>("Quality", "This parameter is used for name translation only."));
     98      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set."));
    10899      #endregion
    109100    }
    110101
    111102    public override IOperation Apply() {
    112       IScope parentsScope = new Scope("Parents");
    113       IScope offspringScope = new Scope("Offspring");
     103      ScopeList parents = new ScopeList();
     104      ScopeList offspring = new ScopeList();
    114105
    115106      // split parents and offspring
    116107      foreach (var scope in CurrentScope.SubScopes) {
    117         parentsScope.SubScopes.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
    118         offspringScope.SubScopes.AddRange(scope.SubScopes.Last().SubScopes);
     108        parents.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
     109        offspring.AddRange(scope.SubScopes.Last().SubScopes);
    119110      }
    120111
    121112      CurrentScope.SubScopes.Clear();
    122113
    123       var orderedParents = Maximization.Value ? parentsScope.SubScopes.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
    124                                                 parentsScope.SubScopes.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
    125       var orderedOffspring = Maximization.Value ? offspringScope.SubScopes.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
    126                                                   offspringScope.SubScopes.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
     114      // attention: assumes that parents are distinct
     115      // distinction might cause a too small reference set (e.g. reference set = {1, 2, 2, 2,..., 2} -> union = {1, 2}
     116
     117      var orderedParents = Maximization.Value ? parents.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
     118                                                parents.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
     119      var orderedOffspring = Maximization.Value ? offspring.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
     120                                                  offspring.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
    127121
    128122      CurrentScope.SubScopes.AddRange(orderedParents);
     
    136130      if (orderedOffspring.Any(hasBetterQuality)) {
    137131        // produce the set union
    138         // attention: distinction might cause a too small reference set! (e.g. reference set = {1, 2, 2, 2, ..., 2} -> union = {1, 2}
    139132        var union = orderedParents.Union(orderedOffspring.Where(hasBetterQuality), new SolutionEqualityComparer<IScope>(SimilarityCalculator.ExecuteCalculation));
    140         if (union.Count() > orderedParents/*.Distinct(new KeyEqualityComparer<IScope>(x => x.Variables[TargetParameter.ActualName].Value.ToString()))*/.Count()) {
     133        if (union.Count() > orderedParents.Count()) {
    141134          var orderedUnion = Maximization.Value ? union.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
    142135                                                  union.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
    143           CurrentScope.SubScopes.Replace(orderedUnion.Take(ReferenceSetSize.Value).ToList());
     136          CurrentScope.SubScopes.Replace(orderedUnion.Take(ReferenceSetSize.Value));
    144137          NewSolutions.Value = true;
    145138        }
     
    149142    }
    150143
    151     private class SolutionEqualityComparer<T> : EqualityComparer<T> {
    152       private readonly Func<T, T, double> diversityCalculator;
     144    public class SolutionEqualityComparer<T> : EqualityComparer<T> {
     145      private readonly Func<T, T, double> similarityCalculator;
    153146
    154       public SolutionEqualityComparer(Func<T, T, double> diversityCalculator) {
    155         this.diversityCalculator = diversityCalculator;
     147      public SolutionEqualityComparer(Func<T, T, double> similarityCalculator) {
     148        this.similarityCalculator = similarityCalculator;
    156149      }
    157150
    158151      public override bool Equals(T x, T y) {
    159         return diversityCalculator(x, y) == 0.0;
     152        if (object.ReferenceEquals(x, y)) return true;
     153        if (x == null || y == null) return false;
     154        return similarityCalculator(x, y) == 1.0;
    160155      }
    161156
    162157      public override int GetHashCode(T obj) {
    163         return obj.GetHashCode();
     158        return 0; // return the same hash code for each object, otherwise Equals will not be called
    164159      }
    165160    }
Note: See TracChangeset for help on using the changeset viewer.