[5381] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16662] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5381] | 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 HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
[16662] | 28 | using HEAL.Attic;
|
---|
[5381] | 29 |
|
---|
| 30 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
| 31 | /// <summary>
|
---|
[5423] | 32 | /// Checks if all elements of a real vector are inside the bounds.
|
---|
| 33 | /// If not, the elements are mirrored at the bounds.
|
---|
[5381] | 34 | /// </summary>
|
---|
| 35 | [Item("ReflectiveBoundsChecker", "Checks if all elements of a real vector are inside the bounds. If not, elements are mirrored at the bounds.")]
|
---|
[16662] | 36 | [StorableType("9F043D1C-1F6F-4E53-95F5-ECDBD7C7FF90")]
|
---|
[5381] | 37 | public class ReflectiveBoundsChecker : SingleSuccessorOperator, IRealVectorBoundsChecker {
|
---|
| 38 | public LookupParameter<RealVector> RealVectorParameter {
|
---|
| 39 | get { return (LookupParameter<RealVector>)Parameters["RealVector"]; }
|
---|
| 40 | }
|
---|
| 41 | public ValueLookupParameter<DoubleMatrix> BoundsParameter {
|
---|
| 42 | get { return (ValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | [StorableConstructor]
|
---|
[16662] | 46 | protected ReflectiveBoundsChecker(StorableConstructorFlag _) : base(_) { }
|
---|
[5381] | 47 | protected ReflectiveBoundsChecker(ReflectiveBoundsChecker original, Cloner cloner) : base(original, cloner) { }
|
---|
| 48 | /// <summary>
|
---|
[5423] | 49 | /// Initializes a new instance of <see cref="ReflectiveBoundsChecker"/> with two parameters
|
---|
[5381] | 50 | /// (<c>RealVector</c>, <c>Bounds</c>).
|
---|
| 51 | /// </summary>
|
---|
| 52 | public ReflectiveBoundsChecker()
|
---|
| 53 | : base() {
|
---|
| 54 | Parameters.Add(new LookupParameter<RealVector>("RealVector", "The real-valued vector for which the bounds should be checked."));
|
---|
| 55 | 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."));
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 59 | return new ReflectiveBoundsChecker(this, cloner);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /// <summary>
|
---|
[5423] | 63 | /// Checks if all elements of the given <paramref name="vector"/> are inside the bounds and if not, they are mirrored at the bounds.
|
---|
[5381] | 64 | /// </summary>
|
---|
| 65 | /// <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>
|
---|
| 66 | /// <param name="vector">The vector to check.</param>
|
---|
| 67 | /// <returns>The corrected real vector.</returns>
|
---|
| 68 | public static void Apply(RealVector vector, DoubleMatrix bounds) {
|
---|
| 69 | for (int i = 0; i < vector.Length; i++) {
|
---|
| 70 | double min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1];
|
---|
| 71 | if (vector[i] < min) {
|
---|
| 72 | int reflectionCount = (int)Math.Truncate((min - vector[i]) / (max - min)) + 1;
|
---|
| 73 | double reflection = (min - vector[i]) % (max - min);
|
---|
| 74 | if (IsOdd(reflectionCount)) vector[i] = min + reflection;
|
---|
| 75 | else vector[i] = max - reflection;
|
---|
| 76 | }
|
---|
| 77 | if (vector[i] > max) {
|
---|
| 78 | int reflectionCount = (int)Math.Truncate((vector[i] - max) / (max - min)) + 1;
|
---|
| 79 | double reflection = (vector[i] - max) % (max - min);
|
---|
| 80 | if (IsOdd(reflectionCount)) vector[i] = max - reflection;
|
---|
| 81 | else vector[i] = min + reflection;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | private static bool IsOdd(int number) {
|
---|
| 87 | return number % 2 == 1;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /// <summary>
|
---|
[5423] | 91 | /// Checks if all elements of the given <paramref name="vector"/> are inside the bounds and if not, they are mirrored at the bounds.
|
---|
[5381] | 92 | /// </summary>
|
---|
| 93 | /// <exception cref="InvalidOperationException">Thrown when either vector or bounds could not be found.</exception>
|
---|
| 94 | /// <remarks>Calls <see cref="Apply(RealVector, DoubleMatrix)"/>.</remarks>
|
---|
| 95 | /// <inheritdoc select="returns" />
|
---|
| 96 | public override IOperation Apply() {
|
---|
| 97 | if (RealVectorParameter.ActualValue == null) throw new InvalidOperationException("ReflectiveBoundsChecker: Parameter " + RealVectorParameter.ActualName + " could not be found.");
|
---|
| 98 | if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("ReflectiveBoundsChecker: Parameter " + BoundsParameter.ActualName + " could not be found.");
|
---|
| 99 | Apply(RealVectorParameter.ActualValue, BoundsParameter.ActualValue);
|
---|
| 100 | return base.Apply();
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|