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
RevLine 
[11740]1#region License Information
2/* HeuristicLab
[17226]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11740]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
[17225]22using System.Collections.Generic;
[11740]23using System.Linq;
[16751]24using HEAL.Attic;
[11740]25using HeuristicLab.Common;
26using HeuristicLab.Core;
[11780]27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
[11740]29
[11949]30namespace HeuristicLab.Optimization {
[16723]31  [StorableType("6F2EC371-0309-4848-B7B1-C9B9C7E3436F")]
[16751]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 {
[17225]38    #region Parameternames
39    public const string MaximizationParameterName = "Maximization";
40    public const string BestKnownFrontParameterName = "BestKnownFront";
41    public const string ReferencePointParameterName = "ReferencePoint";
42    #endregion
[13361]43
[17225]44    #region Parameterproperties
45    public IValueParameter<BoolArray> MaximizationParameter {
46      get { return (IValueParameter<BoolArray>)Parameters[MaximizationParameterName]; }
[16806]47    }
[17225]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
[16806]55
[17225]56
[11740]57    [StorableConstructor]
[16723]58    protected MultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
[11740]59
[16751]60    protected MultiObjectiveProblem(MultiObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
[11740]61      : base(original, cloner) {
62      ParameterizeOperators();
63    }
64
[16806]65    protected MultiObjectiveProblem() : base() {
[17225]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"));
[11753]69      Operators.Add(Evaluator);
[16751]70      Operators.Add(new MultiObjectiveAnalyzer<TEncodedSolution>());
[11740]71      ParameterizeOperators();
72    }
73
[16806]74    protected MultiObjectiveProblem(TEncoding encoding) : base(encoding) {
[17225]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"));
[16806]78      Operators.Add(Evaluator);
79      Operators.Add(new MultiObjectiveAnalyzer<TEncodedSolution>());
80      ParameterizeOperators();
81    }
82
[11740]83    [StorableHook(HookType.AfterDeserialization)]
84    private void AfterDeserialization() {
85      ParameterizeOperators();
86    }
87
[17225]88    public int Objectives {
89      get { return Maximization.Length; }
90    }
[11740]91    public abstract bool[] Maximization { get; }
[17225]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
[16806]127    public abstract double[] Evaluate(TEncodedSolution solution, IRandom random);
128    public virtual void Analyze(TEncodedSolution[] solutions, double[][] qualities, ResultCollection results, IRandom random) { }
[16751]129
[17225]130
[16692]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      }
[16801]141      base.OnOperatorsChanged();
[16692]142    }
[11740]143
[16692]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
[11740]155    protected override void OnEvaluatorChanged() {
156      base.OnEvaluatorChanged();
157      ParameterizeOperators();
158    }
159
[11786]160    private void ParameterizeOperators() {
[16751]161      foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator<TEncodedSolution>>())
[11740]162        op.EvaluateFunc = Evaluate;
[16751]163      foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator<TEncodedSolution>>())
[11740]164        op.AnalyzeAction = Analyze;
165    }
166
[11780]167
168    #region IMultiObjectiveHeuristicOptimizationProblem Members
169    IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
[17225]170      get { return Parameters[MaximizationParameterName]; }
[11780]171    }
172    IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
173      get { return Evaluator; }
174    }
175    #endregion
[11740]176  }
[17225]177}
Note: See TracBrowser for help on using the repository browser.