-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBenchmarkGriewank.java
More file actions
61 lines (51 loc) · 1.61 KB
/
BenchmarkGriewank.java
File metadata and controls
61 lines (51 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package symjava.examples;
import symjava.bytecode.BytecodeBatchFunc;
import symjava.math.SymMath;
import symjava.symbolic.Expr;
import symjava.symbolic.Symbol;
import symjava.symbolic.Symbols;
import symjava.symbolic.utils.AddList;
import symjava.symbolic.utils.JIT;
import symjava.symbolic.utils.MulList;
//http://www.sfu.ca/~ssurjano/griewank.html
public class BenchmarkGriewank {
public static void main(String[] args) {
test();
}
public static void test() {
// TODO Auto-generated method stub
int N = 50;
long start;
Symbol i = new Symbol("i");
Symbols xi = new Symbols("x", i);
start = System.currentTimeMillis();
AddList term1 = new AddList();
for (int j = 0; j < N; j++) {
term1.add(xi.get(j) * xi.get(j) / 4000.0);
}
MulList term2 = new MulList();
for (int j = 0; j < N; j++) {
term2.add(SymMath.cos(xi.get(j) / Math.sqrt(j + 1)));
}
Expr griewank = term1.toExpr() - term2.toExpr() + 1.0;
//System.out.println(griewank);
Expr[] grad = new Expr[N];
for(int j=0;j<N;j++)
grad[j] = griewank.diff(xi.get(j));
long symbolTime = System.currentTimeMillis() - start;
Expr[] args = xi.get(0, N);
start = System.currentTimeMillis();
BytecodeBatchFunc f = JIT.compileBatchFunc(args, grad);
long compileTime = System.currentTimeMillis() - start;
double[] outAry = new double[N];
double[] params = new double[N];
for(int j=0; j<N; j++) {
params[j] = 0.1;
}
f.apply(outAry, 0, params);
for(int j=0;j<N;j++)
System.out.println(outAry[j]);
System.out.println("symbol time: "+symbolTime/1000.0);
System.out.println("compile time: "+compileTime/1000.0);
}
}