[7722] | 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 |
|
---|
[7740] | 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
[7722] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Algorithms.ScatterSearch {
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// An operator that updates the reference set.
|
---|
| 34 | /// </summary>
|
---|
| 35 | [Item("ReferenceSetUpdateMethod", "An operator that updates the reference set.")]
|
---|
| 36 | [StorableClass]
|
---|
| 37 | public sealed class ReferenceSetUpdateMethod : SingleSuccessorOperator {
|
---|
| 38 | #region Parameter properties
|
---|
[7740] | 39 | public ScopeParameter CurrentScopeParameter {
|
---|
[7722] | 40 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
| 41 | }
|
---|
[7740] | 42 | public IValueLookupParameter<IntValue> NumberOfHighQualitySolutionsParameter {
|
---|
| 43 | get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; }
|
---|
[7722] | 44 | }
|
---|
[7740] | 45 | public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
|
---|
| 46 | get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
|
---|
[7722] | 47 | }
|
---|
[7744] | 48 | public IValueLookupParameter<IItem> TargetParameter {
|
---|
| 49 | get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
|
---|
[7740] | 50 | }
|
---|
[7744] | 51 | public IValueLookupParameter<DiversityCalculator> DiversityCalculatorParameter {
|
---|
| 52 | get { return (IValueLookupParameter<DiversityCalculator>)Parameters["DiversityCalculator"]; }
|
---|
| 53 | }
|
---|
[7722] | 54 | #endregion
|
---|
| 55 |
|
---|
| 56 | #region Properties
|
---|
[7740] | 57 | private IScope CurrentScope {
|
---|
[7722] | 58 | get { return CurrentScopeParameter.ActualValue; }
|
---|
| 59 | }
|
---|
[7740] | 60 | private IntValue NumberOfHighQualitySolutions {
|
---|
[7722] | 61 | get { return NumberOfHighQualitySolutionsParameter.ActualValue; }
|
---|
| 62 | }
|
---|
[7740] | 63 | private IntValue ReferenceSetSize {
|
---|
[7722] | 64 | get { return ReferenceSetSizeParameter.ActualValue; }
|
---|
| 65 | }
|
---|
[7744] | 66 | private IItem Target {
|
---|
| 67 | get { return TargetParameter.ActualValue; }
|
---|
[7740] | 68 | }
|
---|
[7744] | 69 | private DiversityCalculator DiversityCalculator {
|
---|
| 70 | get { return DiversityCalculatorParameter.ActualValue; }
|
---|
| 71 | }
|
---|
[7722] | 72 | #endregion
|
---|
| 73 |
|
---|
| 74 | [StorableConstructor]
|
---|
| 75 | private ReferenceSetUpdateMethod(bool deserializing) : base(deserializing) { }
|
---|
| 76 | private ReferenceSetUpdateMethod(ReferenceSetUpdateMethod original, Cloner cloner) : base(original, cloner) { }
|
---|
| 77 | public ReferenceSetUpdateMethod() : base() { Initialize(); }
|
---|
| 78 |
|
---|
| 79 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 80 | return new ReferenceSetUpdateMethod(this, cloner);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | private void Initialize() {
|
---|
| 84 | #region Create parameters
|
---|
[7740] | 85 | Parameters.Add(new ScopeParameter("CurrentScope"));
|
---|
| 86 | Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions"));
|
---|
| 87 | Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));
|
---|
[7744] | 88 | Parameters.Add(new ValueLookupParameter<IItem>("Target"));
|
---|
| 89 | Parameters.Add(new ValueLookupParameter<DiversityCalculator>("DiversityCalculator"));
|
---|
[7722] | 90 | #endregion
|
---|
[7744] | 91 | TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem
|
---|
[7722] | 92 | }
|
---|
| 93 |
|
---|
| 94 | public override IOperation Apply() {
|
---|
[7740] | 95 | var population = new Dictionary<IScope, double>();
|
---|
| 96 | foreach (var pScope in CurrentScope.SubScopes[0].SubScopes) {
|
---|
| 97 | int diversity = 0;
|
---|
[7744] | 98 | var pSol = pScope.Variables[TargetParameter.ActualName].Value;
|
---|
[7740] | 99 | foreach (var rScope in CurrentScope.SubScopes[1].SubScopes) {
|
---|
[7744] | 100 | var rSol = rScope.Variables[TargetParameter.ActualName].Value;
|
---|
| 101 | diversity += DiversityCalculator.ExecuteCalculation(pSol, rSol);
|
---|
[7722] | 102 | }
|
---|
[7740] | 103 | population[pScope] = diversity;
|
---|
[7722] | 104 | }
|
---|
[7740] | 105 | foreach (var entry in population.OrderByDescending(x => x.Value).Take(ReferenceSetSize.Value - NumberOfHighQualitySolutions.Value)) {
|
---|
| 106 | CurrentScope.SubScopes[1].SubScopes.Add(entry.Key);
|
---|
| 107 | CurrentScope.SubScopes[0].SubScopes.Remove(entry.Key);
|
---|
| 108 | }
|
---|
[7722] | 109 | return base.Apply();
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|