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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Operators {
|
---|
31 | [Item("IntDataReducer", "An operator to reduce int values of sub scopes")]
|
---|
32 | [StorableClass]
|
---|
33 | public sealed class IntDataReducer : SingleSuccessorOperator {
|
---|
34 | public ScopeTreeLookupParameter<IntValue> ParameterToReduce {
|
---|
35 | get { return (ScopeTreeLookupParameter<IntValue>)Parameters["ParameterToReduce"]; }
|
---|
36 | }
|
---|
37 | public ValueLookupParameter<StringValue> TargetParameterName {
|
---|
38 | get { return (ValueLookupParameter<StringValue>)Parameters["TargetParameterName"]; }
|
---|
39 | }
|
---|
40 | private LookupParameter<IntValue> TargetParameter {
|
---|
41 | get { return (LookupParameter<IntValue>)Parameters["TargetParameter"]; }
|
---|
42 | }
|
---|
43 | public ValueParameter<ReductionType> ReductionOperation {
|
---|
44 | get { return (ValueParameter<ReductionType>)Parameters["ReductionOperation"]; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | [StorableConstructor]
|
---|
48 | private IntDataReducer(bool deserializing) : base(deserializing) { }
|
---|
49 | private IntDataReducer(IntDataReducer original, Cloner cloner)
|
---|
50 | : base(original, cloner) {
|
---|
51 | }
|
---|
52 | public IntDataReducer()
|
---|
53 | : base() {
|
---|
54 | Parameters.Add(new ScopeTreeLookupParameter<IntValue>("ParameterToReduce", "The actual parameter defined by TargetParameterName."));
|
---|
55 | Parameters.Add(new ValueLookupParameter<StringValue>("TargetParameterName", "Parameter name to add the value to.", new StringValue(string.Empty)));
|
---|
56 | Parameters.Add(new LookupParameter<IntValue>("TargetParameter", "The actual parameter defined by TargetParameterName."));
|
---|
57 | Parameters.Add(new ValueParameter<ReductionType>("ReductionOperation", "The reduction operation to perform."));
|
---|
58 | }
|
---|
59 |
|
---|
60 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
61 | return new IntDataReducer(this, cloner);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override IOperation Apply() {
|
---|
65 | var intValues = ParameterToReduce.ActualValue;
|
---|
66 |
|
---|
67 | if (intValues.Count() > 0) {
|
---|
68 | TargetParameter.ActualName = TargetParameterName.ActualValue.Value;
|
---|
69 | if (TargetParameter.ActualValue == null) TargetParameter.ActualValue = new IntValue();
|
---|
70 |
|
---|
71 | int result;
|
---|
72 | switch (ReductionOperation.Value.Value) {
|
---|
73 | case ReductionTypes.Sum:
|
---|
74 | result = intValues.Sum(x => x.Value);
|
---|
75 | break;
|
---|
76 | case ReductionTypes.Prod:
|
---|
77 | result = 1;
|
---|
78 | intValues.ForEach(x => result *= x.Value);
|
---|
79 | break;
|
---|
80 | case ReductionTypes.Avg:
|
---|
81 | result = (int)Math.Round(intValues.Average(x => x.Value));
|
---|
82 | break;
|
---|
83 | case ReductionTypes.Min:
|
---|
84 | result = intValues.Min(x => x.Value);
|
---|
85 | break;
|
---|
86 | case ReductionTypes.Max:
|
---|
87 | result = intValues.Max(x => x.Value);
|
---|
88 | break;
|
---|
89 | default:
|
---|
90 | throw new ArgumentException("Unknown ReductionType: " + ReductionOperation.Value.Value);
|
---|
91 | }
|
---|
92 | //TODO: there should be at least a Set, Add and Mul operation
|
---|
93 | TargetParameter.ActualValue.Value += result;
|
---|
94 | }
|
---|
95 | return base.Apply();
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|