[11740] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17226] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11740] | 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 |
|
---|
[17317] | 22 | using System;
|
---|
[17225] | 23 | using System.Collections.Generic;
|
---|
[11740] | 24 | using System.Linq;
|
---|
[17320] | 25 | using System.Threading;
|
---|
[16751] | 26 | using HEAL.Attic;
|
---|
[11740] | 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
[11780] | 29 | using HeuristicLab.Data;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
[11740] | 31 |
|
---|
[11949] | 32 | namespace HeuristicLab.Optimization {
|
---|
[16723] | 33 | [StorableType("6F2EC371-0309-4848-B7B1-C9B9C7E3436F")]
|
---|
[16751] | 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<TEncodedSolution>
|
---|
| 39 | where TEncodedSolution : class, IEncodedSolution {
|
---|
[17610] | 40 | #region Parameter names
|
---|
[17225] | 41 | public const string BestKnownFrontParameterName = "BestKnownFront";
|
---|
| 42 | public const string ReferencePointParameterName = "ReferencePoint";
|
---|
| 43 | #endregion
|
---|
[13361] | 44 |
|
---|
[17610] | 45 | #region Parameter properties
|
---|
[17317] | 46 | [Storable] public IValueParameter<BoolArray> MaximizationParameter { get; }
|
---|
[17225] | 47 | public IValueParameter<DoubleMatrix> BestKnownFrontParameter {
|
---|
| 48 | get { return (IValueParameter<DoubleMatrix>)Parameters[BestKnownFrontParameterName]; }
|
---|
| 49 | }
|
---|
| 50 | public IValueParameter<DoubleArray> ReferencePointParameter {
|
---|
| 51 | get { return (IValueParameter<DoubleArray>)Parameters[ReferencePointParameterName]; }
|
---|
| 52 | }
|
---|
| 53 | #endregion
|
---|
[16806] | 54 |
|
---|
[17225] | 55 |
|
---|
[11740] | 56 | [StorableConstructor]
|
---|
[16723] | 57 | protected MultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[11740] | 58 |
|
---|
[16751] | 59 | protected MultiObjectiveProblem(MultiObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
|
---|
[11740] | 60 | : base(original, cloner) {
|
---|
[17317] | 61 | MaximizationParameter = cloner.Clone(original.MaximizationParameter);
|
---|
[11740] | 62 | ParameterizeOperators();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[16806] | 65 | protected MultiObjectiveProblem(TEncoding encoding) : base(encoding) {
|
---|
[17317] | 66 | 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));
|
---|
| 67 | Parameters.Add(MaximizationParameter);
|
---|
[17225] | 68 | Parameters.Add(new OptionalValueParameter<DoubleMatrix>(BestKnownFrontParameterName, "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."));
|
---|
[17610] | 69 | Parameters.Add(new OptionalValueParameter<DoubleArray>(ReferencePointParameterName, "The reference point for hypervolume calculations on this problem"));
|
---|
[16806] | 70 | Operators.Add(Evaluator);
|
---|
| 71 | Operators.Add(new MultiObjectiveAnalyzer<TEncodedSolution>());
|
---|
| 72 | ParameterizeOperators();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[11740] | 75 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 76 | private void AfterDeserialization() {
|
---|
| 77 | ParameterizeOperators();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[17225] | 80 | public int Objectives {
|
---|
| 81 | get { return Maximization.Length; }
|
---|
| 82 | }
|
---|
[17317] | 83 | public bool[] Maximization {
|
---|
| 84 | get { return MaximizationParameter.Value.CloneAsArray(); }
|
---|
| 85 | protected set {
|
---|
| 86 | if (MaximizationParameter.Value.SequenceEqual(value)) return;
|
---|
[17567] | 87 | MaximizationParameter.Value = new BoolArray(value, @readonly: true);
|
---|
[17317] | 88 | OnMaximizationChanged();
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
[17225] | 91 |
|
---|
| 92 | public virtual IReadOnlyList<double[]> BestKnownFront {
|
---|
| 93 | get {
|
---|
| 94 | if (!Parameters.ContainsKey(BestKnownFrontParameterName)) return null;
|
---|
| 95 | var mat = BestKnownFrontParameter.Value;
|
---|
| 96 | if (mat == null) return null;
|
---|
| 97 | var v = new double[mat.Rows][];
|
---|
| 98 | for (var i = 0; i < mat.Rows; i++) {
|
---|
| 99 | var r = v[i] = new double[mat.Columns];
|
---|
| 100 | for (var j = 0; j < mat.Columns; j++) {
|
---|
| 101 | r[j] = mat[i, j];
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | return v;
|
---|
| 105 | }
|
---|
| 106 | set {
|
---|
| 107 | if (value == null || value.Count == 0) {
|
---|
| 108 | BestKnownFrontParameter.Value = new DoubleMatrix();
|
---|
| 109 | return;
|
---|
| 110 | }
|
---|
| 111 | var mat = new DoubleMatrix(value.Count, value[0].Length);
|
---|
| 112 | for (int i = 0; i < value.Count; i++) {
|
---|
| 113 | for (int j = 0; j < value[i].Length; j++) {
|
---|
| 114 | mat[i, j] = value[i][j];
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | BestKnownFrontParameter.Value = mat;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | public virtual double[] ReferencePoint {
|
---|
| 122 | get { return ReferencePointParameter.Value != null ? ReferencePointParameter.Value.CloneAsArray() : null; }
|
---|
| 123 | set { ReferencePointParameter.Value = new DoubleArray(value); }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[17320] | 126 | public virtual double[] Evaluate(TEncodedSolution solution, IRandom random) {
|
---|
| 127 | return Evaluate(solution, random, CancellationToken.None);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | public abstract double[] Evaluate(TEncodedSolution solution, IRandom random, CancellationToken cancellationToken);
|
---|
[16806] | 131 | public virtual void Analyze(TEncodedSolution[] solutions, double[][] qualities, ResultCollection results, IRandom random) { }
|
---|
[16751] | 132 |
|
---|
[17225] | 133 |
|
---|
[16692] | 134 | protected override void OnOperatorsChanged() {
|
---|
| 135 | if (Encoding != null) {
|
---|
| 136 | PruneSingleObjectiveOperators(Encoding);
|
---|
| 137 | var combinedEncoding = Encoding as CombinedEncoding;
|
---|
| 138 | if (combinedEncoding != null) {
|
---|
| 139 | foreach (var encoding in combinedEncoding.Encodings.ToList()) {
|
---|
| 140 | PruneSingleObjectiveOperators(encoding);
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
[16801] | 144 | base.OnOperatorsChanged();
|
---|
[16692] | 145 | }
|
---|
[11740] | 146 |
|
---|
[16692] | 147 | private void PruneSingleObjectiveOperators(IEncoding encoding) {
|
---|
| 148 | if (encoding != null && encoding.Operators.Any(x => x is ISingleObjectiveOperator && !(x is IMultiObjectiveOperator)))
|
---|
| 149 | encoding.Operators = encoding.Operators.Where(x => !(x is ISingleObjectiveOperator) || x is IMultiObjectiveOperator).ToList();
|
---|
| 150 |
|
---|
| 151 | foreach (var multiOp in Encoding.Operators.OfType<IMultiOperator>()) {
|
---|
| 152 | foreach (var soOp in multiOp.Operators.Where(x => x is ISingleObjectiveOperator).ToList()) {
|
---|
| 153 | multiOp.RemoveOperator(soOp);
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[11740] | 158 | protected override void OnEvaluatorChanged() {
|
---|
| 159 | base.OnEvaluatorChanged();
|
---|
| 160 | ParameterizeOperators();
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[11786] | 163 | private void ParameterizeOperators() {
|
---|
[16751] | 164 | foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator<TEncodedSolution>>())
|
---|
[11740] | 165 | op.EvaluateFunc = Evaluate;
|
---|
[16751] | 166 | foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator<TEncodedSolution>>())
|
---|
[11740] | 167 | op.AnalyzeAction = Analyze;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[11780] | 170 |
|
---|
| 171 | #region IMultiObjectiveHeuristicOptimizationProblem Members
|
---|
| 172 | IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
[17317] | 173 | get { return MaximizationParameter; }
|
---|
[11780] | 174 | }
|
---|
| 175 | IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
| 176 | get { return Evaluator; }
|
---|
| 177 | }
|
---|
| 178 | #endregion
|
---|
[17317] | 179 |
|
---|
| 180 | public event EventHandler MaximizationChanged;
|
---|
| 181 | protected void OnMaximizationChanged() {
|
---|
| 182 | MaximizationChanged?.Invoke(this, EventArgs.Empty);
|
---|
| 183 | }
|
---|
[11740] | 184 | }
|
---|
[17225] | 185 | } |
---|