source: branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveProblem.cs @ 13337

Last change on this file since 13337 was 13336, checked in by mkommend, 7 years ago

#2521: Refactored encodings and problems.

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