Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Operators/3.3/IntDataReducer.cs @ 7325

Last change on this file since 7325 was 7325, checked in by ascheibe, 12 years ago

#1745 added result storing operations for the reducer

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Operators {
31  [Item("IntDataReducer", "An operator to reduce int values of sub scopes")]
32  [StorableClass]
33  public sealed class IntDataReducer : SingleSuccessorOperator {
34    #region Parameter Properties
35    public ScopeTreeLookupParameter<IntValue> ParameterToReduce {
36      get { return (ScopeTreeLookupParameter<IntValue>)Parameters["ParameterToReduce"]; }
37    }
38    public ValueLookupParameter<StringValue> TargetParameterName {
39      get { return (ValueLookupParameter<StringValue>)Parameters["TargetParameterName"]; }
40    }
41    private LookupParameter<IntValue> TargetParameter {
42      get { return (LookupParameter<IntValue>)Parameters["TargetParameter"]; }
43    }
44    public ValueParameter<ReductionType> ReductionOperation {
45      get { return (ValueParameter<ReductionType>)Parameters["ReductionOperation"]; }
46    }
47    public ValueParameter<TargetOperationType> TargetOperation {
48      get { return (ValueParameter<TargetOperationType>)Parameters["TargetOperation"]; }
49    }
50    #endregion
51
52    [StorableConstructor]
53    private IntDataReducer(bool deserializing) : base(deserializing) { }
54    private IntDataReducer(IntDataReducer original, Cloner cloner)
55      : base(original, cloner) {
56    }
57    public IntDataReducer()
58      : base() {
59      #region Create parameters
60      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("ParameterToReduce", "The parameter on which the reduction operation should be applied."));
61      Parameters.Add(new ValueLookupParameter<StringValue>("TargetParameterName", "Parameter to store the result in.", new StringValue(string.Empty)));
62      Parameters.Add(new LookupParameter<IntValue>("TargetParameter", "The actual parameter defined by TargetParameterName."));
63      Parameters.Add(new ValueParameter<ReductionType>("ReductionOperation", "The reduction operation to perform."));
64      Parameters.Add(new ValueParameter<TargetOperationType>("TargetOperation", "The target operation to perform."));
65      #endregion
66    }
67
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new IntDataReducer(this, cloner);
70    }
71
72    public override IOperation Apply() {
73      var intValues = ParameterToReduce.ActualValue;
74
75      if (intValues.Count() > 0) {
76        TargetParameter.ActualName = TargetParameterName.ActualValue.Value;
77        if (TargetParameter.ActualValue == null) TargetParameter.ActualValue = new IntValue();
78
79        int result = 1;
80        switch (ReductionOperation.Value.Value) {
81          case ReductionTypes.Sum:
82            result = intValues.Sum(x => x.Value);
83            break;
84          case ReductionTypes.Prod:
85            intValues.ForEach(x => result *= x.Value);
86            break;
87          case ReductionTypes.Avg:
88            result = (int)Math.Round(intValues.Average(x => x.Value));
89            break;
90          case ReductionTypes.Min:
91            result = intValues.Min(x => x.Value);
92            break;
93          case ReductionTypes.Max:
94            result = intValues.Max(x => x.Value);
95            break;
96        }
97
98        switch (TargetOperation.Value.Value) {
99          case TargetOperationTypes.Set:
100            TargetParameter.ActualValue.Value = result;
101            break;
102          case TargetOperationTypes.Sum:
103            TargetParameter.ActualValue.Value += result;
104            break;
105          case TargetOperationTypes.Prod:
106            if (TargetParameter.ActualValue.Value == 0) TargetParameter.ActualValue.Value = 1;
107            TargetParameter.ActualValue.Value *= result;
108            break;
109        }
110      }
111      return base.Apply();
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.