[3054] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3054] | 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 |
|
---|
[3376] | 22 | using HeuristicLab.Common;
|
---|
[3054] | 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
| 28 | [StorableClass]
|
---|
| 29 | [Item("RealVector", "Represents a vector of real values.")]
|
---|
| 30 | public class RealVector : DoubleArray {
|
---|
[4722] | 31 | [StorableConstructor]
|
---|
| 32 | protected RealVector(bool deserializing) : base(deserializing) { }
|
---|
| 33 | protected RealVector(RealVector original, Cloner cloner) : base(original, cloner) { }
|
---|
[3054] | 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) {
|
---|
[14650] | 43 | for (int i = 0; i < historyArray.Length; i++)
|
---|
| 44 | historyArray[i] = elements[i];
|
---|
[3054] | 45 | }
|
---|
| 46 |
|
---|
| 47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 48 | return new RealVector(this, cloner);
|
---|
[3054] | 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++)
|
---|
[14650] | 55 | historyArray[startIndex + i] = min + delta * random.NextDouble();
|
---|
[3054] | 56 | OnReset();
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
[3123] | 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];
|
---|
[14650] | 65 | historyArray[i] = min + (max - min) * random.NextDouble();
|
---|
[3123] | 66 | }
|
---|
| 67 | OnReset();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[3054] | 71 | public void Randomize(IRandom random, double min, double max) {
|
---|
| 72 | Randomize(random, 0, Length, min, max);
|
---|
| 73 | }
|
---|
[3123] | 74 |
|
---|
| 75 | public void Randomize(IRandom random, DoubleMatrix bounds) {
|
---|
| 76 | Randomize(random, 0, Length, bounds);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[3054] | 79 | }
|
---|
| 80 | }
|
---|