source: trunk/sources/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveBasicProblem.cs @ 15080

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

#2783: Moved pareto front calculation from MultiObjectiveBasicProblem to a new class DominationCalculator<T>. Changed FastNonDominatedSort to use the new class.

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Optimization {
32  [StorableClass]
33  public abstract class MultiObjectiveBasicProblem<TEncoding> : BasicProblem<TEncoding, MultiObjectiveEvaluator>, IMultiObjectiveHeuristicOptimizationProblem, IMultiObjectiveProblemDefinition
34  where TEncoding : class, IEncoding {
35    [StorableConstructor]
36    protected MultiObjectiveBasicProblem(bool deserializing) : base(deserializing) { }
37
38    protected MultiObjectiveBasicProblem(MultiObjectiveBasicProblem<TEncoding> original, Cloner cloner)
39      : base(original, cloner) {
40      ParameterizeOperators();
41    }
42
43    protected MultiObjectiveBasicProblem()
44      : base() {
45      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
46
47      Operators.Add(Evaluator);
48      Operators.Add(new MultiObjectiveAnalyzer());
49
50      ParameterizeOperators();
51    }
52
53    [StorableHook(HookType.AfterDeserialization)]
54    private void AfterDeserialization() {
55      ParameterizeOperators();
56    }
57
58    public abstract bool[] Maximization { get; }
59    public abstract double[] Evaluate(Individual individual, IRandom random);
60    public virtual void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { }
61   
62    protected override void OnOperatorsChanged() {
63      base.OnOperatorsChanged();
64      if (Encoding != null) {
65        PruneSingleObjectiveOperators(Encoding);
66        var multiEncoding = Encoding as MultiEncoding;
67        if (multiEncoding != null) {
68          foreach (var encoding in multiEncoding.Encodings.ToList()) {
69            PruneSingleObjectiveOperators(encoding);
70          }
71        }
72      }
73    }
74
75    private void PruneSingleObjectiveOperators(IEncoding encoding) {
76      if (encoding != null && encoding.Operators.Any(x => x is ISingleObjectiveOperator && !(x is IMultiObjectiveOperator)))
77        encoding.Operators = encoding.Operators.Where(x => !(x is ISingleObjectiveOperator) || x is IMultiObjectiveOperator).ToList();
78    }
79
80    protected override void OnEvaluatorChanged() {
81      base.OnEvaluatorChanged();
82      ParameterizeOperators();
83    }
84
85    private void ParameterizeOperators() {
86      foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator>())
87        op.EvaluateFunc = Evaluate;
88      foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator>())
89        op.AnalyzeAction = Analyze;
90    }
91
92
93    #region IMultiObjectiveHeuristicOptimizationProblem Members
94    IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
95      get { return Parameters["Maximization"]; }
96    }
97    IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
98      get { return Evaluator; }
99    }
100    #endregion
101  }
102}
Note: See TracBrowser for help on using the repository browser.