Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HLScript/HeuristicLab.Tests/TestRandom.cs @ 12341

Last change on this file since 12341 was 10324, checked in by gkronber, 11 years ago

#2106: changed methods for sorting in ObservableArray and ObservableList to use a stable sort (via Enumerable.OrderBy()). This is implemented as extension methods in HeuristicLab.Common. This implementation requires additional memory O(n).
The unit tests for tabu search had to be updated as the stable sort changes the results of the sample.
(minor bug fix in TestRandom)

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26
27namespace HeuristicLab.Tests {
28  public class TestRandom : IRandom {
29    #region Variables and Properties
30    private int[] intNumbers;
31    public int[] IntNumbers {
32      get { return intNumbers; }
33      set {
34        if (value == null) intNumbers = new int[0];
35        else intNumbers = value;
36      }
37    }
38    private int nextIntIndex;
39    private double[] doubleNumbers;
40    public double[] DoubleNumbers {
41      get { return doubleNumbers; }
42      set {
43        if (value == null) doubleNumbers = new double[0];
44        else doubleNumbers = value;
45      }
46    }
47    private int nextDoubleIndex;
48    #endregion
49
50    public TestRandom() {
51      intNumbers = new int[0];
52      doubleNumbers = new double[0];
53      nextIntIndex = 0;
54      nextDoubleIndex = 0;
55    }
56
57    protected TestRandom(TestRandom original, Cloner cloner) {
58      this.intNumbers = original.intNumbers.ToArray();
59      this.doubleNumbers = original.doubleNumbers.ToArray();
60    }
61
62    public TestRandom(int[] intNumbers, double[] doubleNumbers) {
63      if (intNumbers == null) this.intNumbers = new int[0];
64      else this.intNumbers = intNumbers;
65      if (doubleNumbers == null) this.doubleNumbers = new double[0];
66      else this.doubleNumbers = doubleNumbers;
67      nextIntIndex = 0;
68      nextDoubleIndex = 0;
69    }
70
71    #region IRandom Members
72
73    public void Reset() {
74      nextIntIndex = 0;
75      nextDoubleIndex = 0;
76    }
77
78    public void Reset(int seed) {
79      throw new NotImplementedException();
80    }
81
82    public int Next() {
83      if (nextIntIndex >= intNumbers.Length) throw new InvalidOperationException("Random: No more integer random numbers available");
84      return intNumbers[nextIntIndex++];
85    }
86
87    public int Next(int maxVal) {
88      if (nextIntIndex >= intNumbers.Length) throw new InvalidOperationException("Random: No more integer random numbers available");
89      if (IntNumbers[nextIntIndex] >= maxVal) throw new InvalidOperationException("Random: Next integer random number (" + IntNumbers[nextIntIndex] + ") is >= " + maxVal);
90      return intNumbers[nextIntIndex++];
91    }
92
93    public int Next(int minVal, int maxVal) {
94      if (nextIntIndex >= intNumbers.Length) throw new InvalidOperationException("Random: No more integer random numbers available");
95      if (IntNumbers[nextIntIndex] < minVal || IntNumbers[nextIntIndex] >= maxVal) throw new InvalidOperationException("Random: Next integer random number (" + IntNumbers[nextIntIndex] + ") is not in the range [" + minVal + ";" + maxVal + ")");
96      return intNumbers[nextIntIndex++];
97    }
98
99    public double NextDouble() {
100      if (nextDoubleIndex >= doubleNumbers.Length) throw new InvalidOperationException("Random: No more double random numbers available");
101      if (doubleNumbers[nextDoubleIndex] < 0.0 || doubleNumbers[nextDoubleIndex] >= 1.0) throw new InvalidOperationException("Random: Next double ranomd number (" + DoubleNumbers[nextDoubleIndex] + ") is not in the range [0;1)");
102      return doubleNumbers[nextDoubleIndex++];
103    }
104
105    #endregion
106
107    #region IItem Members
108
109    public string ItemName {
110      get { throw new NotImplementedException(); }
111    }
112
113    public string ItemDescription {
114      get { throw new NotImplementedException(); }
115    }
116
117    public Version ItemVersion {
118      get { throw new NotImplementedException(); }
119    }
120
121    public System.Drawing.Image ItemImage {
122      get { throw new NotImplementedException(); }
123    }
124
125#pragma warning disable 67
126    public event EventHandler ItemImageChanged;
127    public event EventHandler ToStringChanged;
128#pragma warning restore 67
129    #endregion
130
131    #region IDeepCloneable Members
132
133    public IDeepCloneable Clone(Cloner cloner) {
134      return new TestRandom(this, cloner);
135    }
136
137    #endregion
138
139    #region ICloneable Members
140
141    public object Clone() {
142      throw new NotImplementedException();
143    }
144
145    #endregion
146  }
147}
Note: See TracBrowser for help on using the repository browser.