Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenzotong committed Sep 13, 2023
2 parents 265fe48 + 078d3a4 commit 1637f16
Show file tree
Hide file tree
Showing 158 changed files with 27,311 additions and 1,522 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/code-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

steps:
- name: Checkout git repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Java
uses: actions/setup-java@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy-on-pr-merge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

steps:
- name: Checkout git repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Java
uses: actions/setup-java@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy-on-release-created.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

steps:
- name: Checkout git repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Java
uses: actions/setup-java@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy-weekly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

steps:
- name: Checkout git repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Java
uses: actions/setup-java@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/full-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
run: git config --global core.autocrlf false

- name: Checkout git repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Java
uses: actions/setup-java@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/verify-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:

steps:
- name: Checkout git repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Detect changes against master
# we only want to build matsim (module) if changes are not limited to contribs
Expand Down
2 changes: 1 addition & 1 deletion contribs/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.7.4</version>
<version>4.7.5</version>
</dependency>
<dependency>
<groupId>it.unimi.dsi</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
package org.matsim.smallScaleCommercialTrafficGeneration;

import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.api.core.v01.population.Population;
import org.matsim.api.core.v01.population.PopulationFactory;
import org.matsim.api.core.v01.population.*;
import org.matsim.application.MATSimAppCommand;
import org.matsim.core.population.PopulationUtils;
import picocli.CommandLine;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.SplittableRandom;
import java.util.Random;

@CommandLine.Command(name = "generate-plan-variants-for-freight", description = "Generates variants of plans for a population of freight agents", showDefaultValues = true)

public class CreateDifferentPlansForFreightPopulation implements MATSimAppCommand {

private enum PlanVariantStrategy{changeStartingTimes}
private enum PlanVariantStrategy {changeStartingTimes, activityOrderVariation}

@CommandLine.Parameters(arity = "1", paramLabel = "INPUT", description = "Path to the population", defaultValue = "output/testOutput/testPopulation_new.xml.gz")
@CommandLine.Parameters(arity = "1", paramLabel = "INPUT", description = "Path to the population")
private Path populationPath;
@CommandLine.Option(names = "--outputPopulationPath", description = "Path to the outputPopulation", defaultValue = "output/testOutput/testPopulationVariant.xml.gz", required = true)
@CommandLine.Option(names = "--outputPopulationPath", description = "Path to the outputPopulation", required = true)
private Path outputPopulationPath;
@CommandLine.Option(names = "--numberOfPlanVariants", description = "Set the number of plan variants", required = true, defaultValue = "5")
private static int numberOfPlanVariants;
Expand All @@ -34,38 +31,58 @@ private enum PlanVariantStrategy{changeStartingTimes}
private static int typicalTourDuration;
@CommandLine.Option(names = "--seed", description = "Set seed", defaultValue = "4411")
private static int seed;
private static final SplittableRandom rnd = new SplittableRandom(seed);
private static final Random rnd = new Random(seed);

public static void main(String[] args) {
System.exit(new CommandLine(new CreateDifferentPlansForFreightPopulation()).execute(args));
}

@Override
public Integer call() throws Exception {

PlanVariantStrategy selectedPlanVariantStrategy = PlanVariantStrategy.changeStartingTimes;
PlanVariantStrategy selectedPlanVariantStrategy = PlanVariantStrategy.activityOrderVariation;

Population population = PopulationUtils.readPopulation(populationPath.toString());
createPlanVariants(selectedPlanVariantStrategy, population);
PopulationUtils.writePopulation(population, outputPopulationPath.toString());
return null;
}
public static void createPlanVariantsForPopulations(String planVariantStrategy, Population population, int selectedNumberOfPlanVariants, int selectedEarliestTourStartTime, int selectedLatestTourStartTime, int selectedTypicalTourDuration){
PlanVariantStrategy selectedPlanVariantStrategy;
switch (planVariantStrategy) {
case ("changeStartingTimes") -> {
selectedPlanVariantStrategy = PlanVariantStrategy.changeStartingTimes;
}
default -> throw new RuntimeException("No possible PlanVariantStrategy selected. Possible strategies are: " + Arrays.toString(PlanVariantStrategy.values()));
}

/**
* Creates alternative plans (n = selectedNumberOfPlanVariants) by changing the start time (and end time) of the tour.
*
* @param population
* @param selectedNumberOfPlanVariants
* @param selectedEarliestTourStartTime
* @param selectedLatestTourStartTime
* @param selectedTypicalTourDuration
*/
public static void createMorePlansWithDifferentStartTimes(Population population, int selectedNumberOfPlanVariants,
int selectedEarliestTourStartTime, int selectedLatestTourStartTime,
int selectedTypicalTourDuration) {
PlanVariantStrategy selectedPlanVariantStrategy = PlanVariantStrategy.changeStartingTimes;
numberOfPlanVariants = selectedNumberOfPlanVariants;
earliestTourStartTime = selectedEarliestTourStartTime;
latestTourStartTime = selectedLatestTourStartTime;
typicalTourDuration = selectedTypicalTourDuration;
createPlanVariants(selectedPlanVariantStrategy, population);
}

/**
* Creates alternative plans (n = selectedNumberOfPlanVariants) by changing the order of the activities.
*
* @param population
* @param selectedNumberOfPlanVariants
*/
public static void createMorePlansWithDifferentActivityOrder(Population population, int selectedNumberOfPlanVariants) {
PlanVariantStrategy selectedPlanVariantStrategy = PlanVariantStrategy.activityOrderVariation;
numberOfPlanVariants = selectedNumberOfPlanVariants;
createPlanVariants(selectedPlanVariantStrategy, population);
}

private static void createPlanVariants(PlanVariantStrategy selectedPlanVariantStrategy, Population population) {
for (Person person: population.getPersons().values()) {
switch (selectedPlanVariantStrategy){
for (Person person : population.getPersons().values()) {
switch (selectedPlanVariantStrategy) {
case changeStartingTimes -> {

double initTourStart = PopulationUtils.getFirstActivity(person.getSelectedPlan()).getEndTime().seconds();
Expand All @@ -79,6 +96,35 @@ private static void createPlanVariants(PlanVariantStrategy selectedPlanVariantSt
PopulationUtils.getLastActivity(newPLan).setStartTime(variantEndTime);
}
}
case activityOrderVariation -> {

List<Integer> activityIndexList = new ArrayList<>();
int count = 0;
for (PlanElement planElement : person.getSelectedPlan().getPlanElements()) {
if (planElement instanceof Activity activity) {
if (activity.getType().equals("service")) {
activityIndexList.add(count);
}
}
count++;
}
for (int i = person.getPlans().size(); i < numberOfPlanVariants; i++) {
if (activityIndexList.size() < 2)
continue;
List<Integer> activityNewIndexList = new ArrayList<Integer>(activityIndexList);
Collections.shuffle(activityNewIndexList, rnd);
List<Integer> alreadySwapedActivity = new ArrayList<>();
Plan newPLan = person.createCopyOfSelectedPlanAndMakeSelected();
person.setSelectedPlan(person.getPlans().get(0));
for (int j = 0; j < activityIndexList.size(); j++) {
if (alreadySwapedActivity.contains(activityIndexList.get(j)))
continue;
Collections.swap(newPLan.getPlanElements(), activityIndexList.get(j), activityNewIndexList.get(j));
alreadySwapedActivity.add(activityNewIndexList.get(j));
}
}

}
default -> throw new RuntimeException("No possible PlanVariantStrategy selected");
}
}
Expand All @@ -88,21 +134,23 @@ private static double createStartTimeVariante(List<Double> timeVariants) {
double vehicleStartTime = 0;
while (vehicleStartTime == 0) {
double possibleVehicleStartTime = rnd.nextInt(earliestTourStartTime, latestTourStartTime);
if(checkPossibleVehicleStartTime(possibleVehicleStartTime, timeVariants))
if (checkPossibleVehicleStartTime(possibleVehicleStartTime, timeVariants))
vehicleStartTime = possibleVehicleStartTime;
}
timeVariants.add(vehicleStartTime);
return vehicleStartTime;
}

/** Checks if the new starting time has always a minimum 30 minutes difference to all other possible startimes.
/**
* Checks if the new starting time has always a minimum 30 minutes difference to all other possible startimes.
*
* @param possibleVehicleStartTime
* @param timeVariants
* @return
*/
private static boolean checkPossibleVehicleStartTime(double possibleVehicleStartTime, List<Double> timeVariants) {
for (double usedTimes: timeVariants) {
if (Math.abs((possibleVehicleStartTime-usedTimes)) > 1800)
for (double usedTimes : timeVariants) {
if (Math.abs((possibleVehicleStartTime - usedTimes)) > 1800)
return true;
}
return false;
Expand Down
Loading

0 comments on commit 1637f16

Please sign in to comment.