1 package net.sf.openrocket.utils;
3 import net.sf.openrocket.optimization.general.Function;
4 import net.sf.openrocket.optimization.general.FunctionOptimizer;
5 import net.sf.openrocket.optimization.general.OptimizationController;
6 import net.sf.openrocket.optimization.general.ParallelExecutorCache;
7 import net.sf.openrocket.optimization.general.ParallelFunctionCache;
8 import net.sf.openrocket.optimization.general.Point;
9 import net.sf.openrocket.optimization.general.multidim.MultidirectionalSearchOptimizer;
14 public class TestFunctionOptimizer {
16 private static final int LOOP_COUNT = 1000000;
18 private volatile int evaluations = 0;
19 private volatile int aborted = 0;
20 private volatile int stepCount = 0;
24 private void go(final ParallelFunctionCache functionCache,
25 final FunctionOptimizer optimizer, final Point optimum, final int maxSteps) {
27 Function function = new Function() {
29 public double evaluate(Point p) throws InterruptedException {
30 if (loop(LOOP_COUNT)) {
32 return p.sub(optimum).length2();
40 public double preComputed(Point p) {
41 for (double d : p.asArray()) {
43 return Double.MAX_VALUE;
49 OptimizationController control = new OptimizationController() {
52 public boolean stepTaken(Point oldPoint, double oldValue, Point newPoint, double newValue, double stepSize) {
54 // System.out.println("CSV " + count + ", " + evaluations + ", " + newPoint.sub(optimum).length());
55 // System.out.println("Steps: " + count + " Function evaluations: " + evaluations);
56 // System.out.println("Distance: " + newPoint.sub(optimum).length() + " " + newPoint + " value=" + newValue);
57 return stepCount < maxSteps;
62 functionCache.setFunction(function);
63 optimizer.setFunctionCache(functionCache);
64 optimizer.optimize(new Point(optimum.dim(), 0.5), control);
65 System.err.println("Result: " + optimizer.getOptimumPoint() + " value=" + optimizer.getOptimumValue());
66 System.err.println("Steps: " + stepCount + " Evaluations: " + evaluations);
70 public static double counter;
72 private static boolean loop(int count) {
74 for (int i = 0; i < count; i++) {
75 counter += Math.sin(counter);
77 if (Thread.interrupted()) {
86 public static void main(String[] args) throws InterruptedException {
88 System.err.println("Number of processors: " + Runtime.getRuntime().availableProcessors());
90 for (int i = 0; i < 20; i++) {
91 long t0 = System.currentTimeMillis();
93 long t1 = System.currentTimeMillis();
94 System.err.println("Loop delay at startup: " + (t1 - t0) + "ms");
98 for (int threadCount = 1; threadCount <= 10; threadCount++) {
100 System.err.println("THREAD COUNT: " + threadCount);
101 TestFunctionOptimizer test = new TestFunctionOptimizer();
103 ParallelExecutorCache executor = new ParallelExecutorCache(threadCount);
104 MultidirectionalSearchOptimizer optimizer = new MultidirectionalSearchOptimizer();
105 long t0 = System.currentTimeMillis();
106 test.go(executor, optimizer, new Point(0.2, 0.3, 0.85), 30);
107 long t1 = System.currentTimeMillis();
109 System.err.println("Optimization took " + (t1 - t0) + "ms");
110 System.err.println("" + test.stepCount + " steps, " + test.evaluations +
111 " function evaluations, " + test.aborted + " aborted evaluations");
112 System.err.println("Statistics: " + optimizer.getStatistics());
114 executor.getExecutor().shutdownNow();
117 t0 = System.currentTimeMillis();
119 t1 = System.currentTimeMillis();
120 System.err.println("Loop delay afterwards: " + (t1 - t0) + "ms");
121 System.err.println();