1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 HeuristicLab.Core;
|
---|
23 | using HeuristicLab.Data;
|
---|
24 | using HeuristicLab.Operators;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using System;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Encodings.RealVector {
|
---|
30 | /// <summary>
|
---|
31 | /// Checks if all elements of a real vector are inside a given minimum and maximum value.
|
---|
32 | /// If not, the elements are corrected.
|
---|
33 | /// </summary>
|
---|
34 | [Item("BoundsChecker", "Checks if all elements of a real vector are inside a given minimum and maximum value. If not, elements are corrected.")]
|
---|
35 | [StorableClass(StorableClassType.Empty)]
|
---|
36 | public class BoundsChecker : SingleSuccessorOperator {
|
---|
37 | public LookupParameter<DoubleArrayData> RealVectorParameter {
|
---|
38 | get { return (LookupParameter<DoubleArrayData>)Parameters["RealVector"]; }
|
---|
39 | }
|
---|
40 | public ValueLookupParameter<DoubleData> MinimumParameter {
|
---|
41 | get { return (ValueLookupParameter<DoubleData>)Parameters["Minimum"]; }
|
---|
42 | }
|
---|
43 | public ValueLookupParameter<DoubleData> MaximumParameter {
|
---|
44 | get { return (ValueLookupParameter<DoubleData>)Parameters["Maximum"]; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | /// <summary>
|
---|
48 | /// Initializes a new instance of <see cref="BoundsChecker"/> with three parameters
|
---|
49 | /// (<c>RealVector</c>, <c>Minimum</c> and <c>Maximum</c>).
|
---|
50 | /// </summary>
|
---|
51 | public BoundsChecker()
|
---|
52 | : base() {
|
---|
53 | Parameters.Add(new LookupParameter<DoubleArrayData>("RealVector", "The real-valued vector for which the bounds should be checked."));
|
---|
54 | Parameters.Add(new ValueLookupParameter<DoubleData>("Minimum", "The lower bound for each element in the vector."));
|
---|
55 | Parameters.Add(new ValueLookupParameter<DoubleData>("Maximum", "The upper bound for each element in the vector."));
|
---|
56 | }
|
---|
57 |
|
---|
58 | /// <summary>
|
---|
59 | /// Checks if all elements of the given <paramref name="vector"/> are inside the given minimum
|
---|
60 | /// and maximum value and if not they are corrected.
|
---|
61 | /// </summary>
|
---|
62 | /// <param name="min">The minimum value of the range (inclusive).</param>
|
---|
63 | /// <param name="max">The maximum value of the range (inclusive).</param>
|
---|
64 | /// <param name="vector">The vector to check.</param>
|
---|
65 | /// <returns>The corrected real vector.</returns>
|
---|
66 | public static void Apply(DoubleArrayData vector, DoubleData min, DoubleData max) {
|
---|
67 | for (int i = 0; i < vector.Length; i++) {
|
---|
68 | if (vector[i] < min.Value) vector[i] = min.Value;
|
---|
69 | if (vector[i] > max.Value) vector[i] = max.Value;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | /// <summary>
|
---|
74 | /// Checks if all elements of the given <paramref name="vector"/> are inside the given minimum
|
---|
75 | /// and maximum value and if not they are corrected.
|
---|
76 | /// </summary>
|
---|
77 | /// <exception cref="InvalidOperationException">Thrown when one of the three parameters (vector, minimum, or maximum) could not be found.</exception>
|
---|
78 | /// <remarks>Calls <see cref="Apply(double, double, double[])"/>.</remarks>
|
---|
79 | /// <inheritdoc select="returns" />
|
---|
80 | public override IOperation Apply() {
|
---|
81 | if (RealVectorParameter.ActualValue == null) throw new InvalidOperationException("BoundsChecker: Parameter " + RealVectorParameter.ActualName + " could not be found.");
|
---|
82 | if (MinimumParameter.ActualValue == null) throw new InvalidOperationException("BoundsChecker: Parameter " + MinimumParameter.ActualName + " could not be found.");
|
---|
83 | if (MaximumParameter.ActualValue == null) throw new InvalidOperationException("BoundsChecker: Parameter " + MaximumParameter.ActualName + " could not be found.");
|
---|
84 | Apply(RealVectorParameter.ActualValue, MinimumParameter.ActualValue, MaximumParameter.ActualValue);
|
---|
85 | return base.Apply();
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|