Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.NSGA2/3.3/CrowdedComparisonSorter.cs @ 4067

Last change on this file since 4067 was 4067, checked in by abeham, 12 years ago

#1040

  • Fixed some bugs in NSGA-II
File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace 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
37Genetic 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();
61      Array.Sort(indices, scopes, new CustomComparer(ranks, distances));
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}
Note: See TracBrowser for help on using the repository browser.