[11740] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16057] | 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 |
|
---|
[15051] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[11740] | 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[11780] | 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
[11740] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
[11949] | 31 | namespace HeuristicLab.Optimization {
|
---|
[11740] | 32 | [StorableClass]
|
---|
[11814] | 33 | public abstract class MultiObjectiveBasicProblem<TEncoding> : BasicProblem<TEncoding, MultiObjectiveEvaluator>, IMultiObjectiveHeuristicOptimizationProblem, IMultiObjectiveProblemDefinition
|
---|
[11740] | 34 | where TEncoding : class, IEncoding {
|
---|
| 35 | [StorableConstructor]
|
---|
[11814] | 36 | protected MultiObjectiveBasicProblem(bool deserializing) : base(deserializing) { }
|
---|
[11740] | 37 |
|
---|
[11814] | 38 | protected MultiObjectiveBasicProblem(MultiObjectiveBasicProblem<TEncoding> original, Cloner cloner)
|
---|
[11740] | 39 | : base(original, cloner) {
|
---|
| 40 | ParameterizeOperators();
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[11814] | 43 | protected MultiObjectiveBasicProblem()
|
---|
[11740] | 44 | : base() {
|
---|
[11996] | 45 | Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
|
---|
[11740] | 46 |
|
---|
[11753] | 47 | Operators.Add(Evaluator);
|
---|
[11767] | 48 | Operators.Add(new MultiObjectiveAnalyzer());
|
---|
[11740] | 49 |
|
---|
| 50 | ParameterizeOperators();
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 54 | private void AfterDeserialization() {
|
---|
| 55 | ParameterizeOperators();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public abstract bool[] Maximization { get; }
|
---|
| 59 | public abstract double[] Evaluate(Individual individual, IRandom random);
|
---|
[11880] | 60 | public virtual void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { }
|
---|
[15080] | 61 |
|
---|
[11970] | 62 | protected override void OnOperatorsChanged() {
|
---|
| 63 | base.OnOperatorsChanged();
|
---|
| 64 | if (Encoding != null) {
|
---|
| 65 | PruneSingleObjectiveOperators(Encoding);
|
---|
| 66 | var multiEncoding = Encoding as MultiEncoding;
|
---|
| 67 | if (multiEncoding != null) {
|
---|
| 68 | foreach (var encoding in multiEncoding.Encodings.ToList()) {
|
---|
| 69 | PruneSingleObjectiveOperators(encoding);
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | private void PruneSingleObjectiveOperators(IEncoding encoding) {
|
---|
| 76 | if (encoding != null && encoding.Operators.Any(x => x is ISingleObjectiveOperator && !(x is IMultiObjectiveOperator)))
|
---|
| 77 | encoding.Operators = encoding.Operators.Where(x => !(x is ISingleObjectiveOperator) || x is IMultiObjectiveOperator).ToList();
|
---|
[15084] | 78 |
|
---|
| 79 | foreach (var multiOp in Encoding.Operators.OfType<IMultiOperator>()) {
|
---|
| 80 | foreach (var soOp in multiOp.Operators.Where(x => x is ISingleObjectiveOperator).ToList()) {
|
---|
| 81 | multiOp.RemoveOperator(soOp);
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
[11970] | 84 | }
|
---|
| 85 |
|
---|
[11740] | 86 | protected override void OnEvaluatorChanged() {
|
---|
| 87 | base.OnEvaluatorChanged();
|
---|
| 88 | ParameterizeOperators();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[11786] | 91 | private void ParameterizeOperators() {
|
---|
[11740] | 92 | foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator>())
|
---|
| 93 | op.EvaluateFunc = Evaluate;
|
---|
| 94 | foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator>())
|
---|
| 95 | op.AnalyzeAction = Analyze;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[11780] | 98 |
|
---|
| 99 | #region IMultiObjectiveHeuristicOptimizationProblem Members
|
---|
| 100 | IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
| 101 | get { return Parameters["Maximization"]; }
|
---|
| 102 | }
|
---|
| 103 | IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
| 104 | get { return Evaluator; }
|
---|
| 105 | }
|
---|
| 106 | #endregion
|
---|
[11740] | 107 | }
|
---|
| 108 | }
|
---|