Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveBasicProblem.cs @ 16948

Last change on this file since 16948 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Optimization {
32  [StorableClass]
33  public abstract class MultiObjectiveBasicProblem<TEncoding> : BasicProblem<TEncoding, MultiObjectiveEvaluator>, IMultiObjectiveHeuristicOptimizationProblem, IMultiObjectiveProblemDefinition
34  where TEncoding : class, IEncoding {
35    [StorableConstructor]
36    protected MultiObjectiveBasicProblem(bool deserializing) : base(deserializing) { }
37
38    protected MultiObjectiveBasicProblem(MultiObjectiveBasicProblem<TEncoding> original, Cloner cloner)
39      : base(original, cloner) {
40      ParameterizeOperators();
41    }
42
43    protected MultiObjectiveBasicProblem()
44      : base() {
45      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
46
47      Operators.Add(Evaluator);
48      Operators.Add(new MultiObjectiveAnalyzer());
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);
60    public virtual void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { }
61   
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();
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      }
84    }
85
86    protected override void OnEvaluatorChanged() {
87      base.OnEvaluatorChanged();
88      ParameterizeOperators();
89    }
90
91    private void ParameterizeOperators() {
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
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
107  }
108}
Note: See TracBrowser for help on using the repository browser.