Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveBasicProblem.cs @ 15567

Last change on this file since 15567 was 15084, checked in by abeham, 7 years ago

#2775:

  • All single-objective operators inside IMultiOperators are now removed by the multi-objective problem
  • All multi-objective operators inside IMultiOperators are now removed by the single-objective problem
File size: 4.1 KB
RevLine 
[11740]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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]22using System;
23using System.Collections.Generic;
[11740]24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
[11780]27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
[11740]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
[11949]31namespace 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}
Note: See TracBrowser for help on using the repository browser.