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