Free cookie consent management tool by TermsFeed Policy Generator

Ticket #1040: CrowdedComparisonSorter.cs

File CrowdedComparisonSorter.cs, 2.8 KB (added by gkronber, 14 years ago)

CrowdedComparisonSorter.cs

Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
21using System;
22using System.Collections.Generic;
23using System.Linq;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Common;
27using HeuristicLab.Data;
28using HeuristicLab.DataAnalysis;
29using HeuristicLab.Modeling;
30using HeuristicLab.GP;
31using HeuristicLab.GP.StructureIdentification;
32using HeuristicLab.GP.Interfaces;
33
34namespace HeuristicLab.LinearRegression {
35  /// <summary>
36  /// 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  /// </summary>
39  public class CrowdedComparisonSorter : OperatorBase {
40
41    public override string Description {
42      get {
43        return
44@"CrowdedComparisonSorter as described in: Deb, Pratap, Agrawal and Meyarivan, ""A Fast and Elitist Multiobjective
45Genetic Algorithm: NSGA-II"", IEEE Transactions On Evolutionary Computation, Vol. 6, No. 2, April 2002";
46      }
47    }
48
49    public CrowdedComparisonSorter() {
50    }
51
52    public override IOperation Apply(IScope scope) {
53      var scopes = new List<IScope>(scope.SubScopes);
54
55      scopes.Sort((a, b) => {
56        if (GetRank(a) < GetRank(b)) return -1;
57        else if (GetRank(a) > GetRank(b)) return 1;
58        else { // ranks are the same -> compare by distance
59          if (GetDistance(a) > GetDistance(b)) return -1;
60          else if (GetDistance(a) < GetDistance(b)) return 1;
61          else return 0; // same distance
62        }
63      });
64
65      while (scope.SubScopes.Count > 0) scope.RemoveSubScope(scope.SubScopes[0]);
66      foreach (var subScope in scopes) {
67        scope.AddSubScope(subScope);
68      }
69
70      return null;
71    }
72
73    private double GetRank(IScope a) {
74      return a.GetVariableValue<DoubleData>("Rank", false).Data;
75    }
76
77    private double GetDistance(IScope a) {
78      return a.GetVariableValue<DoubleData>("Distance", false).Data;
79    }
80  }
81}