Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Refactored maximization property for multi-objective problems

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