1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.RealVector {
|
---|
8 | class BoundsChecker : OperatorBase {
|
---|
9 | public override string Description {
|
---|
10 | get { return "Checks if all elements of a real vector are inside a given minimum and maximum value. If not, elements are corrected."; }
|
---|
11 | }
|
---|
12 |
|
---|
13 | public BoundsChecker()
|
---|
14 | : base() {
|
---|
15 | AddVariableInfo(new VariableInfo("RealVector", "Real vector to check", typeof(DoubleArrayData), VariableKind.In | VariableKind.Out));
|
---|
16 | AddVariableInfo(new VariableInfo("Minimum", "Minimum value of each vector element (included).", typeof(DoubleData), VariableKind.In));
|
---|
17 | AddVariableInfo(new VariableInfo("Maximum", "Maximum value of each vector element (included).", typeof(DoubleData), VariableKind.In));
|
---|
18 | }
|
---|
19 |
|
---|
20 | public static double[] Apply(double min, double max, double[] vector) {
|
---|
21 | int length = vector.Length;
|
---|
22 | double[] result = (double[])vector.Clone();
|
---|
23 |
|
---|
24 | for (int i = 0; i < length; i++) {
|
---|
25 | if (result[i] < min) result[i] = min;
|
---|
26 | if (result[i] > max) result[i] = max;
|
---|
27 | }
|
---|
28 | return result;
|
---|
29 | }
|
---|
30 |
|
---|
31 | public override IOperation Apply(IScope scope) {
|
---|
32 | DoubleArrayData vector = GetVariableValue<DoubleArrayData>("RealVector", scope, false);
|
---|
33 | double min = GetVariableValue<DoubleData>("Minimum", scope, true).Data;
|
---|
34 | double max = GetVariableValue<DoubleData>("Maximum", scope, true).Data;
|
---|
35 | vector.Data = Apply(min, max, vector.Data);
|
---|
36 | return null;
|
---|
37 | }
|
---|
38 | }
|
---|
39 | }
|
---|