Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Benchmark Generator/HeuristicLab.BenchmarkGenerator/3.3/CustomLanguageRuntime.cs @ 16375

Last change on this file since 16375 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: 2.3 KB
Line 
1using System;
2using System.Linq;
3using System.Linq.Expressions;
4using Irony.Interpreter;
5using Irony.Parsing;
6
7namespace HeuristicLab.BenchmarkGenerator {
8  public class CustomLanguageRuntime : LanguageRuntime {
9    public CustomLanguageRuntime(LanguageData data)
10      : base(data) {
11    }
12
13    protected override void InitOperatorImplementations() {
14      base.InitOperatorImplementations();
15      AddBinary(ExpressionType.AddChecked, typeof(Object[]), (x, y) => ComponentSum((object[])x, (object[])y));
16      AddBinary(ExpressionType.SubtractChecked, typeof(Object[]), (x, y) => ComponentDifference((object[])x, (object[])y));
17      AddBinary(ExpressionType.MultiplyChecked, typeof(Object[]), (x, y) => ComponentProduct((object[])x, (object[])y));
18      AddBinary(ExpressionType.Divide, typeof(Object[]), (x, y) => ComponentDivision((object[])x, (object[])y));
19      //      AddBinary(ExpressionType.Multiply, typeof(DoubleArray), (x, y) => (DoubleArray)x * (DoubleArray)y);
20      //      AddBinary(ExpressionType.Add, typeof(DoubleArray), (x, y) => (DoubleArray)x + (DoubleArray)y);
21      //      AddBinary(ExpressionType.Divide, typeof(DoubleArray), (x, y) => (DoubleArray)x / (DoubleArray)y);
22      //      AddBinary(ExpressionType.Subtract, typeof(DoubleArray), (x, y) => (DoubleArray)x - (DoubleArray)y);
23      //      AddConverter(typeof(double[]), typeof(DoubleArray), x => new DoubleArray((double[])x));
24    }
25
26    private object[] ComponentProduct(object[] a, object[] b) {
27      var aa = a.Cast<double>();
28      var bb = b.Cast<double>();
29      return aa.Zip(bb, (v1, v2) => v1 * v2).Cast<object>().ToArray();
30    }
31
32    private object[] ComponentDivision(object[] a, object[] b) {
33      var aa = a.Cast<double>();
34      var bb = b.Cast<double>();
35      return aa.Zip(bb, (v1, v2) => v1 / v2).Cast<object>().ToArray();
36    }
37
38    private object[] ComponentSum(object[] a, object[] b) {
39      var aa = a.Cast<double>();
40      var bb = b.Cast<double>();
41      return aa.Zip(bb, (v1, v2) => v1 + v2).Cast<object>().ToArray();
42    }
43
44    private object[] ComponentDifference(object[] a, object[] b) {
45      var aa = a.Cast<double>();
46      var bb = b.Cast<double>();
47      return aa.Zip(bb, (v1, v2) => v1 - v2).Cast<object>().ToArray();
48    }
49  }
50}
Note: See TracBrowser for help on using the repository browser.