Free cookie consent management tool by TermsFeed Policy Generator

Ticket #1040: CrowdingDistanceAssignment.cs

File CrowdingDistanceAssignment.cs, 4.0 KB (added by gkronber, 14 years ago)

CrowdingDistanceAssignment.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  /// CrowdingDistanceAssignment 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 CrowdingDistanceAssignment : OperatorBase {
40    private static double constant = 1.0;
41
42    public override string Description {
43      get {
44        return
45@"CrowdingDistanceAssignment as described in: Deb, Pratap, Agrawal and Meyarivan, ""A Fast and Elitist Multiobjective
46Genetic Algorithm: NSGA-II"", IEEE Transactions On Evolutionary Computation, Vol. 6, No. 2, April 2002";
47      }
48    }
49
50
51    public CrowdingDistanceAssignment() {
52      AddVariableInfo(new VariableInfo("Quality", "Vector of quality values", typeof(ItemList<DoubleData>), VariableKind.In));
53    }
54
55    public override IOperation Apply(IScope scope) {
56      IVariableInfo qualityInfo = GetVariableInfo("Quality");
57      int l = scope.SubScopes.Count;
58      double[] distance = new double[l];
59      foreach (var s in scope.SubScopes) {
60        SetDistance(s, 0.0);
61      }
62      ItemList<DoubleData> quality = scope.SubScopes[0].GetVariableValue<ItemList<DoubleData>>("Quality", false);
63      for (int m = 0; m < quality.Count; m++) {
64        List<IScope> sortedScopes = new List<IScope>(scope.SubScopes);
65        Sort(sortedScopes, m);
66        SetDistance(sortedScopes[0], double.MaxValue);
67        SetDistance(sortedScopes[l - 1], double.MaxValue);
68        double minM = ObjectiveValue(sortedScopes[0], m);
69        double maxM = ObjectiveValue(sortedScopes[l - 1], m);
70        for (int i = 2; i < l - 1; i++) {
71          SetDistance(sortedScopes[i], GetDistance(sortedScopes[i]) + (ObjectiveValue(sortedScopes[i + 1], m) - ObjectiveValue(sortedScopes[i - 1], m)) / (maxM - minM));
72        }
73      }
74      return null;
75    }
76
77    private static double GetDistance(IScope s) {
78      return s.GetVariableValue<DoubleData>("Distance", false).Data;
79    }
80
81    private static void SetDistance(IScope s, double p) {
82      DoubleData distance = s.GetVariableValue<DoubleData>("Distance", false, false);
83      if (distance != null) {
84        distance.Data = p;
85      } else {
86        s.AddVariable(new HeuristicLab.Core.Variable("Distance", new DoubleData(p)));
87      }
88    }
89
90    private static double ObjectiveValue(IScope s, int m) {
91      return s.GetVariableValue<ItemList<DoubleData>>("Quality", false)[m].Data;
92    }
93
94    private static void Sort(List<IScope> sortedScopes, int m) {
95      sortedScopes.Sort((a, b) => {
96        double va = ObjectiveValue(a, m);
97        double vb = ObjectiveValue(b, m);
98        if (va < vb) return -1;
99        else if (va > vb) return +1;
100        else return 0;
101      });
102    }
103  }
104}