Step 4: Running the first experiment
Now that we have the Scenario
class, it’s time to create our first scenario and run an experiment.
The ExperimentRun class
First, let’s create the following ExperimentRun
class:
package tutorial;
import tutorial.model.Model;
import tutorial.scenario.Scenario;
import java.time.LocalDateTime;
import com.amalgamasimulation.engine.Engine;
public class ExperimentRun {
private final Model model;
private final Scenario scenario;
private final Engine engine;
public ExperimentRun(Scenario scenario, Engine engine) {
this.scenario = scenario;
this.engine = engine;
this.model = new Model(engine, scenario);
}
public void run() {
model.engine().setFastMode(true);
model.engine().run(true);
}
public Scenario getScenario() {
return scenario;
}
public Engine getEngine() {
return engine;
}
public LocalDateTime getSimulationEndDateTime() {
return model.engine().date();
}
}
The run()
method starts the engine and waits until it stops.
The getSimulationEndDateTime()
method returns the results of the simulation -
currently we take the 'model astronomical date&time' as the only output parameter of an experiment.
We will build up this part of the class later.
Changes to the Main class
Go to the Main
class.
Make sure you have all these import statements in it:
import java.time.LocalDateTime;
import java.util.List;
import com.amalgamasimulation.engine.Engine;
import com.amalgamasimulation.utils.format.Formats;
import tutorial.scenario.RouteLengthContainer;
import tutorial.scenario.Scenario;
import tutorial.scenario.Store;
import tutorial.scenario.Warehouse;
Add new fields and the static{}
section:
private static final double TRUCK_SPEED = 40;
private static final double INTERVAL_BETWEEN_REQUESTS_HRS = 0.5;
private static final double MAX_DELIVERY_TIME_HRS = 6;
private static List<Warehouse> warehouses;
private static List<Store> stores;
private static RouteLengthContainer routeLengthContainer;
static {
Warehouse wh1 = new Warehouse("wh1", "Warehouse 1");
Warehouse wh2 = new Warehouse("wh2", "Warehouse 2");
Warehouse wh3 = new Warehouse("wh3", "Warehouse 3");
warehouses = List.of(wh1, wh2, wh3);
Store st1 = new Store("st1", "Store 1");
Store st2 = new Store("st2", "Store 2");
Store st3 = new Store("st3", "Store 3");
stores = List.of(st1, st2, st3);
routeLengthContainer = new RouteLengthContainer();
routeLengthContainer.add(wh1, st1, 40);
routeLengthContainer.add(wh1, st2, 40);
routeLengthContainer.add(wh1, st3, 50);
routeLengthContainer.add(wh2, st1, 40);
routeLengthContainer.add(wh2, st2, 40);
routeLengthContainer.add(wh2, st3, 50);
routeLengthContainer.add(wh3, st1, 50);
routeLengthContainer.add(wh3, st2, 50);
routeLengthContainer.add(wh3, st3, 40);
}
Add createAndRunOneExperiment()
method:
private static void createAndRunOneExperiment() {
Scenario scenario = new Scenario( 1,
TRUCK_SPEED,
INTERVAL_BETWEEN_REQUESTS_HRS,
MAX_DELIVERY_TIME_HRS,
warehouses,
stores,
routeLengthContainer,
LocalDateTime.of(2023, 1, 1, 0, 0),
LocalDateTime.of(2023, 1, 1, 12, 0));
runExperiment(scenario);
}
Add runExperiment()
method:
private static void runExperiment(Scenario scenario) {
ExperimentRun experiment = new ExperimentRun(scenario, new Engine());
experiment.run();
}
Replace the main()
method with the following:
public static void main(String[] args) {
createAndRunOneExperiment();
}
Here a scenario with the following parameters gets created:
-
1 truck;
-
average truck speed is 40 km/h;
-
average interval between requests is 0.5 hours;
-
each request must be fulfilled within 6 hours (max delivery time);
-
transportation is done among 3 warehouses and 3 stores;
-
the simulation begins at 'Jan 1 2023 00:00' and ends at 'Jan 1 2023 12:00'.
Here is the full listing of the Main
class that you should have by now:
package tutorial;
import java.time.LocalDateTime;
import java.util.List;
import com.amalgamasimulation.engine.Engine;
import com.amalgamasimulation.utils.format.Formats;
import tutorial.scenario.RouteLengthContainer;
import tutorial.scenario.Scenario;
import tutorial.scenario.Store;
import tutorial.scenario.Warehouse;
public class Main {
private static final double TRUCK_SPEED = 40;
private static final double INTERVAL_BETWEEN_REQUESTS_HRS = 0.5;
private static final double MAX_DELIVERY_TIME_HRS = 6;
private static List<Warehouse> warehouses;
private static List<Store> stores;
private static RouteLengthContainer routeLengthContainer;
static {
Warehouse wh1 = new Warehouse("wh1", "Warehouse 1");
Warehouse wh2 = new Warehouse("wh2", "Warehouse 2");
Warehouse wh3 = new Warehouse("wh3", "Warehouse 3");
warehouses = List.of(wh1, wh2, wh3);
Store st1 = new Store("st1", "Store 1");
Store st2 = new Store("st2", "Store 2");
Store st3 = new Store("st3", "Store 3");
stores = List.of(st1, st2, st3);
routeLengthContainer = new RouteLengthContainer();
routeLengthContainer.add(wh1, st1, 40);
routeLengthContainer.add(wh1, st2, 40);
routeLengthContainer.add(wh1, st3, 50);
routeLengthContainer.add(wh2, st1, 40);
routeLengthContainer.add(wh2, st2, 40);
routeLengthContainer.add(wh2, st3, 50);
routeLengthContainer.add(wh3, st1, 50);
routeLengthContainer.add(wh3, st2, 50);
routeLengthContainer.add(wh3, st3, 40);
}
public static void main(String[] args) {
createAndRunOneExperiment();
}
private static void createAndRunOneExperiment() {
Scenario scenario = new Scenario( 1,
TRUCK_SPEED,
INTERVAL_BETWEEN_REQUESTS_HRS,
MAX_DELIVERY_TIME_HRS,
warehouses,
stores,
routeLengthContainer,
LocalDateTime.of(2023, 1, 1, 0, 0),
LocalDateTime.of(2023, 1, 1, 12, 0));
runExperiment(scenario);
}
private static void runExperiment(Scenario scenario) {
ExperimentRun experiment = new ExperimentRun(scenario, new Engine());
experiment.run();
}
}