1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.RealVector {
|
---|
29 | /// <summary>
|
---|
30 | /// Checks if all elements of a real vector are inside a given minimum and maximum value.
|
---|
31 | /// If not, the elements are corrected.
|
---|
32 | /// </summary>
|
---|
33 | public class BoundsChecker : OperatorBase {
|
---|
34 | /// <inheritdoc select="summary"/>
|
---|
35 | public override string Description {
|
---|
36 | get { return "Checks if all elements of a real vector are inside a given minimum and maximum value. If not, elements are corrected."; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Initializes a new instance of <see cref="BoundsChecker"/> with three variable infos
|
---|
41 | /// (<c>RealVector</c>, <c>Minimum</c> and <c>Maximum</c>).
|
---|
42 | /// </summary>
|
---|
43 | public BoundsChecker()
|
---|
44 | : base() {
|
---|
45 | AddVariableInfo(new VariableInfo("RealVector", "Real vector to check", typeof(DoubleArrayData), VariableKind.In | VariableKind.Out));
|
---|
46 | AddVariableInfo(new VariableInfo("Minimum", "Minimum value of each vector element (included).", typeof(DoubleData), VariableKind.In));
|
---|
47 | AddVariableInfo(new VariableInfo("Maximum", "Maximum value of each vector element (included).", typeof(DoubleData), VariableKind.In));
|
---|
48 | }
|
---|
49 |
|
---|
50 | /// <summary>
|
---|
51 | /// Checks if all elements of the given <paramref name="vector"/> are inside the given minimum
|
---|
52 | /// and maximum value and if not they are corrected.
|
---|
53 | /// </summary>
|
---|
54 | /// <param name="min">The minimum value of the range (inclusive).</param>
|
---|
55 | /// <param name="max">The maximum value of the range (inclusive).</param>
|
---|
56 | /// <param name="vector">The vector to check.</param>
|
---|
57 | /// <returns>The corrected real vector.</returns>
|
---|
58 | public static double[] Apply(double min, double max, double[] vector) {
|
---|
59 | int length = vector.Length;
|
---|
60 | double[] result = (double[])vector.Clone();
|
---|
61 |
|
---|
62 | for (int i = 0; i < length; i++) {
|
---|
63 | if (result[i] < min) result[i] = min;
|
---|
64 | if (result[i] > max) result[i] = max;
|
---|
65 | }
|
---|
66 | return result;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /// <summary>
|
---|
70 | /// Checks if all elements of the given <paramref name="vector"/> are inside the given minimum
|
---|
71 | /// and maximum value and if not they are corrected.
|
---|
72 | /// </summary>
|
---|
73 | /// <remarks>Calls <see cref="Apply(double, double, double[])"/>.</remarks>
|
---|
74 | /// <param name="scope">The current scope.</param>
|
---|
75 | /// <returns><c>null</c>.</returns>
|
---|
76 | public override IOperation Apply(IScope scope) {
|
---|
77 | DoubleArrayData vector = GetVariableValue<DoubleArrayData>("RealVector", scope, false);
|
---|
78 | double min = GetVariableValue<DoubleData>("Minimum", scope, true).Data;
|
---|
79 | double max = GetVariableValue<DoubleData>("Maximum", scope, true).Data;
|
---|
80 | vector.Data = Apply(min, max, vector.Data);
|
---|
81 | return null;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|