Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/18/12 19:01:21 (12 years ago)
Author:
jkarder
Message:

#1331:

  • added custom crossover operator (NChildCrossover)
  • added parameters and adjusted types
  • replaced SolutionCombinationMethod with a placeholder
  • adjusted event handling
  • changed access levels
  • minor code improvements
File:
1 edited

Legend:

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

    r7724 r7740  
    2020#endregion
    2121
     22using System;
     23using System.Collections.Generic;
    2224using System.Linq;
    2325using HeuristicLab.Common;
     
    3638  public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator {
    3739    #region Parameter properties
    38     private ScopeParameter CurrentScopeParameter {
     40    public ScopeParameter CurrentScopeParameter {
    3941      get { return (ScopeParameter)Parameters["CurrentScope"]; }
    4042    }
    41     private ValueLookupParameter<BoolValue> NewSolutionsParameter {
    42       get { return (ValueLookupParameter<BoolValue>)Parameters["NewSolutions"]; }
     43    public IValueLookupParameter<BoolValue> NewSolutionsParameter {
     44      get { return (IValueLookupParameter<BoolValue>)Parameters["NewSolutions"]; }
    4345    }
    44     private ValueLookupParameter<IntValue> ReferenceSetSizeParameter {
    45       get { return (ValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
     46    public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
     47      get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
     48    }
     49    public IValueLookupParameter<IItem> QualityParameter {
     50      get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; }
     51    }
     52    public IValueLookupParameter<IItem> SolutionParameter {
     53      get { return (IValueLookupParameter<IItem>)Parameters["Solution"]; }
    4654    }
    4755    #endregion
     
    5866      set { ReferenceSetSizeParameter.ActualValue = value; }
    5967    }
     68    private IItem Quality {
     69      get { return QualityParameter.ActualValue; }
     70    }
    6071    #endregion
    6172
     
    7182    private void Initialize() {
    7283      #region Create parameters
    73       Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes."));
    74       Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions", "Indicates if new solutions have been found."));
    75       Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set."));
     84      Parameters.Add(new ScopeParameter("CurrentScope"));
     85      Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions"));
     86      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));
     87      Parameters.Add(new ValueLookupParameter<IItem>("Quality"));
     88      Parameters.Add(new ValueLookupParameter<IItem>("Solution"));
    7689      #endregion
    77 
    78       #region Create operators
    79       #endregion
    80 
    81       #region Create operator graph
    82       #endregion
     90      SolutionParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem
    8391    }
    8492
     
    9199      }
    92100      CurrentScope.SubScopes.Clear();
    93       CurrentScope.SubScopes.AddRange(parents.SubScopes.OrderByDescending(o => o.Variables["Quality"].Value));
    94       if ((offspring.SubScopes.OrderByDescending(o => o.Variables["Quality"].Value).First().Variables["Quality"].Value as DoubleValue).Value
    95           > (parents.SubScopes.OrderByDescending(o => o.Variables["Quality"].Value).First().Variables["Quality"].Value as DoubleValue).Value) {
    96         CurrentScope.SubScopes.Replace(CurrentScope.SubScopes.Union(offspring.SubScopes).OrderByDescending(o => o.Variables["Quality"].Value).Take(ReferenceSetSize.Value));
     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));
    97105        NewSolutions.Value = true;
    98106      }
     107
    99108      return base.Apply();
     109    }
     110
     111    private class KeyEqualityComparer<T> : IEqualityComparer<T> {
     112      private readonly Func<T, object> keyExtractor;
     113
     114      public KeyEqualityComparer(Func<T, object> keyExtractor) {
     115        this.keyExtractor = keyExtractor;
     116      }
     117
     118      public bool Equals(T x, T y) {
     119        return keyExtractor(x).Equals(keyExtractor(y));
     120      }
     121
     122      public int GetHashCode(T obj) {
     123        return keyExtractor(obj).GetHashCode();
     124      }
    100125    }
    101126  }
Note: See TracChangeset for help on using the changeset viewer.