1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HEAL.Attic;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Optimization.Operators {
|
---|
33 | [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.")]
|
---|
34 | [StorableType("F7DF8B74-F1E6-45D6-A1A8-5D381F20B382")]
|
---|
35 | public class CrowdingDistanceAssignment : SingleSuccessorOperator, IMultiObjectiveOperator {
|
---|
36 |
|
---|
37 | public ScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
|
---|
38 | get { return (ScopeTreeLookupParameter<DoubleArray>)Parameters["Qualities"]; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public ScopeTreeLookupParameter<DoubleValue> CrowdingDistanceParameter {
|
---|
42 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["CrowdingDistance"]; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | private void QualitiesParameter_DepthChanged(object sender, EventArgs e) {
|
---|
46 | CrowdingDistanceParameter.Depth = QualitiesParameter.Depth;
|
---|
47 | }
|
---|
48 |
|
---|
49 | [StorableConstructor]
|
---|
50 | protected CrowdingDistanceAssignment(StorableConstructorFlag _) : base(_) { }
|
---|
51 | protected CrowdingDistanceAssignment(CrowdingDistanceAssignment original, Cloner cloner) : base(original, cloner) { }
|
---|
52 | public CrowdingDistanceAssignment() {
|
---|
53 | Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The vector of quality values."));
|
---|
54 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("CrowdingDistance", "Sets the crowding distance in each sub-scope."));
|
---|
55 | RegisterEventHandlers();
|
---|
56 | }
|
---|
57 |
|
---|
58 | [StorableHook(HookType.AfterDeserialization)]
|
---|
59 | private void AfterDeserialization() {
|
---|
60 | RegisterEventHandlers();
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void RegisterEventHandlers() {
|
---|
64 | QualitiesParameter.DepthChanged += new EventHandler(QualitiesParameter_DepthChanged);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public static void Apply(DoubleArray[] qualities, DoubleValue[] distances) {
|
---|
68 | int populationSize = qualities.Length;
|
---|
69 | int objectiveCount = qualities[0].Length;
|
---|
70 | for (int m = 0; m < objectiveCount; m++) {
|
---|
71 | Array.Sort<DoubleArray, DoubleValue>(qualities, distances, new QualitiesComparer(m));
|
---|
72 |
|
---|
73 | distances[0].Value = double.MaxValue;
|
---|
74 | distances[populationSize - 1].Value = double.MaxValue;
|
---|
75 |
|
---|
76 | double minQuality = qualities[0][m];
|
---|
77 | double maxQuality = qualities[populationSize - 1][m];
|
---|
78 | for (int i = 1; i < populationSize - 1; i++) {
|
---|
79 | distances[i].Value += (qualities[i + 1][m] - qualities[i - 1][m]) / (maxQuality - minQuality);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | public override IOperation Apply() {
|
---|
85 | DoubleArray[] qualities = QualitiesParameter.ActualValue.ToArray();
|
---|
86 | int populationSize = qualities.Length;
|
---|
87 | DoubleValue[] distances = new DoubleValue[populationSize];
|
---|
88 | for (int i = 0; i < populationSize; i++)
|
---|
89 | distances[i] = new DoubleValue(0);
|
---|
90 |
|
---|
91 | CrowdingDistanceParameter.ActualValue = new ItemArray<DoubleValue>(distances);
|
---|
92 |
|
---|
93 | Apply(qualities, distances);
|
---|
94 |
|
---|
95 | return base.Apply();
|
---|
96 | }
|
---|
97 |
|
---|
98 | private void Initialize(ItemArray<DoubleValue> distances) {
|
---|
99 | for (int i = 0; i < distances.Length; i++) {
|
---|
100 | if (distances[i] == null) distances[i] = new DoubleValue(0);
|
---|
101 | else distances[i].Value = 0;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | [StorableType("30fd1927-b268-4ce7-8960-b04bfa83f1e6")]
|
---|
106 | private class QualitiesComparer : IComparer<DoubleArray> {
|
---|
107 | private int index;
|
---|
108 |
|
---|
109 | public QualitiesComparer(int index) {
|
---|
110 | this.index = index;
|
---|
111 | }
|
---|
112 |
|
---|
113 | #region IComparer<DoubleArray> Members
|
---|
114 |
|
---|
115 | public int Compare(DoubleArray x, DoubleArray y) {
|
---|
116 | if (x[index] < y[index]) return -1;
|
---|
117 | else if (x[index] > y[index]) return +1;
|
---|
118 | else return 0;
|
---|
119 | }
|
---|
120 |
|
---|
121 | #endregion
|
---|
122 | }
|
---|
123 |
|
---|
124 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
125 | return new CrowdingDistanceAssignment(this, cloner);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|