Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.RealVector/BoundsChecker.cs @ 75

Last change on this file since 75 was 75, checked in by swagner, 16 years ago

Fixed ticket #65

  • added operator BoundsChecker
File size: 1.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6
7namespace 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}
Note: See TracBrowser for help on using the repository browser.