Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2708_ScopedAlgorithms/HeuristicLab.Algorithms.ALPS/3.3/WeightingReducer.cs @ 18242

Last change on this file since 18242 was 13127, checked in by pfleck, 9 years ago

#2269

  • Added some missing wiring.
  • Unified some parameter properties.
  • Removed some operators.
File size: 3.2 KB
Line 
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
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace 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    public IScopeTreeLookupParameter<DoubleValue> ParameterToReduce {
36      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["ParameterToReduce"]; }
37    }
38    public ILookupParameter<DoubleValue> TargetParameter {
39      get { return (ILookupParameter<DoubleValue>)Parameters["TargetParameter"]; }
40    }
41    public IValueLookupParameter<DoubleValue> WeightParameter {
42      get { return (IValueLookupParameter<DoubleValue>)Parameters["Weight"]; }
43    }
44
45    [StorableConstructor]
46    private WeightingReducer(bool deserializing) : base(deserializing) { }
47    private WeightingReducer(WeightingReducer original, Cloner cloner)
48      : base(original, cloner) {
49    }
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new WeightingReducer(this, cloner);
52    }
53    public WeightingReducer()
54      : base() {
55      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParameterToReduce", "The parameter on which the weighting should be applied."));
56      Parameters.Add(new LookupParameter<DoubleValue>("TargetParameter", "The target variable in which the weighted value should be stored."));
57      Parameters.Add(new ValueLookupParameter<DoubleValue>("Weight", "Weight of the two values (0.0 for the smaller, 1.0 for the larger)."));
58    }
59
60    public override IOperation Apply() {
61      var values = ParameterToReduce.ActualValue;
62      if (values.Length != 2)
63        throw new InvalidOperationException("Weighting between values can only done for two values.");
64
65      double weight = WeightParameter.ActualValue.Value;
66
67      double max = values.Max(v => v.Value);
68      double min = values.Min(v => v.Value);
69
70      double result = max * weight + min * (1 - weight);
71
72      if (TargetParameter.ActualValue == null)
73        TargetParameter.ActualValue = new DoubleValue(result);
74      else TargetParameter.ActualValue.Value = result;
75
76      return base.Apply();
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.