Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/BoundsChecker.cs @ 4165

Last change on this file since 4165 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 4.0 KB
Line 
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
22using System;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.RealVectorEncoding {
30  /// <summary>
31  /// Checks if all elements of a real vector are inside the bounds.
32  /// If not, the elements are corrected.
33  /// </summary>
34  [Item("BoundsChecker", "Checks if all elements of a real vector are inside the bounds. If not, elements are corrected.")]
35  [StorableClass]
36  public class BoundsChecker : SingleSuccessorOperator, IRealVectorBoundsChecker {
37    public LookupParameter<RealVector> RealVectorParameter {
38      get { return (LookupParameter<RealVector>)Parameters["RealVector"]; }
39    }
40    public ValueLookupParameter<DoubleMatrix> BoundsParameter {
41      get { return (ValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
42    }
43
44    /// <summary>
45    /// Initializes a new instance of <see cref="BoundsChecker"/> with two parameters
46    /// (<c>RealVector</c>, <c>Bounds</c>).
47    /// </summary>
48    public BoundsChecker()
49      : base() {
50      Parameters.Add(new LookupParameter<RealVector>("RealVector", "The real-valued vector for which the bounds should be checked."));
51      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled."));
52    }
53
54    /// <summary>
55    /// Checks if all elements of the given <paramref name="vector"/> are inside the bounds and if not they are corrected.
56    /// </summary>
57    /// <param name="bounds">The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.</param>
58    /// <param name="vector">The vector to check.</param>
59    /// <returns>The corrected real vector.</returns>
60    public static void Apply(RealVector vector, DoubleMatrix bounds) {
61      for (int i = 0; i < vector.Length; i++) {
62        double min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1];
63        if (vector[i] < min) vector[i] = min;
64        if (vector[i] > max) vector[i] = max;
65      }
66    }
67
68    /// <summary>
69    /// Checks if all elements of the given <paramref name="vector"/> are inside the bounds and if not they are corrected.
70    /// </summary>
71    /// <exception cref="InvalidOperationException">Thrown when either vector or bounds could not be found.</exception>
72    /// <remarks>Calls <see cref="Apply(RealVector, DoubleMatrix)"/>.</remarks>
73    /// <inheritdoc select="returns" />
74    public override IOperation Apply() {
75      if (RealVectorParameter.ActualValue == null) throw new InvalidOperationException("BoundsChecker: Parameter " + RealVectorParameter.ActualName + " could not be found.");
76      if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("BoundsChecker: Parameter " + BoundsParameter.ActualName + " could not be found.");
77      Apply(RealVectorParameter.ActualValue, BoundsParameter.ActualValue);
78      return base.Apply();
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.