Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveBasicProblem.cs @ 11975

Last change on this file since 11975 was 11970, checked in by abeham, 10 years ago

#2174:

  • Added ISingleObjectiveOperator and IMultiObjectiveOperator interfaces
    • Added interface to all operators that would fall in either of these categories (excluding MainLoop operators)
  • Added some checks in BasicProblem on whether the MultiEncoding is being used
    • A new event handler is added that reacts on encodings being removed or added to the MultiEncoding
  • Added code to remove the non-fit operators in (Single|Multi)ObjectiveBasicProblem
File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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>, IMultiObjectiveHeuristicOptimizationProblem, IMultiObjectiveProblemDefinition
32  where TEncoding : class, IEncoding {
33    [StorableConstructor]
34    protected MultiObjectiveBasicProblem(bool deserializing) : base(deserializing) { }
35
36    protected MultiObjectiveBasicProblem(MultiObjectiveBasicProblem<TEncoding> original, Cloner cloner)
37      : base(original, cloner) {
38      ParameterizeOperators();
39    }
40
41    protected MultiObjectiveBasicProblem()
42      : base() {
43      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", new BoolArray()));
44
45      Operators.Add(Evaluator);
46      Operators.Add(new MultiObjectiveAnalyzer());
47
48      ParameterizeOperators();
49    }
50
51    [StorableHook(HookType.AfterDeserialization)]
52    private void AfterDeserialization() {
53      ParameterizeOperators();
54    }
55
56    public abstract bool[] Maximization { get; }
57    public abstract double[] Evaluate(Individual individual, IRandom random);
58    public virtual void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { }
59
60    protected override void OnEncodingChanged() {
61      base.OnEncodingChanged();
62      Parameters["Maximization"].ActualValue = new BoolArray(Maximization);
63    }
64
65    protected override void OnOperatorsChanged() {
66      base.OnOperatorsChanged();
67      if (Encoding != null) {
68        PruneSingleObjectiveOperators(Encoding);
69        var multiEncoding = Encoding as MultiEncoding;
70        if (multiEncoding != null) {
71          foreach (var encoding in multiEncoding.Encodings.ToList()) {
72            PruneSingleObjectiveOperators(encoding);
73          }
74        }
75      }
76    }
77
78    private void PruneSingleObjectiveOperators(IEncoding encoding) {
79      if (encoding != null && encoding.Operators.Any(x => x is ISingleObjectiveOperator && !(x is IMultiObjectiveOperator)))
80        encoding.Operators = encoding.Operators.Where(x => !(x is ISingleObjectiveOperator) || x is IMultiObjectiveOperator).ToList();
81    }
82
83    protected override void OnEvaluatorChanged() {
84      base.OnEvaluatorChanged();
85      ParameterizeOperators();
86    }
87
88    private void ParameterizeOperators() {
89      foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator>())
90        op.EvaluateFunc = Evaluate;
91      foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator>())
92        op.AnalyzeAction = Analyze;
93    }
94
95
96    #region IMultiObjectiveHeuristicOptimizationProblem Members
97    IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
98      get { return Parameters["Maximization"]; }
99    }
100    IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
101      get { return Evaluator; }
102    }
103    #endregion
104  }
105}
Note: See TracBrowser for help on using the repository browser.