Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Made encodings non-generic classes (the TEncodedSolution type parameter is not actually used), this will make it considerably easier to port the VRP to the new architecture

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