[7775] | 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]
|
---|
[7786] | 38 | public sealed class SolutionPool2TierUpdateMethod : SingleSuccessorOperator {
|
---|
[7775] | 39 | #region Parameter properties
|
---|
| 40 | public ScopeParameter CurrentScopeParameter {
|
---|
| 41 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
| 42 | }
|
---|
[7786] | 43 | public IValueLookupParameter<SimilarityCalculator> SimilarityCalculatorParameter {
|
---|
| 44 | get { return (IValueLookupParameter<SimilarityCalculator>)Parameters["SimilarityCalculator"]; }
|
---|
[7775] | 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 | }
|
---|
[7786] | 55 | public IValueLookupParameter<IItem> QualityParameter {
|
---|
| 56 | get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; }
|
---|
| 57 | }
|
---|
[7775] | 58 | public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
|
---|
| 59 | get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
|
---|
| 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 | }
|
---|
[7786] | 70 | private SimilarityCalculator SimilarityCalculator {
|
---|
| 71 | get { return SimilarityCalculatorParameter.ActualValue; }
|
---|
[7775] | 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 | }
|
---|
[7786] | 83 | private IItem Quality {
|
---|
| 84 | get { return QualityParameter.ActualValue; }
|
---|
| 85 | }
|
---|
[7775] | 86 | private IntValue ReferenceSetSize {
|
---|
| 87 | get { return ReferenceSetSizeParameter.ActualValue; }
|
---|
| 88 | set { ReferenceSetSizeParameter.ActualValue = value; }
|
---|
| 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<BoolValue>("Maximization"));
|
---|
| 108 | Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions"));
|
---|
| 109 | Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions"));
|
---|
[7786] | 110 | Parameters.Add(new ValueLookupParameter<IItem>("Quality"));
|
---|
[7775] | 111 | Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));
|
---|
[7786] | 112 | Parameters.Add(new ValueLookupParameter<SimilarityCalculator>("SimilarityCalculator"));
|
---|
[7775] | 113 | Parameters.Add(new ValueLookupParameter<IItem>("Target"));
|
---|
| 114 | #endregion
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | public override IOperation Apply() {
|
---|
[7786] | 118 | IScope parentsScope = new Scope("Parents");
|
---|
| 119 | IScope offspringScope = new Scope("Offspring");
|
---|
[7775] | 120 |
|
---|
| 121 | // split parents and offspring
|
---|
| 122 | foreach (var scope in CurrentScope.SubScopes) {
|
---|
| 123 | parentsScope.SubScopes.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
|
---|
| 124 | offspringScope.SubScopes.AddRange(scope.SubScopes.Last().SubScopes);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | var orderedParents = Maximization.Value ? parentsScope.SubScopes.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
|
---|
| 128 | parentsScope.SubScopes.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
|
---|
| 129 | var orderedOffspring = Maximization.Value ? offspringScope.SubScopes.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
|
---|
| 130 | offspringScope.SubScopes.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
|
---|
| 131 |
|
---|
| 132 | var highQualityParents = orderedParents.Take(NumberOfHighQualitySolutions.Value).ToList();
|
---|
[7786] | 133 | IList<Tuple<IScope, double>> highSimilarityParents = new List<Tuple<IScope, double>>();
|
---|
[7775] | 134 | foreach (var oScope in orderedParents.Skip(NumberOfHighQualitySolutions.Value)) {
|
---|
[7786] | 135 | double similarity = 0.0;
|
---|
[7775] | 136 | foreach (var hScope in highQualityParents) {
|
---|
[7786] | 137 | similarity += SimilarityCalculator.ExecuteCalculation(oScope, hScope);
|
---|
[7775] | 138 | }
|
---|
[7786] | 139 | highSimilarityParents.Add(new Tuple<IScope, double>(oScope, similarity));
|
---|
[7775] | 140 | }
|
---|
| 141 |
|
---|
[7786] | 142 | IList<Tuple<IScope, double>> offspring = new List<Tuple<IScope, double>>();
|
---|
[7775] | 143 | foreach (var oScope in orderedOffspring) {
|
---|
[7786] | 144 | double similarity = 0.0;
|
---|
[7775] | 145 | foreach (var hScope in highQualityParents) {
|
---|
[7786] | 146 | similarity += SimilarityCalculator.ExecuteCalculation(oScope, hScope);
|
---|
[7775] | 147 | }
|
---|
[7786] | 148 | offspring.Add(new Tuple<IScope, double>(oScope, similarity));
|
---|
[7775] | 149 | }
|
---|
| 150 |
|
---|
| 151 | // update high quality part of the reference set
|
---|
| 152 | 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; }) :
|
---|
| 153 | (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; });
|
---|
| 154 | if (offspring.Any(hasBetterQuality)) NewSolutions.Value = true;
|
---|
| 155 | while (offspring.Any(hasBetterQuality)) { // better offspring available
|
---|
| 156 | // select best offspring
|
---|
| 157 | var bestChild = Maximization.Value ? offspring.OrderByDescending(x => x.Item1.Variables[QualityParameter.ActualName].Value).First() :
|
---|
| 158 | offspring.OrderBy(x => x.Item1.Variables[QualityParameter.ActualName].Value).First();
|
---|
| 159 | // select worst parent
|
---|
| 160 | var worstParent = Maximization.Value ? highQualityParents.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value).Last() :
|
---|
| 161 | highQualityParents.OrderBy(x => x.Variables[QualityParameter.ActualName].Value).Last();
|
---|
| 162 | highQualityParents.Remove(worstParent);
|
---|
| 163 | highQualityParents.Add(bestChild.Item1);
|
---|
| 164 | offspring.Remove(bestChild);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | // update diversity part of the reference set
|
---|
[7786] | 168 | var hasBetterDiversity = (Func<Tuple<IScope, double>, bool>)(x => { return x.Item2 < highSimilarityParents.OrderByDescending(y => y.Item2).First().Item2; });
|
---|
[7775] | 169 | if (offspring.Any(hasBetterDiversity)) NewSolutions.Value = true;
|
---|
| 170 | while (offspring.Any(hasBetterDiversity)) { // better offspring available
|
---|
| 171 | // select best offspring
|
---|
[7786] | 172 | var bestChild = offspring.OrderBy(x => x.Item2).First();
|
---|
[7775] | 173 | // select worst parent
|
---|
[7786] | 174 | var worstParent = highSimilarityParents.OrderByDescending(x => x.Item2).First();
|
---|
| 175 | highSimilarityParents.Remove(worstParent);
|
---|
| 176 | highSimilarityParents.Add(bestChild);
|
---|
[7775] | 177 | offspring.Remove(bestChild);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[7786] | 180 | CurrentScope.SubScopes.Replace(highQualityParents.Concat(highSimilarityParents.Select(x => x.Item1)).ToList());
|
---|
[7775] | 181 |
|
---|
| 182 | return base.Apply();
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | }
|
---|