Free cookie consent management tool by TermsFeed Policy Generator

Ticket #2944: CustomHVAnalyzer.cs

File CustomHVAnalyzer.cs, 6.1 KB (added by abeham, 6 years ago)

contributed by David Schmaranzer

Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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;
25
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Problems.TestFunctions.MultiObjective;
34
35namespace HeuristicLab.Analysis {
36//namespace HeuristicLab.Problems.TestFunctions.MultiObjective {
37
38    [StorableClass]
39  public class CustomHVAnalyzer : SingleSuccessorOperator, IAnalyzer, IMultiObjectiveOperator {
40    public virtual bool EnabledByDefault { get { return true; } }
41
42    public ILookupParameter<BoolArray> MaximizationParameter {
43        get { return (ILookupParameter<BoolArray>)Parameters["Maximization"]; }
44    }
45
46    //parameters from MOTFAnalyzer
47        public IScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
48        get { return (IScopeTreeLookupParameter<DoubleArray>)Parameters["Qualities"]; }
49    }
50
51    public ILookupParameter<ResultCollection> ResultsParameter {
52        get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
53    }
54   
55    public ILookupParameter<DoubleMatrix> BestKnownFrontParameter {
56        get { return (ILookupParameter<DoubleMatrix>)Parameters["BestKnownFront"]; }
57    }
58   
59    //parameters from HypervolumeAnalyzer
60    public ILookupParameter<DoubleArray> ReferencePointParameter {
61        get { return (ILookupParameter<DoubleArray>)Parameters["ReferencePoint"]; }
62    }
63
64    public IResultParameter<DoubleValue> HypervolumeResultParameter {
65        get { return (IResultParameter<DoubleValue>)Parameters["Hypervolume"]; }
66    }
67
68    public IResultParameter<DoubleValue> BestKnownHypervolumeResultParameter {
69        get { return (IResultParameter<DoubleValue>)Parameters["Best known hypervolume"]; }
70    }
71
72    public IResultParameter<DoubleValue> HypervolumeDistanceResultParameter {
73        get { return (IResultParameter<DoubleValue>)Parameters["Absolute Distance to BestKnownHypervolume"]; }
74    }
75   
76   
77    [StorableConstructor]
78    protected CustomHVAnalyzer(bool deserializing) : base(deserializing) { }
79    protected CustomHVAnalyzer(CustomHVAnalyzer original, Cloner cloner) : base(original, cloner){ }
80
81   
82    public override IDeepCloneable Clone(Cloner cloner)
83    {
84        return new CustomHVAnalyzer(this, cloner);
85    }
86   
87   
88
89    public CustomHVAnalyzer()
90    {
91        Parameters.Add(new LookupParameter<BoolArray>("Maximization", "maximization or min. of objectives"));
92
93        //add parameters from MOTFAnalyzer
94        Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The qualities of the parameter vector."));
95        Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to write to."));
96        Parameters.Add(new LookupParameter<DoubleMatrix>("BestKnownFront", "The currently best known Pareto front"));
97
98        //add parameters from HypervolumeAnalyzer
99        Parameters.Add(new LookupParameter<DoubleArray>("ReferencePoint", "The reference point for hypervolume calculation"));
100        Parameters.Add(new ResultParameter<DoubleValue>("Hypervolume", "The hypervolume of the current generation"));
101        Parameters.Add(new ResultParameter<DoubleValue>("Best known hypervolume", "The optimal hypervolume"));
102        Parameters.Add(new ResultParameter<DoubleValue>("Absolute Distance to BestKnownHypervolume", "The difference between the best known and the current hypervolume"));
103       
104        //set parameters from MOTFAnalyzer - NONE
105       
106        //set parameters from HypervolumeAnalyzer
107        HypervolumeResultParameter.DefaultValue = new DoubleValue(0);
108        BestKnownHypervolumeResultParameter.DefaultValue = new DoubleValue(0);
109        HypervolumeDistanceResultParameter.DefaultValue = new DoubleValue(0);     
110        //ReferencePointParameter.ActualValue = new DoubleArray(new double[] { 120000, 8 });
111
112        }
113
114        public override IOperation Apply()
115        {
116            var qualities = QualitiesParameter.ActualValue;
117            int objectives = qualities[0].Length;
118            bool[] maximization = new bool[objectives];
119            maximization = MaximizationParameter.ActualValue.ToArray();
120
121            var referencePoint = ReferencePointParameter.ActualValue;
122
123            double best = BestKnownHypervolumeResultParameter.ActualValue.Value;
124            /*
125            if (referencePoint.SequenceEqual(ReferencePointParameter.ActualValue))
126            {
127                best = Math.Max(best, !!!Optimal Hypervolume!!!)
128            }
129            */
130
131            IEnumerable<double[]> front = NonDominatedSelect.SelectNonDominatedVectors(qualities.Select(q => q.ToArray()), maximization, true);
132
133            double hv = Hypervolume.Calculate(front, referencePoint.ToArray(), maximization);
134
135            if (hv > best)
136            {
137                best = hv;
138            }
139
140            HypervolumeResultParameter.ActualValue.Value = hv;
141            BestKnownHypervolumeResultParameter.ActualValue.Value = best;
142            HypervolumeDistanceResultParameter.ActualValue.Value = best - hv;
143           
144            return base.Apply();
145        }
146  }
147}