Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization.Operators/3.3/MultiObjective/CrowdedComparisonSorter.cs @ 17456

Last change on this file since 17456 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HEAL.Attic;
31
32namespace 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
38Genetic Algorithm: NSGA-II"", IEEE Transactions On Evolutionary Computation, Vol. 6, No. 2, April 2002.")]
39  [StorableType("48F09742-454D-4BAB-A057-96BDF4ACDCE9")]
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(StorableConstructorFlag _) : base(_) { }
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    [StorableType("c16dd393-bd08-4a81-abb5-1b7e918a1038")]
72    private class CustomComparer : IComparer<int> {
73      ItemArray<IntValue> ranks;
74      ItemArray<DoubleValue> distances;
75
76      public CustomComparer(ItemArray<IntValue> ranks, ItemArray<DoubleValue> distances) {
77        this.ranks = ranks;
78        this.distances = distances;
79      }
80
81      #region IComparer<int> Members
82
83      public int Compare(int x, int y) {
84        if (ranks[x].Value < ranks[y].Value) return -1;
85        else if (ranks[x].Value > ranks[y].Value) return 1;
86        else { // ranks are the same -> compare by distance
87          if (distances[x].Value > distances[y].Value) return -1;
88          else if (distances[x].Value < distances[y].Value) return 1;
89          else return 0; // same distance
90        }
91      }
92
93      #endregion
94    }
95
96    public override IDeepCloneable Clone(Cloner cloner) {
97      return new CrowdedComparisonSorter(this, cloner);
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.