[8354] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8354] | 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.Optimization;
|
---|
[12069] | 27 | using HeuristicLab.Parameters;
|
---|
[14927] | 28 | using HeuristicLab.Persistence;
|
---|
[8377] | 29 | using HeuristicLab.Selection;
|
---|
[8354] | 30 |
|
---|
[8377] | 31 | namespace HeuristicLab.Algorithms.RAPGA {
|
---|
[8354] | 32 | /// <summary>
|
---|
| 33 | /// A selection operator which considers a single solution representation and selects duplicates.
|
---|
| 34 | /// </summary>
|
---|
[8377] | 35 | /// <remarks>
|
---|
| 36 | /// The remaining scope then contains unique solutions and the selected scope their duplicates.
|
---|
| 37 | /// </remarks>
|
---|
| 38 | [Item("DuplicatesSelector", "A selection operator which considers a single solution representation and selects duplicates. The remaining scope then contains unique solutions and the selected scope their duplicates.")]
|
---|
[14927] | 39 | [StorableType("ddb4275c-8643-49eb-b58b-2d666096cf79")]
|
---|
[12069] | 40 | public sealed class DuplicatesSelector : SingleObjectiveSelector {
|
---|
| 41 | public IValueLookupParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
|
---|
| 42 | get { return (IValueLookupParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
|
---|
| 43 | }
|
---|
[8354] | 44 |
|
---|
| 45 | [StorableConstructor]
|
---|
[15018] | 46 | private DuplicatesSelector(StorableConstructorFlag deserializing) : base(deserializing) { }
|
---|
[12069] | 47 | private DuplicatesSelector(DuplicatesSelector original, Cloner cloner) : base(original, cloner) { }
|
---|
| 48 | public DuplicatesSelector()
|
---|
| 49 | : base() {
|
---|
| 50 | Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
|
---|
[8354] | 51 | }
|
---|
[8377] | 52 |
|
---|
[8354] | 53 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[8377] | 54 | return new DuplicatesSelector(this, cloner);
|
---|
[8354] | 55 | }
|
---|
| 56 |
|
---|
[12069] | 57 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 58 | private void AfterDeserialization() {
|
---|
| 59 | // BackwardsCompatibility3.3
|
---|
| 60 | #region Backwards compatible code, remove with 3.4
|
---|
| 61 | if (!Parameters.ContainsKey("SimilarityCalculator"))
|
---|
| 62 | Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
|
---|
| 63 | #endregion
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[8354] | 66 | protected override IScope[] Select(List<IScope> scopes) {
|
---|
| 67 | bool copy = CopySelected.Value;
|
---|
| 68 |
|
---|
| 69 | var marks = new bool[scopes.Count];
|
---|
[8989] | 70 | for (int i = 0; i < scopes.Count; i++)
|
---|
| 71 | for (int j = i + 1; j < scopes.Count; j++)
|
---|
[12069] | 72 | marks[j] = SimilarityCalculatorParameter.ActualValue.Equals(scopes[i], scopes[j]);
|
---|
[8354] | 73 |
|
---|
[8989] | 74 |
|
---|
[8354] | 75 | var selected = new IScope[marks.Count(x => x)];
|
---|
| 76 | int k = 0;
|
---|
| 77 | if (copy) {
|
---|
| 78 | for (int i = 0; i < scopes.Count; i++)
|
---|
| 79 | if (marks[i]) {
|
---|
| 80 | selected[k++] = (IScope)scopes[i].Clone();
|
---|
| 81 | }
|
---|
| 82 | } else {
|
---|
| 83 | for (int i = 0; i < scopes.Count; i++)
|
---|
| 84 | if (marks[i]) {
|
---|
| 85 | selected[k++] = scopes[i];
|
---|
| 86 | scopes[i] = null;
|
---|
| 87 | }
|
---|
| 88 | scopes.RemoveAll(x => x == null);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | return selected;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|