1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.ScatterSearch {
|
---|
33 | /// <summary>
|
---|
34 | /// An operator that updates the reference set.
|
---|
35 | /// </summary>
|
---|
36 | [Item("ReferenceSetUpdateMethod", "An operator that updates the reference set.")]
|
---|
37 | [StorableType("a385fb1b-ac3f-4892-a56f-00e65559228c")]
|
---|
38 | public sealed class ReferenceSetUpdateMethod : SingleSuccessorOperator {
|
---|
39 | #region Parameter properties
|
---|
40 | public ScopeParameter CurrentScopeParameter {
|
---|
41 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
42 | }
|
---|
43 | public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
|
---|
44 | get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
|
---|
45 | }
|
---|
46 | public IValueLookupParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
|
---|
47 | get { return (IValueLookupParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
|
---|
48 | }
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | #region Properties
|
---|
52 | private IScope CurrentScope {
|
---|
53 | get { return CurrentScopeParameter.ActualValue; }
|
---|
54 | }
|
---|
55 | private IntValue ReferenceSetSize {
|
---|
56 | get { return ReferenceSetSizeParameter.ActualValue; }
|
---|
57 | }
|
---|
58 | #endregion
|
---|
59 |
|
---|
60 | [StorableConstructor]
|
---|
61 | private ReferenceSetUpdateMethod(StorableConstructorFlag deserializing) : base(deserializing) { }
|
---|
62 | private ReferenceSetUpdateMethod(ReferenceSetUpdateMethod original, Cloner cloner) : base(original, cloner) { }
|
---|
63 | public ReferenceSetUpdateMethod()
|
---|
64 | : base() {
|
---|
65 | #region Create parameters
|
---|
66 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the population and the reference set."));
|
---|
67 | Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set."));
|
---|
68 | Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
|
---|
69 | #endregion
|
---|
70 | }
|
---|
71 |
|
---|
72 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
73 | return new ReferenceSetUpdateMethod(this, cloner);
|
---|
74 | }
|
---|
75 |
|
---|
76 | [StorableHook(HookType.AfterDeserialization)]
|
---|
77 | private void AfterDeserialization() {
|
---|
78 | // BackwardsCompatibility3.3
|
---|
79 | #region Backwards compatible code, remove with 3.4
|
---|
80 | if (!Parameters.ContainsKey("SimilarityCalculator"))
|
---|
81 | Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
|
---|
82 | #endregion
|
---|
83 | }
|
---|
84 |
|
---|
85 | public override IOperation Apply() {
|
---|
86 | var populationSimilarity = new Dictionary<IScope, double>();
|
---|
87 | var populationScope = CurrentScope.SubScopes[0];
|
---|
88 | var refSetScope = CurrentScope.SubScopes[1];
|
---|
89 | var similarityMatrix = SimilarityCalculatorParameter.ActualValue.CalculateSolutionCrowdSimilarity(populationScope, refSetScope);
|
---|
90 | for (int i = 0; i < populationScope.SubScopes.Count; i++) {
|
---|
91 | populationSimilarity[populationScope.SubScopes[i]] = similarityMatrix[i].Sum();
|
---|
92 | }
|
---|
93 | int numberOfHighQualitySolutions = CurrentScope.SubScopes[1].SubScopes.Count;
|
---|
94 | foreach (var entry in populationSimilarity.OrderBy(x => x.Value).Take(ReferenceSetSize.Value - numberOfHighQualitySolutions)) {
|
---|
95 | CurrentScope.SubScopes[1].SubScopes.Add(entry.Key);
|
---|
96 | CurrentScope.SubScopes[0].SubScopes.Remove(entry.Key);
|
---|
97 | }
|
---|
98 | return base.Apply();
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|