@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(); }
@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(); }
@Nullable @Override public Scenario apply(@Nullable SimArgs input) { return verifyNotNull(input).getScenario(); } }
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(); }
@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(); }
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)); } }
/** * @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 boolean equals(Object o) { if (o == this) { return true; } if (o instanceof Experiment.SimArgs) { Experiment.SimArgs that = (Experiment.SimArgs) o; return (this.scenario.equals(that.getScenario())) && (this.masConfig.equals(that.getMasConfig())) && (this.randomSeed == that.getRandomSeed()) && (this.repetition == that.getRepetition()) && (this.showGui == that.isShowGui()) && (this.postProcessor.equals(that.getPostProcessor())) && (this.uiCreator.equals(that.getUiCreator())); } return false; }
/** * @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 boolean equals(Object o) { if (o == this) { return true; } if (o instanceof Experiment.SimArgs) { Experiment.SimArgs that = (Experiment.SimArgs) o; return (this.scenario.equals(that.getScenario())) && (this.masConfig.equals(that.getMasConfig())) && (this.randomSeed == that.getRandomSeed()) && (this.repetition == that.getRepetition()) && (this.showGui == that.isShowGui()) && (this.postProcessor.equals(that.getPostProcessor())) && (this.uiCreator.equals(that.getUiCreator())); } return false; }
@Override public int compareTo(@Nullable SimulationResult o) { assert o != null; return ComparisonChain.start() .compare(getSimArgs().getScenario().getProblemClass().getId(), o.getSimArgs().getScenario().getProblemClass().getId()) .compare(getSimArgs().getScenario().getProblemInstanceId(), o.getSimArgs().getScenario().getProblemInstanceId()) .result(); }
static void assertSimRes(SimulationResult sr, Scenario s, MASConfiguration c, int r) { assertThat(sr.getSimArgs().getScenario()).isEqualTo(s); assertThat(sr.getSimArgs().getMasConfig()).isEqualTo(c); assertThat(sr.getSimArgs().getRepetition()).isEqualTo(r); }
@Nullable @Override public Scenario apply(@Nullable SimArgs input) { return verifyNotNull(input).getScenario(); } }
@Override public int compareTo(@Nullable SimulationResult o) { assert o != null; return ComparisonChain.start() .compare(getSimArgs().getScenario().getProblemClass().getId(), o.getSimArgs().getScenario().getProblemClass().getId()) .compare(getSimArgs().getScenario().getProblemInstanceId(), o.getSimArgs().getScenario().getProblemInstanceId()) .result(); }