[13672] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[13672] | 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 | using System;
|
---|
[13620] | 22 | using System.Collections.Generic;
|
---|
[13672] | 23 | using System.Linq;
|
---|
[13421] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[13620] | 31 | using HeuristicLab.Problems.Instances;
|
---|
[13421] | 32 |
|
---|
[14111] | 33 | namespace HeuristicLab.Problems.TestFunctions.MultiObjective {
|
---|
[13421] | 34 | [StorableClass]
|
---|
[14073] | 35 | [Creatable(CreatableAttribute.Categories.Problems, Priority = 95)]
|
---|
| 36 | [Item("Test Function (multi-objective)", "Test functions with real valued inputs and multiple objectives.")]
|
---|
[13620] | 37 | public class MultiObjectiveTestFunctionProblem : MultiObjectiveBasicProblem<RealVectorEncoding>, IProblemInstanceConsumer<MOTFData> {
|
---|
[13421] | 38 |
|
---|
[13672] | 39 | #region Parameter Properties
|
---|
[16171] | 40 | public new IValueParameter<BoolArray> MaximizationParameter {
|
---|
[14073] | 41 | get { return (IValueParameter<BoolArray>)Parameters["Maximization"]; }
|
---|
[13421] | 42 | }
|
---|
[14073] | 43 | public IFixedValueParameter<IntValue> ProblemSizeParameter {
|
---|
[13421] | 44 | get { return (IFixedValueParameter<IntValue>)Parameters["ProblemSize"]; }
|
---|
| 45 | }
|
---|
[14073] | 46 | public IFixedValueParameter<IntValue> ObjectivesParameter {
|
---|
[13672] | 47 | get { return (IFixedValueParameter<IntValue>)Parameters["Objectives"]; }
|
---|
[13421] | 48 | }
|
---|
[14073] | 49 | public IValueParameter<DoubleMatrix> BoundsParameter {
|
---|
[13421] | 50 | get { return (IValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
| 51 | }
|
---|
| 52 | public IValueParameter<IMultiObjectiveTestFunction> TestFunctionParameter {
|
---|
| 53 | get { return (IValueParameter<IMultiObjectiveTestFunction>)Parameters["TestFunction"]; }
|
---|
| 54 | }
|
---|
[13725] | 55 |
|
---|
[13421] | 56 | #endregion
|
---|
| 57 |
|
---|
| 58 | #region Properties
|
---|
[14073] | 59 | public override bool[] Maximization {
|
---|
[16171] | 60 | get{ return Parameters.ContainsKey(MaximizationParameterName) ? MaximizationParameter.Value.CloneAsArray() : new Fonseca().Maximization(2); }
|
---|
[13672] | 61 | }
|
---|
| 62 |
|
---|
| 63 | public int ProblemSize {
|
---|
[13421] | 64 | get { return ProblemSizeParameter.Value.Value; }
|
---|
| 65 | set { ProblemSizeParameter.Value.Value = value; }
|
---|
| 66 | }
|
---|
[13620] | 67 | public int Objectives {
|
---|
[13672] | 68 | get { return ObjectivesParameter.Value.Value; }
|
---|
| 69 | set { ObjectivesParameter.Value.Value = value; }
|
---|
[13421] | 70 | }
|
---|
| 71 | public DoubleMatrix Bounds {
|
---|
| 72 | get { return BoundsParameter.Value; }
|
---|
| 73 | set { BoundsParameter.Value = value; }
|
---|
| 74 | }
|
---|
| 75 | public IMultiObjectiveTestFunction TestFunction {
|
---|
| 76 | get { return TestFunctionParameter.Value; }
|
---|
| 77 | set { TestFunctionParameter.Value = value; }
|
---|
| 78 | }
|
---|
| 79 | #endregion
|
---|
| 80 |
|
---|
| 81 | [StorableConstructor]
|
---|
[13729] | 82 | protected MultiObjectiveTestFunctionProblem(bool deserializing) : base(deserializing) { }
|
---|
[14073] | 83 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 84 | private void AfterDeserialization() {
|
---|
| 85 | RegisterEventHandlers();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[13729] | 88 | protected MultiObjectiveTestFunctionProblem(MultiObjectiveTestFunctionProblem original, Cloner cloner)
|
---|
[13421] | 89 | : base(original, cloner) {
|
---|
| 90 | RegisterEventHandlers();
|
---|
| 91 | }
|
---|
[14073] | 92 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 93 | return new MultiObjectiveTestFunctionProblem(this, cloner);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[13421] | 96 | public MultiObjectiveTestFunctionProblem()
|
---|
[13515] | 97 | : base() {
|
---|
[13421] | 98 | Parameters.Add(new FixedValueParameter<IntValue>("ProblemSize", "The dimensionality of the problem instance (number of variables in the function).", new IntValue(2)));
|
---|
[13672] | 99 | Parameters.Add(new FixedValueParameter<IntValue>("Objectives", "The dimensionality of the solution vector (number of objectives).", new IntValue(2)));
|
---|
[13448] | 100 | Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The bounds of the solution given as either one line for all variables or a line for each variable. The first column specifies lower bound, the second upper bound.", new DoubleMatrix(new double[,] { { -4, 4 } })));
|
---|
[13421] | 101 | Parameters.Add(new ValueParameter<IMultiObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Fonseca()));
|
---|
| 102 |
|
---|
| 103 | Encoding.LengthParameter = ProblemSizeParameter;
|
---|
| 104 | Encoding.BoundsParameter = BoundsParameter;
|
---|
[13725] | 105 | BestKnownFrontParameter.Hidden = true;
|
---|
[13421] | 106 |
|
---|
[14073] | 107 | UpdateParameterValues();
|
---|
[13421] | 108 | InitializeOperators();
|
---|
| 109 | RegisterEventHandlers();
|
---|
| 110 | }
|
---|
[14073] | 111 |
|
---|
| 112 | private void RegisterEventHandlers() {
|
---|
| 113 | TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
|
---|
| 114 | ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged;
|
---|
| 115 | ObjectivesParameter.Value.ValueChanged += ObjectivesOnValueChanged;
|
---|
[13421] | 116 | }
|
---|
| 117 |
|
---|
[14073] | 118 |
|
---|
[13672] | 119 | public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
|
---|
| 120 | base.Analyze(individuals, qualities, results, random);
|
---|
[16171] | 121 | if (results.ContainsKey("Pareto Front"))
|
---|
[13725] | 122 | ((DoubleMatrix)results["Pareto Front"].Value).SortableView = true;
|
---|
[13421] | 123 | }
|
---|
| 124 |
|
---|
[13776] | 125 | /// <summary>
|
---|
| 126 | /// Checks whether a given solution violates the contraints of this function.
|
---|
| 127 | /// </summary>
|
---|
| 128 | /// <param name="individual"></param>
|
---|
| 129 | /// <returns>a double array that holds the distances that describe how much every contraint is violated (0 is not violated). If the current TestFunction does not have constraints an array of length 0 is returned</returns>
|
---|
[14068] | 130 | public double[] CheckContraints(RealVector individual) {
|
---|
| 131 | var constrainedTestFunction = (IConstrainedTestFunction)TestFunction;
|
---|
| 132 | if (constrainedTestFunction != null) {
|
---|
| 133 | return constrainedTestFunction.CheckConstraints(individual, Objectives);
|
---|
[13776] | 134 | }
|
---|
| 135 | return new double[0];
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[14073] | 138 | public double[] Evaluate(RealVector individual) {
|
---|
[13620] | 139 | return TestFunction.Evaluate(individual, Objectives);
|
---|
[13421] | 140 | }
|
---|
[13448] | 141 |
|
---|
| 142 | public override double[] Evaluate(Individual individual, IRandom random) {
|
---|
[14073] | 143 | return Evaluate(individual.RealVector());
|
---|
[13421] | 144 | }
|
---|
| 145 |
|
---|
[13620] | 146 | public void Load(MOTFData data) {
|
---|
[14065] | 147 | TestFunction = data.TestFunction;
|
---|
[13620] | 148 | }
|
---|
[13448] | 149 |
|
---|
[14073] | 150 | #region Events
|
---|
| 151 | private void UpdateParameterValues() {
|
---|
[16171] | 152 | Parameters.Remove(MaximizationParameterName);
|
---|
| 153 | Parameters.Add(new FixedValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(TestFunction.Maximization(Objectives)).AsReadOnly()));
|
---|
[14073] | 154 |
|
---|
[16171] | 155 | Parameters.Remove(BestKnownFrontParameterName);
|
---|
[14085] | 156 | var front = TestFunction.OptimalParetoFront(Objectives);
|
---|
[16171] | 157 | var bkf = front != null ? (DoubleMatrix)Utilities.ToMatrix(front).AsReadOnly() : null;
|
---|
| 158 | Parameters.Add(new FixedValueParameter<DoubleMatrix>(BestKnownFrontParameterName, "A double matrix representing the best known qualites for this problem (aka points on the Pareto front). Points are to be given in a row-wise fashion.", bkf));
|
---|
[14085] | 159 |
|
---|
[16171] | 160 | Parameters.Remove(ReferencePointParameterName);
|
---|
| 161 | Parameters.Add(new FixedValueParameter<DoubleArray>(ReferencePointParameterName, "The refrence point for hypervolume calculations on this problem", new DoubleArray(TestFunction.ReferencePoint(Objectives))));
|
---|
[14085] | 162 |
|
---|
| 163 | BoundsParameter.Value = new DoubleMatrix(TestFunction.Bounds(Objectives));
|
---|
[13672] | 164 | }
|
---|
| 165 |
|
---|
[13421] | 166 | protected override void OnEncodingChanged() {
|
---|
| 167 | base.OnEncodingChanged();
|
---|
[14073] | 168 | UpdateParameterValues();
|
---|
[13672] | 169 | ParameterizeAnalyzers();
|
---|
[13421] | 170 | }
|
---|
| 171 | protected override void OnEvaluatorChanged() {
|
---|
| 172 | base.OnEvaluatorChanged();
|
---|
[14073] | 173 | UpdateParameterValues();
|
---|
[13672] | 174 | ParameterizeAnalyzers();
|
---|
[13421] | 175 | }
|
---|
[13448] | 176 |
|
---|
[13421] | 177 | private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
[14073] | 178 | ProblemSize = Math.Max(TestFunction.MinimumSolutionLength, Math.Min(ProblemSize, TestFunction.MaximumSolutionLength));
|
---|
| 179 | Objectives = Math.Max(TestFunction.MinimumObjectives, Math.Min(Objectives, TestFunction.MaximumObjectives));
|
---|
[16171] | 180 | Parameters.Remove(ReferencePointParameterName);
|
---|
| 181 | Parameters.Add(new FixedValueParameter<DoubleArray>(ReferencePointParameterName, "The refrence point for hypervolume calculations on this problem", new DoubleArray(TestFunction.ReferencePoint(Objectives))));
|
---|
[13672] | 182 | ParameterizeAnalyzers();
|
---|
[14073] | 183 | UpdateParameterValues();
|
---|
[13421] | 184 | OnReset();
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | private void ProblemSizeOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
[14073] | 188 | ProblemSize = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, ProblemSize));
|
---|
| 189 | UpdateParameterValues();
|
---|
[13421] | 190 | }
|
---|
| 191 |
|
---|
[13672] | 192 | private void ObjectivesOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
[14073] | 193 | Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives));
|
---|
| 194 | UpdateParameterValues();
|
---|
[13421] | 195 | }
|
---|
| 196 |
|
---|
| 197 | #endregion
|
---|
| 198 |
|
---|
| 199 | #region Helpers
|
---|
| 200 | private void InitializeOperators() {
|
---|
[13672] | 201 | Operators.Add(new CrowdingAnalyzer());
|
---|
| 202 | Operators.Add(new GenerationalDistanceAnalyzer());
|
---|
| 203 | Operators.Add(new InvertedGenerationalDistanceAnalyzer());
|
---|
| 204 | Operators.Add(new HypervolumeAnalyzer());
|
---|
| 205 | Operators.Add(new SpacingAnalyzer());
|
---|
| 206 | Operators.Add(new ScatterPlotAnalyzer());
|
---|
[14073] | 207 |
|
---|
[13672] | 208 | ParameterizeAnalyzers();
|
---|
[13421] | 209 | }
|
---|
| 210 |
|
---|
[14073] | 211 | private IEnumerable<IMultiObjectiveTestFunctionAnalyzer> Analyzers {
|
---|
| 212 | get { return Operators.OfType<IMultiObjectiveTestFunctionAnalyzer>(); }
|
---|
[13421] | 213 | }
|
---|
[13672] | 214 |
|
---|
| 215 | private void ParameterizeAnalyzers() {
|
---|
| 216 | foreach (var analyzer in Analyzers) {
|
---|
| 217 | analyzer.ResultsParameter.ActualName = "Results";
|
---|
| 218 | analyzer.QualitiesParameter.ActualName = Evaluator.QualitiesParameter.ActualName;
|
---|
[13725] | 219 | analyzer.TestFunctionParameter.ActualName = TestFunctionParameter.Name;
|
---|
| 220 | analyzer.BestKnownFrontParameter.ActualName = BestKnownFrontParameter.Name;
|
---|
| 221 |
|
---|
[14044] | 222 | var scatterPlotAnalyzer = analyzer as ScatterPlotAnalyzer;
|
---|
| 223 | if (scatterPlotAnalyzer != null) {
|
---|
| 224 | scatterPlotAnalyzer.IndividualsParameter.ActualName = Encoding.Name;
|
---|
| 225 | }
|
---|
[13672] | 226 | }
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[13421] | 229 | #endregion
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 |
|
---|