[8354] | 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.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[8377] | 28 | using HeuristicLab.Selection;
|
---|
[8354] | 29 |
|
---|
[8377] | 30 | namespace HeuristicLab.Algorithms.RAPGA {
|
---|
[8354] | 31 | /// <summary>
|
---|
| 32 | /// A selection operator which considers a single solution representation and selects duplicates.
|
---|
| 33 | /// </summary>
|
---|
[8377] | 34 | /// <remarks>
|
---|
| 35 | /// The remaining scope then contains unique solutions and the selected scope their duplicates.
|
---|
| 36 | /// </remarks>
|
---|
| 37 | [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.")]
|
---|
[8354] | 38 | [StorableClass]
|
---|
[8855] | 39 | public sealed class DuplicatesSelector : SingleObjectiveSelector, ISimilarityBasedOperator {
|
---|
[8354] | 40 | #region ISimilarityBasedOperator Members
|
---|
| 41 | [Storable]
|
---|
| 42 | public ISolutionSimilarityCalculator SimilarityCalculator { get; set; }
|
---|
| 43 | #endregion
|
---|
| 44 |
|
---|
| 45 | [StorableConstructor]
|
---|
[8377] | 46 | private DuplicatesSelector(bool deserializing) : base(deserializing) { }
|
---|
| 47 | private DuplicatesSelector(DuplicatesSelector original, Cloner cloner)
|
---|
[8354] | 48 | : base(original, cloner) {
|
---|
| 49 | this.SimilarityCalculator = cloner.Clone(original.SimilarityCalculator);
|
---|
| 50 | }
|
---|
[8377] | 51 | public DuplicatesSelector() : base() { }
|
---|
| 52 |
|
---|
[8354] | 53 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[8377] | 54 | return new DuplicatesSelector(this, cloner);
|
---|
[8354] | 55 | }
|
---|
| 56 |
|
---|
| 57 | protected override IScope[] Select(List<IScope> scopes) {
|
---|
| 58 | bool copy = CopySelected.Value;
|
---|
| 59 |
|
---|
| 60 | var marks = new bool[scopes.Count];
|
---|
| 61 | for (int i = 0; i < scopes.Count; i++) {
|
---|
| 62 | for (int j = i + 1; j < scopes.Count; j++) {
|
---|
| 63 | if (SimilarityCalculator.Equals(scopes[i], scopes[j])) marks[j] = true;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | var selected = new IScope[marks.Count(x => x)];
|
---|
| 68 | int k = 0;
|
---|
| 69 | if (copy) {
|
---|
| 70 | for (int i = 0; i < scopes.Count; i++)
|
---|
| 71 | if (marks[i]) {
|
---|
| 72 | selected[k++] = (IScope)scopes[i].Clone();
|
---|
| 73 | }
|
---|
| 74 | } else {
|
---|
| 75 | for (int i = 0; i < scopes.Count; i++)
|
---|
| 76 | if (marks[i]) {
|
---|
| 77 | selected[k++] = scopes[i];
|
---|
| 78 | scopes[i] = null;
|
---|
| 79 | }
|
---|
| 80 | scopes.RemoveAll(x => x == null);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | return selected;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|