1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022011 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  private TimeSpan timeLimit;


44  public TimeSpan TimeLimit {


45  get { return timeLimit; }


46  set { timeLimit = value; }


47  }


48 


49  private bool stopBenchmark;


50 


51  private CancellationToken cancellationToken;


52 


53  public string ItemName {


54  get { return ItemAttribute.GetName(this.GetType()); }


55  }


56 


57  public string ItemDescription {


58  get { return ItemAttribute.GetDescription(this.GetType()); }


59  }


60 


61  public Version ItemVersion {


62  get { return ItemAttribute.GetVersion(this.GetType()); }


63  }


64 


65  public Image ItemImage {


66  get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }


67  }


68 


69  #region Benchmark Fields


70 


71  private long begin_time;


72  private long end_time;


73 


74  private int ITERATIONS;


75  private int numberOfCycles;


76  private int cycleNo;


77  private double x1, x2, x3, x4, x, y, t, t1, t2;


78  private double[] z = new double[1];


79  private double[] e1 = new double[4];


80  private int i, j, k, l, n1, n2, n3, n4, n6, n7, n8, n9, n10, n11;


81 


82  #endregion


83 


84  #region Costructors


85 


86  public WhetstoneBenchmark() {


87  }


88 


89  private WhetstoneBenchmark(WhetstoneBenchmark original, Cloner cloner) {


90  cloner.RegisterClonedObject(original, this);


91  }


92 


93  #endregion


94 


95  #region Whetstone Benchmark


96  // implementation based on Java version: www.aicas.com/download/Whetstone.java


97 


98  public void Run(CancellationToken token, ResultCollection results) {


99  cancellationToken = token;


100  stopBenchmark = false;


101 


102  ITERATIONS = 100; // ITERATIONS / 10 = Millions Whetstone instructions


103 


104  numberOfCycles = 100;


105  int defaultNumberOfRuns = 10;


106  float elapsedTime = 0;


107  float meanTime = 0;


108  float rating = 0;


109  float meanRating = 0;


110  int intRating = 0;


111 


112  long runNumber = 1;


113  Stopwatch sw = new Stopwatch();


114  sw.Start();


115 


116  while (!stopBenchmark) {


117  elapsedTime = (float)(MainCalc() / 1000);


118  meanTime = meanTime + (elapsedTime * 1000 / numberOfCycles);


119  rating = (1000 * numberOfCycles) / elapsedTime;


120  meanRating = meanRating + rating;


121  intRating = (int)rating;


122  numberOfCycles += 10;


123 


124  if (cancellationToken.IsCancellationRequested) {


125  throw new OperationCanceledException(cancellationToken);


126  }


127 


128  if ((timeLimit == null)  (timeLimit.TotalMilliseconds == 0)) {


129  if (runNumber > defaultNumberOfRuns) {


130  stopBenchmark = true;


131  }


132  } else if (sw.Elapsed > timeLimit) {


133  stopBenchmark = true;


134  }


135 


136  runNumber++;


137  }


138  sw.Stop();


139  meanTime = meanTime / runNumber;


140  meanRating = meanRating / runNumber;


141  intRating = (int)meanRating;


142 


143  results.Add(new Result("MWIPS", new IntValue(intRating / 1000)));


144  }


145 


146  private double MainCalc() {


147  // initialize constants


148  t = 0.499975;


149  t1 = 0.50025;


150  t2 = 2.0;


151 


152  // set values of module weights


153  n1 = 0 * ITERATIONS;


154  n2 = 12 * ITERATIONS;


155  n3 = 14 * ITERATIONS;


156  n4 = 345 * ITERATIONS;


157  n6 = 210 * ITERATIONS;


158  n7 = 32 * ITERATIONS;


159  n8 = 899 * ITERATIONS;


160  n9 = 616 * ITERATIONS;


161  n10 = 0 * ITERATIONS;


162  n11 = 93 * ITERATIONS;


163 


164  begin_time = DateTime.Now.Ticks / 10000; // get ms


165 


166  for (cycleNo = 1; cycleNo <= numberOfCycles; cycleNo++) {


167  /* MODULE 1: simple identifiers */


168  x1 = 1.0;


169  x2 = x3 = x4 = 1.0;


170  for (i = 1; i <= n1; i += 1) {


171  x1 = (x1 + x2 + x3  x4) * t;


172  x2 = (x1 + x2  x3 + x4) * t; // correction: x2 = ( x1 + x2  x3  x4 ) * t;


173  x3 = (x1  x2 + x3 + x4) * t; // correction: x3 = ( x1  x2 + x3 + x4 ) * t;


174  x4 = (x1 + x2 + x3 + x4) * t;


175  }


176 


177  /* MODULE 2: array elements */


178  e1[0] = 1.0;


179  e1[1] = e1[2] = e1[3] = 1.0;


180  for (i = 1; i <= n2; i += 1) {


181  e1[0] = (e1[0] + e1[1] + e1[2]  e1[3]) * t;


182  e1[1] = (e1[0] + e1[1]  e1[2] + e1[3]) * t;


183  e1[2] = (e1[0]  e1[1] + e1[2] + e1[3]) * t;


184  e1[3] = (e1[0] + e1[1] + e1[2] + e1[3]) * t;


185  }


186 


187  /* MODULE 3: array as parameter */


188  for (i = 1; i <= n3; i += 1)


189  pa(e1);


190 


191  /* MODULE 4: conditional jumps */


192  j = 1;


193  for (i = 1; i <= n4; i += 1) {


194  if (j == 1)


195  j = 2;


196  else


197  j = 3;


198  if (j > 2)


199  j = 0;


200  else


201  j = 1;


202  if (j < 1)


203  j = 1;


204  else


205  j = 0;


206  }


207 


208  /* MODULE 5: omitted */


209 


210  /* MODULE 6: integer arithmetic */


211  j = 1;


212  k = 2;


213  l = 3;


214  for (i = 1; i <= n6; i += 1) {


215  j = j * (k  j) * (l  k);


216  k = l * k  (l  j) * k;


217  l = (l  k) * (k + j);


218  e1[l  2] = j + k + l; /* C arrays are zero based */


219  e1[k  2] = j * k * l;


220  }


221 


222  /* MODULE 7: trig. functions */


223  x = y = 0.5;


224  for (i = 1; i <= n7; i += 1) {


225  x = t * Math.Atan(t2 * Math.Sin(x) * Math.Cos(x) / (Math.Cos(x + y) + Math.Cos(x  y)  1.0));


226  y = t * Math.Atan(t2 * Math.Sin(y) * Math.Cos(y) / (Math.Cos(x + y) + Math.Cos(x  y)  1.0));


227  }


228 


229  /* MODULE 8: procedure calls */


230  x = y = z[0] = 1.0;


231  for (i = 1; i <= n8; i += 1)


232  p3(x, y, z);


233 


234  /* MODULE9: array references */


235  j = 0;


236  k = 1;


237  l = 2;


238  e1[0] = 1.0;


239  e1[1] = 2.0;


240  e1[2] = 3.0;


241  for (i = 1; i <= n9; i++)


242  p0();


243 


244  /* MODULE10: integer arithmetic */


245  j = 2;


246  k = 3;


247  for (i = 1; i <= n10; i += 1) {


248  j = j + k;


249  k = j + k;


250  j = k  j;


251  k = k  j  j;


252  }


253 


254  /* MODULE11: standard functions */


255  x = 0.75;


256  for (i = 1; i <= n11; i += 1)


257  x = Math.Sqrt(Math.Exp(Math.Log(x) / t1));


258  } /* for */


259 


260  end_time = DateTime.Now.Ticks / 10000; // get ms


261 


262  return (end_time  begin_time);


263  }


264 


265  public void pa(double[] e) {


266  int j;


267  j = 0;


268  do {


269  e[0] = (e[0] + e[1] + e[2]  e[3]) * t;


270  e[1] = (e[0] + e[1]  e[2] + e[3]) * t;


271  e[2] = (e[0]  e[1] + e[2] + e[3]) * t;


272  e[3] = (e[0] + e[1] + e[2] + e[3]) / t2;


273  j += 1;


274  }


275  while (j < 6);


276  }


277 


278  public void p3(double x, double y, double[] z) {


279  x = t * (x + y);


280  y = t * (x + y);


281  z[0] = (x + y) / t2;


282  }


283 


284  public void p0() {


285  e1[j] = e1[k];


286  e1[k] = e1[l];


287  e1[l] = e1[j];


288  }


289 


290  #endregion


291 


292  #region Clone


293 


294  public IDeepCloneable Clone(Cloner cloner) {


295  return new WhetstoneBenchmark(this, cloner);


296  }


297 


298  public object Clone() {


299  return Clone(new Cloner());


300  }


301 


302  #endregion


303 


304  #region Events


305 


306  public event EventHandler ItemImageChanged;


307  protected virtual void OnItemImageChanged() {


308  EventHandler handler = ItemImageChanged;


309  if (handler != null) handler(this, EventArgs.Empty);


310  }


311 


312  public event EventHandler ToStringChanged;


313  protected virtual void OnToStringChanged() {


314  EventHandler handler = ToStringChanged;


315  if (handler != null) handler(this, EventArgs.Empty);


316  }


317 


318  #endregion


319  }


320  }

