create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / utils / TestFunctionOptimizer.java
1 package net.sf.openrocket.utils;
2
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.OptimizationException;
7 import net.sf.openrocket.optimization.general.ParallelExecutorCache;
8 import net.sf.openrocket.optimization.general.ParallelFunctionCache;
9 import net.sf.openrocket.optimization.general.Point;
10 import net.sf.openrocket.optimization.general.multidim.MultidirectionalSearchOptimizer;
11
12
13
14
15 public class TestFunctionOptimizer {
16         
17         private static final int LOOP_COUNT = 1000000;
18         
19         private volatile int evaluations = 0;
20         private volatile int aborted = 0;
21         private volatile int stepCount = 0;
22         
23         
24
25         private void go(final ParallelFunctionCache functionCache,
26                         final FunctionOptimizer optimizer, final Point optimum, final int maxSteps) throws OptimizationException {
27                 
28                 Function function = new Function() {
29                         @Override
30                         public double evaluate(Point p) throws InterruptedException {
31                                 if (loop(LOOP_COUNT)) {
32                                         evaluations++;
33                                         return p.sub(optimum).length2();
34                                 } else {
35                                         aborted++;
36                                         return Double.NaN;
37                                 }
38                         }
39                 };
40                 
41                 OptimizationController control = new OptimizationController() {
42                         
43                         @Override
44                         public boolean stepTaken(Point oldPoint, double oldValue, Point newPoint, double newValue, double stepSize) {
45                                 stepCount++;
46                                 //                              System.out.println("CSV " + count + ", " + evaluations + ", " + newPoint.sub(optimum).length());
47                                 //                              System.out.println("Steps: " + count + "  Function evaluations: " + evaluations);
48                                 //                              System.out.println("Distance: " + newPoint.sub(optimum).length() + "   " + newPoint + "  value=" + newValue);
49                                 return stepCount < maxSteps;
50                         }
51                 };
52                 ;
53                 
54                 functionCache.setFunction(function);
55                 optimizer.setFunctionCache(functionCache);
56                 optimizer.optimize(new Point(optimum.dim(), 0.5), control);
57                 System.err.println("Result: " + optimizer.getOptimumPoint() + "  value=" + optimizer.getOptimumValue());
58                 System.err.println("Steps: " + stepCount + " Evaluations: " + evaluations);
59         }
60         
61         
62         public static double counter;
63         
64         private static boolean loop(int count) {
65                 counter = 1.0;
66                 for (int i = 0; i < count; i++) {
67                         counter += Math.sin(counter);
68                         if (i % 1024 == 0) {
69                                 if (Thread.interrupted()) {
70                                         return false;
71                                 }
72                         }
73                 }
74                 return true;
75         }
76         
77         
78         public static void main(String[] args) throws InterruptedException, OptimizationException {
79                 
80                 System.err.println("Number of processors: " + Runtime.getRuntime().availableProcessors());
81                 
82                 for (int i = 0; i < 20; i++) {
83                         long t0 = System.currentTimeMillis();
84                         loop(LOOP_COUNT);
85                         long t1 = System.currentTimeMillis();
86                         System.err.println("Loop delay at startup: " + (t1 - t0) + "ms");
87                 }
88                 System.err.println();
89                 
90                 for (int threadCount = 1; threadCount <= 10; threadCount++) {
91                         
92                         System.err.println("THREAD COUNT:  " + threadCount);
93                         TestFunctionOptimizer test = new TestFunctionOptimizer();
94                         
95                         ParallelExecutorCache executor = new ParallelExecutorCache(threadCount);
96                         MultidirectionalSearchOptimizer optimizer = new MultidirectionalSearchOptimizer();
97                         long t0 = System.currentTimeMillis();
98                         test.go(executor, optimizer, new Point(0.2, 0.3, 0.85), 30);
99                         long t1 = System.currentTimeMillis();
100                         
101                         System.err.println("Optimization took " + (t1 - t0) + "ms");
102                         System.err.println("" + test.stepCount + " steps, " + test.evaluations +
103                                         " function evaluations, " + test.aborted + " aborted evaluations");
104                         System.err.println("Statistics: " + optimizer.getStatistics());
105                         
106                         executor.getExecutor().shutdownNow();
107                         Thread.sleep(1000);
108                         
109                         t0 = System.currentTimeMillis();
110                         loop(LOOP_COUNT);
111                         t1 = System.currentTimeMillis();
112                         System.err.println("Loop delay afterwards: " + (t1 - t0) + "ms");
113                         System.err.println();
114                 }
115         }
116         
117
118
119 }