Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Benchmark Generator/HeuristicLab.BenchmarkGenerator/3.3/Functions.cs @ 12450

Last change on this file since 12450 was 10088, checked in by bburlacu, 11 years ago

#2083: Removed old project and rewritten the Benchmark Generator as a HeuristicLab plugin. Added new functions and visualization capability.

File size: 5.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Core;
5using HeuristicLab.Problems.Instances.DataAnalysis;
6using HeuristicLab.Random;
7using Irony.Interpreter;
8
9namespace HeuristicLab.BenchmarkGenerator {
10  public static class Functions {
11    private static readonly IRandom Random = new MersenneTwister();
12
13    public static void SetSeed(int seed) { Random.Reset(seed); }
14
15    public static object NormalDouble(ScriptThread thread, object[] args) {
16      if (args.Length != 2) throw new ArgumentException("The NormalDouble function accepts exactly two arguments.");
17      var mu = (double)args[0];
18      var sigma = (double)args[1];
19      return NormalDouble(mu, sigma);
20    }
21
22    public static object UniformDouble(ScriptThread thread, object[] args) {
23      if (args.Length != 2) throw new ArgumentException("The UniformDouble function accepts exactly two arguments.");
24      var a = (double)args[0];
25      var b = (double)args[1];
26      return UniformDouble(a, b);
27    }
28
29    public static object NormalDistribution(ScriptThread thread, object[] args) {
30      if (args.Length != 3) throw new ArgumentException("The dnormal function accepts exactly three arguments.");
31      var n = (int)(double)args[0];
32      var start = (double)args[1];
33      var end = (double)args[2];
34      return NormalDistribution(n, start, end).Cast<object>().ToArray();
35    }
36
37    public static object UniformDistribution(ScriptThread thread, object[] args) {
38      if (args.Length != 3) throw new ArgumentException("The duniform function accepts exactly three arguments.");
39      var n = (int)(double)args[0];
40      var start = (double)args[1];
41      var end = (double)args[2];
42      return UniformDistribution(n, start, end).Cast<object>().ToArray();
43    }
44
45    public static object[] Steps(ScriptThread thread, object[] args) {
46      if (args.Length != 3) throw new ArgumentException("The Steps function accepts exactly three arguments.");
47      var start = (double)args[0];
48      var end = (double)args[1];
49      var step = (double)args[2];
50      return Steps(start, end, step).Cast<object>().ToArray();
51    }
52
53    public static object[] Sin(ScriptThread thread, object[] args) {
54      if (args.Length != 1) throw new ArgumentException("The Sin function accepts exactly one argument.");
55      var values = (object[])args[0];
56      return values.Cast<double>().Select(Math.Sin).Cast<object>().ToArray();
57    }
58
59    public static object[] Cos(ScriptThread thread, object[] args) {
60      if (args.Length != 1) throw new ArgumentException("The Cos function accepts exactly one argument.");
61      var values = (object[])args[0];
62      return values.Cast<double>().Select(Math.Cos).Cast<object>().ToArray();
63    }
64
65    public static object Length(ScriptThread thread, object[] args) {
66      if (args.Length != 1) throw new ArgumentException("The Length function accepts exactly one argument which must be an object array.");
67      var values = (object[])args[0];
68      return (double)values.Length;
69    }
70
71    public static object DotProduct(ScriptThread thread, object[] args) {
72      if (args.Length != 2) throw new ArgumentException("The DotProduct function accepts exactly two arguments which must be object arrays of the same length.");
73      var a = (object[])args[0];
74      var b = (object[])args[1];
75      return DotProduct(a.Cast<double>(), b.Cast<double>());
76    }
77
78    public static object[] Abs(ScriptThread thread, object[] args) {
79      if (args.Length != 1) throw new ArgumentException("The Length function accepts exactly one argument which must be an object array.");
80      var values = (object[])args[0];
81      return values.Cast<double>().Select(Math.Abs).Cast<object>().ToArray();
82    }
83
84    public static object[] Repeat(ScriptThread thread, object[] args) {
85      if (args.Length != 2) throw new ArgumentException("The Repeat function accepts exactly two arguments.");
86      var n = (int)(double)args[0];
87      var v = (double)args[1];
88      return Enumerable.Repeat(v, n).Cast<object>().ToArray();
89    }
90
91    #region convenience methods for less typing
92    private static double UniformDouble(double min, double max) {
93      return UniformDistributedRandom.NextDouble(Random, min, max);
94    }
95    private static double NormalDouble(double mu, double sigma) {
96      return NormalDistributedRandom.NextDouble(Random, mu, sigma);
97    }
98    private static IEnumerable<double> Steps(double start, double end, double step) {
99      return ValueGenerator.GenerateSteps(start, end, step);
100    }
101    private static IEnumerable<double> UniformDistribution(int n, double start, double end) {
102      return ValueGenerator.GenerateUniformDistributedValues(n, start, end);
103    }
104    private static IEnumerable<double> NormalDistribution(int n, double start, double end) {
105      return ValueGenerator.GenerateNormalDistributedValues(n, start, end);
106    }
107    private static double DotProduct(IEnumerable<double> a, IEnumerable<double> b) {
108      return a.Zip(b, (x, y) => x * y).Sum();
109    }
110    #endregion
111  }
112}
Note: See TracBrowser for help on using the repository browser.