#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 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 HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Termination {
[Item("ThresholdTerminator", "Base class for all termination criteria which specifies some threshold.")]
[StorableClass]
public abstract class ThresholdTerminator : Terminator where T : class, IItem, IStringConvertibleValue, new() {
public IFixedValueParameter ThresholdParameter {
get { return (IFixedValueParameter)Parameters["Threshold"]; }
}
public T Threshold {
get { return ThresholdParameter.Value; }
}
[StorableConstructor]
protected ThresholdTerminator(bool deserializing) : base(deserializing) { }
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
Initialize();
}
protected ThresholdTerminator(ThresholdTerminator original, Cloner cloner)
: base(original, cloner) {
Initialize();
}
protected ThresholdTerminator()
: this(new T()) { }
protected ThresholdTerminator(T threshold)
: base() {
if (threshold == null) throw new ArgumentNullException("threshold");
Parameters.Add(new FixedValueParameter("Threshold", "The limit of the termiation criterion.", threshold));
Initialize();
}
private void Initialize() {
Threshold.ValueChanged += new EventHandler(Threshold_ValueChanged);
}
private void Threshold_ValueChanged(object sender, EventArgs e) {
OnThresholdChanged();
}
protected virtual void OnThresholdChanged() {
OnToStringChanged();
}
public override string ToString() {
if (ThresholdParameter.Value == null) return Name;
else return Name + ": " + ThresholdParameter.Value;
}
}
}