enhanced motor handling, bug fixes, initial optimization code
[debian/openrocket] / src / net / sf / openrocket / utils / TestFunctionOptimizerLoop.java
1 package net.sf.openrocket.utils;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.concurrent.ArrayBlockingQueue;
6 import java.util.concurrent.ExecutorService;
7 import java.util.concurrent.ThreadPoolExecutor;
8 import java.util.concurrent.TimeUnit;
9
10 import net.sf.openrocket.optimization.Function;
11 import net.sf.openrocket.optimization.FunctionOptimizer;
12 import net.sf.openrocket.optimization.MultidirectionalSearchOptimizer;
13 import net.sf.openrocket.optimization.OptimizationController;
14 import net.sf.openrocket.optimization.ParallelExecutorCache;
15 import net.sf.openrocket.optimization.Point;
16 import net.sf.openrocket.util.MathUtil;
17
18
19 public class TestFunctionOptimizerLoop {
20         
21         private static final double PRECISION = 0.01;
22         
23         private Point optimum;
24         private int stepCount = 0;
25         private int evaluations = 0;
26         
27         
28
29         private void go(final FunctionOptimizer optimizer, final Point optimum, final int maxSteps, ExecutorService executor) {
30                 
31                 Function function = new Function() {
32                         @Override
33                         public double evaluate(Point p) throws InterruptedException {
34                                 evaluations++;
35                                 return p.sub(optimum).length2();
36                         }
37                         
38                         @Override
39                         public double preComputed(Point p) {
40                                 for (double d : p.asArray()) {
41                                         if (d < 0 || d > 1)
42                                                 return Double.MAX_VALUE;
43                                 }
44                                 return Double.NaN;
45                         }
46                 };
47                 
48                 OptimizationController control = new OptimizationController() {
49                         
50                         @Override
51                         public boolean stepTaken(Point oldPoint, double oldValue, Point newPoint, double newValue, double stepSize) {
52                                 stepCount++;
53                                 if (stepCount % 1000 == 0) {
54                                         System.err.println("WARNING: Over " + stepCount + " steps required for optimum=" + optimum +
55                                                                 " position=" + newPoint);
56                                 }
57                                 double distance = newPoint.sub(optimum).length();
58                                 return distance >= PRECISION;
59                         }
60                 };
61                 ;
62                 
63                 ParallelExecutorCache cache = new ParallelExecutorCache(executor);
64                 cache.setFunction(function);
65                 optimizer.setFunctionCache(cache);
66                 optimizer.optimize(new Point(optimum.dim(), 0.5), control);
67         }
68         
69         
70         public static void main(String[] args) {
71                 
72                 System.err.println("PRECISION = " + PRECISION);
73                 
74                 ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100));
75                 
76                 for (int dim = 1; dim <= 10; dim++) {
77                         
78                         List<Integer> stepCount = new ArrayList<Integer>();
79                         List<Integer> functionCount = new ArrayList<Integer>();
80                         
81                         MultidirectionalSearchOptimizer optimizer = new MultidirectionalSearchOptimizer();
82                         for (int count = 0; count < 200; count++) {
83                                 TestFunctionOptimizerLoop test = new TestFunctionOptimizerLoop();
84                                 double[] point = new double[dim];
85                                 for (int i = 0; i < dim; i++) {
86                                         point[i] = Math.random();
87                                 }
88                                 //                              point[0] = 0.7;
89                                 test.go(optimizer, new Point(point), 20, executor);
90                                 stepCount.add(test.stepCount);
91                                 functionCount.add(test.evaluations);
92                         }
93                         
94                         //                      System.err.println("StepCount = " + stepCount);
95                         
96                         System.out.printf("dim=%d  Steps avg=%5.2f dev=%5.2f median=%.1f  " +
97                                         "Evaluations avg=%5.2f dev=%5.2f median=%.1f\n",
98                                         dim, MathUtil.average(stepCount), MathUtil.stddev(stepCount), MathUtil.median(stepCount),
99                                         MathUtil.average(functionCount), MathUtil.stddev(functionCount), MathUtil.median(functionCount));
100                         System.out.println("stat: " + optimizer.getStatistics());
101                         
102                 }
103                 
104                 executor.shutdownNow();
105         }
106         
107
108
109 }