[7789] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7789] | 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.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
[8086] | 28 | using HeuristicLab.Optimization;
|
---|
[7789] | 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.
|
---|
| 35 | /// </summary>
|
---|
| 36 | [Item("SolutionPoolUpdateMethod", "An operator that updates the solution pool.")]
|
---|
| 37 | [StorableClass]
|
---|
[12069] | 38 | public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator, ISingleObjectiveOperator {
|
---|
[7789] | 39 | #region Parameter properties
|
---|
| 40 | public ScopeParameter CurrentScopeParameter {
|
---|
| 41 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
| 42 | }
|
---|
| 43 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 44 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 45 | }
|
---|
| 46 | public IValueLookupParameter<BoolValue> NewSolutionsParameter {
|
---|
| 47 | get { return (IValueLookupParameter<BoolValue>)Parameters["NewSolutions"]; }
|
---|
| 48 | }
|
---|
| 49 | public IValueLookupParameter<IItem> QualityParameter {
|
---|
| 50 | get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; }
|
---|
| 51 | }
|
---|
| 52 | public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
|
---|
| 53 | get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
|
---|
| 54 | }
|
---|
[12069] | 55 | public IValueLookupParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
|
---|
| 56 | get { return (IValueLookupParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
|
---|
| 57 | }
|
---|
[7789] | 58 | #endregion
|
---|
| 59 |
|
---|
| 60 | #region Properties
|
---|
| 61 | private IScope CurrentScope {
|
---|
| 62 | get { return CurrentScopeParameter.ActualValue; }
|
---|
| 63 | }
|
---|
| 64 | private BoolValue Maximization {
|
---|
| 65 | get { return MaximizationParameter.ActualValue; }
|
---|
| 66 | set { MaximizationParameter.ActualValue = value; }
|
---|
| 67 | }
|
---|
| 68 | private BoolValue NewSolutions {
|
---|
| 69 | get { return NewSolutionsParameter.ActualValue; }
|
---|
| 70 | }
|
---|
| 71 | private IItem Quality {
|
---|
| 72 | get { return QualityParameter.ActualValue; }
|
---|
| 73 | }
|
---|
| 74 | private IntValue ReferenceSetSize {
|
---|
| 75 | get { return ReferenceSetSizeParameter.ActualValue; }
|
---|
| 76 | set { ReferenceSetSizeParameter.ActualValue = value; }
|
---|
| 77 | }
|
---|
| 78 | #endregion
|
---|
| 79 |
|
---|
| 80 | [StorableConstructor]
|
---|
| 81 | private SolutionPoolUpdateMethod(bool deserializing) : base(deserializing) { }
|
---|
[12069] | 82 | private SolutionPoolUpdateMethod(SolutionPoolUpdateMethod original, Cloner cloner) : base(original, cloner) { }
|
---|
| 83 | public SolutionPoolUpdateMethod()
|
---|
| 84 | : base() {
|
---|
[7789] | 85 | #region Create parameters
|
---|
[8086] | 86 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that is the reference set."));
|
---|
| 87 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
| 88 | Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions", "True if new solutions have been found, otherwise false."));
|
---|
| 89 | Parameters.Add(new ValueLookupParameter<IItem>("Quality", "This parameter is used for name translation only."));
|
---|
| 90 | Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set."));
|
---|
[12069] | 91 | Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
|
---|
[7789] | 92 | #endregion
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[12069] | 95 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 96 | return new SolutionPoolUpdateMethod(this, cloner);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 100 | private void AfterDeserialization() {
|
---|
| 101 | // BackwardsCompatibility3.3
|
---|
| 102 | #region Backwards compatible code, remove with 3.4
|
---|
| 103 | if (!Parameters.ContainsKey("SimilarityCalculator"))
|
---|
| 104 | Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
|
---|
| 105 | #endregion
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[7789] | 108 | public override IOperation Apply() {
|
---|
[8086] | 109 | ScopeList parents = new ScopeList();
|
---|
| 110 | ScopeList offspring = new ScopeList();
|
---|
[7789] | 111 |
|
---|
| 112 | // split parents and offspring
|
---|
| 113 | foreach (var scope in CurrentScope.SubScopes) {
|
---|
[8086] | 114 | parents.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
|
---|
| 115 | offspring.AddRange(scope.SubScopes.Last().SubScopes);
|
---|
[7789] | 116 | }
|
---|
| 117 |
|
---|
| 118 | CurrentScope.SubScopes.Clear();
|
---|
| 119 |
|
---|
[8086] | 120 | // attention: assumes that parents are distinct
|
---|
| 121 | // distinction might cause a too small reference set (e.g. reference set = {1, 2, 2, 2,..., 2} -> union = {1, 2}
|
---|
[7789] | 122 |
|
---|
[8086] | 123 | var orderedParents = Maximization.Value ? parents.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
|
---|
| 124 | parents.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
|
---|
| 125 | var orderedOffspring = Maximization.Value ? offspring.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
|
---|
| 126 | offspring.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
|
---|
| 127 |
|
---|
[7789] | 128 | CurrentScope.SubScopes.AddRange(orderedParents);
|
---|
| 129 |
|
---|
| 130 | double worstParentQuality = (orderedParents.Last().Variables[QualityParameter.ActualName].Value as DoubleValue).Value;
|
---|
| 131 |
|
---|
| 132 | var hasBetterQuality = Maximization.Value ? (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value > worstParentQuality; }) :
|
---|
| 133 | (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value < worstParentQuality; });
|
---|
| 134 |
|
---|
| 135 | // is there any offspring better than the worst parent?
|
---|
| 136 | if (orderedOffspring.Any(hasBetterQuality)) {
|
---|
| 137 | // produce the set union
|
---|
[12069] | 138 | var union = orderedParents.Union(orderedOffspring.Where(hasBetterQuality), SimilarityCalculatorParameter.ActualValue);
|
---|
[8086] | 139 | if (union.Count() > orderedParents.Count()) {
|
---|
[7789] | 140 | var orderedUnion = Maximization.Value ? union.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
|
---|
| 141 | union.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
|
---|
[8086] | 142 | CurrentScope.SubScopes.Replace(orderedUnion.Take(ReferenceSetSize.Value));
|
---|
[7789] | 143 | NewSolutions.Value = true;
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | return base.Apply();
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | }
|
---|