Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2943_MOBasicProblem_MOCMAES/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveBasicProblem.cs

Last change on this file was 16310, checked in by bwerth, 6 years ago

#2943 worked on MOBasicProblem and MOAnalyzers

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Optimization {
30  [StorableClass]
31  public abstract class MultiObjectiveBasicProblem<TEncoding> : BasicProblem<TEncoding, MultiObjectiveEvaluator>, IMultiObjectiveBasicProblem
32  where TEncoding : class, IEncoding {
33
34    #region Parameternames
35    public const string MaximizationParameterName = "Maximization";
36    public const string BestKnownFrontParameterName = "BestKnownFront";
37    public const string ReferencePointParameterName = "ReferencePoint";
38    #endregion
39
40    #region Parameterproperties
41    public IValueParameter<BoolArray> MaximizationParameter {
42      get { return (IValueParameter<BoolArray>) Parameters[MaximizationParameterName]; }
43    }
44    public IValueParameter<DoubleMatrix> BestKnownFrontParameter {
45      get { return (IValueParameter<DoubleMatrix>)Parameters[BestKnownFrontParameterName]; }
46    }
47    public IValueParameter<DoubleArray> ReferencePointParameter {
48      get { return (IValueParameter<DoubleArray>)Parameters[ReferencePointParameterName]; }
49    }
50    #endregion
51
52    #region Properties
53
54    public abstract bool[] Maximization { get; }
55
56    public DoubleMatrix BestKnownFront {
57      get { return Parameters.ContainsKey(BestKnownFrontParameterName) ? BestKnownFrontParameter.Value : null; }
58      set { BestKnownFrontParameter.Value = value; }
59    }
60    public DoubleArray ReferencePoint {
61      get { return Parameters.ContainsKey(ReferencePointParameterName) ? ReferencePointParameter.Value : null; }
62      set { ReferencePointParameter.Value = value; }
63    }
64    #endregion
65
66    [StorableConstructor]
67    protected MultiObjectiveBasicProblem(bool deserializing) : base(deserializing) { }
68
69    protected MultiObjectiveBasicProblem(MultiObjectiveBasicProblem<TEncoding> original, Cloner cloner)
70      : base(original, cloner) {
71      ParameterizeOperators();
72    }
73
74    protected MultiObjectiveBasicProblem()
75      : base() {
76      Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
77      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."));
78      Parameters.Add(new OptionalValueParameter<DoubleArray>(ReferencePointParameterName, "The refrence point for hypervolume calculations on this problem"));
79      Operators.Add(Evaluator);
80      Operators.Add(new MultiObjectiveAnalyzer());
81
82      ParameterizeOperators();
83    }
84
85    [StorableHook(HookType.AfterDeserialization)]
86    private void AfterDeserialization() {
87      ParameterizeOperators();
88    }
89
90   
91    public abstract double[] Evaluate(Individual individual, IRandom random);
92    public virtual void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { }
93   
94    protected override void OnOperatorsChanged() {
95      base.OnOperatorsChanged();
96      if (Encoding != null) {
97        PruneSingleObjectiveOperators(Encoding);
98        var multiEncoding = Encoding as MultiEncoding;
99        if (multiEncoding != null) {
100          foreach (var encoding in multiEncoding.Encodings.ToList()) {
101            PruneSingleObjectiveOperators(encoding);
102          }
103        }
104      }
105    }
106
107    private void PruneSingleObjectiveOperators(IEncoding encoding) {
108      if (encoding != null && encoding.Operators.Any(x => x is ISingleObjectiveOperator && !(x is IMultiObjectiveOperator)))
109        encoding.Operators = encoding.Operators.Where(x => !(x is ISingleObjectiveOperator) || x is IMultiObjectiveOperator).ToList();
110
111      foreach (var multiOp in Encoding.Operators.OfType<IMultiOperator>()) {
112        foreach (var soOp in multiOp.Operators.Where(x => x is ISingleObjectiveOperator).ToList()) {
113          multiOp.RemoveOperator(soOp);
114        }
115      }
116    }
117
118    protected override void OnEvaluatorChanged() {
119      base.OnEvaluatorChanged();
120      ParameterizeOperators();
121    }
122
123    private void ParameterizeOperators() {
124      foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator>())
125        op.EvaluateFunc = Evaluate;
126      foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator>())
127        op.AnalyzeAction = Analyze;
128    }
129
130    #region IMultiObjectiveHeuristicOptimizationProblem Members
131    IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
132      get { return Parameters[MaximizationParameterName]; }
133    }
134    IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
135      get { return Evaluator; }
136    }
137    #endregion
138  }
139}
Note: See TracBrowser for help on using the repository browser.