@Override public String toString() { return new StringBuilder() .append("SimArgs{problemClass=") .append(getScenario().getProblemClass().toString()) .append(",instancedId=") .append(getScenario().getProblemInstanceId()) .append(",masConfig=") .append(getMasConfig().getName()) .append(",randomSeed=") .append(getRandomSeed()) .append(",repetition=") .append(getRepetition()) .append(",postProcessor=") .append(getPostProcessor()) .append("}") .toString(); }
runnerBuilder.add(SimArgs.create(verifyNotNull(s), verifyNotNull(c), seed.get(), rep.get(), showGui, postProc, uiCreator));
sr.getSimArgs().getRandomSeed() + " " + sr.getResultObject());
sr.getSimArgs().getRandomSeed() + " " + sr.getResultObject());
runnerBuilder.add(SimArgs.create(verifyNotNull(s), verifyNotNull(c), seed.get(), rep.get(), showGui, postProc, uiCreator));
@Override public void run() { // gather data from provider final DataProvider dataProvider = getDataProvider(); checkNotNull( dataProvider, "Probable problem: your MASConfiguration/PostProcessor may not be " + "fully serializable."); final Supplier<Scenario> scenario = getDataProvider().getParameter( scenarioId); final MASConfiguration configuration = getDataProvider().getParameter( configurationId); final PostProcessor<?> postProcessor = getDataProvider().getParameter( postProcessorId); final Scenario s = scenario.get(); final SimArgs simArgs = SimArgs.create(s, configuration, seed, repetition, false, postProcessor, null); Object simResult; do { simResult = Experiment.perform(simArgs); } while (simResult == FailureStrategy.RETRY); checkArgument(simResult instanceof Serializable, "Your PostProcessor must generate Serializable objects, found %s.", simResult); setResult(simResult); }
@Override public void run() { // gather data from provider final DataProvider dataProvider = getDataProvider(); checkNotNull( dataProvider, "Probable problem: your MASConfiguration/PostProcessor may not be " + "fully serializable."); final Supplier<Scenario> scenario = getDataProvider().getParameter( scenarioId); final MASConfiguration configuration = getDataProvider().getParameter( configurationId); final PostProcessor<?> postProcessor = getDataProvider().getParameter( postProcessorId); final Scenario s = scenario.get(); final SimArgs simArgs = SimArgs.create(s, configuration, seed, repetition, false, postProcessor, null); Object simResult; do { simResult = Experiment.perform(simArgs); } while (simResult == FailureStrategy.RETRY); checkArgument(simResult instanceof Serializable, "Your PostProcessor must generate Serializable objects, found %s.", simResult); setResult(simResult); }
static Object perform(SimArgs args) { final Simulator sim = Experiment.init(args.getScenario(), args.getMasConfig(), args.getRandomSeed(), args.isShowGui(), args.getUiCreator()); try { sim.start(); final Object resultObject = args.getPostProcessor().collectResults(sim, args); checkNotNull(resultObject, "PostProcessor may not return null."); return resultObject; } catch (final Exception e) { final FailureStrategy strategy = args.getPostProcessor().handleFailure(e, sim, args); checkNotNull(strategy, "An exception (%s) occured in the simulation but the PostProcessor %s " + "failed to handle the failure.", e.toString(), args.getPostProcessor()); if (strategy == FailureStrategy.INCLUDE) { return args.getPostProcessor().collectResults(sim, args); } else if (strategy == FailureStrategy.ABORT_EXPERIMENT_RUN) { throw new AbortExperimentException("Failed: " + args, e); } return strategy; } }
static Object perform(SimArgs args) { final Simulator sim = Experiment.init(args.getScenario(), args.getMasConfig(), args.getRandomSeed(), args.isShowGui(), args.getUiCreator()); try { sim.start(); final Object resultObject = args.getPostProcessor().collectResults(sim, args); checkNotNull(resultObject, "PostProcessor may not return null."); return resultObject; } catch (final Exception e) { final FailureStrategy strategy = args.getPostProcessor().handleFailure(e, sim, args); checkNotNull(strategy, "An exception (%s) occured in the simulation but the PostProcessor %s " + "failed to handle the failure.", e.toString(), args.getPostProcessor()); if (strategy == FailureStrategy.INCLUDE) { return args.getPostProcessor().collectResults(sim, args); } else if (strategy == FailureStrategy.ABORT_EXPERIMENT_RUN) { throw new AbortExperimentException("Failed: " + args, e); } return strategy; } }
@Override public String collectResults(Simulator sim, SimArgs args) { // Read state of simulator, check how many vehicles exist in road model: final Set<Vehicle> vehicles = sim.getModelProvider() .getModel(RoadModel.class).getObjectsOfType(Vehicle.class); // Construct a result string based on the simulator state, of course, in // actual code the result should not be a string but a value object // containing the values of interest. final StringBuilder sb = new StringBuilder(); if (vehicles.isEmpty()) { sb.append("No vehicles were added"); } else { sb.append(vehicles.size()).append(" vehicles were added"); } if (sim.getCurrentTime() >= args.getScenario().getTimeWindow().end()) { sb.append(", simulation has completed."); } else { sb.append(", simulation was stopped prematurely."); } return sb.toString(); }
/** * Can be used to run a single simulation run. * @param scenario The scenario to run on. * @param configuration The configuration to use. * @param seed The seed of the run. * @param showGui If <code>true</code> enables the gui. * @param postProcessor The post processor to use for this run. * @param uic The UICreator to use. * @return The {@link SimulationResult} generated in the run. */ public static SimulationResult singleRun(Scenario scenario, MASConfiguration configuration, long seed, boolean showGui, PostProcessor<?> postProcessor, @Nullable ModelBuilder<?, ?> uic) { final ExperimentRunner er = new ExperimentRunner(SimArgs.create(scenario, configuration, seed, 0, showGui, postProcessor, uic)); final SimulationResult res = er.call(); checkState(res != null); return res; }
@Override public String collectResults(Simulator sim, SimArgs args) { // Read state of simulator, check how many vehicles exist in road model: final Set<Vehicle> vehicles = sim.getModelProvider() .getModel(RoadModel.class).getObjectsOfType(Vehicle.class); // Construct a result string based on the simulator state, of course, in // actual code the result should not be a string but a value object // containing the values of interest. final StringBuilder sb = new StringBuilder(); if (vehicles.isEmpty()) { sb.append("No vehicles were added"); } else { sb.append(vehicles.size()).append(" vehicles were added"); } if (sim.getCurrentTime() >= args.getScenario().getTimeWindow().end()) { sb.append(", simulation has completed."); } else { sb.append(", simulation was stopped prematurely."); } return sb.toString(); }
@Test public void testCustomPostProcessor() { TestUtil.testEnum(Experiment.Computers.class); final Scenario scenario = ScenarioTestUtil.createRandomScenario(123L, StatsTracker.builder()); final Experiment.Builder builder = Experiment.builder() .addScenario(scenario) .addConfiguration(ExperimentTestUtil.testConfig("test")) .usePostProcessor(ExperimentTestUtil.testPostProcessor()) .addResultListener(new CommandLineProgress(System.out)) .withRandomSeed(123); final ExperimentResults er = builder.perform(); assertEquals(123, er.getMasterSeed()); assertEquals(123, er.getResults().asList().get(0).getSimArgs().getRandomSeed()); @SuppressWarnings("unchecked") final List<Point> positions = (List<Point>) er.getResults().asList().get(0).getResultObject(); assertEquals(10, positions.size()); }
/** * Can be used to run a single simulation run. * @param scenario The scenario to run on. * @param configuration The configuration to use. * @param seed The seed of the run. * @param showGui If <code>true</code> enables the gui. * @param postProcessor The post processor to use for this run. * @param uic The UICreator to use. * @return The {@link SimulationResult} generated in the run. */ public static SimulationResult singleRun(Scenario scenario, MASConfiguration configuration, long seed, boolean showGui, PostProcessor<?> postProcessor, @Nullable ModelBuilder<?, ?> uic) { final ExperimentRunner er = new ExperimentRunner(SimArgs.create(scenario, configuration, seed, 0, showGui, postProcessor, uic)); final SimulationResult res = er.call(); checkState(res != null); return res; }
static void constructTasks( Set<SimArgs> inputs, List<SimulationTask> tasks, IdMap<MASConfiguration> configMap, IdMap<ScenarioProvider> scenarioMap, IdMap<PostProcessor<?>> ppMap, Map<String, Scenario> scenariosMap) { for (final SimArgs args : inputs) { final String configId = configMap.storeAndGenerateId( args.getMasConfig()); final String scenId = scenarioMap.storeAndGenerateId( new ScenarioProvider(ScenarioIO.write(args.getScenario()), args.getScenario().getClass())); scenariosMap.put(scenId, args.getScenario()); final String postProcId = ppMap.storeAndGenerateId(args.getPostProcessor()); tasks.add(new SimulationTask(args.getRandomSeed(), args.getRepetition(), scenId, configId, postProcId)); } }
static void constructTasks( Set<SimArgs> inputs, List<SimulationTask> tasks, IdMap<MASConfiguration> configMap, IdMap<ScenarioProvider> scenarioMap, IdMap<PostProcessor<?>> ppMap, Map<String, Scenario> scenariosMap) { for (final SimArgs args : inputs) { final String configId = configMap.storeAndGenerateId( args.getMasConfig()); final String scenId = scenarioMap.storeAndGenerateId( new ScenarioProvider(ScenarioIO.write(args.getScenario()), args.getScenario().getClass())); scenariosMap.put(scenId, args.getScenario()); final String postProcId = ppMap.storeAndGenerateId(args.getPostProcessor()); tasks.add(new SimulationTask(args.getRandomSeed(), args.getRepetition(), scenId, configId, postProcId)); } }
static SimulationResult processResult(SimulationTask simTask, final Map<String, Scenario> scenariosMap, final Map<Task<?>, JPPFJob> jobMap) { checkNotNull(simTask); if (simTask.getThrowable() != null) { throw new IllegalArgumentException(simTask.getThrowable()); } final Object result = simTask.getResult(); final Scenario scen = scenariosMap.get(simTask.getScenarioId()); final DataProvider dp = jobMap.get(simTask).getDataProvider(); final MASConfiguration conf = dp.getParameter(simTask.getConfigurationId()); final PostProcessor<?> pp = dp.getParameter(simTask.getPostProcessorId()); final SimArgs args = SimArgs.create(scen, conf, simTask.getSeed(), simTask.getRepetition(), false, pp, null); return SimulationResult.create(args, result); }
static SimulationResult processResult(SimulationTask simTask, final Map<String, Scenario> scenariosMap, final Map<Task<?>, JPPFJob> jobMap) { checkNotNull(simTask); if (simTask.getThrowable() != null) { throw new IllegalArgumentException(simTask.getThrowable()); } final Object result = simTask.getResult(); final Scenario scen = scenariosMap.get(simTask.getScenarioId()); final DataProvider dp = jobMap.get(simTask).getDataProvider(); final MASConfiguration conf = dp.getParameter(simTask.getConfigurationId()); final PostProcessor<?> pp = dp.getParameter(simTask.getPostProcessorId()); final SimArgs args = SimArgs.create(scen, conf, simTask.getSeed(), simTask.getRepetition(), false, pp, null); return SimulationResult.create(args, result); }
/** * @return A very compact string representation. */ public String toShortString() { return new StringBuilder(getScenario().getProblemClass().getId()) .append(DASH) .append(getScenario().getProblemInstanceId()) .append(DASH) .append(getMasConfig().getName()) .append("-s") .append(getRandomSeed()) .append(DASH) .append("r") .append(getRepetition()) .toString(); }
@Override public String toString() { return new StringBuilder() .append("SimArgs{problemClass=") .append(getScenario().getProblemClass().toString()) .append(",instancedId=") .append(getScenario().getProblemInstanceId()) .append(",masConfig=") .append(getMasConfig().getName()) .append(",randomSeed=") .append(getRandomSeed()) .append(",repetition=") .append(getRepetition()) .append(",postProcessor=") .append(getPostProcessor()) .append("}") .toString(); }