a57ea78a71125577eac7075514575acbc9be0455
[debian/openrocket] / 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.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;
10
11
12
13
14 public class TestFunctionOptimizer {
15         
16         private static final int LOOP_COUNT = 1000000;
17         
18         private volatile int evaluations = 0;
19         private volatile int aborted = 0;
20         private volatile int stepCount = 0;
21         
22         
23
24         private void go(final ParallelFunctionCache functionCache,
25                         final FunctionOptimizer optimizer, final Point optimum, final int maxSteps) {
26                 
27                 Function function = new Function() {
28                         @Override
29                         public double evaluate(Point p) throws InterruptedException {
30                                 if (loop(LOOP_COUNT)) {
31                                         evaluations++;
32                                         return p.sub(optimum).length2();
33                                 } else {
34                                         aborted++;
35                                         return Double.NaN;
36                                 }
37                         }
38                         
39                         @Override
40                         public double preComputed(Point p) {
41                                 for (double d : p.asArray()) {
42                                         if (d < 0 || d > 1)
43                                                 return Double.MAX_VALUE;
44                                 }
45                                 return Double.NaN;
46                         }
47                 };
48                 
49                 OptimizationController control = new OptimizationController() {
50                         
51                         @Override
52                         public boolean stepTaken(Point oldPoint, double oldValue, Point newPoint, double newValue, double stepSize) {
53                                 stepCount++;
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;
58                         }
59                 };
60                 ;
61                 
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);
67         }
68         
69         
70         public static double counter;
71         
72         private static boolean loop(int count) {
73                 counter = 1.0;
74                 for (int i = 0; i < count; i++) {
75                         counter += Math.sin(counter);
76                         if (i % 1024 == 0) {
77                                 if (Thread.interrupted()) {
78                                         return false;
79                                 }
80                         }
81                 }
82                 return true;
83         }
84         
85         
86         public static void main(String[] args) throws InterruptedException {
87                 
88                 System.err.println("Number of processors: " + Runtime.getRuntime().availableProcessors());
89                 
90                 for (int i = 0; i < 20; i++) {
91                         long t0 = System.currentTimeMillis();
92                         loop(LOOP_COUNT);
93                         long t1 = System.currentTimeMillis();
94                         System.err.println("Loop delay at startup: " + (t1 - t0) + "ms");
95                 }
96                 System.err.println();
97                 
98                 for (int threadCount = 1; threadCount <= 10; threadCount++) {
99                         
100                         System.err.println("THREAD COUNT:  " + threadCount);
101                         TestFunctionOptimizer test = new TestFunctionOptimizer();
102                         
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();
108                         
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());
113                         
114                         executor.getExecutor().shutdownNow();
115                         Thread.sleep(1000);
116                         
117                         t0 = System.currentTimeMillis();
118                         loop(LOOP_COUNT);
119                         t1 = System.currentTimeMillis();
120                         System.err.println("Loop delay afterwards: " + (t1 - t0) + "ms");
121                         System.err.println();
122                 }
123         }
124         
125
126
127 }