#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 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.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Algorithms.ALPS {
[Item("WeightingReducer", "An operator that combines two values based on the weight of the lower (0.0) and higher (1.0) value.")]
[StorableClass]
public sealed class WeightingReducer : SingleSuccessorOperator {
public IScopeTreeLookupParameter ParameterToReduce {
get { return (IScopeTreeLookupParameter)Parameters["ParameterToReduce"]; }
}
public ILookupParameter TargetParameter {
get { return (ILookupParameter)Parameters["TargetParameter"]; }
}
public IValueLookupParameter WeightParameter {
get { return (IValueLookupParameter)Parameters["Weight"]; }
}
[StorableConstructor]
private WeightingReducer(bool deserializing) : base(deserializing) { }
private WeightingReducer(WeightingReducer original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new WeightingReducer(this, cloner);
}
public WeightingReducer()
: base() {
Parameters.Add(new ScopeTreeLookupParameter("ParameterToReduce", "The parameter on which the weighting should be applied."));
Parameters.Add(new LookupParameter("TargetParameter", "The target variable in which the weighted value should be stored."));
Parameters.Add(new ValueLookupParameter("Weight", "Weight of the two values (0.0 for the smaller, 1.0 for the larger)."));
}
public override IOperation Apply() {
var values = ParameterToReduce.ActualValue;
if (values.Length != 2)
throw new InvalidOperationException("Weighting between values can only done for two values.");
double weight = WeightParameter.ActualValue.Value;
double max = values.Max(v => v.Value);
double min = values.Min(v => v.Value);
double result = max * weight + min * (1 - weight);
if (TargetParameter.ActualValue == null)
TargetParameter.ActualValue = new DoubleValue(result);
else TargetParameter.ActualValue.Value = result;
return base.Apply();
}
}
}