[12997] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 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 |
|
---|
| 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;
|
---|
[14927] | 29 | using HeuristicLab.Persistence;
|
---|
[12997] | 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.")]
|
---|
[14927] | 33 | [StorableType("ae74d494-0f32-4684-97a2-382d56fc827a")]
|
---|
[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]
|
---|
[15018] | 46 | private WeightingReducer(StorableConstructorFlag deserializing) : base(deserializing) { }
|
---|
[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 | } |
---|