Changeset 7744 for branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs
- Timestamp:
- 04/20/12 17:00:09 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs
r7740 r7744 36 36 [Item("SolutionPoolUpdateMethod", "An operator that updates the solution pool.")] 37 37 [StorableClass] 38 public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator {38 public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator, IScatterSearchTargetProcessor { 39 39 #region Parameter properties 40 40 public ScopeParameter CurrentScopeParameter { 41 41 get { return (ScopeParameter)Parameters["CurrentScope"]; } 42 } 43 public IValueLookupParameter<BoolValue> MaximizationParameter { 44 get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; } 42 45 } 43 46 public IValueLookupParameter<BoolValue> NewSolutionsParameter { … … 50 53 get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; } 51 54 } 52 public IValueLookupParameter<IItem> SolutionParameter {53 get { return (IValueLookupParameter<IItem>)Parameters[" Solution"]; }55 public IValueLookupParameter<IItem> TargetParameter { 56 get { return (IValueLookupParameter<IItem>)Parameters["Target"]; } 54 57 } 55 58 #endregion … … 58 61 private IScope CurrentScope { 59 62 get { return CurrentScopeParameter.ActualValue; } 63 } 64 private BoolValue Maximization { 65 get { return MaximizationParameter.ActualValue; } 66 set { MaximizationParameter.ActualValue = value; } 60 67 } 61 68 private BoolValue NewSolutions { … … 68 75 private IItem Quality { 69 76 get { return QualityParameter.ActualValue; } 77 } 78 private IItem Target { 79 get { return TargetParameter.ActualValue; } 70 80 } 71 81 #endregion … … 83 93 #region Create parameters 84 94 Parameters.Add(new ScopeParameter("CurrentScope")); 95 Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization")); 85 96 Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions")); 86 97 Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize")); 87 98 Parameters.Add(new ValueLookupParameter<IItem>("Quality")); 88 Parameters.Add(new ValueLookupParameter<IItem>(" Solution"));99 Parameters.Add(new ValueLookupParameter<IItem>("Target")); 89 100 #endregion 90 SolutionParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem101 TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem 91 102 } 92 103 93 104 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 96 109 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); 99 112 } 113 100 114 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 } 106 139 } 107 140
Note: See TracChangeset
for help on using the changeset viewer.