Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/IntegerVector.cs @ 15529

Last change on this file since 15529 was 13364, checked in by mkommend, 9 years ago

#2521: Refactored IntegerVectorEncoding, KnapsackProblem, and P3.

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