#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Threading;
using HEAL.Attic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
namespace HeuristicLab.Analysis.FitnessLandscape {
[Item("FLA Characterizer", "An algorithm that executes a characteristic calculator.")]
[StorableType("9843C276-463D-4B79-A44B-7BD1F3B23FB6")]
[Creatable(CreatableAttribute.Categories.TestingAndAnalysis + CreatableAttribute.Categories.SplitToken + "1" + CreatableAttribute.Categories.OrderToken + "FLA", Priority = 300)]
public sealed class FLACharacterizer : BasicAlgorithm {
public override bool SupportsPause { get { return false; } }
public IValueParameter CalculatorParameter {
get { return (IValueParameter)Parameters["Calculator"]; }
}
public ICharacteristicCalculator Calculator {
get { return CalculatorParameter.Value; }
}
[StorableConstructor]
private FLACharacterizer(StorableConstructorFlag _) : base(_) { }
private FLACharacterizer(FLACharacterizer original, Cloner cloner)
: base(original, cloner) {
RegisterEventHandlers();
}
public FLACharacterizer() : base() {
Parameters.Add(new ValueParameter("Calculator", "The FLA characteristic calculator.", new RandomWalkCalculator()));
RegisterEventHandlers();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new FLACharacterizer(this, cloner);
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterEventHandlers();
}
private void RegisterEventHandlers() {
CalculatorParameter.ValueChanged += CalculatorParameterOnValueChanged;
}
private void CalculatorParameterOnValueChanged(object sender, EventArgs e) {
Calculator.Problem = Problem;
}
protected override void OnProblemChanged() {
base.OnProblemChanged();
Calculator.Problem = Problem;
}
protected override void Run(CancellationToken cancellationToken) {
var calculator = Calculator;
if (calculator.CanCalculate()) {
foreach (var result in calculator.Calculate()) {
Results.Add(result);
}
} else throw new InvalidOperationException("Calculator cannot be applied.");
}
}
}