Skip to content

Commit

Permalink
Merge pull request #116 from matsim-org/workshop_boiler_plate
Browse files Browse the repository at this point in the history
added boiler plate batch classes for episim workshop
  • Loading branch information
jakobrehmann authored Apr 29, 2024
2 parents e7e0322 + d2d4d9e commit 1390a01
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/matsim/run/CreateBatteryForCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public class CreateBatteryForCluster<T> implements Callable<Integer> {
@CommandLine.Option(names = "--jvm-opts", description = "Additional options for JVM", defaultValue = "-Xms82G -Xmx82G -XX:+UseParallelGC")
private String jvmOpts;

@CommandLine.Option(names = "--setup", defaultValue = "org.matsim.run.batch.CologneBMBF202310XX_soup")
@CommandLine.Option(names = "--setup", defaultValue = "org.matsim.run.batch.StarterBatchCologne")
private Class<? extends BatchRun<T>> setup;

@CommandLine.Option(names = "--params", defaultValue = "org.matsim.run.batch.CologneBMBF202310XX_soup$Params")
@CommandLine.Option(names = "--params", defaultValue = "org.matsim.run.batch.StarterBatchCologne$Params")
private Class<T> params;

@SuppressWarnings("rawtypes")
Expand Down
120 changes: 120 additions & 0 deletions src/main/java/org/matsim/run/batch/StarterBatchBerlin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.matsim.run.batch;

import com.google.inject.Module;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.episim.BatchRun;
import org.matsim.episim.EpisimConfigGroup;
import org.matsim.episim.VirusStrainConfigGroup;
import org.matsim.episim.analysis.OutputAnalysis;
import org.matsim.episim.model.InfectionModelWithAntibodies;
import org.matsim.run.RunParallel;
import org.matsim.run.modules.SnzBerlinProductionScenario;
import org.matsim.run.modules.SnzCologneProductionScenario;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;


/**
* boilerplate batch for berlin
*/
public class StarterBatchBerlin implements BatchRun<StarterBatchBerlin.Params> {

/*
* here you can swap out vaccination model, antibody model, etc.
* See CologneBMBF202310XX_soup.java for an example
*/
@Nullable
@Override
public Module getBindings(int id, @Nullable Params params) {
return getBindings(params);
}


/*
* here you select & modify models specified in the SnzCologneProductionScenario & SnzProductionScenario.
*/
private SnzBerlinProductionScenario getBindings(Params params) {
return new SnzBerlinProductionScenario.Builder()
.setActivityHandling(EpisimConfigGroup.ActivityHandling.startOfDay)
.setInfectionModel(InfectionModelWithAntibodies.class)
.setEasterModel(SnzBerlinProductionScenario.EasterModel.no)
.setChristmasModel(SnzBerlinProductionScenario.ChristmasModel.no)
.setSample(1)
.build();
}

/*
* Metadata is needed for covid-sim.
*/
@Override
public Metadata getMetadata() {
return Metadata.of("berlin", "calibration");
}


/*
* Here you can add post-processing classes, that will be executed after the simulation.
*/
@Override
public Collection<OutputAnalysis> postProcessing() {
return List.of();
}

/*
* Here you can specify configuration options
*/
@Override
public Config prepareConfig(int id, Params params) {

// Level 1: General (matsim) config. Here you can specify number of iterations and the seed.
Config config = getBindings(params).config();

config.global().setRandomSeed(params.seed);

// Level 2: Episim specific configs:
// 2a: general episim config
EpisimConfigGroup episimConfig = ConfigUtils.addOrGetModule(config, EpisimConfigGroup.class);

episimConfig.setCalibrationParameter(episimConfig.getCalibrationParameter() * 1.2 * 1.7 * params.thetaFactor);

// 2b: specific config groups, e.g. virusStrainConfigGroup
VirusStrainConfigGroup virusStrainConfigGroup = ConfigUtils.addOrGetModule(config, VirusStrainConfigGroup.class);

return config;
}


/*
* Specify parameter combinations that will be run.
*/
public static final class Params {
// general
@GenerateSeeds(1)
public long seed;

@Parameter({1.0, 2.0})
public double thetaFactor;
}



/*
* top-level parameters for a run on your local machine.
*/
public static void main(String[] args) {
String[] args2 = {
RunParallel.OPTION_SETUP, StarterBatchBerlin.class.getName(),
RunParallel.OPTION_PARAMS, Params.class.getName(),
RunParallel.OPTION_TASKS, Integer.toString(8),
RunParallel.OPTION_ITERATIONS, Integer.toString(50),
RunParallel.OPTION_METADATA
};

RunParallel.main(args2);
}

}

136 changes: 136 additions & 0 deletions src/main/java/org/matsim/run/batch/StarterBatchCologne.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package org.matsim.run.batch;

import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.Singleton;
import com.google.inject.multibindings.Multibinder;
import com.google.inject.util.Modules;
import it.unimi.dsi.fastutil.ints.Int2DoubleAVLTreeMap;
import it.unimi.dsi.fastutil.ints.Int2DoubleMap;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.episim.*;
import org.matsim.episim.analysis.HospitalNumbersFromEvents;
import org.matsim.episim.analysis.OutputAnalysis;
import org.matsim.episim.model.*;
import org.matsim.episim.model.listener.HouseholdSusceptibility;
import org.matsim.episim.model.vaccination.VaccinationModel;
import org.matsim.episim.model.vaccination.VaccinationStrategyBMBF202310XX;
import org.matsim.episim.policy.FixedPolicy;
import org.matsim.episim.policy.Restriction;
import org.matsim.run.RunParallel;
import org.matsim.run.modules.SnzCologneProductionScenario;

import javax.annotation.Nullable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.Map.Entry;


/**
* boilerplate batch for cologne
*/
public class StarterBatchCologne implements BatchRun<StarterBatchCologne.Params> {

/*
* here you can swap out vaccination model, antibody model, etc.
* See CologneBMBF202310XX_soup.java for an example
*/
@Nullable
@Override
public Module getBindings(int id, @Nullable Params params) {
return getBindings(params);
}


/*
* here you select & modify models specified in the SnzCologneProductionScenario & SnzProductionScenario.
*/
private SnzCologneProductionScenario getBindings(Params params) {
return new SnzCologneProductionScenario.Builder()
.setCarnivalModel(SnzCologneProductionScenario.CarnivalModel.yes)
.setFutureVacations(SnzCologneProductionScenario.FutureVacations.yes)
.setSebastianUpdate(true)
.setLeisureCorrection(1.3) //params == null ? 0.0 : params.actCorrection)
.setScaleForActivityLevels(1.3)
.setSuscHouseholds_pct(0.35)
.setActivityHandling(EpisimConfigGroup.ActivityHandling.startOfDay)
.setInfectionModel(InfectionModelWithAntibodies.class)
.build();
}

/*
* Metadata is needed for covid-sim.
*/
@Override
public Metadata getMetadata() {
return Metadata.of("cologne", "calibration");
}


/*
* Here you can add post-processing classes, that will be executed after the simulation.
*/
@Override
public Collection<OutputAnalysis> postProcessing() {
return List.of();
}

/*
* Here you can specify configuration options
*/
@Override
public Config prepareConfig(int id, Params params) {

// Level 1: General (matsim) config. Here you can specify number of iterations and the seed.
Config config = getBindings(params).config();

config.global().setRandomSeed(params.seed);

// Level 2: Episim specific configs:
// 2a: general episim config
EpisimConfigGroup episimConfig = ConfigUtils.addOrGetModule(config, EpisimConfigGroup.class);

episimConfig.setCalibrationParameter(episimConfig.getCalibrationParameter() * 1.2 * 1.7 * params.thetaFactor);

// 2b: specific config groups, e.g. virusStrainConfigGroup
VirusStrainConfigGroup virusStrainConfigGroup = ConfigUtils.addOrGetModule(config, VirusStrainConfigGroup.class);

return config;
}


/*
* Specify parameter combinations that will be run.
*/
public static final class Params {
// general
@GenerateSeeds(1)
public long seed;

@Parameter({1.0, 2.0})
public double thetaFactor;
}



/*
* top-level parameters for a run on your local machine.
*/
public static void main(String[] args) {
String[] args2 = {
RunParallel.OPTION_SETUP, StarterBatchCologne.class.getName(),
RunParallel.OPTION_PARAMS, Params.class.getName(),
RunParallel.OPTION_TASKS, Integer.toString(8),
RunParallel.OPTION_ITERATIONS, Integer.toString(10),
RunParallel.OPTION_METADATA
};

RunParallel.main(args2);
}

}

0 comments on commit 1390a01

Please sign in to comment.