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

Last change on this file since 16801 was 16801, checked in by mkommend, 3 years ago

#2521: Merged trunk changes and adapted programmable problem templates for combined solutions.

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28
29namespace HeuristicLab.Optimization {
30  [StorableType("6F2EC371-0309-4848-B7B1-C9B9C7E3436F")]
31  public abstract class MultiObjectiveProblem<TEncoding, TEncodedSolution> :
32    Problem<TEncoding, TEncodedSolution, MultiObjectiveEvaluator<TEncodedSolution>>,
33    IMultiObjectiveProblem<TEncoding, TEncodedSolution>,
34    IMultiObjectiveProblemDefinition<TEncoding, TEncodedSolution>
35    where TEncoding : class, IEncoding<TEncodedSolution>
36    where TEncodedSolution : class, IEncodedSolution {
37
38    [StorableConstructor]
39    protected MultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
40
41    protected MultiObjectiveProblem(MultiObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
42      : base(original, cloner) {
43      ParameterizeOperators();
44    }
45
46    protected MultiObjectiveProblem()
47      : base() {
48      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
49
50      Operators.Add(Evaluator);
51      Operators.Add(new MultiObjectiveAnalyzer<TEncodedSolution>());
52
53      ParameterizeOperators();
54    }
55
56    [StorableHook(HookType.AfterDeserialization)]
57    private void AfterDeserialization() {
58      ParameterizeOperators();
59    }
60
61    public abstract bool[] Maximization { get; }
62    public abstract double[] Evaluate(TEncodedSolution individual, IRandom random);
63    public virtual void Analyze(TEncodedSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random) { }
64
65    protected override void OnOperatorsChanged() {
66      if (Encoding != null) {
67        PruneSingleObjectiveOperators(Encoding);
68        var combinedEncoding = Encoding as CombinedEncoding;
69        if (combinedEncoding != null) {
70          foreach (var encoding in combinedEncoding.Encodings.ToList()) {
71            PruneSingleObjectiveOperators(encoding);
72          }
73        }
74      }
75      base.OnOperatorsChanged();
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      foreach (var multiOp in Encoding.Operators.OfType<IMultiOperator>()) {
83        foreach (var soOp in multiOp.Operators.Where(x => x is ISingleObjectiveOperator).ToList()) {
84          multiOp.RemoveOperator(soOp);
85        }
86      }
87    }
88
89    protected override void OnEvaluatorChanged() {
90      base.OnEvaluatorChanged();
91      ParameterizeOperators();
92    }
93
94    private void ParameterizeOperators() {
95      foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator<TEncodedSolution>>())
96        op.EvaluateFunc = Evaluate;
97      foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator<TEncodedSolution>>())
98        op.AnalyzeAction = Analyze;
99    }
100
101
102    #region IMultiObjectiveHeuristicOptimizationProblem Members
103    IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
104      get { return Parameters["Maximization"]; }
105    }
106    IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
107      get { return Evaluator; }
108    }
109    #endregion
110  }
111}
Note: See TracBrowser for help on using the repository browser.