Changeset 7740 for branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs
- Timestamp:
- 04/18/12 19:01:21 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs
r7724 r7740 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 22 24 using System.Linq; 23 25 using HeuristicLab.Common; … … 36 38 public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator { 37 39 #region Parameter properties 38 p rivateScopeParameter CurrentScopeParameter {40 public ScopeParameter CurrentScopeParameter { 39 41 get { return (ScopeParameter)Parameters["CurrentScope"]; } 40 42 } 41 p rivateValueLookupParameter<BoolValue> NewSolutionsParameter {42 get { return ( ValueLookupParameter<BoolValue>)Parameters["NewSolutions"]; }43 public IValueLookupParameter<BoolValue> NewSolutionsParameter { 44 get { return (IValueLookupParameter<BoolValue>)Parameters["NewSolutions"]; } 43 45 } 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"]; } 46 54 } 47 55 #endregion … … 58 66 set { ReferenceSetSizeParameter.ActualValue = value; } 59 67 } 68 private IItem Quality { 69 get { return QualityParameter.ActualValue; } 70 } 60 71 #endregion 61 72 … … 71 82 private void Initialize() { 72 83 #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")); 76 89 #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 83 91 } 84 92 … … 91 99 } 92 100 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).Value95 > (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)); 97 105 NewSolutions.Value = true; 98 106 } 107 99 108 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 } 100 125 } 101 126 }
Note: See TracChangeset
for help on using the changeset viewer.