[11740] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 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 |
|
---|
| 22 | using System.Linq;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
[11780] | 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
[11740] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
[11949] | 29 | namespace HeuristicLab.Optimization {
|
---|
[11740] | 30 | [StorableClass]
|
---|
[16171] | 31 | public abstract class MultiObjectiveBasicProblem<TEncoding> : BasicProblem<TEncoding, MultiObjectiveEvaluator>, IMultiObjectiveBasicProblem
|
---|
[11740] | 32 | where TEncoding : class, IEncoding {
|
---|
[16171] | 33 |
|
---|
| 34 | #region Parameternames
|
---|
| 35 | public const string MaximizationParameterName = "Maximization";
|
---|
| 36 | public const string BestKnownFrontParameterName = "BestKnownFront";
|
---|
| 37 | public const string ReferencePointParameterName = "ReferencePoint";
|
---|
| 38 | #endregion
|
---|
| 39 |
|
---|
| 40 | #region Parameterproperties
|
---|
| 41 | public IValueParameter<BoolArray> MaximizationParameter {
|
---|
| 42 | get { return (IValueParameter<BoolArray>) Parameters[MaximizationParameterName]; }
|
---|
| 43 | }
|
---|
| 44 | public IValueParameter<DoubleMatrix> BestKnownFrontParameter {
|
---|
| 45 | get { return (IValueParameter<DoubleMatrix>)Parameters[BestKnownFrontParameterName]; }
|
---|
| 46 | }
|
---|
| 47 | public IValueParameter<DoubleArray> ReferencePointParameter {
|
---|
| 48 | get { return (IValueParameter<DoubleArray>)Parameters[ReferencePointParameterName]; }
|
---|
| 49 | }
|
---|
| 50 | #endregion
|
---|
| 51 |
|
---|
| 52 | #region Properties
|
---|
| 53 |
|
---|
| 54 | public abstract bool[] Maximization { get; }
|
---|
| 55 |
|
---|
| 56 | public DoubleMatrix BestKnownFront {
|
---|
| 57 | get { return Parameters.ContainsKey(BestKnownFrontParameterName) ? BestKnownFrontParameter.Value : null; }
|
---|
| 58 | set { BestKnownFrontParameter.Value = value; }
|
---|
| 59 | }
|
---|
| 60 | public DoubleArray ReferencePoint {
|
---|
| 61 | get { return Parameters.ContainsKey(ReferencePointParameterName) ? ReferencePointParameter.Value : null; }
|
---|
| 62 | set { ReferencePointParameter.Value = value; }
|
---|
| 63 | }
|
---|
| 64 | #endregion
|
---|
| 65 |
|
---|
[11740] | 66 | [StorableConstructor]
|
---|
[11814] | 67 | protected MultiObjectiveBasicProblem(bool deserializing) : base(deserializing) { }
|
---|
[11740] | 68 |
|
---|
[11814] | 69 | protected MultiObjectiveBasicProblem(MultiObjectiveBasicProblem<TEncoding> original, Cloner cloner)
|
---|
[11740] | 70 | : base(original, cloner) {
|
---|
| 71 | ParameterizeOperators();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[11814] | 74 | protected MultiObjectiveBasicProblem()
|
---|
[11740] | 75 | : base() {
|
---|
[16171] | 76 | Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
|
---|
| 77 | 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."));
|
---|
| 78 | Parameters.Add(new OptionalValueParameter<DoubleArray>(ReferencePointParameterName, "The refrence point for hypervolume calculations on this problem"));
|
---|
[11753] | 79 | Operators.Add(Evaluator);
|
---|
[11767] | 80 | Operators.Add(new MultiObjectiveAnalyzer());
|
---|
[11740] | 81 |
|
---|
| 82 | ParameterizeOperators();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 86 | private void AfterDeserialization() {
|
---|
| 87 | ParameterizeOperators();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[16171] | 90 |
|
---|
[11740] | 91 | public abstract double[] Evaluate(Individual individual, IRandom random);
|
---|
[11880] | 92 | public virtual void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { }
|
---|
[15080] | 93 |
|
---|
[11970] | 94 | protected override void OnOperatorsChanged() {
|
---|
| 95 | base.OnOperatorsChanged();
|
---|
| 96 | if (Encoding != null) {
|
---|
| 97 | PruneSingleObjectiveOperators(Encoding);
|
---|
| 98 | var multiEncoding = Encoding as MultiEncoding;
|
---|
| 99 | if (multiEncoding != null) {
|
---|
| 100 | foreach (var encoding in multiEncoding.Encodings.ToList()) {
|
---|
| 101 | PruneSingleObjectiveOperators(encoding);
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | private void PruneSingleObjectiveOperators(IEncoding encoding) {
|
---|
| 108 | if (encoding != null && encoding.Operators.Any(x => x is ISingleObjectiveOperator && !(x is IMultiObjectiveOperator)))
|
---|
| 109 | encoding.Operators = encoding.Operators.Where(x => !(x is ISingleObjectiveOperator) || x is IMultiObjectiveOperator).ToList();
|
---|
[15084] | 110 |
|
---|
| 111 | foreach (var multiOp in Encoding.Operators.OfType<IMultiOperator>()) {
|
---|
| 112 | foreach (var soOp in multiOp.Operators.Where(x => x is ISingleObjectiveOperator).ToList()) {
|
---|
| 113 | multiOp.RemoveOperator(soOp);
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
[11970] | 116 | }
|
---|
| 117 |
|
---|
[11740] | 118 | protected override void OnEvaluatorChanged() {
|
---|
| 119 | base.OnEvaluatorChanged();
|
---|
| 120 | ParameterizeOperators();
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[11786] | 123 | private void ParameterizeOperators() {
|
---|
[11740] | 124 | foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator>())
|
---|
| 125 | op.EvaluateFunc = Evaluate;
|
---|
| 126 | foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator>())
|
---|
| 127 | op.AnalyzeAction = Analyze;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[11780] | 130 | #region IMultiObjectiveHeuristicOptimizationProblem Members
|
---|
| 131 | IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
[16171] | 132 | get { return Parameters[MaximizationParameterName]; }
|
---|
[11780] | 133 | }
|
---|
| 134 | IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
| 135 | get { return Evaluator; }
|
---|
| 136 | }
|
---|
| 137 | #endregion
|
---|
[11740] | 138 | }
|
---|
| 139 | }
|
---|