1 package net.sf.openrocket.utils;
3 import java.util.ArrayList;
5 import java.util.concurrent.ArrayBlockingQueue;
6 import java.util.concurrent.ExecutorService;
7 import java.util.concurrent.ThreadPoolExecutor;
8 import java.util.concurrent.TimeUnit;
10 import net.sf.openrocket.optimization.general.Function;
11 import net.sf.openrocket.optimization.general.FunctionOptimizer;
12 import net.sf.openrocket.optimization.general.OptimizationController;
13 import net.sf.openrocket.optimization.general.ParallelExecutorCache;
14 import net.sf.openrocket.optimization.general.Point;
15 import net.sf.openrocket.optimization.general.multidim.MultidirectionalSearchOptimizer;
16 import net.sf.openrocket.util.MathUtil;
19 public class TestFunctionOptimizerLoop {
21 private static final double PRECISION = 0.01;
23 private Point optimum;
24 private int stepCount = 0;
25 private int evaluations = 0;
29 private void go(final FunctionOptimizer optimizer, final Point optimum, final int maxSteps, ExecutorService executor) {
31 Function function = new Function() {
33 public double evaluate(Point p) throws InterruptedException {
35 return p.sub(optimum).length2();
39 public double preComputed(Point p) {
40 for (double d : p.asArray()) {
42 return Double.MAX_VALUE;
48 OptimizationController control = new OptimizationController() {
51 public boolean stepTaken(Point oldPoint, double oldValue, Point newPoint, double newValue, double stepSize) {
53 if (stepCount % 1000 == 0) {
54 System.err.println("WARNING: Over " + stepCount + " steps required for optimum=" + optimum +
55 " position=" + newPoint);
57 double distance = newPoint.sub(optimum).length();
58 return distance >= PRECISION;
63 ParallelExecutorCache cache = new ParallelExecutorCache(executor);
64 cache.setFunction(function);
65 optimizer.setFunctionCache(cache);
66 optimizer.optimize(new Point(optimum.dim(), 0.5), control);
70 public static void main(String[] args) {
72 System.err.println("PRECISION = " + PRECISION);
74 ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100));
76 for (int dim = 1; dim <= 10; dim++) {
78 List<Integer> stepCount = new ArrayList<Integer>();
79 List<Integer> functionCount = new ArrayList<Integer>();
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();
89 test.go(optimizer, new Point(point), 20, executor);
90 stepCount.add(test.stepCount);
91 functionCount.add(test.evaluations);
94 // System.err.println("StepCount = " + stepCount);
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());
104 executor.shutdownNow();