#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 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.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.Operators {
[Item("DataReducer", "An operator to reduce values of sub scopes.")]
[StorableClass]
public sealed class DataReducer : SingleSuccessorOperator {
#region Parameter Properties
public ScopeTreeLookupParameter ParameterToReduce {
get { return (ScopeTreeLookupParameter)Parameters["ParameterToReduce"]; }
}
public LookupParameter TargetParameter {
get { return (LookupParameter)Parameters["TargetParameter"]; }
}
public ValueParameter ReductionOperation {
get { return (ValueParameter)Parameters["ReductionOperation"]; }
}
public ValueParameter TargetOperation {
get { return (ValueParameter)Parameters["TargetOperation"]; }
}
#endregion
[StorableConstructor]
private DataReducer(bool deserializing) : base(deserializing) { }
private DataReducer(DataReducer original, Cloner cloner)
: base(original, cloner) {
}
public DataReducer()
: base() {
#region Create parameters
Parameters.Add(new ScopeTreeLookupParameter("ParameterToReduce", "The parameter on which the reduction operation should be applied."));
Parameters.Add(new LookupParameter("TargetParameter", "The target variable in which the reduced value should be stored."));
Parameters.Add(new ValueParameter("ReductionOperation", "The operation which is applied on the parameters to reduce."));
Parameters.Add(new ValueParameter("TargetOperation", "The operation used to apply the reduced value to the target variable."));
#endregion
}
public override IDeepCloneable Clone(Cloner cloner) {
return new DataReducer(this, cloner);
}
public override IOperation Apply() {
var values = ParameterToReduce.ActualValue;
if (values.Count() > 0) {
if (values.All(x => typeof(IntValue).IsAssignableFrom(x.GetType()))) {
CalculateResult(values.OfType().Select(x => x.Value), values.First().GetType());
} else if (values.All(x => typeof(DoubleValue).IsAssignableFrom(x.GetType()))) {
CalculateResult(values.OfType().Select(x => x.Value), values.First().GetType());
} else if (values.All(x => typeof(TimeSpanValue).IsAssignableFrom(x.GetType()))) {
CalculateResult(values.OfType().Select(x => x.Value), values.First().GetType());
} else {
throw new ArgumentException(string.Format("Type {0} is not supported by the DataReducer.", values.First().GetType()));
}
}
return base.Apply();
}
#region integer reduction
private void CalculateResult(IEnumerable values, Type targetType) {
int result;
switch (ReductionOperation.Value.Value) {
case ReductionOperations.Sum:
result = values.Sum();
break;
case ReductionOperations.Product:
result = values.Aggregate(1, (x, y) => x * y);
break;
case ReductionOperations.Count:
result = values.Count();
break;
case ReductionOperations.Min:
result = values.Min();
break;
case ReductionOperations.Max:
result = values.Max();
break;
case ReductionOperations.Avg:
result = (int)Math.Round(values.Average());
break;
case ReductionOperations.Assign:
result = values.Last();
break;
default:
throw new InvalidOperationException(string.Format("Operation {0} is not supported as ReductionOperation for type: {1}.", ReductionOperation.Value.Value, targetType));
}
IntValue target;
switch (TargetOperation.Value.Value) {
case ReductionOperations.Sum:
target = InitializeTarget(targetType, 0);
target.Value += result;
break;
case ReductionOperations.Product:
target = InitializeTarget(targetType, 1);
target.Value = target.Value * result;
break;
case ReductionOperations.Min:
target = InitializeTarget(targetType, int.MaxValue);
target.Value = Math.Min(target.Value, result);
break;
case ReductionOperations.Max:
target = InitializeTarget(targetType, int.MinValue);
target.Value = Math.Max(target.Value, result);
break;
case ReductionOperations.Avg:
target = InitializeTarget(targetType, result);
target.Value = (int)Math.Round((target.Value + result) / 2.0);
break;
case ReductionOperations.Assign:
target = InitializeTarget(targetType, 0);
target.Value = result;
break;
default:
throw new InvalidOperationException(string.Format("Operation {0} is not supported as TargetOperation for type: {1}.", TargetOperation.Value.Value, targetType));
}
}
#endregion
#region double reduction
private void CalculateResult(IEnumerable values, Type targetType) {
double result;
switch (ReductionOperation.Value.Value) {
case ReductionOperations.Sum:
result = values.Sum();
break;
case ReductionOperations.Product:
result = values.Aggregate(1.0, (x, y) => x * y);
break;
case ReductionOperations.Count:
result = values.Count();
break;
case ReductionOperations.Min:
result = values.Min();
break;
case ReductionOperations.Max:
result = values.Max();
break;
case ReductionOperations.Avg:
result = values.Average();
break;
case ReductionOperations.Assign:
result = values.Last();
break;
default:
throw new InvalidOperationException(string.Format("Operation {0} is not supported as ReductionOperation for type: {1}.", ReductionOperation.Value.Value, targetType));
}
DoubleValue target;
switch (TargetOperation.Value.Value) {
case ReductionOperations.Sum:
target = InitializeTarget(targetType, 0.0);
target.Value += result;
break;
case ReductionOperations.Product:
target = InitializeTarget(targetType, 1.0);
target.Value = target.Value * result;
break;
case ReductionOperations.Min:
target = InitializeTarget(targetType, double.MaxValue);
target.Value = Math.Min(target.Value, result);
break;
case ReductionOperations.Max:
target = InitializeTarget(targetType, double.MinValue);
target.Value = Math.Max(target.Value, result);
break;
case ReductionOperations.Avg:
target = InitializeTarget(targetType, result);
target.Value = (target.Value + result) / 2.0;
break;
case ReductionOperations.Assign:
target = InitializeTarget(targetType, 0.0);
target.Value = result;
break;
default:
throw new InvalidOperationException(string.Format("Operation {0} is not supported as TargetOperation for type: {1}.", TargetOperation.Value.Value, targetType));
}
}
#endregion
#region TimeSpan reduction
private void CalculateResult(IEnumerable values, Type targetType) {
TimeSpan result;
switch (ReductionOperation.Value.Value) {
case ReductionOperations.Sum:
result = values.Aggregate(new TimeSpan(), (x, y) => x + y);
break;
case ReductionOperations.Min:
result = values.Min();
break;
case ReductionOperations.Max:
result = values.Max();
break;
case ReductionOperations.Avg:
result = TimeSpan.FromMilliseconds(values.Average(x => x.TotalMilliseconds));
break;
case ReductionOperations.Assign:
result = values.Last();
break;
default:
throw new InvalidOperationException(string.Format("Operation {0} is not supported as ReductionOperation for type: {1}.", ReductionOperation.Value.Value, targetType));
}
TimeSpanValue target;
switch (TargetOperation.Value.Value) {
case ReductionOperations.Sum:
target = InitializeTarget(targetType, new TimeSpan());
target.Value += result;
break;
case ReductionOperations.Min:
target = InitializeTarget(targetType, TimeSpan.MaxValue);
target.Value = target.Value < result ? target.Value : result;
break;
case ReductionOperations.Max:
target = InitializeTarget(targetType, TimeSpan.MinValue);
target.Value = target.Value > result ? target.Value : result;
break;
case ReductionOperations.Avg:
target = InitializeTarget(targetType, result);
target.Value = TimeSpan.FromMilliseconds((target.Value.TotalMilliseconds + result.TotalMilliseconds) / 2);
break;
case ReductionOperations.Assign:
target = InitializeTarget(targetType, new TimeSpan());
target.Value = result;
break;
default:
throw new InvalidOperationException(string.Format("Operation {0} is not supported as TargetOperation for type: {1}.", TargetOperation.Value.Value, targetType));
}
}
#endregion
#region helpers
private T1 InitializeTarget(Type targetType, T2 initialValue)
where T1 : ValueTypeValue
where T2 : struct {
T1 target = (T1)TargetParameter.ActualValue;
if (target == null) {
target = (T1)Activator.CreateInstance(targetType);
TargetParameter.ActualValue = target;
target.Value = initialValue;
}
return target;
}
#endregion
}
}