[4039] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4039] | 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
|
---|
[4040] | 21 |
|
---|
[4039] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
[4040] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[4902] | 30 | using HeuristicLab.Common;
|
---|
[4039] | 31 |
|
---|
| 32 | namespace HeuristicLab.Algorithms.NSGA2 {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// CrowdedComparisonSorter as described in: Deb, Pratap, Agrawal and Meyarivan, "A Fast and Elitist Multiobjective
|
---|
| 35 | /// Genetic Algorithm: NSGA-II", IEEE Transactions On Evolutionary Computation, Vol. 6, No. 2, April 2002
|
---|
| 36 | /// </summary>
|
---|
| 37 | [Item("CrowdedComparisonSorter", @"CrowdedComparisonSorter as described in: Deb, Pratap, Agrawal and Meyarivan, ""A Fast and Elitist Multiobjective
|
---|
| 38 | Genetic Algorithm: NSGA-II"", IEEE Transactions On Evolutionary Computation, Vol. 6, No. 2, April 2002.")]
|
---|
| 39 | [StorableClass]
|
---|
| 40 | public class CrowdedComparisonSorter : SingleSuccessorOperator {
|
---|
| 41 |
|
---|
| 42 | public IScopeTreeLookupParameter<IntValue> RankParameter {
|
---|
| 43 | get { return (IScopeTreeLookupParameter<IntValue>)Parameters["Rank"]; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public IScopeTreeLookupParameter<DoubleValue> CrowdingDistanceParameter {
|
---|
| 47 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["CrowdingDistance"]; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[4902] | 50 | [StorableConstructor]
|
---|
| 51 | protected CrowdedComparisonSorter(bool deserializing) : base(deserializing) { }
|
---|
| 52 | protected CrowdedComparisonSorter(CrowdedComparisonSorter original, Cloner cloner) : base(original, cloner) { }
|
---|
[4039] | 53 | public CrowdedComparisonSorter() {
|
---|
| 54 | Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Rank", "The rank of the solution."));
|
---|
| 55 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("CrowdingDistance", "The crowding distance of the solution."));
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public override IOperation Apply() {
|
---|
| 59 | ItemArray<IntValue> ranks = RankParameter.ActualValue;
|
---|
| 60 | ItemArray<DoubleValue> distances = CrowdingDistanceParameter.ActualValue;
|
---|
| 61 | int size = ranks.Length;
|
---|
| 62 | int[] indices = Enumerable.Range(0, size).ToArray();
|
---|
| 63 |
|
---|
| 64 | IScope[] scopes = ExecutionContext.Scope.SubScopes.ToArray();
|
---|
[4067] | 65 | Array.Sort(indices, scopes, new CustomComparer(ranks, distances));
|
---|
[4039] | 66 | ExecutionContext.Scope.SubScopes.Clear();
|
---|
| 67 | ExecutionContext.Scope.SubScopes.AddRange(scopes);
|
---|
| 68 | return base.Apply();
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | private class CustomComparer : IComparer<int> {
|
---|
| 72 | ItemArray<IntValue> ranks;
|
---|
| 73 | ItemArray<DoubleValue> distances;
|
---|
| 74 |
|
---|
| 75 | public CustomComparer(ItemArray<IntValue> ranks, ItemArray<DoubleValue> distances) {
|
---|
| 76 | this.ranks = ranks;
|
---|
| 77 | this.distances = distances;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | #region IComparer<int> Members
|
---|
| 81 |
|
---|
| 82 | public int Compare(int x, int y) {
|
---|
| 83 | if (ranks[x].Value < ranks[y].Value) return -1;
|
---|
| 84 | else if (ranks[x].Value > ranks[y].Value) return 1;
|
---|
| 85 | else { // ranks are the same -> compare by distance
|
---|
| 86 | if (distances[x].Value > distances[y].Value) return -1;
|
---|
| 87 | else if (distances[x].Value < distances[y].Value) return 1;
|
---|
| 88 | else return 0; // same distance
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | #endregion
|
---|
| 93 | }
|
---|
[4902] | 94 |
|
---|
| 95 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 96 | return new CrowdedComparisonSorter(this, cloner);
|
---|
| 97 | }
|
---|
[4039] | 98 | }
|
---|
| 99 | }
|
---|