Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate "demand generation of parcel delivery" (based on master thesis) in matsim-libs #3665

Merged
merged 53 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
059e82b
Matsim Advanced
May 14, 2024
3044f4e
Matsim Advanced - Small fixes
May 14, 2024
2e49324
Matsim Advanced - Small fixes 2
May 14, 2024
83a2671
Merge branch 'matsim-org:master' into master
AnaUeb May 24, 2024
dde8994
Masterarbeit 1
May 24, 2024
f566eda
Merge branch 'master' of https://github.com/AnaUeb/matsim-libs
May 24, 2024
5d4c0c2
Merge branch 'matsim-org:master' into master
AnaUeb May 24, 2024
7a8fd39
DemandPerAge - MA
Jun 5, 2024
2100fda
DemandPerAge - MA 2
Jun 7, 2024
ba55915
DemandPerAge - MA 3
Jun 11, 2024
8368088
MA - Erros
Jun 21, 2024
bf3029c
Modify freight demand generation tool
Jun 22, 2024
6921cf3
Changes 2
Jun 25, 2024
5c4307d
Packages per Person
Jun 25, 2024
09f38a6
Stop time
Jul 16, 2024
6e19035
Presentation day
Aug 6, 2024
6a3974e
Code merge-conflicts day
Aug 9, 2024
d99b43e
merge in upstream master from matsim-libs
kt86 Aug 9, 2024
be0c562
auto indent lines. no changes in behaviour
kt86 Aug 9, 2024
65ab468
First run after merge
Aug 13, 2024
f794d0e
PLZ-runs
Oct 2, 2024
67cf249
poisson
Oct 9, 2024
c83fb61
commit MA
Dec 8, 2024
1b21f54
commit MA 2
Dec 9, 2024
754049b
marge in matsim-libs/master and resolve conflicts
kt86 Dec 10, 2024
8744950
fix compile error
kt86 Dec 10, 2024
faf938c
test was already removed, after class itself was removed.
kt86 Dec 11, 2024
7052e39
add data for new column in CarrierLoadAnalysis
kt86 Dec 11, 2024
97aaafd
add check for null
kt86 Dec 11, 2024
e1db475
Revert "add check for null" -- because it leads to an infinty running...
kt86 Dec 11, 2024
0e25410
update FreightDemandGeneration by adding functionality for specific u…
rewertvsp Jan 7, 2025
aeb2b1f
add implementation for parcel delivery
rewertvsp Jan 7, 2025
c6d3953
rename method and extend java doc
rewertvsp Jan 7, 2025
0f8e4ee
add demand information to ageGroup file 1
rewertvsp Jan 7, 2025
65a6a84
move stuff into brackets
rewertvsp Jan 9, 2025
8a82a1e
revert renaming
rewertvsp Jan 9, 2025
ff95066
add tests
rewertvsp Jan 9, 2025
0f2b841
rename test input files
rewertvsp Jan 10, 2025
8591463
add java doc
rewertvsp Jan 10, 2025
dc9e722
Merge remote-tracking branch 'refs/remotes/upstream/master' into upda…
rewertvsp Jan 13, 2025
2533395
rename to related object
rewertvsp Jan 13, 2025
6797c6a
fix controller
rewertvsp Jan 13, 2025
e0eb2f2
Merge pull request #4 from AnaUeb/updateAna2-by-Ricardo
rewertvsp Jan 13, 2025
6be7865
Merge branch 'matsim-org:master' into ana2
kt86 Jan 13, 2025
01ecf5b
move link finding for persons to more effective place
rewertvsp Jan 14, 2025
2a45de0
Merge remote-tracking branch 'upstream/master' into ana2
rewertvsp Jan 15, 2025
eae01d8
use recent Builder for services
rewertvsp Jan 15, 2025
e7486a3
Merge branch 'master' into ana2
rewertvsp Jan 15, 2025
0fc834f
Merge remote-tracking branch 'upstream/master' into ana2
rewertvsp Jan 20, 2025
4b1a0e6
Merge remote-tracking branch 'upstream/master' into ana2
rewertvsp Jan 20, 2025
7f1785e
mark Freight analysis in vsp contrib as deprecated, but make it runab…
rewertvsp Jan 20, 2025
6e56d39
delete unneeded test files
rewertvsp Jan 20, 2025
2db78f7
Some cleanup (#5)
kt86 Jan 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add tests
  • Loading branch information
rewertvsp committed Jan 9, 2025
commit ff95066c5641bb176c9bc243b030c8cfa73f767e
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import org.matsim.freightDemandGeneration.CarrierReaderFromCSV.CarrierInformationElement;
import org.matsim.freightDemandGeneration.DemandReaderFromCSV.DemandInformationElement;
import org.matsim.testcases.MatsimTestUtils;
import org.matsim.freightDemandGeneration.FreightDemandGeneration.TotalDemandGenerationsOption;
import org.matsim.freightDemandGeneration.FreightDemandGeneration.DemandDistributionOption;

import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
Expand Down Expand Up @@ -336,6 +334,66 @@ void demandCreationNoSampling() throws IOException {
}
}

@Test
void demandCreationParcelsNoSampling() throws IOException {
// read inputs
Config config = ConfigUtils.createConfig();
config.network().setInputFile(
"https://raw.githubusercontent.com/matsim-org/matsim-libs/master/examples/scenarios/freight-chessboard-9x9/grid9x9.xml");
Scenario scenario = ScenarioUtils.loadScenario(config);
FreightCarriersConfigGroup freightCarriersConfigGroup = ConfigUtils.addOrGetModule(scenario.getConfig(),
FreightCarriersConfigGroup.class);
freightCarriersConfigGroup.setCarriersVehicleTypesFile(utils.getPackageInputDirectory() + "testVehicleTypes.xml");
Path carrierCSVLocation = Path.of(utils.getPackageInputDirectory() + "testCarrierCSV_1.csv");
Path demandCSVLocation = Path.of(utils.getPackageInputDirectory() + "testDemandCSV_1.csv");
Path shapeFilePath = Path.of(utils.getPackageInputDirectory() + "testShape/testShape.shp");
ShpOptions shp = new ShpOptions(shapeFilePath, "WGS84", null);
String shapeCategory = "Ortsteil";
ShpOptions.Index indexShape = shp.createIndex("Ortsteil");
String populationLocation = utils.getPackageInputDirectory() + "testPopulation.xml";
Population population = PopulationUtils.readPopulation(populationLocation);
FreightDemandGenerationUtils.preparePopulation(population, 0.5, 0.5, "changeDemandOnLocation");
Boolean combineSimilarJobs = false;

// run methods
createDemandAndCheckCarrierForParcel(carrierCSVLocation, scenario, freightCarriersConfigGroup, indexShape, demandCSVLocation, shapeCategory,
population, combineSimilarJobs);

// check carrier 1
Network network = scenario.getNetwork();

Assertions.assertEquals(1, CarriersUtils.getCarriers(scenario).getCarriers().size());

Carrier testCarrier1 = CarriersUtils.getCarriers(scenario).getCarriers()
.get(Id.create("testCarrier1", Carrier.class));

Assertions.assertEquals(3, testCarrier1.getShipments().values().stream().mapToInt(CarrierShipment::getSize).sum());
Map<String, Set<String>> locationsPerShipmentElement = new HashMap<>();
for (CarrierShipment shipment : testCarrier1.getShipments().values()) {
Assertions.assertEquals("i(2,0)", shipment.getFrom().toString());
Assertions.assertEquals(1, shipment.getSize());
Assertions.assertEquals(0, shipment.getPickupServiceTime(), MatsimTestUtils.EPSILON);
Assertions.assertEquals(180, shipment.getDeliveryServiceTime(), MatsimTestUtils.EPSILON);
Assertions.assertEquals(TimeWindow.newInstance(25200, 64800), shipment.getPickupTimeWindow());
Assertions.assertEquals(TimeWindow.newInstance(25200, 64800), shipment.getDeliveryTimeWindow());
locationsPerShipmentElement.computeIfAbsent("ShipmentElement1_pickup", (k) -> new HashSet<>())
.add(shipment.getFrom().toString());
locationsPerShipmentElement.computeIfAbsent("ShipmentElement1_delivery", (k) -> new HashSet<>())
.add(shipment.getTo().toString());
}
Assertions.assertEquals(1, locationsPerShipmentElement.get("ShipmentElement1_pickup").size());
Assertions.assertEquals(3, locationsPerShipmentElement.get("ShipmentElement1_delivery").size());

for (String locationsOfShipmentElement : locationsPerShipmentElement.get("ShipmentElement1_delivery")) {
Link link = network.getLinks().get(Id.createLinkId(locationsOfShipmentElement));
Assertions.assertTrue(
FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, null, null));
Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape,
new String[] { "area1" }, null) || FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape,
new String[] { "area2" }, null));
}
}

@Test
void csvDemandReader() throws IOException {

Expand Down Expand Up @@ -417,15 +475,15 @@ void csvDemandReader() throws IOException {
} else if (demandInformationElement.getCarrierName().equals("testCarrier3")) {
Assertions.assertEquals(20, (int) demandInformationElement.getDemandToDistribute());
Assertions.assertNull(demandInformationElement.getNumberOfJobs());
Assertions.assertEquals(0.125, (double) demandInformationElement.getShareOfPopulationWithFirstJobElement(),
Assertions.assertEquals(0.125, demandInformationElement.getShareOfPopulationWithFirstJobElement(),
MatsimTestUtils.EPSILON);
Assertions.assertNull(demandInformationElement.getAreasFirstJobElement());
Assertions.assertNull(demandInformationElement.getNumberOfFirstJobElementLocations());
Assertions.assertNull(demandInformationElement.getLocationsOfFirstJobElement());
Assertions.assertEquals(400, (int) demandInformationElement.getFirstJobElementTimePerUnit());
Assertions.assertEquals(TimeWindow.newInstance(8000, 50000),
demandInformationElement.getFirstJobElementTimeWindow());
Assertions.assertEquals(0.4, (double) demandInformationElement.getShareOfPopulationWithSecondJobElement(),
Assertions.assertEquals(0.4, demandInformationElement.getShareOfPopulationWithSecondJobElement(),
MatsimTestUtils.EPSILON);
Assertions.assertEquals(1, demandInformationElement.getAreasSecondJobElement().length);
Assertions.assertEquals("area1", demandInformationElement.getAreasSecondJobElement()[0]);
Expand All @@ -442,8 +500,6 @@ void csvDemandReader() throws IOException {
private static void createDemandAndCheckCarrier(Path carrierCSVLocation, Scenario scenario, FreightCarriersConfigGroup freightCarriersConfigGroup,
ShpOptions.Index indexShape, Path demandCSVLocation, String shapeCategory,
Population population, Boolean combineSimilarJobs) throws IOException {
DemandDistributionOption demandDistributionOption = DemandDistributionOption.NoSelection;
TotalDemandGenerationsOption totalDemandGenerationOption = TotalDemandGenerationsOption.NoSelection;

DemandGenerationSpecification demandGenerationSpecification = new DefaultDemandGenerationSpecification();
// run methods
Expand All @@ -464,6 +520,25 @@ private static void createDemandAndCheckCarrier(Path carrierCSVLocation, Scenari
CarriersUtils.getCarriers(scenario).getCarriers().containsKey(Id.create("testCarrier3", Carrier.class)));
}

private static void createDemandAndCheckCarrierForParcel(Path carrierCSVLocation, Scenario scenario, FreightCarriersConfigGroup freightCarriersConfigGroup,
ShpOptions.Index indexShape, Path demandCSVLocation, String shapeCategory,
Population population, Boolean combineSimilarJobs) throws IOException {

DemandGenerationSpecification demandGenerationSpecification = new DemandGenerationSpecificationForParcelDelivery(0.5, 2.0, true);
// run methods
Set<CarrierInformationElement> allNewCarrierInformation = CarrierReaderFromCSV
.readCarrierInformation(carrierCSVLocation);
CarrierReaderFromCSV.createNewCarrierAndAddVehicleTypes(scenario, allNewCarrierInformation, freightCarriersConfigGroup,
indexShape, 1, null);
Set<DemandInformationElement> demandInformation = DemandReaderFromCSV.readDemandInformation(demandCSVLocation);
DemandReaderFromCSV.checkNewDemand(scenario, demandInformation, indexShape, shapeCategory);
DemandReaderFromCSV.createDemandForCarriers(scenario, indexShape, demandInformation, population, combineSimilarJobs,
null, demandGenerationSpecification);
Assertions.assertEquals(1, CarriersUtils.getCarriers(scenario).getCarriers().size());
Assertions.assertTrue(
CarriersUtils.getCarriers(scenario).getCarriers().containsKey(Id.create("testCarrier1", Carrier.class)));
}

/**
* These results should be the same for these carriers.
* The difference is only based on the population sample methods for carrier3, because a shareOfThePopulation is used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class FreightDemandGenerationTest {
private MatsimTestUtils utils = new MatsimTestUtils();

@Test
void testMain() {
void testMainRun() {
try {
Path output = Path.of(utils.getOutputDirectory());
Path vehicleFilePath = Path.of(utils.getPackageInputDirectory() + "testVehicleTypes.xml");
Expand Down Expand Up @@ -53,11 +53,52 @@ void testMain() {
"--populationSamplingTo", "1.0",
"--defaultJspritIterations", "3"
);
} catch (Exception ee) {
LogManager.getLogger(this.getClass()).fatal("there was an exception: \n" + ee);
ee.printStackTrace();
} catch (Exception e) {
LogManager.getLogger(this.getClass()).error("An error occurred while processing run the test: {}", e.getMessage(), e);
// if one catches an exception, then one needs to explicitly fail the test:
Assertions.fail();
}
}

@Test
void testMainRunWithParcelImplementation() {
DemandGenerationSpecification demandGenerationSpecificationForParcelDelivery = new DemandGenerationSpecificationForParcelDelivery(0.5, 2.0, true);
try {
Path output = Path.of(utils.getOutputDirectory());
Path vehicleFilePath = Path.of(utils.getPackageInputDirectory() + "testVehicleTypes.xml");
Path carrierCSVLocation = Path.of(utils.getPackageInputDirectory() + "testCarrierCSV_1.csv");
Path demandCSVLocation = Path.of(utils.getPackageInputDirectory() + "testDemandCSV_1.csv");
Path shapeFilePath = Path.of(utils.getPackageInputDirectory() + "testShape/testShape.shp");
String populationLocation = utils.getPackageInputDirectory() + "testPopulation.xml";
String network = "https://raw.githubusercontent.com/matsim-org/matsim-libs/master/examples/scenarios/freight-chessboard-9x9/grid9x9.xml";
String shapeCategory = "Ortsteil";
new FreightDemandGeneration(demandGenerationSpecificationForParcelDelivery).execute(
"--output", output.toString(),
"--carrierOption", "createCarriersFromCSV",
"--demandOption", "createDemandFromCSVAndUsePopulation",
"--populationOption", "usePopulationInShape",
"--populationSamplingOption", "createMoreLocations",
"--VRPSolutionsOption", "runJspritAndMATSim",
"--combineSimilarJobs", "false",
"--carrierFileLocation", "",
"--carrierVehicleFileLocation", vehicleFilePath.toString(),
"--shapeFileLocation", shapeFilePath.toString(),
"--shapeCRS", "WGS84",
"--populationFileLocation", populationLocation,
"--populationCRS", "WGS84",
"--network", network,
"--networkCRS", "WGS84",
"--networkChangeEvents", "",
"--shapeCategory", shapeCategory,
"--inputCarrierCSV", carrierCSVLocation.toString(),
"--inputDemandCSV", demandCSVLocation.toString(),
"--populationSample", "1.0",
"--populationSamplingTo", "1.0",
"--defaultJspritIterations", "3"
);
} catch (Exception e) {
LogManager.getLogger(this.getClass()).error("An error occurred while processing run the test: {}", e.getMessage(), e);
Assertions.fail();
}
}
}
Loading