Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveProblem.cs @ 17317

Last change on this file since 17317 was 17317, checked in by abeham, 4 years ago

#2521: refactored multi-objective problems' maximization

  • Add ForceValue method to IValueParameter to perform changes even when it is read-only
  • Add MaximizationChanged event handler
File size: 8.2 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 HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30
31namespace HeuristicLab.Optimization {
32  [StorableType("6F2EC371-0309-4848-B7B1-C9B9C7E3436F")]
33  public abstract class MultiObjectiveProblem<TEncoding, TEncodedSolution> :
34    Problem<TEncoding, TEncodedSolution, MultiObjectiveEvaluator<TEncodedSolution>>,
35    IMultiObjectiveProblem<TEncoding, TEncodedSolution>,
36    IMultiObjectiveProblemDefinition<TEncoding, TEncodedSolution>
37    where TEncoding : class, IEncoding<TEncodedSolution>
38    where TEncodedSolution : class, IEncodedSolution {
39    #region Parameternames
40    public const string BestKnownFrontParameterName = "BestKnownFront";
41    public const string ReferencePointParameterName = "ReferencePoint";
42    #endregion
43
44    #region Parameterproperties
45    [Storable] public IValueParameter<BoolArray> MaximizationParameter { get; }
46    public IValueParameter<DoubleMatrix> BestKnownFrontParameter {
47      get { return (IValueParameter<DoubleMatrix>)Parameters[BestKnownFrontParameterName]; }
48    }
49    public IValueParameter<DoubleArray> ReferencePointParameter {
50      get { return (IValueParameter<DoubleArray>)Parameters[ReferencePointParameterName]; }
51    }
52    #endregion
53
54
55    [StorableConstructor]
56    protected MultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
57
58    protected MultiObjectiveProblem(MultiObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
59      : base(original, cloner) {
60      MaximizationParameter = cloner.Clone(original.MaximizationParameter);
61      ParameterizeOperators();
62    }
63
64    protected MultiObjectiveProblem() : base() {
65      MaximizationParameter = new ValueParameter<BoolArray>("Maximization", "The dimensions correspond to the different objectives: False if the objective should be minimized, true if it should be maximized..", new BoolArray(new bool[] { }, @readonly: true));
66      Parameters.Add(MaximizationParameter);
67      Parameters.Add(new OptionalValueParameter<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."));
68      Parameters.Add(new OptionalValueParameter<DoubleArray>(ReferencePointParameterName, "The refrence point for hypervolume calculations on this problem"));
69      Operators.Add(Evaluator);
70      Operators.Add(new MultiObjectiveAnalyzer<TEncodedSolution>());
71      ParameterizeOperators();
72    }
73
74    protected MultiObjectiveProblem(TEncoding encoding) : base(encoding) {
75      MaximizationParameter = new ValueParameter<BoolArray>("Maximization", "The dimensions correspond to the different objectives: False if the objective should be minimized, true if it should be maximized..", new BoolArray(new bool[] { }, @readonly: true));
76      Parameters.Add(MaximizationParameter);
77      Parameters.Add(new OptionalValueParameter<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."));
78      Parameters.Add(new OptionalValueParameter<DoubleArray>(ReferencePointParameterName, "The refrence point for hypervolume calculations on this problem"));
79      Operators.Add(Evaluator);
80      Operators.Add(new MultiObjectiveAnalyzer<TEncodedSolution>());
81      ParameterizeOperators();
82    }
83
84    [StorableHook(HookType.AfterDeserialization)]
85    private void AfterDeserialization() {
86      ParameterizeOperators();
87    }
88
89    public int Objectives {
90      get { return Maximization.Length; }
91    }
92    public bool[] Maximization {
93      get { return MaximizationParameter.Value.CloneAsArray(); }
94      protected set {
95        if (MaximizationParameter.Value.SequenceEqual(value)) return;
96        MaximizationParameter.ForceValue(new BoolArray(value, @readonly: true));
97        OnMaximizationChanged();
98      }
99    }
100
101    public virtual IReadOnlyList<double[]> BestKnownFront {
102      get {
103        if (!Parameters.ContainsKey(BestKnownFrontParameterName)) return null;
104        var mat = BestKnownFrontParameter.Value;
105        if (mat == null) return null;
106        var v = new double[mat.Rows][];
107        for (var i = 0; i < mat.Rows; i++) {
108          var r = v[i] = new double[mat.Columns];
109          for (var j = 0; j < mat.Columns; j++) {
110            r[j] = mat[i, j];
111          }
112        }
113        return v;
114      }
115      set {
116        if (value == null || value.Count == 0) {
117          BestKnownFrontParameter.Value = new DoubleMatrix();
118          return;
119        }
120        var mat = new DoubleMatrix(value.Count, value[0].Length);
121        for (int i = 0; i < value.Count; i++) {
122          for (int j = 0; j < value[i].Length; j++) {
123            mat[i, j] = value[i][j];
124          }
125        }
126
127        BestKnownFrontParameter.Value = mat;
128      }
129    }
130    public virtual double[] ReferencePoint {
131      get { return ReferencePointParameter.Value != null ? ReferencePointParameter.Value.CloneAsArray() : null; }
132      set { ReferencePointParameter.Value = new DoubleArray(value); }
133    }
134
135    public abstract double[] Evaluate(TEncodedSolution solution, IRandom random);
136    public virtual void Analyze(TEncodedSolution[] solutions, double[][] qualities, ResultCollection results, IRandom random) { }
137
138
139    protected override void OnOperatorsChanged() {
140      if (Encoding != null) {
141        PruneSingleObjectiveOperators(Encoding);
142        var combinedEncoding = Encoding as CombinedEncoding;
143        if (combinedEncoding != null) {
144          foreach (var encoding in combinedEncoding.Encodings.ToList()) {
145            PruneSingleObjectiveOperators(encoding);
146          }
147        }
148      }
149      base.OnOperatorsChanged();
150    }
151
152    private void PruneSingleObjectiveOperators(IEncoding encoding) {
153      if (encoding != null && encoding.Operators.Any(x => x is ISingleObjectiveOperator && !(x is IMultiObjectiveOperator)))
154        encoding.Operators = encoding.Operators.Where(x => !(x is ISingleObjectiveOperator) || x is IMultiObjectiveOperator).ToList();
155
156      foreach (var multiOp in Encoding.Operators.OfType<IMultiOperator>()) {
157        foreach (var soOp in multiOp.Operators.Where(x => x is ISingleObjectiveOperator).ToList()) {
158          multiOp.RemoveOperator(soOp);
159        }
160      }
161    }
162
163    protected override void OnEvaluatorChanged() {
164      base.OnEvaluatorChanged();
165      ParameterizeOperators();
166    }
167
168    private void ParameterizeOperators() {
169      foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator<TEncodedSolution>>())
170        op.EvaluateFunc = Evaluate;
171      foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator<TEncodedSolution>>())
172        op.AnalyzeAction = Analyze;
173    }
174
175
176    #region IMultiObjectiveHeuristicOptimizationProblem Members
177    IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
178      get { return MaximizationParameter; }
179    }
180    IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
181      get { return Evaluator; }
182    }
183    #endregion
184
185    public event EventHandler MaximizationChanged;
186    protected void OnMaximizationChanged() {
187      MaximizationChanged?.Invoke(this, EventArgs.Empty);
188    }
189  }
190}
Note: See TracBrowser for help on using the repository browser.