[5143] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5143] | 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.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Analysis {
|
---|
[5438] | 32 | [Item("RankBasedParetoFrontAnalyzer", "Uses the rank value that is computed by e.g. the NSGA2's fast non dominated sort operator to collect all solutions and their qualities of front 0 (the current Pareto front).")]
|
---|
[5143] | 33 | [StorableClass]
|
---|
| 34 | public class RankBasedParetoFrontAnalyzer : ParetoFrontAnalyzer {
|
---|
| 35 | public IScopeTreeLookupParameter<IntValue> RankParameter {
|
---|
| 36 | get { return (IScopeTreeLookupParameter<IntValue>)Parameters["Rank"]; }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | [StorableConstructor]
|
---|
| 40 | protected RankBasedParetoFrontAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 41 | protected RankBasedParetoFrontAnalyzer(RankBasedParetoFrontAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
| 42 | public RankBasedParetoFrontAnalyzer() {
|
---|
| 43 | Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Rank", "The rank of solution [0..N] describes to which front it belongs."));
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | protected override void Analyze(ItemArray<DoubleArray> qualities, ResultCollection results) {
|
---|
| 47 | ItemArray<IntValue> ranks = RankParameter.ActualValue;
|
---|
| 48 |
|
---|
| 49 | bool populationLevel = RankParameter.Depth == 1;
|
---|
| 50 |
|
---|
| 51 | int objectives = qualities[0].Length;
|
---|
| 52 | int frontSize = ranks.Count(x => x.Value == 0);
|
---|
| 53 | ItemArray<IScope> paretoArchive = null;
|
---|
| 54 | if (populationLevel) paretoArchive = new ItemArray<IScope>(frontSize);
|
---|
| 55 |
|
---|
| 56 | DoubleMatrix front = new DoubleMatrix(frontSize, objectives);
|
---|
| 57 | int counter = 0;
|
---|
| 58 | for (int i = 0; i < ranks.Length; i++) {
|
---|
| 59 | if (ranks[i].Value == 0) {
|
---|
| 60 | for (int k = 0; k < objectives; k++)
|
---|
| 61 | front[counter, k] = qualities[i][k];
|
---|
| 62 | if (populationLevel) {
|
---|
| 63 | paretoArchive[counter] = (IScope)ExecutionContext.Scope.SubScopes[i].Clone();
|
---|
| 64 | }
|
---|
| 65 | counter++;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | front.RowNames = GetRowNames(front);
|
---|
| 70 | front.ColumnNames = GetColumnNames(front);
|
---|
| 71 |
|
---|
| 72 | if (results.ContainsKey("Pareto Front"))
|
---|
| 73 | results["Pareto Front"].Value = front;
|
---|
| 74 | else results.Add(new Result("Pareto Front", front));
|
---|
| 75 |
|
---|
| 76 | if (populationLevel) {
|
---|
| 77 | if (results.ContainsKey("Pareto Archive"))
|
---|
| 78 | results["Pareto Archive"].Value = paretoArchive;
|
---|
| 79 | else results.Add(new Result("Pareto Archive", paretoArchive));
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | private IEnumerable<string> GetRowNames(DoubleMatrix front) {
|
---|
| 84 | for (int i = 1; i <= front.Rows; i++)
|
---|
| 85 | yield return "Solution " + i.ToString();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | private IEnumerable<string> GetColumnNames(DoubleMatrix front) {
|
---|
| 89 | for (int i = 1; i <= front.Columns; i++)
|
---|
| 90 | yield return "Objective " + i.ToString();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 94 | return new RankBasedParetoFrontAnalyzer(this, cloner);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|