Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization.Operators/3.3/MultiObjective/CrowdingDistanceAssignment.cs

Last change on this file was 17226, checked in by mkommend, 5 years ago

#2521: Merged trunk changes into problem refactoring branch.

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HEAL.Attic;
31
32namespace 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    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(StorableConstructorFlag _) : base(_) { }
50    protected CrowdingDistanceAssignment(CrowdingDistanceAssignment original, Cloner cloner) : base(original, cloner) { }
51    public CrowdingDistanceAssignment() {
52      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The vector of quality values."));
53      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("CrowdingDistance", "Sets the crowding distance in each sub-scope."));
54      RegisterEventHandlers();
55    }
56
57    [StorableHook(HookType.AfterDeserialization)]
58    private void AfterDeserialization() {
59      RegisterEventHandlers();
60    }
61
62    private void RegisterEventHandlers() {
63      QualitiesParameter.DepthChanged += new EventHandler(QualitiesParameter_DepthChanged);
64    }
65
66    public static void Apply(DoubleArray[] qualities, DoubleValue[] distances) {
67      var dist = CrowdingCalculator.CalculateCrowdingDistances(qualities.Select(x => x.ToArray()).ToArray());
68      for (var i = 0; i < distances.Length; i++) distances[i].Value = dist[i];
69    }
70
71    public override IOperation Apply() {
72      var dist = CrowdingCalculator.CalculateCrowdingDistances(QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray());
73      CrowdingDistanceParameter.ActualValue = new ItemArray<DoubleValue>(dist.Select(d => new DoubleValue(d)));
74      return base.Apply();
75    }
76
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new CrowdingDistanceAssignment(this, cloner);
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.