Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/IntegerVector.cs @ 17181

Last change on this file since 17181 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HEAL.Attic;
27
28namespace HeuristicLab.Encodings.IntegerVectorEncoding {
29  [StorableType("2DDD33C1-4732-478B-83B1-17226A8F2B61")]
30  [Item("IntegerVector", "Represents a vector of integer values.")]
31  public class IntegerVector : IntArray {
32    [StorableConstructor]
33    protected IntegerVector(StorableConstructorFlag _) : base(_) { }
34    protected IntegerVector(IntegerVector original, Cloner cloner) : base(original, cloner) { }
35    public IntegerVector() : base() { }
36    public IntegerVector(int length) : base(length) { }
37    public IntegerVector(int length, IRandom random, int min, int max)
38      : this(length) {
39      Randomize(random, min, max);
40    }
41    public IntegerVector(int[] elements) : base(elements) { }
42    public IntegerVector(IntArray elements)
43      : this(elements.Length) {
44      for (int i = 0; i < array.Length; i++)
45        array[i] = elements[i];
46    }
47
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new IntegerVector(this, cloner);
50    }
51
52    public virtual void Randomize(IRandom random, int startIndex, int length, int min, int max, int step = 1) {
53      if (length > 0) {
54        int numbers = (int)Math.Floor((max - min) / (double)step);
55        for (int i = startIndex; i < startIndex + length; i++) {
56          array[i] = random.Next(numbers) * step + min;
57        }
58        OnReset();
59      }
60    }
61    public virtual void Randomize(IRandom random, int startIndex, int length, IntMatrix bounds) {
62      if (length > 0) {
63        for (int i = startIndex; i < startIndex + length; i++) {
64          int min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1], step = 1;
65          if (bounds.Columns > 2) step = bounds[i % bounds.Rows, 2];
66          int numbers = (int)Math.Floor((max - min) / (double)step);
67          array[i] = random.Next(numbers) * step + min;
68        }
69        OnReset();
70      }
71    }
72    public void Randomize(IRandom random, int min, int max, int step = 1) {
73      Randomize(random, 0, Length, min, max, step);
74    }
75    public void Randomize(IRandom random, IntMatrix bounds) {
76      Randomize(random, 0, Length, bounds);
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.