Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVector.cs @ 3160

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

Removed Creatable test attribute (#935).

File size: 2.7 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 HeuristicLab.Core;
23using HeuristicLab.Data;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25
26namespace HeuristicLab.Encodings.RealVectorEncoding {
27  [StorableClass]
28  [Item("RealVector", "Represents a vector of real values.")]
29  public class RealVector : DoubleArray {
30    public RealVector() : base() { }
31    public RealVector(int length) : base(length) { }
32    public RealVector(int length, IRandom random, double min, double max)
33      : this(length) {
34      Randomize(random, min, max);
35    }
36    public RealVector(double[] elements) : base(elements) { }
37    public RealVector(DoubleArray elements)
38      : this(elements.Length) {
39      for (int i = 0; i < array.Length; i++)
40        array[i] = elements[i];
41    }
42
43    public override IDeepCloneable Clone(Cloner cloner) {
44      RealVector clone = new RealVector(array);
45      cloner.RegisterClonedObject(this, clone);
46      return clone;
47    }
48
49    public virtual void Randomize(IRandom random, int startIndex, int length, double min, double max) {
50      double delta = max - min;
51      if (length > 0) {
52        for (int i = 0; i < length; i++)
53          array[startIndex + i] = min + delta * random.NextDouble();
54        OnReset();
55      }
56    }
57
58    public virtual void Randomize(IRandom random, int startIndex, int length, DoubleMatrix bounds) {
59      if (length > 0) {
60        for (int i = startIndex; i < startIndex + length; i++) {
61          double min = bounds[i % bounds.Rows, 0];
62          double max = bounds[i % bounds.Rows, 1];
63          array[i] = min + (max - min) * random.NextDouble();
64        }
65        OnReset();
66      }
67    }
68
69    public void Randomize(IRandom random, double min, double max) {
70      Randomize(random, 0, Length, min, max);
71    }
72
73    public void Randomize(IRandom random, DoubleMatrix bounds) {
74      Randomize(random, 0, Length, bounds);
75    }
76
77  }
78}
Note: See TracBrowser for help on using the repository browser.