[7395] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7395] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Operators {
|
---|
| 32 | [Item("DataReducer", "An operator to reduce values of sub scopes.")]
|
---|
| 33 | [StorableClass]
|
---|
| 34 | public sealed class DataReducer : SingleSuccessorOperator {
|
---|
| 35 | #region Parameter Properties
|
---|
| 36 | public ScopeTreeLookupParameter<IItem> ParameterToReduce {
|
---|
| 37 | get { return (ScopeTreeLookupParameter<IItem>)Parameters["ParameterToReduce"]; }
|
---|
| 38 | }
|
---|
| 39 | public LookupParameter<IItem> TargetParameter {
|
---|
| 40 | get { return (LookupParameter<IItem>)Parameters["TargetParameter"]; }
|
---|
| 41 | }
|
---|
| 42 | public ValueParameter<ReductionOperation> ReductionOperation {
|
---|
| 43 | get { return (ValueParameter<ReductionOperation>)Parameters["ReductionOperation"]; }
|
---|
| 44 | }
|
---|
| 45 | public ValueParameter<ReductionOperation> TargetOperation {
|
---|
| 46 | get { return (ValueParameter<ReductionOperation>)Parameters["TargetOperation"]; }
|
---|
| 47 | }
|
---|
| 48 | #endregion
|
---|
| 49 |
|
---|
| 50 | [StorableConstructor]
|
---|
| 51 | private DataReducer(bool deserializing) : base(deserializing) { }
|
---|
| 52 | private DataReducer(DataReducer original, Cloner cloner)
|
---|
| 53 | : base(original, cloner) {
|
---|
| 54 | }
|
---|
| 55 | public DataReducer()
|
---|
| 56 | : base() {
|
---|
| 57 | #region Create parameters
|
---|
| 58 | Parameters.Add(new ScopeTreeLookupParameter<IItem>("ParameterToReduce", "The parameter on which the reduction operation should be applied."));
|
---|
| 59 | Parameters.Add(new LookupParameter<IItem>("TargetParameter", "The target variable in which the reduced value should be stored."));
|
---|
| 60 | Parameters.Add(new ValueParameter<ReductionOperation>("ReductionOperation", "The operation which is applied on the parameters to reduce."));
|
---|
| 61 | Parameters.Add(new ValueParameter<ReductionOperation>("TargetOperation", "The operation used to apply the reduced value to the target variable."));
|
---|
| 62 | #endregion
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 66 | return new DataReducer(this, cloner);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public override IOperation Apply() {
|
---|
| 70 | var values = ParameterToReduce.ActualValue;
|
---|
[10196] | 71 | if (!values.Any()) return base.Apply();
|
---|
| 72 |
|
---|
| 73 | if (values.All(x => x is IntValue)) {
|
---|
| 74 | CalculateResult(values.OfType<IntValue>().Select(x => x.Value), values.First().GetType());
|
---|
| 75 | } else if (values.All(x => x is DoubleValue)) {
|
---|
| 76 | CalculateResult(values.OfType<DoubleValue>().Select(x => x.Value), values.First().GetType());
|
---|
| 77 | } else if (values.All(x => x is TimeSpanValue)) {
|
---|
| 78 | CalculateResult(values.OfType<TimeSpanValue>().Select(x => x.Value), values.First().GetType());
|
---|
| 79 | } else if (values.All(x => x is BoolValue)) {
|
---|
| 80 | CalculateResult(values.OfType<BoolValue>().Select(x => x.Value), values.First().GetType());
|
---|
| 81 | } else {
|
---|
| 82 | throw new ArgumentException(string.Format("Type {0} is not supported by the DataReducer.", values.First().GetType()));
|
---|
[7395] | 83 | }
|
---|
[10196] | 84 |
|
---|
[7395] | 85 | return base.Apply();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[8252] | 88 | #region integer reduction
|
---|
| 89 | private void CalculateResult(IEnumerable<int> values, Type targetType) {
|
---|
| 90 | int result;
|
---|
[7395] | 91 | switch (ReductionOperation.Value.Value) {
|
---|
| 92 | case ReductionOperations.Sum:
|
---|
[8252] | 93 | result = values.Sum();
|
---|
[7395] | 94 | break;
|
---|
[8252] | 95 | case ReductionOperations.Product:
|
---|
| 96 | result = values.Aggregate(1, (x, y) => x * y);
|
---|
[7395] | 97 | break;
|
---|
[8252] | 98 | case ReductionOperations.Count:
|
---|
| 99 | result = values.Count();
|
---|
[7395] | 100 | break;
|
---|
| 101 | case ReductionOperations.Min:
|
---|
[8252] | 102 | result = values.Min();
|
---|
[7395] | 103 | break;
|
---|
| 104 | case ReductionOperations.Max:
|
---|
[8252] | 105 | result = values.Max();
|
---|
[7395] | 106 | break;
|
---|
[8252] | 107 | case ReductionOperations.Avg:
|
---|
| 108 | result = (int)Math.Round(values.Average());
|
---|
| 109 | break;
|
---|
| 110 | case ReductionOperations.Assign:
|
---|
| 111 | result = values.Last();
|
---|
| 112 | break;
|
---|
[7395] | 113 | default:
|
---|
[8256] | 114 | throw new InvalidOperationException(string.Format("Operation {0} is not supported as ReductionOperation for type: {1}.", ReductionOperation.Value.Value, targetType));
|
---|
[7395] | 115 | }
|
---|
| 116 |
|
---|
[8252] | 117 | IntValue target;
|
---|
[7395] | 118 | switch (TargetOperation.Value.Value) {
|
---|
| 119 | case ReductionOperations.Sum:
|
---|
[8252] | 120 | target = InitializeTarget<IntValue, int>(targetType, 0);
|
---|
[7395] | 121 | target.Value += result;
|
---|
| 122 | break;
|
---|
[8252] | 123 | case ReductionOperations.Product:
|
---|
| 124 | target = InitializeTarget<IntValue, int>(targetType, 1);
|
---|
| 125 | target.Value = target.Value * result;
|
---|
[7395] | 126 | break;
|
---|
[8252] | 127 | case ReductionOperations.Min:
|
---|
| 128 | target = InitializeTarget<IntValue, int>(targetType, int.MaxValue);
|
---|
| 129 | target.Value = Math.Min(target.Value, result);
|
---|
| 130 | break;
|
---|
| 131 | case ReductionOperations.Max:
|
---|
| 132 | target = InitializeTarget<IntValue, int>(targetType, int.MinValue);
|
---|
| 133 | target.Value = Math.Max(target.Value, result);
|
---|
| 134 | break;
|
---|
| 135 | case ReductionOperations.Avg:
|
---|
| 136 | target = InitializeTarget<IntValue, int>(targetType, result);
|
---|
| 137 | target.Value = (int)Math.Round((target.Value + result) / 2.0);
|
---|
| 138 | break;
|
---|
| 139 | case ReductionOperations.Assign:
|
---|
| 140 | target = InitializeTarget<IntValue, int>(targetType, 0);
|
---|
| 141 | target.Value = result;
|
---|
| 142 | break;
|
---|
[7395] | 143 | default:
|
---|
[8256] | 144 | throw new InvalidOperationException(string.Format("Operation {0} is not supported as TargetOperation for type: {1}.", TargetOperation.Value.Value, targetType));
|
---|
[7395] | 145 | }
|
---|
| 146 | }
|
---|
[8252] | 147 | #endregion
|
---|
| 148 | #region double reduction
|
---|
| 149 | private void CalculateResult(IEnumerable<double> values, Type targetType) {
|
---|
| 150 | double result;
|
---|
[7395] | 151 | switch (ReductionOperation.Value.Value) {
|
---|
| 152 | case ReductionOperations.Sum:
|
---|
[8252] | 153 | result = values.Sum();
|
---|
[7395] | 154 | break;
|
---|
[8252] | 155 | case ReductionOperations.Product:
|
---|
| 156 | result = values.Aggregate(1.0, (x, y) => x * y);
|
---|
[7395] | 157 | break;
|
---|
[8252] | 158 | case ReductionOperations.Count:
|
---|
| 159 | result = values.Count();
|
---|
[7395] | 160 | break;
|
---|
| 161 | case ReductionOperations.Min:
|
---|
[8252] | 162 | result = values.Min();
|
---|
[7395] | 163 | break;
|
---|
| 164 | case ReductionOperations.Max:
|
---|
[8252] | 165 | result = values.Max();
|
---|
[7395] | 166 | break;
|
---|
[8252] | 167 | case ReductionOperations.Avg:
|
---|
| 168 | result = values.Average();
|
---|
| 169 | break;
|
---|
| 170 | case ReductionOperations.Assign:
|
---|
| 171 | result = values.Last();
|
---|
| 172 | break;
|
---|
[7395] | 173 | default:
|
---|
[8256] | 174 | throw new InvalidOperationException(string.Format("Operation {0} is not supported as ReductionOperation for type: {1}.", ReductionOperation.Value.Value, targetType));
|
---|
[7395] | 175 | }
|
---|
| 176 |
|
---|
[8252] | 177 | DoubleValue target;
|
---|
[7395] | 178 | switch (TargetOperation.Value.Value) {
|
---|
| 179 | case ReductionOperations.Sum:
|
---|
[8252] | 180 | target = InitializeTarget<DoubleValue, double>(targetType, 0.0);
|
---|
[7395] | 181 | target.Value += result;
|
---|
| 182 | break;
|
---|
[8252] | 183 | case ReductionOperations.Product:
|
---|
| 184 | target = InitializeTarget<DoubleValue, double>(targetType, 1.0);
|
---|
| 185 | target.Value = target.Value * result;
|
---|
[7395] | 186 | break;
|
---|
[8252] | 187 | case ReductionOperations.Min:
|
---|
| 188 | target = InitializeTarget<DoubleValue, double>(targetType, double.MaxValue);
|
---|
| 189 | target.Value = Math.Min(target.Value, result);
|
---|
| 190 | break;
|
---|
| 191 | case ReductionOperations.Max:
|
---|
| 192 | target = InitializeTarget<DoubleValue, double>(targetType, double.MinValue);
|
---|
| 193 | target.Value = Math.Max(target.Value, result);
|
---|
| 194 | break;
|
---|
| 195 | case ReductionOperations.Avg:
|
---|
| 196 | target = InitializeTarget<DoubleValue, double>(targetType, result);
|
---|
| 197 | target.Value = (target.Value + result) / 2.0;
|
---|
| 198 | break;
|
---|
| 199 | case ReductionOperations.Assign:
|
---|
| 200 | target = InitializeTarget<DoubleValue, double>(targetType, 0.0);
|
---|
| 201 | target.Value = result;
|
---|
| 202 | break;
|
---|
[7395] | 203 | default:
|
---|
[8256] | 204 | throw new InvalidOperationException(string.Format("Operation {0} is not supported as TargetOperation for type: {1}.", TargetOperation.Value.Value, targetType));
|
---|
[7395] | 205 | }
|
---|
| 206 | }
|
---|
[8252] | 207 | #endregion
|
---|
| 208 | #region TimeSpan reduction
|
---|
| 209 | private void CalculateResult(IEnumerable<TimeSpan> values, Type targetType) {
|
---|
| 210 | TimeSpan result;
|
---|
[7395] | 211 | switch (ReductionOperation.Value.Value) {
|
---|
| 212 | case ReductionOperations.Sum:
|
---|
[8252] | 213 | result = values.Aggregate(new TimeSpan(), (x, y) => x + y);
|
---|
[7395] | 214 | break;
|
---|
| 215 | case ReductionOperations.Min:
|
---|
[8252] | 216 | result = values.Min();
|
---|
[7395] | 217 | break;
|
---|
| 218 | case ReductionOperations.Max:
|
---|
[8252] | 219 | result = values.Max();
|
---|
[7395] | 220 | break;
|
---|
[8252] | 221 | case ReductionOperations.Avg:
|
---|
| 222 | result = TimeSpan.FromMilliseconds(values.Average(x => x.TotalMilliseconds));
|
---|
| 223 | break;
|
---|
| 224 | case ReductionOperations.Assign:
|
---|
| 225 | result = values.Last();
|
---|
| 226 | break;
|
---|
[7395] | 227 | default:
|
---|
[8256] | 228 | throw new InvalidOperationException(string.Format("Operation {0} is not supported as ReductionOperation for type: {1}.", ReductionOperation.Value.Value, targetType));
|
---|
[7395] | 229 | }
|
---|
| 230 |
|
---|
[8252] | 231 | TimeSpanValue target;
|
---|
[7395] | 232 | switch (TargetOperation.Value.Value) {
|
---|
[8252] | 233 | case ReductionOperations.Sum:
|
---|
| 234 | target = InitializeTarget<TimeSpanValue, TimeSpan>(targetType, new TimeSpan());
|
---|
| 235 | target.Value += result;
|
---|
| 236 | break;
|
---|
| 237 | case ReductionOperations.Min:
|
---|
| 238 | target = InitializeTarget<TimeSpanValue, TimeSpan>(targetType, TimeSpan.MaxValue);
|
---|
| 239 | target.Value = target.Value < result ? target.Value : result;
|
---|
| 240 | break;
|
---|
| 241 | case ReductionOperations.Max:
|
---|
| 242 | target = InitializeTarget<TimeSpanValue, TimeSpan>(targetType, TimeSpan.MinValue);
|
---|
| 243 | target.Value = target.Value > result ? target.Value : result;
|
---|
| 244 | break;
|
---|
| 245 | case ReductionOperations.Avg:
|
---|
| 246 | target = InitializeTarget<TimeSpanValue, TimeSpan>(targetType, result);
|
---|
| 247 | target.Value = TimeSpan.FromMilliseconds((target.Value.TotalMilliseconds + result.TotalMilliseconds) / 2);
|
---|
| 248 | break;
|
---|
[7395] | 249 | case ReductionOperations.Assign:
|
---|
[8252] | 250 | target = InitializeTarget<TimeSpanValue, TimeSpan>(targetType, new TimeSpan());
|
---|
[7395] | 251 | target.Value = result;
|
---|
| 252 | break;
|
---|
| 253 | default:
|
---|
[8256] | 254 | throw new InvalidOperationException(string.Format("Operation {0} is not supported as TargetOperation for type: {1}.", TargetOperation.Value.Value, targetType));
|
---|
[7395] | 255 | }
|
---|
| 256 | }
|
---|
[8252] | 257 | #endregion
|
---|
[10196] | 258 | #region bool reduction
|
---|
| 259 | private void CalculateResult(IEnumerable<bool> values, Type targetType) {
|
---|
| 260 | bool result;
|
---|
| 261 | switch (ReductionOperation.Value.Value) {
|
---|
| 262 | case ReductionOperations.All:
|
---|
| 263 | result = values.All(x => x);
|
---|
| 264 | break;
|
---|
| 265 | case ReductionOperations.Any:
|
---|
| 266 | result = values.Any(x => x);
|
---|
| 267 | break;
|
---|
| 268 | default:
|
---|
| 269 | throw new InvalidOperationException(string.Format("Operation {0} is not supported as ReductionOperation for type: {1}.", ReductionOperation.Value.Value, targetType));
|
---|
| 270 | }
|
---|
[8252] | 271 |
|
---|
[10196] | 272 | BoolValue target;
|
---|
| 273 | switch (TargetOperation.Value.Value) {
|
---|
| 274 | case ReductionOperations.Assign:
|
---|
| 275 | target = InitializeTarget<BoolValue, bool>(targetType, true);
|
---|
| 276 | target.Value = result;
|
---|
| 277 | break;
|
---|
| 278 | default:
|
---|
| 279 | throw new InvalidOperationException(string.Format("Operation {0} is not supported as TargetOperation for type: {1}.", TargetOperation.Value.Value, targetType));
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 | #endregion
|
---|
| 283 |
|
---|
[8252] | 284 | #region helpers
|
---|
| 285 | private T1 InitializeTarget<T1, T2>(Type targetType, T2 initialValue)
|
---|
| 286 | where T1 : ValueTypeValue<T2>
|
---|
| 287 | where T2 : struct {
|
---|
| 288 | T1 target = (T1)TargetParameter.ActualValue;
|
---|
| 289 | if (target == null) {
|
---|
| 290 | target = (T1)Activator.CreateInstance(targetType);
|
---|
| 291 | TargetParameter.ActualValue = target;
|
---|
| 292 | target.Value = initialValue;
|
---|
| 293 | }
|
---|
| 294 | return target;
|
---|
| 295 | }
|
---|
| 296 | #endregion
|
---|
[7395] | 297 | }
|
---|
| 298 | }
|
---|