1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.ScatterSearch {
|
---|
33 | /// <summary>
|
---|
34 | /// An operator that updates the solution pool using a 2-tier strategy.
|
---|
35 | /// </summary>
|
---|
36 | [Item("SolutionPool2TierUpdateMethod", "An operator that updates the solution pool using a 2-tier strategy.")]
|
---|
37 | [StorableClass]
|
---|
38 | public sealed class SolutionPool2TierUpdateMethod : SingleSuccessorOperator, IScatterSearchTargetProcessor {
|
---|
39 | #region Parameter properties
|
---|
40 | public ScopeParameter CurrentScopeParameter {
|
---|
41 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
42 | }
|
---|
43 | public IValueLookupParameter<DiversityCalculator> DiversityCalculatorParameter {
|
---|
44 | get { return (IValueLookupParameter<DiversityCalculator>)Parameters["DiversityCalculator"]; }
|
---|
45 | }
|
---|
46 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
47 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
48 | }
|
---|
49 | public IValueLookupParameter<BoolValue> NewSolutionsParameter {
|
---|
50 | get { return (IValueLookupParameter<BoolValue>)Parameters["NewSolutions"]; }
|
---|
51 | }
|
---|
52 | public IValueLookupParameter<IntValue> NumberOfHighQualitySolutionsParameter {
|
---|
53 | get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; }
|
---|
54 | }
|
---|
55 | public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
|
---|
56 | get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
|
---|
57 | }
|
---|
58 | public IValueLookupParameter<IItem> QualityParameter {
|
---|
59 | get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; }
|
---|
60 | }
|
---|
61 | public IValueLookupParameter<IItem> TargetParameter {
|
---|
62 | get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
|
---|
63 | }
|
---|
64 | #endregion
|
---|
65 |
|
---|
66 | #region Properties
|
---|
67 | private IScope CurrentScope {
|
---|
68 | get { return CurrentScopeParameter.ActualValue; }
|
---|
69 | }
|
---|
70 | private DiversityCalculator DiversityCalculator {
|
---|
71 | get { return DiversityCalculatorParameter.ActualValue; }
|
---|
72 | }
|
---|
73 | private BoolValue Maximization {
|
---|
74 | get { return MaximizationParameter.ActualValue; }
|
---|
75 | set { MaximizationParameter.ActualValue = value; }
|
---|
76 | }
|
---|
77 | private BoolValue NewSolutions {
|
---|
78 | get { return NewSolutionsParameter.ActualValue; }
|
---|
79 | }
|
---|
80 | private IntValue NumberOfHighQualitySolutions {
|
---|
81 | get { return NumberOfHighQualitySolutionsParameter.ActualValue; }
|
---|
82 | }
|
---|
83 | private IntValue ReferenceSetSize {
|
---|
84 | get { return ReferenceSetSizeParameter.ActualValue; }
|
---|
85 | set { ReferenceSetSizeParameter.ActualValue = value; }
|
---|
86 | }
|
---|
87 | private IItem Quality {
|
---|
88 | get { return QualityParameter.ActualValue; }
|
---|
89 | }
|
---|
90 | private IItem Target {
|
---|
91 | get { return TargetParameter.ActualValue; }
|
---|
92 | }
|
---|
93 | #endregion
|
---|
94 |
|
---|
95 | [StorableConstructor]
|
---|
96 | private SolutionPool2TierUpdateMethod(bool deserializing) : base(deserializing) { }
|
---|
97 | private SolutionPool2TierUpdateMethod(SolutionPool2TierUpdateMethod original, Cloner cloner) : base(original, cloner) { }
|
---|
98 | public SolutionPool2TierUpdateMethod() : base() { Initialize(); }
|
---|
99 |
|
---|
100 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
101 | return new SolutionPool2TierUpdateMethod(this, cloner);
|
---|
102 | }
|
---|
103 |
|
---|
104 | private void Initialize() {
|
---|
105 | #region Create parameters
|
---|
106 | Parameters.Add(new ScopeParameter("CurrentScope"));
|
---|
107 | Parameters.Add(new ValueLookupParameter<DiversityCalculator>("DiversityCalculator"));
|
---|
108 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization"));
|
---|
109 | Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions"));
|
---|
110 | Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions"));
|
---|
111 | Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));
|
---|
112 | Parameters.Add(new ValueLookupParameter<IItem>("Quality"));
|
---|
113 | Parameters.Add(new ValueLookupParameter<IItem>("Target"));
|
---|
114 | #endregion
|
---|
115 | TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem
|
---|
116 | }
|
---|
117 |
|
---|
118 | public override IOperation Apply() {
|
---|
119 | var parentsScope = new Scope("Parents");
|
---|
120 | var offspringScope = new Scope("Offspring");
|
---|
121 |
|
---|
122 | // split parents and offspring
|
---|
123 | foreach (var scope in CurrentScope.SubScopes) {
|
---|
124 | parentsScope.SubScopes.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
|
---|
125 | offspringScope.SubScopes.AddRange(scope.SubScopes.Last().SubScopes);
|
---|
126 | }
|
---|
127 |
|
---|
128 | var orderedParents = Maximization.Value ? parentsScope.SubScopes.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
|
---|
129 | parentsScope.SubScopes.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
|
---|
130 | var orderedOffspring = Maximization.Value ? offspringScope.SubScopes.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
|
---|
131 | offspringScope.SubScopes.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
|
---|
132 |
|
---|
133 | var highQualityParents = orderedParents.Take(NumberOfHighQualitySolutions.Value).ToList();
|
---|
134 | var highDiversityParents = new List<Tuple<IScope, double>>();
|
---|
135 | foreach (var oScope in orderedParents.Skip(NumberOfHighQualitySolutions.Value)) {
|
---|
136 | double diversity = 0.0;
|
---|
137 | var oSol = oScope.Variables[TargetParameter.ActualName].Value;
|
---|
138 | foreach (var hScope in highQualityParents) {
|
---|
139 | var hSol = hScope.Variables[TargetParameter.ActualName].Value;
|
---|
140 | diversity += DiversityCalculator.ExecuteCalculation(oSol, hSol);
|
---|
141 | }
|
---|
142 | highDiversityParents.Add(new Tuple<IScope, double>(oScope, diversity));
|
---|
143 | }
|
---|
144 |
|
---|
145 | var offspring = new List<Tuple<IScope, double>>();
|
---|
146 | foreach (var oScope in orderedOffspring) {
|
---|
147 | double diversity = 0.0;
|
---|
148 | var oSol = oScope.Variables[TargetParameter.ActualName].Value;
|
---|
149 | foreach (var hScope in highQualityParents) {
|
---|
150 | var hSol = hScope.Variables[TargetParameter.ActualName].Value;
|
---|
151 | diversity += DiversityCalculator.ExecuteCalculation(oSol, hSol);
|
---|
152 | }
|
---|
153 | offspring.Add(new Tuple<IScope, double>(oScope, diversity));
|
---|
154 | }
|
---|
155 |
|
---|
156 | // update high quality part of the reference set
|
---|
157 | var hasBetterQuality = Maximization.Value ? (Func<Tuple<IScope, double>, bool>)(x => { return (x.Item1.Variables[QualityParameter.ActualName].Value as DoubleValue).Value > (highQualityParents.OrderBy(y => y.Variables[QualityParameter.ActualName].Value).First().Variables[QualityParameter.ActualName].Value as DoubleValue).Value; }) :
|
---|
158 | (Func<Tuple<IScope, double>, bool>)(x => { return (x.Item1.Variables[QualityParameter.ActualName].Value as DoubleValue).Value < (highQualityParents.OrderByDescending(y => y.Variables[QualityParameter.ActualName].Value).First().Variables[QualityParameter.ActualName].Value as DoubleValue).Value; });
|
---|
159 | if (offspring.Any(hasBetterQuality)) NewSolutions.Value = true;
|
---|
160 | while (offspring.Any(hasBetterQuality)) { // better offspring available
|
---|
161 | // select best offspring
|
---|
162 | var bestChild = Maximization.Value ? offspring.OrderByDescending(x => x.Item1.Variables[QualityParameter.ActualName].Value).First() :
|
---|
163 | offspring.OrderBy(x => x.Item1.Variables[QualityParameter.ActualName].Value).First();
|
---|
164 | // select worst parent
|
---|
165 | var worstParent = Maximization.Value ? highQualityParents.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value).Last() :
|
---|
166 | highQualityParents.OrderBy(x => x.Variables[QualityParameter.ActualName].Value).Last();
|
---|
167 | highQualityParents.Remove(worstParent);
|
---|
168 | highQualityParents.Add(bestChild.Item1);
|
---|
169 | offspring.Remove(bestChild);
|
---|
170 | }
|
---|
171 |
|
---|
172 | // update diversity part of the reference set
|
---|
173 | var hasBetterDiversity = (Func<Tuple<IScope, double>, bool>)(x => { return x.Item2 > highDiversityParents.OrderBy(y => y.Item2).First().Item2; });
|
---|
174 | if (offspring.Any(hasBetterDiversity)) NewSolutions.Value = true;
|
---|
175 | while (offspring.Any(hasBetterDiversity)) { // better offspring available
|
---|
176 | // select best offspring
|
---|
177 | var bestChild = offspring.OrderByDescending(x => x.Item2).First();
|
---|
178 | // select worst parent
|
---|
179 | var worstParent = highDiversityParents.OrderBy(x => x.Item2).First();
|
---|
180 | highDiversityParents.Remove(worstParent);
|
---|
181 | highDiversityParents.Add(bestChild);
|
---|
182 | offspring.Remove(bestChild);
|
---|
183 | }
|
---|
184 |
|
---|
185 | CurrentScope.SubScopes.Replace(highQualityParents.Concat(highDiversityParents.Select(x => x.Item1)).ToList());
|
---|
186 |
|
---|
187 | return base.Apply();
|
---|
188 | }
|
---|
189 |
|
---|
190 | private class KeyEqualityComparer<T> : IEqualityComparer<T> {
|
---|
191 | private readonly Func<T, object> keyExtractor;
|
---|
192 |
|
---|
193 | public KeyEqualityComparer(Func<T, object> keyExtractor) {
|
---|
194 | this.keyExtractor = keyExtractor;
|
---|
195 | }
|
---|
196 |
|
---|
197 | public bool Equals(T x, T y) {
|
---|
198 | return keyExtractor(x).Equals(keyExtractor(y));
|
---|
199 | }
|
---|
200 |
|
---|
201 | public int GetHashCode(T obj) {
|
---|
202 | return keyExtractor(obj).GetHashCode();
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|