Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/20/12 17:00:09 (12 years ago)
Author:
jkarder
Message:

#1331:

  • added problem specific improvement operators (KnapsackImprovementOperator, TravelingSalesmanImprovementOperator)
  • added custom interface (IScatterSearchTargetProcessor) for Scatter Search specific operators that use a target parameter
  • added custom operator (OffspringProcessor) to process multiple children that were created by crossovers
  • extracted diversity calculation and added problem/encoding specific operators (BinaryVectorDiversityCalculator, PermutationDiversityCalculator) for it
  • added parameters and adjusted types
  • adjusted event handling
  • minor code improvements
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs

    r7740 r7744  
    3636  [Item("SolutionPoolUpdateMethod", "An operator that updates the solution pool.")]
    3737  [StorableClass]
    38   public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator {
     38  public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator, IScatterSearchTargetProcessor {
    3939    #region Parameter properties
    4040    public ScopeParameter CurrentScopeParameter {
    4141      get { return (ScopeParameter)Parameters["CurrentScope"]; }
     42    }
     43    public IValueLookupParameter<BoolValue> MaximizationParameter {
     44      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
    4245    }
    4346    public IValueLookupParameter<BoolValue> NewSolutionsParameter {
     
    5053      get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; }
    5154    }
    52     public IValueLookupParameter<IItem> SolutionParameter {
    53       get { return (IValueLookupParameter<IItem>)Parameters["Solution"]; }
     55    public IValueLookupParameter<IItem> TargetParameter {
     56      get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
    5457    }
    5558    #endregion
     
    5861    private IScope CurrentScope {
    5962      get { return CurrentScopeParameter.ActualValue; }
     63    }
     64    private BoolValue Maximization {
     65      get { return MaximizationParameter.ActualValue; }
     66      set { MaximizationParameter.ActualValue = value; }
    6067    }
    6168    private BoolValue NewSolutions {
     
    6875    private IItem Quality {
    6976      get { return QualityParameter.ActualValue; }
     77    }
     78    private IItem Target {
     79      get { return TargetParameter.ActualValue; }
    7080    }
    7181    #endregion
     
    8393      #region Create parameters
    8494      Parameters.Add(new ScopeParameter("CurrentScope"));
     95      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization"));
    8596      Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions"));
    8697      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));
    8798      Parameters.Add(new ValueLookupParameter<IItem>("Quality"));
    88       Parameters.Add(new ValueLookupParameter<IItem>("Solution"));
     99      Parameters.Add(new ValueLookupParameter<IItem>("Target"));
    89100      #endregion
    90       SolutionParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem
     101      TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem
    91102    }
    92103
    93104    public override IOperation Apply() {
    94       IScope parents = new Scope("Parents");
    95       IScope offspring = new Scope("Offspring");
     105      var parentsScope = new Scope("Parents");
     106      var offspringScope = new Scope("Offspring");
     107
     108      // split parents and offspring
    96109      foreach (var scope in CurrentScope.SubScopes) {
    97         parents.SubScopes.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
    98         offspring.SubScopes.AddRange(scope.SubScopes.Last().SubScopes);
     110        parentsScope.SubScopes.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
     111        offspringScope.SubScopes.AddRange(scope.SubScopes.Last().SubScopes);
    99112      }
     113
    100114      CurrentScope.SubScopes.Clear();
    101       CurrentScope.SubScopes.AddRange(parents.SubScopes.OrderByDescending(o => o.Variables[QualityParameter.ActualName].Value));
    102       if ((offspring.SubScopes.OrderByDescending(o => o.Variables[QualityParameter.ActualName].Value).First().Variables[QualityParameter.ActualName].Value as DoubleValue).Value
    103           > (parents.SubScopes.OrderByDescending(o => o.Variables[QualityParameter.ActualName].Value).Last().Variables[QualityParameter.ActualName].Value as DoubleValue).Value) {
    104         CurrentScope.SubScopes.Replace(parents.SubScopes.Concat(offspring.SubScopes.Distinct(new KeyEqualityComparer<IScope>(x => x.Variables[SolutionParameter.ActualName].Value.ToString()))).OrderByDescending(o => o.Variables[QualityParameter.ActualName].Value).Take(ReferenceSetSize.Value));
    105         NewSolutions.Value = true;
     115
     116      var orderedParents = Maximization.Value ? parentsScope.SubScopes.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
     117                                                parentsScope.SubScopes.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
     118      var orderedOffspring = Maximization.Value ? offspringScope.SubScopes.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
     119                                                  offspringScope.SubScopes.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
     120
     121      CurrentScope.SubScopes.AddRange(orderedParents);
     122
     123      var worstParentQuality = (orderedParents.Last().Variables[QualityParameter.ActualName].Value as DoubleValue).Value;
     124
     125      var Constraint = Maximization.Value ? (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value > worstParentQuality; }) :
     126                                            (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value < worstParentQuality; });
     127
     128      // is there any offspring better than the worst parent?
     129      if (orderedOffspring.Any(Constraint)) {
     130        // produce the set union
     131        // attention: distinction might cause a too small reference set! (e.g. reference set = {1, 2, 2, 2, ..., 2} -> union = {1, 2}
     132        var union = orderedParents.Union(orderedOffspring.Where(Constraint), new KeyEqualityComparer<IScope>(x => x.Variables[TargetParameter.ActualName].Value.ToString()));
     133        if (union.Count() > orderedParents./*Distinct(new KeyEqualityComparer<IScope>(x => x.Variables[TargetParameter.ActualName].Value.ToString())).*/Count()) {
     134          var orderedUnion = Maximization.Value ? union.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
     135                                                  union.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
     136          CurrentScope.SubScopes.Replace(orderedUnion.Take(ReferenceSetSize.Value).ToList());
     137          NewSolutions.Value = true;
     138        }
    106139      }
    107140
Note: See TracChangeset for help on using the changeset viewer.