optimization updates
[debian/openrocket] / src / net / sf / openrocket / optimization / rocketoptimization / parameters / SimulationBasedParameter.java
1 package net.sf.openrocket.optimization.rocketoptimization.parameters;
2
3 import java.util.Arrays;
4
5 import net.sf.openrocket.document.Simulation;
6 import net.sf.openrocket.logging.LogHelper;
7 import net.sf.openrocket.optimization.general.OptimizationException;
8 import net.sf.openrocket.optimization.rocketoptimization.OptimizableParameter;
9 import net.sf.openrocket.simulation.FlightData;
10 import net.sf.openrocket.simulation.exception.MotorIgnitionException;
11 import net.sf.openrocket.simulation.exception.SimulationCancelledException;
12 import net.sf.openrocket.simulation.exception.SimulationException;
13 import net.sf.openrocket.simulation.exception.SimulationLaunchException;
14 import net.sf.openrocket.simulation.listeners.SimulationListener;
15 import net.sf.openrocket.simulation.listeners.system.InterruptListener;
16 import net.sf.openrocket.startup.Application;
17
18 /**
19  * An abstract optimization parameter that simulates a rocket flight and obtains
20  * a value from the simulation result.
21  * 
22  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
23  */
24 public abstract class SimulationBasedParameter implements OptimizableParameter {
25         
26         private static final LogHelper log = Application.getLogger();
27         
28         @Override
29         public double computeValue(Simulation simulation) throws OptimizationException, InterruptedException {
30                 try {
31                         log.debug("Running simulation for " + getName());
32                         
33                         SimulationListener[] listeners = getSimulationListeners();
34                         listeners = Arrays.copyOf(listeners, listeners.length + 1);
35                         listeners[listeners.length - 1] = new InterruptListener();
36                         simulation.simulate(listeners);
37                         
38                         double value = getResultValue(simulation.getSimulatedData());
39                         log.debug("Parameter '" + getName() + " was " + value);
40                         return value;
41                 } catch (MotorIgnitionException e) {
42                         // A problem with motor ignition will cause optimization to fail
43                         throw new OptimizationException(e);
44                 } catch (SimulationLaunchException e) {
45                         // Other launch exceptions result in illegal value
46                         return Double.NaN;
47                 } catch (SimulationCancelledException e) {
48                         // Simulation cancellation stops the optimization
49                         throw (InterruptedException) new InterruptedException("Optimization was interrupted").initCause(e);
50                 } catch (SimulationException e) {
51                         // Other exceptions fail
52                         throw new OptimizationException(e);
53                 }
54         }
55         
56         
57         /**
58          * Return the optimization parameter from the simulation flight data.
59          * 
60          * @param simulatedData         the simulated data.
61          * @return                                      the optimization parameter.
62          */
63         protected abstract double getResultValue(FlightData simulatedData);
64         
65         /**
66          * Return an array of simulation listeners to provide to the simulation.
67          * This may include a listener that stops the simulation after the necessary value
68          * has been computed.
69          * <p>
70          * This array should NOT contain InterruptListener, it will be added implicitly.
71          * 
72          * @return      an array of simulation listeners to include.
73          */
74         protected SimulationListener[] getSimulationListeners() {
75                 return new SimulationListener[0];
76         }
77         
78 }