1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Diagnostics;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Threading;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.Benchmarks {
|
---|
33 | [Item("Whetstone Algorithm", "Whetstone benchmarking algorithm.")]
|
---|
34 | [StorableClass]
|
---|
35 | public class WhetstoneBenchmark : IBenchmark {
|
---|
36 | [Storable]
|
---|
37 | private byte[][] chunk;
|
---|
38 | public byte[][] ChunkData {
|
---|
39 | get { return chunk; }
|
---|
40 | set { chunk = value; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | [Storable]
|
---|
44 | private TimeSpan timeLimit;
|
---|
45 | public TimeSpan TimeLimit {
|
---|
46 | get { return timeLimit; }
|
---|
47 | set { timeLimit = value; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | private bool stopBenchmark;
|
---|
51 |
|
---|
52 | private CancellationToken cancellationToken;
|
---|
53 |
|
---|
54 | public string ItemName {
|
---|
55 | get { return ItemAttribute.GetName(this.GetType()); }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public string ItemDescription {
|
---|
59 | get { return ItemAttribute.GetDescription(this.GetType()); }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public Version ItemVersion {
|
---|
63 | get { return ItemAttribute.GetVersion(this.GetType()); }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public Image ItemImage {
|
---|
67 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | #region Benchmark Fields
|
---|
71 |
|
---|
72 | private long begin_time;
|
---|
73 | private long end_time;
|
---|
74 |
|
---|
75 | private int ITERATIONS;
|
---|
76 | private int numberOfCycles;
|
---|
77 | private int cycleNo;
|
---|
78 | private double x1, x2, x3, x4, x, y, t, t1, t2;
|
---|
79 | private double[] z = new double[1];
|
---|
80 | private double[] e1 = new double[4];
|
---|
81 | private int i, j, k, l, n1, n2, n3, n4, n6, n7, n8, n9, n10, n11;
|
---|
82 |
|
---|
83 | #endregion
|
---|
84 |
|
---|
85 | #region Costructors
|
---|
86 |
|
---|
87 | [StorableConstructor]
|
---|
88 | public WhetstoneBenchmark(bool deserializing) { }
|
---|
89 |
|
---|
90 | public WhetstoneBenchmark() { }
|
---|
91 |
|
---|
92 | protected WhetstoneBenchmark(WhetstoneBenchmark original, Cloner cloner) {
|
---|
93 | cloner.RegisterClonedObject(original, this);
|
---|
94 | }
|
---|
95 |
|
---|
96 | #endregion
|
---|
97 |
|
---|
98 | #region Whetstone Benchmark
|
---|
99 | // implementation based on Java version: www.aicas.com/download/Whetstone.java
|
---|
100 |
|
---|
101 | public void Run(CancellationToken token, ResultCollection results) {
|
---|
102 | cancellationToken = token;
|
---|
103 | stopBenchmark = false;
|
---|
104 |
|
---|
105 | ITERATIONS = 100; // ITERATIONS / 10 = Millions Whetstone instructions
|
---|
106 |
|
---|
107 | numberOfCycles = 100;
|
---|
108 | int defaultNumberOfRuns = 10;
|
---|
109 | float elapsedTime = 0;
|
---|
110 | float meanTime = 0;
|
---|
111 | float rating = 0;
|
---|
112 | float meanRating = 0;
|
---|
113 | int intRating = 0;
|
---|
114 |
|
---|
115 | long runNumber = 1;
|
---|
116 | Stopwatch sw = new Stopwatch();
|
---|
117 | sw.Start();
|
---|
118 |
|
---|
119 | while (!stopBenchmark) {
|
---|
120 | elapsedTime = (float)(MainCalc() / 1000);
|
---|
121 | meanTime = meanTime + (elapsedTime * 1000 / numberOfCycles);
|
---|
122 | rating = (1000 * numberOfCycles) / elapsedTime;
|
---|
123 | meanRating = meanRating + rating;
|
---|
124 | intRating = (int)rating;
|
---|
125 | numberOfCycles += 10;
|
---|
126 |
|
---|
127 | if (cancellationToken.IsCancellationRequested) {
|
---|
128 | throw new OperationCanceledException(cancellationToken);
|
---|
129 | }
|
---|
130 |
|
---|
131 | if ((timeLimit == null) || (timeLimit.TotalMilliseconds == 0)) {
|
---|
132 | if (runNumber > defaultNumberOfRuns) {
|
---|
133 | stopBenchmark = true;
|
---|
134 | }
|
---|
135 | } else if (sw.Elapsed > timeLimit) {
|
---|
136 | stopBenchmark = true;
|
---|
137 | }
|
---|
138 |
|
---|
139 | runNumber++;
|
---|
140 | }
|
---|
141 | sw.Stop();
|
---|
142 | meanTime = meanTime / runNumber;
|
---|
143 | meanRating = meanRating / runNumber;
|
---|
144 | intRating = (int)meanRating;
|
---|
145 |
|
---|
146 | results.Add(new Result("MWIPS", new IntValue(intRating / 1000)));
|
---|
147 | }
|
---|
148 |
|
---|
149 | private double MainCalc() {
|
---|
150 | // initialize constants
|
---|
151 | t = 0.499975;
|
---|
152 | t1 = 0.50025;
|
---|
153 | t2 = 2.0;
|
---|
154 |
|
---|
155 | // set values of module weights
|
---|
156 | n1 = 0 * ITERATIONS;
|
---|
157 | n2 = 12 * ITERATIONS;
|
---|
158 | n3 = 14 * ITERATIONS;
|
---|
159 | n4 = 345 * ITERATIONS;
|
---|
160 | n6 = 210 * ITERATIONS;
|
---|
161 | n7 = 32 * ITERATIONS;
|
---|
162 | n8 = 899 * ITERATIONS;
|
---|
163 | n9 = 616 * ITERATIONS;
|
---|
164 | n10 = 0 * ITERATIONS;
|
---|
165 | n11 = 93 * ITERATIONS;
|
---|
166 |
|
---|
167 | begin_time = DateTime.Now.Ticks / 10000; // get ms
|
---|
168 |
|
---|
169 | for (cycleNo = 1; cycleNo <= numberOfCycles; cycleNo++) {
|
---|
170 | /* MODULE 1: simple identifiers */
|
---|
171 | x1 = 1.0;
|
---|
172 | x2 = x3 = x4 = -1.0;
|
---|
173 | for (i = 1; i <= n1; i += 1) {
|
---|
174 | x1 = (x1 + x2 + x3 - x4) * t;
|
---|
175 | x2 = (x1 + x2 - x3 + x4) * t; // correction: x2 = ( x1 + x2 - x3 - x4 ) * t;
|
---|
176 | x3 = (x1 - x2 + x3 + x4) * t; // correction: x3 = ( x1 - x2 + x3 + x4 ) * t;
|
---|
177 | x4 = (-x1 + x2 + x3 + x4) * t;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /* MODULE 2: array elements */
|
---|
181 | e1[0] = 1.0;
|
---|
182 | e1[1] = e1[2] = e1[3] = -1.0;
|
---|
183 | for (i = 1; i <= n2; i += 1) {
|
---|
184 | e1[0] = (e1[0] + e1[1] + e1[2] - e1[3]) * t;
|
---|
185 | e1[1] = (e1[0] + e1[1] - e1[2] + e1[3]) * t;
|
---|
186 | e1[2] = (e1[0] - e1[1] + e1[2] + e1[3]) * t;
|
---|
187 | e1[3] = (-e1[0] + e1[1] + e1[2] + e1[3]) * t;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /* MODULE 3: array as parameter */
|
---|
191 | for (i = 1; i <= n3; i += 1)
|
---|
192 | pa(e1);
|
---|
193 |
|
---|
194 | /* MODULE 4: conditional jumps */
|
---|
195 | j = 1;
|
---|
196 | for (i = 1; i <= n4; i += 1) {
|
---|
197 | if (j == 1)
|
---|
198 | j = 2;
|
---|
199 | else
|
---|
200 | j = 3;
|
---|
201 | if (j > 2)
|
---|
202 | j = 0;
|
---|
203 | else
|
---|
204 | j = 1;
|
---|
205 | if (j < 1)
|
---|
206 | j = 1;
|
---|
207 | else
|
---|
208 | j = 0;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /* MODULE 5: omitted */
|
---|
212 |
|
---|
213 | /* MODULE 6: integer arithmetic */
|
---|
214 | j = 1;
|
---|
215 | k = 2;
|
---|
216 | l = 3;
|
---|
217 | for (i = 1; i <= n6; i += 1) {
|
---|
218 | j = j * (k - j) * (l - k);
|
---|
219 | k = l * k - (l - j) * k;
|
---|
220 | l = (l - k) * (k + j);
|
---|
221 | e1[l - 2] = j + k + l; /* C arrays are zero based */
|
---|
222 | e1[k - 2] = j * k * l;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /* MODULE 7: trig. functions */
|
---|
226 | x = y = 0.5;
|
---|
227 | for (i = 1; i <= n7; i += 1) {
|
---|
228 | x = t * Math.Atan(t2 * Math.Sin(x) * Math.Cos(x) / (Math.Cos(x + y) + Math.Cos(x - y) - 1.0));
|
---|
229 | y = t * Math.Atan(t2 * Math.Sin(y) * Math.Cos(y) / (Math.Cos(x + y) + Math.Cos(x - y) - 1.0));
|
---|
230 | }
|
---|
231 |
|
---|
232 | /* MODULE 8: procedure calls */
|
---|
233 | x = y = z[0] = 1.0;
|
---|
234 | for (i = 1; i <= n8; i += 1)
|
---|
235 | p3(x, y, z);
|
---|
236 |
|
---|
237 | /* MODULE9: array references */
|
---|
238 | j = 0;
|
---|
239 | k = 1;
|
---|
240 | l = 2;
|
---|
241 | e1[0] = 1.0;
|
---|
242 | e1[1] = 2.0;
|
---|
243 | e1[2] = 3.0;
|
---|
244 | for (i = 1; i <= n9; i++)
|
---|
245 | p0();
|
---|
246 |
|
---|
247 | /* MODULE10: integer arithmetic */
|
---|
248 | j = 2;
|
---|
249 | k = 3;
|
---|
250 | for (i = 1; i <= n10; i += 1) {
|
---|
251 | j = j + k;
|
---|
252 | k = j + k;
|
---|
253 | j = k - j;
|
---|
254 | k = k - j - j;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /* MODULE11: standard functions */
|
---|
258 | x = 0.75;
|
---|
259 | for (i = 1; i <= n11; i += 1)
|
---|
260 | x = Math.Sqrt(Math.Exp(Math.Log(x) / t1));
|
---|
261 | } /* for */
|
---|
262 |
|
---|
263 | end_time = DateTime.Now.Ticks / 10000; // get ms
|
---|
264 |
|
---|
265 | return (end_time - begin_time);
|
---|
266 | }
|
---|
267 |
|
---|
268 | public void pa(double[] e) {
|
---|
269 | int j;
|
---|
270 | j = 0;
|
---|
271 | do {
|
---|
272 | e[0] = (e[0] + e[1] + e[2] - e[3]) * t;
|
---|
273 | e[1] = (e[0] + e[1] - e[2] + e[3]) * t;
|
---|
274 | e[2] = (e[0] - e[1] + e[2] + e[3]) * t;
|
---|
275 | e[3] = (-e[0] + e[1] + e[2] + e[3]) / t2;
|
---|
276 | j += 1;
|
---|
277 | }
|
---|
278 | while (j < 6);
|
---|
279 | }
|
---|
280 |
|
---|
281 | public void p3(double x, double y, double[] z) {
|
---|
282 | x = t * (x + y);
|
---|
283 | y = t * (x + y);
|
---|
284 | z[0] = (x + y) / t2;
|
---|
285 | }
|
---|
286 |
|
---|
287 | public void p0() {
|
---|
288 | e1[j] = e1[k];
|
---|
289 | e1[k] = e1[l];
|
---|
290 | e1[l] = e1[j];
|
---|
291 | }
|
---|
292 |
|
---|
293 | #endregion
|
---|
294 |
|
---|
295 | #region Clone
|
---|
296 |
|
---|
297 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
298 | return new WhetstoneBenchmark(this, cloner);
|
---|
299 | }
|
---|
300 |
|
---|
301 | public object Clone() {
|
---|
302 | return Clone(new Cloner());
|
---|
303 | }
|
---|
304 |
|
---|
305 | #endregion
|
---|
306 |
|
---|
307 | #region Events
|
---|
308 |
|
---|
309 | public event EventHandler ItemImageChanged;
|
---|
310 | protected virtual void OnItemImageChanged() {
|
---|
311 | EventHandler handler = ItemImageChanged;
|
---|
312 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
313 | }
|
---|
314 |
|
---|
315 | public event EventHandler ToStringChanged;
|
---|
316 | protected virtual void OnToStringChanged() {
|
---|
317 | EventHandler handler = ToStringChanged;
|
---|
318 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
319 | }
|
---|
320 |
|
---|
321 | #endregion
|
---|
322 | }
|
---|
323 | }
|
---|