Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Analysis.FitnessLandscape/3.3/Algorithms/FLACharacterizer.cs @ 15817

Last change on this file since 15817 was 15332, checked in by abeham, 7 years ago

#2457: added event handling to FLA Characterizer

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Threading;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29
30namespace HeuristicLab.Analysis.FitnessLandscape {
31  [Item("FLA Characterizer", "An algorithm that executes a characteristic calculator.")]
32  [StorableClass]
33  [Creatable(CreatableAttribute.Categories.TestingAndAnalysis + CreatableAttribute.Categories.SplitToken + "1" + CreatableAttribute.Categories.OrderToken + "FLA", Priority = 300)]
34  public sealed class FLACharacterizer : BasicAlgorithm {
35    public override bool SupportsPause { get { return false; } }
36   
37    public IValueParameter<ICharacteristicCalculator> CalculatorParameter {
38      get { return (IValueParameter<ICharacteristicCalculator>)Parameters["Calculator"]; }
39    }
40
41    public ICharacteristicCalculator Calculator {
42      get { return CalculatorParameter.Value; }
43    }
44
45    [StorableConstructor]
46    private FLACharacterizer(bool deserializing) : base(deserializing) { }
47    private FLACharacterizer(FLACharacterizer original, Cloner cloner)
48      : base(original, cloner) {
49      RegisterEventHandlers();
50    }
51    public FLACharacterizer() : base() {
52      Parameters.Add(new ValueParameter<ICharacteristicCalculator>("Calculator", "The FLA characteristic calculator.", new RandomWalkCalculator()));
53      RegisterEventHandlers();
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new FLACharacterizer(this, cloner);
58    }
59   
60    [StorableHook(HookType.AfterDeserialization)]
61    private void AfterDeserialization() {
62      RegisterEventHandlers();
63    }
64
65    private void RegisterEventHandlers() {
66      CalculatorParameter.ValueChanged += CalculatorParameterOnValueChanged;
67    }
68
69    private void CalculatorParameterOnValueChanged(object sender, EventArgs e) {
70      Calculator.Problem = Problem;
71    }
72
73    protected override void OnProblemChanged() {
74      base.OnProblemChanged();
75      Calculator.Problem = Problem;
76    }
77
78    protected override void Run(CancellationToken cancellationToken) {
79      var calculator = Calculator;
80      if (calculator.CanCalculate()) {
81        foreach (var result in calculator.Calculate()) {
82          Results.Add(result);
83        }
84      } else throw new InvalidOperationException("Calculator cannot be applied.");
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.