using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.DataAnalysis { [Item("Shift to Range Transformation", "f(x) = k * x + d, start <= f(x) <= end | Represents a linear Transformation using Parameters defining a target range")] public class ShiftToRangeTransformation : LinearTransformation { protected const string RangeParameterName = "Range"; #region Parameters public IValueParameter RangeParameter { get { return (IValueParameter)Parameters[RangeParameterName]; } } #endregion #region properties public override string ShortName { get { return "Sft"; } } public DoubleRange Range { get { return RangeParameter.Value; } } #endregion [StorableConstructor] protected ShiftToRangeTransformation(bool deserializing) : base(deserializing) { } protected ShiftToRangeTransformation(ShiftToRangeTransformation original, Cloner cloner) : base(original, cloner) { } public ShiftToRangeTransformation(IEnumerable allowedColumns) : base(allowedColumns) { MultiplierParameter.Hidden = true; AddendParameter.Hidden = true; Parameters.Add(new ValueParameter(RangeParameterName, "start, end | Range for the target window of the linear transformation", new DoubleRange(0.0, 1.0))); } public override IDeepCloneable Clone(Cloner cloner) { return new ShiftToRangeTransformation(this, cloner); } public override IEnumerable Apply(IEnumerable data) { ConfigureParameters(data); return base.Apply(data); } public override bool Check(IEnumerable data, out string errorMsg) { ConfigureParameters(data); return base.Check(data, out errorMsg); } protected void ConfigureParameters(IEnumerable data) { double originalRangeStart = data.Min(); double originalRangeEnd = data.Max(); double originalRangeWidth = originalRangeEnd - originalRangeStart; double targetRangeWidth = Range.End - Range.Start; Multiplier = targetRangeWidth / originalRangeWidth; Addend = Range.Start - originalRangeStart * Multiplier; } } }