Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.NSGA2/3.3/CrowdingDistanceAssignment.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: 4.5 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  [Item("CrowdingDistanceAssignment", "Calculates the crowding distances for each sub-scope as described in Deb et al. 2002. A Fast and Elitist Multiobjective Genetic Algorithm: NSGA-II. IEEE Transactions on Evolutionary Computation, 6(2), pp. 182-197.")]
33  [StorableClass]
34  public class CrowdingDistanceAssignment : SingleSuccessorOperator {
35
36    public ScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
37      get { return (ScopeTreeLookupParameter<DoubleArray>)Parameters["Qualities"]; }
38    }
39
40    public ScopeTreeLookupParameter<DoubleValue> CrowdingDistanceParameter {
41      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["CrowdingDistance"]; }
42    }
43
44    private void QualitiesParameter_DepthChanged(object sender, EventArgs e) {
45      CrowdingDistanceParameter.Depth = QualitiesParameter.Depth;
46    }
47
48    [StorableConstructor]
49    protected CrowdingDistanceAssignment(bool deserializing) : base(deserializing) { }
50    public CrowdingDistanceAssignment() {
51      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The vector of quality values."));
52      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("CrowdingDistance", "Sets the crowding distance in each sub-scope."));
53      AttachEventHandlers();
54    }
55
56    [StorableHook(HookType.AfterDeserialization)]
57    private void AttachEventHandlers() {
58      QualitiesParameter.DepthChanged += new EventHandler(QualitiesParameter_DepthChanged);
59    }
60
61    public static void Apply(DoubleArray[] qualities, DoubleValue[] distances) {
62      int populationSize = qualities.Length;
63      int objectiveCount = qualities[0].Length;
64      for (int m = 0; m < objectiveCount; m++) {
65        Array.Sort<DoubleArray, DoubleValue>(qualities, distances, new QualitiesComparer(m));
66
67        distances[0].Value = double.MaxValue;
68        distances[populationSize - 1].Value = double.MaxValue;
69
70        double minQuality = qualities[0][m];
71        double maxQuality = qualities[populationSize - 1][m];
72        for (int i = 1; i < populationSize - 1; i++) {
73          distances[i].Value += (qualities[i + 1][m] - qualities[i - 1][m]) / (maxQuality - minQuality);
74        }
75      }
76    }
77
78    public override IOperation Apply() {
79      DoubleArray[] qualities = QualitiesParameter.ActualValue.ToArray();
80      int populationSize = qualities.Length;
81      DoubleValue[] distances = new DoubleValue[populationSize];
82      for (int i = 0; i < populationSize; i++)
83        distances[i] = new DoubleValue(0);
84
85      CrowdingDistanceParameter.ActualValue = new ItemArray<DoubleValue>(distances);
86     
87      Apply(qualities, distances);
88
89      return base.Apply();
90    }
91
92    private void Initialize(ItemArray<DoubleValue> distances) {
93      for (int i = 0; i < distances.Length; i++) {
94        if (distances[i] == null) distances[i] = new DoubleValue(0);
95        else distances[i].Value = 0;
96      }
97    }
98
99    private class QualitiesComparer : IComparer<DoubleArray> {
100      private int index;
101
102      public QualitiesComparer(int index) {
103        this.index = index;
104      }
105
106      #region IComparer<DoubleArray> Members
107
108      public int Compare(DoubleArray x, DoubleArray y) {
109        if (x[index] < y[index]) return -1;
110        else if (x[index] > y[index]) return +1;
111        else return 0;
112      }
113
114      #endregion
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.