1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Threading;
|
---|
26 | using HEAL.Attic;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Optimization {
|
---|
33 | [StorableType("6F2EC371-0309-4848-B7B1-C9B9C7E3436F")]
|
---|
34 | public abstract class MultiObjectiveProblem<TEncoding, TEncodedSolution> :
|
---|
35 | Problem<TEncoding, TEncodedSolution, MultiObjectiveEvaluator<TEncodedSolution>>,
|
---|
36 | IMultiObjectiveProblem<TEncoding, TEncodedSolution>,
|
---|
37 | IMultiObjectiveProblemDefinition<TEncoding, TEncodedSolution>
|
---|
38 | where TEncoding : class, IEncoding
|
---|
39 | where TEncodedSolution : class, IEncodedSolution {
|
---|
40 |
|
---|
41 | #region Parameter properties
|
---|
42 | [Storable] public IValueParameter<BoolArray> MaximizationParameter { get; }
|
---|
43 | [Storable] public IValueParameter<DoubleMatrix> BestKnownFrontParameter { get; }
|
---|
44 | [Storable] public IValueParameter<DoubleArray> ReferencePointParameter { get; }
|
---|
45 | #endregion
|
---|
46 |
|
---|
47 |
|
---|
48 | [StorableConstructor]
|
---|
49 | protected MultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
|
---|
50 |
|
---|
51 | protected MultiObjectiveProblem(MultiObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
|
---|
52 | : base(original, cloner) {
|
---|
53 | MaximizationParameter = cloner.Clone(original.MaximizationParameter);
|
---|
54 | BestKnownFrontParameter = cloner.Clone(original.BestKnownFrontParameter);
|
---|
55 | ReferencePointParameter = cloner.Clone(original.ReferencePointParameter);
|
---|
56 | ParameterizeOperators();
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected MultiObjectiveProblem(TEncoding encoding) : base(encoding) {
|
---|
60 | Parameters.Add(MaximizationParameter = new ValueParameter<BoolArray>("Maximization", "The dimensions correspond to the different objectives: False if the objective should be minimized, true if it should be maximized..", new BoolArray(new bool[] { }, @readonly: true)));
|
---|
61 | Parameters.Add(BestKnownFrontParameter = new OptionalValueParameter<DoubleMatrix>("Best Known Front", "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."));
|
---|
62 | Parameters.Add(ReferencePointParameter = new OptionalValueParameter<DoubleArray>("Reference Point", "The reference point for hypervolume calculations on this problem"));
|
---|
63 | Operators.Add(Evaluator);
|
---|
64 | Operators.Add(new MultiObjectiveAnalyzer<TEncodedSolution>());
|
---|
65 | ParameterizeOperators();
|
---|
66 | }
|
---|
67 |
|
---|
68 | [StorableHook(HookType.AfterDeserialization)]
|
---|
69 | private void AfterDeserialization() {
|
---|
70 | ParameterizeOperators();
|
---|
71 | }
|
---|
72 |
|
---|
73 | public int Objectives {
|
---|
74 | get { return Maximization.Length; }
|
---|
75 | }
|
---|
76 | public bool[] Maximization {
|
---|
77 | get { return MaximizationParameter.Value.CloneAsArray(); }
|
---|
78 | protected set {
|
---|
79 | if (MaximizationParameter.Value.SequenceEqual(value)) return;
|
---|
80 | MaximizationParameter.Value = new BoolArray(value, @readonly: true);
|
---|
81 | OnMaximizationChanged();
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public virtual IReadOnlyList<double[]> BestKnownFront {
|
---|
86 | get {
|
---|
87 | var mat = BestKnownFrontParameter.Value;
|
---|
88 | if (mat == null) return null;
|
---|
89 | return mat.CloneByRows().ToList();
|
---|
90 | }
|
---|
91 | }
|
---|
92 | public virtual void SetBestKnownFront(IList<double[]> front) {
|
---|
93 | if (front == null || front.Count == 0) {
|
---|
94 | BestKnownFrontParameter.Value = null;
|
---|
95 | return;
|
---|
96 | }
|
---|
97 | BestKnownFrontParameter.Value = DoubleMatrix.FromRows(front);
|
---|
98 | }
|
---|
99 | public virtual double[] ReferencePoint {
|
---|
100 | get { return ReferencePointParameter.Value?.CloneAsArray(); }
|
---|
101 | set { ReferencePointParameter.Value = new DoubleArray(value); }
|
---|
102 | }
|
---|
103 |
|
---|
104 | public virtual double[] Evaluate(TEncodedSolution solution, IRandom random) {
|
---|
105 | return Evaluate(solution, random, CancellationToken.None);
|
---|
106 | }
|
---|
107 |
|
---|
108 | public abstract double[] Evaluate(TEncodedSolution solution, IRandom random, CancellationToken cancellationToken);
|
---|
109 | public virtual void Analyze(TEncodedSolution[] solutions, double[][] qualities, ResultCollection results, IRandom random) { }
|
---|
110 |
|
---|
111 |
|
---|
112 | protected override void OnOperatorsChanged() {
|
---|
113 | if (Encoding != null) {
|
---|
114 | PruneSingleObjectiveOperators(Encoding);
|
---|
115 | var combinedEncoding = Encoding as CombinedEncoding;
|
---|
116 | if (combinedEncoding != null) {
|
---|
117 | foreach (var encoding in combinedEncoding.Encodings.ToList()) {
|
---|
118 | PruneSingleObjectiveOperators(encoding);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 | base.OnOperatorsChanged();
|
---|
123 | }
|
---|
124 |
|
---|
125 | private void PruneSingleObjectiveOperators(IEncoding encoding) {
|
---|
126 | if (encoding != null && encoding.Operators.Any(x => x is ISingleObjectiveOperator && !(x is IMultiObjectiveOperator)))
|
---|
127 | encoding.Operators = encoding.Operators.Where(x => !(x is ISingleObjectiveOperator) || x is IMultiObjectiveOperator).ToList();
|
---|
128 |
|
---|
129 | foreach (var multiOp in Encoding.Operators.OfType<IMultiOperator>()) {
|
---|
130 | foreach (var soOp in multiOp.Operators.Where(x => x is ISingleObjectiveOperator).ToList()) {
|
---|
131 | multiOp.RemoveOperator(soOp);
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | protected override void OnEvaluatorChanged() {
|
---|
137 | base.OnEvaluatorChanged();
|
---|
138 | ParameterizeOperators();
|
---|
139 | }
|
---|
140 |
|
---|
141 | protected override void ParameterizeOperators() {
|
---|
142 | base.ParameterizeOperators();
|
---|
143 | Parameterize();
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void Parameterize() {
|
---|
147 | foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator<TEncodedSolution>>())
|
---|
148 | op.EvaluateFunc = Evaluate;
|
---|
149 | foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator<TEncodedSolution>>())
|
---|
150 | op.AnalyzeAction = Analyze;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | #region IMultiObjectiveHeuristicOptimizationProblem Members
|
---|
155 | IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
156 | get { return MaximizationParameter; }
|
---|
157 | }
|
---|
158 | IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
159 | get { return Evaluator; }
|
---|
160 | }
|
---|
161 | #endregion
|
---|
162 |
|
---|
163 | public event EventHandler MaximizationChanged;
|
---|
164 | protected void OnMaximizationChanged() {
|
---|
165 | MaximizationChanged?.Invoke(this, EventArgs.Empty);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | } |
---|