Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.ALPS/3.3/WeightingReducer.cs @ 17456

Last change on this file since 17456 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.2 KB
RevLine 
[12997]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12997]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;
[17097]29using HEAL.Attic;
[12997]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.")]
[17097]33  [StorableType("A2B65E34-61EC-414A-827C-AAA3F1A1E46C")]
[12997]34  public sealed class WeightingReducer : SingleSuccessorOperator {
[13046]35    public IScopeTreeLookupParameter<DoubleValue> ParameterToReduce {
36      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["ParameterToReduce"]; }
[12997]37    }
[13046]38    public ILookupParameter<DoubleValue> TargetParameter {
39      get { return (ILookupParameter<DoubleValue>)Parameters["TargetParameter"]; }
[12997]40    }
41    public IValueLookupParameter<DoubleValue> WeightParameter {
42      get { return (IValueLookupParameter<DoubleValue>)Parameters["Weight"]; }
43    }
44
45    [StorableConstructor]
[17097]46    private WeightingReducer(StorableConstructorFlag _) : base(_) { }
[12997]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() {
[13046]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."));
[12997]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
[13046]65      double weight = WeightParameter.ActualValue.Value;
[12997]66
[13046]67      double max = values.Max(v => v.Value);
68      double min = values.Min(v => v.Value);
[12997]69
[13046]70      double result = max * weight + min * (1 - weight);
[12997]71
72      if (TargetParameter.ActualValue == null)
[13046]73        TargetParameter.ActualValue = new DoubleValue(result);
[12997]74      else TargetParameter.ActualValue.Value = result;
75
76      return base.Apply();
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.