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