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.Optimization.Operators;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.ScatterSearch {
|
---|
34 | /// <summary>
|
---|
35 | /// An operator that updates the solution pool using a 2-tier strategy.
|
---|
36 | /// </summary>
|
---|
37 | [Item("SolutionPool2TierUpdateMethod", "An operator that updates the solution pool using a 2-tier strategy.")]
|
---|
38 | [StorableClass]
|
---|
39 | public sealed class SolutionPool2TierUpdateMethod : SingleSuccessorOperator, IScatterSearchOperator {
|
---|
40 | #region Parameter properties
|
---|
41 | public ScopeParameter CurrentScopeParameter {
|
---|
42 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
43 | }
|
---|
44 | public IValueLookupParameter<SimilarityCalculator> SimilarityCalculatorParameter {
|
---|
45 | get { return (IValueLookupParameter<SimilarityCalculator>)Parameters["SimilarityCalculator"]; }
|
---|
46 | }
|
---|
47 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
48 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
49 | }
|
---|
50 | public IValueLookupParameter<BoolValue> NewSolutionsParameter {
|
---|
51 | get { return (IValueLookupParameter<BoolValue>)Parameters["NewSolutions"]; }
|
---|
52 | }
|
---|
53 | public IValueLookupParameter<IntValue> NumberOfHighQualitySolutionsParameter {
|
---|
54 | get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; }
|
---|
55 | }
|
---|
56 | public IValueLookupParameter<IItem> QualityParameter {
|
---|
57 | get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; }
|
---|
58 | }
|
---|
59 | public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
|
---|
60 | get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
|
---|
61 | }
|
---|
62 | public IValueLookupParameter<IItem> TargetParameter {
|
---|
63 | get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
|
---|
64 | }
|
---|
65 | #endregion
|
---|
66 |
|
---|
67 | #region Properties
|
---|
68 | private IScope CurrentScope {
|
---|
69 | get { return CurrentScopeParameter.ActualValue; }
|
---|
70 | }
|
---|
71 | private SimilarityCalculator SimilarityCalculator {
|
---|
72 | get { return SimilarityCalculatorParameter.ActualValue; }
|
---|
73 | }
|
---|
74 | private BoolValue Maximization {
|
---|
75 | get { return MaximizationParameter.ActualValue; }
|
---|
76 | set { MaximizationParameter.ActualValue = value; }
|
---|
77 | }
|
---|
78 | private BoolValue NewSolutions {
|
---|
79 | get { return NewSolutionsParameter.ActualValue; }
|
---|
80 | }
|
---|
81 | private IntValue NumberOfHighQualitySolutions {
|
---|
82 | get { return NumberOfHighQualitySolutionsParameter.ActualValue; }
|
---|
83 | }
|
---|
84 | private IItem Quality {
|
---|
85 | get { return QualityParameter.ActualValue; }
|
---|
86 | }
|
---|
87 | private IntValue ReferenceSetSize {
|
---|
88 | get { return ReferenceSetSizeParameter.ActualValue; }
|
---|
89 | set { ReferenceSetSizeParameter.ActualValue = value; }
|
---|
90 | }
|
---|
91 | private IItem Target {
|
---|
92 | get { return TargetParameter.ActualValue; }
|
---|
93 | }
|
---|
94 | #endregion
|
---|
95 |
|
---|
96 | [StorableConstructor]
|
---|
97 | private SolutionPool2TierUpdateMethod(bool deserializing) : base(deserializing) { }
|
---|
98 | private SolutionPool2TierUpdateMethod(SolutionPool2TierUpdateMethod original, Cloner cloner) : base(original, cloner) { }
|
---|
99 | public SolutionPool2TierUpdateMethod() : base() { Initialize(); }
|
---|
100 |
|
---|
101 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
102 | return new SolutionPool2TierUpdateMethod(this, cloner);
|
---|
103 | }
|
---|
104 |
|
---|
105 | private void Initialize() {
|
---|
106 | #region Create parameters
|
---|
107 | Parameters.Add(new ScopeParameter("CurrentScope"));
|
---|
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<IItem>("Quality"));
|
---|
112 | Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));
|
---|
113 | Parameters.Add(new ValueLookupParameter<SimilarityCalculator>("SimilarityCalculator"));
|
---|
114 | Parameters.Add(new ValueLookupParameter<IItem>("Target"));
|
---|
115 | #endregion
|
---|
116 | }
|
---|
117 |
|
---|
118 | public override IOperation Apply() {
|
---|
119 | IScope parentsScope = new Scope("Parents");
|
---|
120 | IScope 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 | IList<Tuple<IScope, double>> highSimilarityParents = new List<Tuple<IScope, double>>();
|
---|
135 | foreach (var oScope in orderedParents.Skip(NumberOfHighQualitySolutions.Value)) {
|
---|
136 | double similarity = 0.0;
|
---|
137 | foreach (var hScope in highQualityParents) {
|
---|
138 | similarity += SimilarityCalculator.ExecuteCalculation(oScope, hScope);
|
---|
139 | }
|
---|
140 | highSimilarityParents.Add(new Tuple<IScope, double>(oScope, similarity));
|
---|
141 | }
|
---|
142 |
|
---|
143 | IList<Tuple<IScope, double>> offspring = new List<Tuple<IScope, double>>();
|
---|
144 | foreach (var oScope in orderedOffspring) {
|
---|
145 | double similarity = 0.0;
|
---|
146 | foreach (var hScope in highQualityParents) {
|
---|
147 | similarity += SimilarityCalculator.ExecuteCalculation(oScope, hScope);
|
---|
148 | }
|
---|
149 | offspring.Add(new Tuple<IScope, double>(oScope, similarity));
|
---|
150 | }
|
---|
151 |
|
---|
152 | // update high quality part of the reference set
|
---|
153 | 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; }) :
|
---|
154 | (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; });
|
---|
155 | if (offspring.Any(hasBetterQuality)) NewSolutions.Value = true;
|
---|
156 | while (offspring.Any(hasBetterQuality)) { // better offspring available
|
---|
157 | // select best offspring
|
---|
158 | var bestChild = Maximization.Value ? offspring.OrderByDescending(x => x.Item1.Variables[QualityParameter.ActualName].Value).First() :
|
---|
159 | offspring.OrderBy(x => x.Item1.Variables[QualityParameter.ActualName].Value).First();
|
---|
160 | // select worst parent
|
---|
161 | var worstParent = Maximization.Value ? highQualityParents.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value).Last() :
|
---|
162 | highQualityParents.OrderBy(x => x.Variables[QualityParameter.ActualName].Value).Last();
|
---|
163 | highQualityParents.Remove(worstParent);
|
---|
164 | highQualityParents.Add(bestChild.Item1);
|
---|
165 | offspring.Remove(bestChild);
|
---|
166 | }
|
---|
167 |
|
---|
168 | // update diversity part of the reference set
|
---|
169 | var hasBetterDiversity = (Func<Tuple<IScope, double>, bool>)(x => { return x.Item2 < highSimilarityParents.OrderByDescending(y => y.Item2).First().Item2; });
|
---|
170 | if (offspring.Any(hasBetterDiversity)) NewSolutions.Value = true;
|
---|
171 | while (offspring.Any(hasBetterDiversity)) { // better offspring available
|
---|
172 | // select best offspring
|
---|
173 | var bestChild = offspring.OrderBy(x => x.Item2).First();
|
---|
174 | // select worst parent
|
---|
175 | var worstParent = highSimilarityParents.OrderByDescending(x => x.Item2).First();
|
---|
176 | highSimilarityParents.Remove(worstParent);
|
---|
177 | highSimilarityParents.Add(bestChild);
|
---|
178 | offspring.Remove(bestChild);
|
---|
179 | }
|
---|
180 |
|
---|
181 | CurrentScope.SubScopes.Replace(highQualityParents.Concat(highSimilarityParents.Select(x => x.Item1)).ToList());
|
---|
182 |
|
---|
183 | return base.Apply();
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|