Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17610 was 17610, checked in by mkommend, 4 years ago

#2521: Removed unnecessary default ctors from problem base classes.

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