Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged cloning refactoring branch back into trunk (#922)

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