Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17226 was 17226, checked in by mkommend, 5 years ago

#2521: Merged trunk changes into problem refactoring branch.

File size: 7.6 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 MaximizationParameterName = "Maximization";
40    public const string BestKnownFrontParameterName = "BestKnownFront";
41    public const string ReferencePointParameterName = "ReferencePoint";
42    #endregion
43
44    #region Parameterproperties
45    public IValueParameter<BoolArray> MaximizationParameter {
46      get { return (IValueParameter<BoolArray>)Parameters[MaximizationParameterName]; }
47    }
48    public IValueParameter<DoubleMatrix> BestKnownFrontParameter {
49      get { return (IValueParameter<DoubleMatrix>)Parameters[BestKnownFrontParameterName]; }
50    }
51    public IValueParameter<DoubleArray> ReferencePointParameter {
52      get { return (IValueParameter<DoubleArray>)Parameters[ReferencePointParameterName]; }
53    }
54    #endregion
55
56
57    [StorableConstructor]
58    protected MultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
59
60    protected MultiObjectiveProblem(MultiObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
61      : base(original, cloner) {
62      ParameterizeOperators();
63    }
64
65    protected MultiObjectiveProblem() : base() {
66      Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
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      Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
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    public abstract bool[] Maximization { get; }
92
93    public virtual IReadOnlyList<double[]> BestKnownFront {
94      get {
95        if (!Parameters.ContainsKey(BestKnownFrontParameterName)) return null;
96        var mat = BestKnownFrontParameter.Value;
97        if (mat == null) return null;
98        var v = new double[mat.Rows][];
99        for (var i = 0; i < mat.Rows; i++) {
100          var r = v[i] = new double[mat.Columns];
101          for (var j = 0; j < mat.Columns; j++) {
102            r[j] = mat[i, j];
103          }
104        }
105        return v;
106      }
107      set {
108        if (value == null || value.Count == 0) {
109          BestKnownFrontParameter.Value = new DoubleMatrix();
110          return;
111        }
112        var mat = new DoubleMatrix(value.Count, value[0].Length);
113        for (int i = 0; i < value.Count; i++) {
114          for (int j = 0; j < value[i].Length; j++) {
115            mat[i, j] = value[i][j];
116          }
117        }
118
119        BestKnownFrontParameter.Value = mat;
120      }
121    }
122    public virtual double[] ReferencePoint {
123      get { return ReferencePointParameter.Value != null ? ReferencePointParameter.Value.CloneAsArray() : null; }
124      set { ReferencePointParameter.Value = new DoubleArray(value); }
125    }
126
127    public abstract double[] Evaluate(TEncodedSolution solution, IRandom random);
128    public virtual void Analyze(TEncodedSolution[] solutions, double[][] qualities, ResultCollection results, IRandom random) { }
129
130
131    protected override void OnOperatorsChanged() {
132      if (Encoding != null) {
133        PruneSingleObjectiveOperators(Encoding);
134        var combinedEncoding = Encoding as CombinedEncoding;
135        if (combinedEncoding != null) {
136          foreach (var encoding in combinedEncoding.Encodings.ToList()) {
137            PruneSingleObjectiveOperators(encoding);
138          }
139        }
140      }
141      base.OnOperatorsChanged();
142    }
143
144    private void PruneSingleObjectiveOperators(IEncoding encoding) {
145      if (encoding != null && encoding.Operators.Any(x => x is ISingleObjectiveOperator && !(x is IMultiObjectiveOperator)))
146        encoding.Operators = encoding.Operators.Where(x => !(x is ISingleObjectiveOperator) || x is IMultiObjectiveOperator).ToList();
147
148      foreach (var multiOp in Encoding.Operators.OfType<IMultiOperator>()) {
149        foreach (var soOp in multiOp.Operators.Where(x => x is ISingleObjectiveOperator).ToList()) {
150          multiOp.RemoveOperator(soOp);
151        }
152      }
153    }
154
155    protected override void OnEvaluatorChanged() {
156      base.OnEvaluatorChanged();
157      ParameterizeOperators();
158    }
159
160    private void ParameterizeOperators() {
161      foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator<TEncodedSolution>>())
162        op.EvaluateFunc = Evaluate;
163      foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator<TEncodedSolution>>())
164        op.AnalyzeAction = Analyze;
165    }
166
167
168    #region IMultiObjectiveHeuristicOptimizationProblem Members
169    IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
170      get { return Parameters[MaximizationParameterName]; }
171    }
172    IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
173      get { return Evaluator; }
174    }
175    #endregion
176  }
177}
Note: See TracBrowser for help on using the repository browser.