1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Optimization.Operators {
|
---|
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 |
|
---|
50 | [StorableConstructor]
|
---|
51 | protected CrowdedComparisonSorter(bool deserializing) : base(deserializing) { }
|
---|
52 | protected CrowdedComparisonSorter(CrowdedComparisonSorter original, Cloner cloner) : base(original, cloner) { }
|
---|
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();
|
---|
65 | Array.Sort(indices, scopes, new CustomComparer(ranks, distances));
|
---|
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 | }
|
---|
94 |
|
---|
95 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
96 | return new CrowdedComparisonSorter(this, cloner);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|