| 1 | package de.uka.ipd.sdq.simucomframework; |
| 2 | |
| 3 | import org.apache.log4j.Logger; |
| 4 | |
| 5 | import de.uka.ipd.sdq.simucomframework.model.SimuComModel; |
| 6 | import de.uka.ipd.sdq.statistics.IBatchAlgorithm; |
| 7 | import de.uka.ipd.sdq.statistics.PhiMixingBatchAlgorithm; |
| 8 | import de.uka.ipd.sdq.statistics.StaticBatchAlgorithm; |
| 9 | import de.uka.ipd.sdq.statistics.estimation.SampleMeanEstimator; |
| 10 | |
| 11 | /** |
| 12 | * Helper class to actually perform a simulation run using desmo-j |
| 13 | * @author Steffen Becker |
| 14 | * |
| 15 | */ |
| 16 | public class ExperimentRunner { |
| 17 | private static Logger logger = |
| 18 | Logger.getLogger(ExperimentRunner.class.getName()); |
| 19 | |
| 20 | /** |
| 21 | * Run the given simulation model until the given simulation time |
| 22 | * is reached |
| 23 | * @param model Simulation model to execute |
| 24 | * @param simTime Maximum simulation time to run the simulation for |
| 25 | */ |
| 26 | public static double run(SimuComModel model, long simTime) { |
| 27 | logger.debug("Setting up experiment runner"); |
| 28 | setupStopConditions(model, simTime); |
| 29 | |
| 30 | // measure elapsed time for the simulation |
| 31 | double startTime = System.nanoTime(); |
| 32 | |
| 33 | model.getSimulationControl().start(); |
| 34 | |
| 35 | return System.nanoTime() - startTime; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param model |
| 40 | * @param simTime |
| 41 | */ |
| 42 | private static void setupStopConditions(SimuComModel model, long simTime) { |
| 43 | if (model.getConfig().getMaxMeasurementsCount() <= 0 && simTime <= 0) { |
| 44 | logger.debug("Deactivating maximum simulation time stop condition per user request"); |
| 45 | model.getSimulationControl().setMaxSimTime(0); |
| 46 | } else { |
| 47 | logger.debug("Enabling simulation stop condition at maximum simulation time of "+simTime); |
| 48 | if (simTime > 0) |
| 49 | model.getSimulationControl().setMaxSimTime(simTime); // set end of simulation at 1500 time |
| 50 | // units |
| 51 | } |
| 52 | |
| 53 | model.getSimulationControl().addStopCondition(new MaxMeasurementsStopCondition(model)); |
| 54 | |
| 55 | // Add confidence stop condition if configured |
| 56 | if (model.getConfiguration().isUseConfidence()) { |
| 57 | double level = model.getConfiguration().getConfidenceLevel() / 100.0; |
| 58 | double halfWidth = model.getConfiguration().getConfidenceHalfWidth() / 100.0; |
| 59 | |
| 60 | IBatchAlgorithm batchAlgorithm = null; |
| 61 | if (model.getConfiguration().isAutomaticBatches() ){ |
| 62 | batchAlgorithm = new PhiMixingBatchAlgorithm(); |
| 63 | } else { |
| 64 | int batchSize = model.getConfiguration().getBatchSize(); |
| 65 | int minNumberOfBatches = model.getConfiguration().getMinNumberOfBatches(); |
| 66 | batchAlgorithm = new StaticBatchAlgorithm(batchSize, minNumberOfBatches); |
| 67 | } |
| 68 | |
| 69 | model.getSimulationControl().addStopCondition( |
| 70 | new ConfidenceStopCondition(model, batchAlgorithm, |
| 71 | new SampleMeanEstimator(), level, halfWidth)); |
| 72 | } |
| 73 | } |
| 74 | } |