1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Algorithms.ALPS {
|
---|
32 | [Item("WeightingReducer", "An operator that combines two values based on the weight of the lower (0.0) and higher (1.0) value.")]
|
---|
33 | [StorableClass]
|
---|
34 | public sealed class WeightingReducer : SingleSuccessorOperator {
|
---|
35 |
|
---|
36 | public IScopeTreeLookupParameter<DoubleValue> ParameterToReduce {
|
---|
37 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["ParameterToReduce"]; }
|
---|
38 | }
|
---|
39 | public ILookupParameter<DoubleValue> TargetParameter {
|
---|
40 | get { return (ILookupParameter<DoubleValue>)Parameters["TargetParameter"]; }
|
---|
41 | }
|
---|
42 | public IValueLookupParameter<DoubleValue> WeightParameter {
|
---|
43 | get { return (IValueLookupParameter<DoubleValue>)Parameters["Weight"]; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [StorableConstructor]
|
---|
47 | private WeightingReducer(bool deserializing) : base(deserializing) { }
|
---|
48 | private WeightingReducer(WeightingReducer original, Cloner cloner)
|
---|
49 | : base(original, cloner) {
|
---|
50 | }
|
---|
51 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
52 | return new WeightingReducer(this, cloner);
|
---|
53 | }
|
---|
54 | public WeightingReducer()
|
---|
55 | : base() {
|
---|
56 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParameterToReduce", "The parameter on which the weighting should be applied."));
|
---|
57 | Parameters.Add(new LookupParameter<DoubleValue>("TargetParameter", "The target variable in which the weighted value should be stored."));
|
---|
58 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Weight", "Weight of the two values (0.0 for the smaller, 1.0 for the larger)."));
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override IOperation Apply() {
|
---|
62 | var values = ParameterToReduce.ActualValue;
|
---|
63 | if (values.Length != 2)
|
---|
64 | throw new InvalidOperationException("Weighting between values can only done for two values.");
|
---|
65 |
|
---|
66 | double weight = WeightParameter.ActualValue.Value;
|
---|
67 |
|
---|
68 | double max = values.Max(v => v.Value);
|
---|
69 | double min = values.Min(v => v.Value);
|
---|
70 |
|
---|
71 | double result = max * weight + min * (1 - weight);
|
---|
72 |
|
---|
73 | if (TargetParameter.ActualValue == null)
|
---|
74 | TargetParameter.ActualValue = new DoubleValue(result);
|
---|
75 | else TargetParameter.ActualValue.Value = result;
|
---|
76 |
|
---|
77 | return base.Apply();
|
---|
78 | }
|
---|
79 |
|
---|
80 | }
|
---|
81 | } |
---|