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

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

#2521: Added StorableType attributes to interfaces and adapted basic problems.

File size: 4.9 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    protected IValueParameter<BoolArray> MaximizationParameter {
39      get { return (IValueParameter<BoolArray>)Parameters["Maximization"]; }
40    }
41
42    [StorableConstructor]
43    protected MultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
44
45    protected MultiObjectiveProblem(MultiObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
46      : base(original, cloner) {
47      ParameterizeOperators();
48    }
49
50    protected MultiObjectiveProblem() : base() {
51      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
52
53      Operators.Add(Evaluator);
54      Operators.Add(new MultiObjectiveAnalyzer<TEncodedSolution>());
55
56      ParameterizeOperators();
57    }
58
59    protected MultiObjectiveProblem(TEncoding encoding) : base(encoding) {
60      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
61
62      Operators.Add(Evaluator);
63      Operators.Add(new MultiObjectiveAnalyzer<TEncodedSolution>());
64
65      ParameterizeOperators();
66    }
67
68    [StorableHook(HookType.AfterDeserialization)]
69    private void AfterDeserialization() {
70      ParameterizeOperators();
71    }
72
73    public int Objectives => Maximization.Length;
74    public abstract bool[] Maximization { get; }
75    public abstract double[] Evaluate(TEncodedSolution solution, IRandom random);
76    public virtual void Analyze(TEncodedSolution[] solutions, double[][] qualities, ResultCollection results, IRandom random) { }
77
78    protected override void OnOperatorsChanged() {
79      if (Encoding != null) {
80        PruneSingleObjectiveOperators(Encoding);
81        var combinedEncoding = Encoding as CombinedEncoding;
82        if (combinedEncoding != null) {
83          foreach (var encoding in combinedEncoding.Encodings.ToList()) {
84            PruneSingleObjectiveOperators(encoding);
85          }
86        }
87      }
88      base.OnOperatorsChanged();
89    }
90
91    private void PruneSingleObjectiveOperators(IEncoding encoding) {
92      if (encoding != null && encoding.Operators.Any(x => x is ISingleObjectiveOperator && !(x is IMultiObjectiveOperator)))
93        encoding.Operators = encoding.Operators.Where(x => !(x is ISingleObjectiveOperator) || x is IMultiObjectiveOperator).ToList();
94
95      foreach (var multiOp in Encoding.Operators.OfType<IMultiOperator>()) {
96        foreach (var soOp in multiOp.Operators.Where(x => x is ISingleObjectiveOperator).ToList()) {
97          multiOp.RemoveOperator(soOp);
98        }
99      }
100    }
101
102    protected override void OnEvaluatorChanged() {
103      base.OnEvaluatorChanged();
104      ParameterizeOperators();
105    }
106
107    private void ParameterizeOperators() {
108      foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator<TEncodedSolution>>())
109        op.EvaluateFunc = Evaluate;
110      foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator<TEncodedSolution>>())
111        op.AnalyzeAction = Analyze;
112    }
113
114
115    #region IMultiObjectiveHeuristicOptimizationProblem Members
116    IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
117      get { return Parameters["Maximization"]; }
118    }
119    IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
120      get { return Evaluator; }
121    }
122    #endregion
123  }
124}
Note: See TracBrowser for help on using the repository browser.