Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3133_ProblemModifiers/HeuristicLab.Problems.Modifiers/Modifier/ProblemModifier.cs @ 18029

Last change on this file since 18029 was 18029, checked in by bwerth, 3 years ago

#3133 added implementation of problem modifiers

File size: 4.2 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.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29
30namespace HeuristicLab.Problems.Modifiers {
31  [StorableType("03EC0CED-F927-4CCF-8262-716AECE267BE")]
32  [Item("MultiObjectiveProblemModifier", "A modifier attached to a problem")]
33  public abstract class ProblemModifier : ParameterizedNamedItem {
34
35    [Storable] private ProblemModifier nextModifier;
36    public virtual IEncoding Encoding => NextModifier?.Encoding;
37    public virtual BoolArray Maximization => NextModifier?.Maximization;
38    public virtual ISolutionCreator SolutionCreator => NextModifier?.SolutionCreator;
39    public ProblemModifier NextModifier {
40      get => nextModifier;
41      set
42      {
43        if (nextModifier == value) return;
44        ResetListeners(nextModifier, value);
45        nextModifier = value;
46        InvokeEncodingChanged();
47        InvokeMaximizationChanged();
48      }
49    }
50
51    #region Constructors
52    [StorableConstructor]
53    protected ProblemModifier(StorableConstructorFlag _) : base(_) { }
54    protected ProblemModifier(ProblemModifier original, Cloner cloner) : base(original, cloner) {
55      nextModifier = cloner.Clone(original.NextModifier);
56    }
57    protected ProblemModifier() { }
58
59    [StorableHook(HookType.AfterDeserialization)]
60    private void AfterDeserialization() {
61      ResetListeners(nextModifier, nextModifier);
62    }
63    #endregion
64
65    public virtual void Initialize() { }
66
67    public virtual double[] ModifiedEvaluate(Individual individual, IRandom random) {
68      return nextModifier.ModifiedEvaluate(individual, random);
69    }
70
71    public virtual void ModifiedAnalyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
72      nextModifier.ModifiedAnalyze(individuals, qualities, results, random);
73    }
74
75    #region updateEvents
76    public event EventHandler EncodingChanged;
77    public event EventHandler MaximizationChanged;
78    public event EventHandler SolutionCreatorChanged;
79    protected void InvokeEncodingChanged() {
80      EncodingChanged?.Invoke(this, EventArgs.Empty);
81    }
82    protected void InvokeMaximizationChanged() {
83      MaximizationChanged?.Invoke(this, EventArgs.Empty);
84    }
85    protected void InvokeSolutionCreatorChanged() {
86      SolutionCreatorChanged?.Invoke(this, EventArgs.Empty);
87    }
88    protected virtual void OnNextMaximizationChanged(object sender, EventArgs e) {
89      InvokeMaximizationChanged();
90    }
91    protected virtual void OnNextEncodingChanged(object sender, EventArgs e) {
92      InvokeEncodingChanged();
93    }
94    protected virtual void OnNextSolutionCreatorChanged(object sender, EventArgs e) {
95      InvokeSolutionCreatorChanged();
96    }
97    #endregion
98
99    #region helpers
100    protected virtual void ResetListeners(ProblemModifier oldNext, ProblemModifier newNext) {
101      if (oldNext != null) {
102        oldNext.EncodingChanged -= OnNextEncodingChanged;
103        oldNext.MaximizationChanged -= OnNextMaximizationChanged;
104        oldNext.SolutionCreatorChanged -= OnNextSolutionCreatorChanged;
105      }
106      if (newNext == null) return;
107      newNext.EncodingChanged += OnNextEncodingChanged;
108      newNext.MaximizationChanged += OnNextMaximizationChanged;
109      newNext.SolutionCreatorChanged += OnNextSolutionCreatorChanged;
110    }
111    #endregion
112
113
114  }
115}
Note: See TracBrowser for help on using the repository browser.