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

How to use a specific vehicleType for drt vehicles? #3121

Open
markusstraub opened this issue Feb 20, 2024 · 8 comments
Open

How to use a specific vehicleType for drt vehicles? #3121

markusstraub opened this issue Feb 20, 2024 · 8 comments

Comments

@markusstraub
Copy link
Contributor

markusstraub commented Feb 20, 2024

I try to set a maximum velocity for drt vehicles and tried to do this via vehicleDefinitions like this:

<?xml version="1.0" encoding="UTF-8"?>
<vehicleDefinitions xmlns="http://www.matsim.org/files/dtd"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.matsim.org/files/dtd http://www.matsim.org/files/dtd/vehicleDefinitions_v1.0.xsd">
  <vehicleType id="car">
  </vehicleType>
  <vehicleType id="drt">
    <maximumVelocity meterPerSecond="5.5"/>
  </vehicleType>
  <vehicle id="drtVehicle1" type="drt" />
  <vehicle id="drtVehicle2" type="drt" />
</vehicleDefinitions>

in combination with

<module name="qsim">
    <param name="vehiclesSource" value="modeVehicleTypesFromVehiclesData" />
</module>
<module name="vehicles">
    <param name="vehiclesFile" value="path/to/vehicleDefinitions.xml" />
</module>

.. but this does not work.

Why? Apparently vehicleType is only parsed for main modes (and drt must not be defined as main mode): see PrepareForSimImpl.getVehicleTypesForAllNetworkAndMainModes.

The VrpAgentSource called in QSim.createAgents was constructed with this vehicle type.

This happens because VrpAgentSourceQSimModule gets the default vehicle type after calling getModalInstance(VehicleType.class)).

I guess in the end it all boils down to DrtModeModule calling a FleetModule constructor that simply instantiates the default vehicle type. There is no way I see via config.xml to call this constructor instead: public FleetModule(String mode, URL fleetSpecificationUrl, VehicleType vehicleType)

So my question is: is there a way to properly configure a vehicle type for DVRP / DRT vehicles to e.g. set a maximum velocity? Ideally via the config.xml.

@markusstraub
Copy link
Contributor Author

@michalmac maybe you could shed light on this since you are the author of DrtModeModule :)

@steffenaxer
Copy link
Collaborator

Dear @markusstraub sorry for the late repy. Always had your post in my mind but not the time to look into the code. I'm using the VehicleUtils.createVehiclesContainer() to create a Vehicles object. Steps to create a custom vehicle type are as follows:

  • DvrpVehicleSpecification to create a specs for a drt vehicle
  • Create a custom Vehicle with a custom VehicleType with the help ofVehicleUtils.createVehicle(Id.createVehicleId(vehicleId), vehType);
  • Add this Vehicle into Vehicles. Dump this via MatsimVehicleWriter
  • Use this file in your vehicles config group.

Sorry I have no shareable code available.

@markusstraub
Copy link
Contributor Author

Thanks a lot @steffenaxer for taking the time to answer! I followed your advice and came up with the example code below, but when comparing the output to the files I already have I see no difference. So your workflow is also loading vehicle xml files via config (and not somehow in code)?

As mentioned above I debugged the vehicle creation process and did not see a way for DrtModeModule to pick up the configured vehicle :/ .. and confirmed this by watching my drt vehicles drive too fast in Simunto Via.

To be clear: there is no problem in having drt vehicles with the correct id, but everything specified in the vehicleType (in my case max velocity, but this would also affect, length, etc.) is lost.

public static void main(String[] args) {
	String vehicleId = "testVehicleId";

	VehicleType vehicleType = VehicleUtils.createVehicleType(Id.create("customType", VehicleType.class));
	vehicleType.setMaximumVelocity(8.33333);
	vehicleType.getCapacity().setSeats(5);
	Vehicle vehicle = VehicleUtils.createVehicle(Id.createVehicleId(vehicleId), vehicleType);

	Vehicles vehicles = VehicleUtils.createVehiclesContainer();
	vehicles.addVehicleType(vehicleType);
	vehicles.addVehicle(vehicle);

	List<DvrpVehicleSpecification> dvrpSpecs = new ArrayList<>();
	dvrpSpecs.add(ImmutableDvrpVehicleSpecification
			.newBuilder()
			.id(Id.create(vehicleId, DvrpVehicle.class))
			.startLinkId(Id.createLinkId("1234"))
			.capacity(4)
			.serviceBeginTime(0d)
			.serviceEndTime(86400d).build());

	new FleetWriter(dvrpSpecs.stream()).write("/tmp/vehicles_fleet.xml");
	new MatsimVehicleWriter(vehicles).writeFile("/tmp/vehicles.xml");
}

@steffenaxer
Copy link
Collaborator

@markusstraub sorry, now I got your point. Yes I would support your finding. You are absolutely right. The FleetModule loads per default the defaultVehicleType. Thanks for pointing to this issue. Technically it should be possible that each vehicle uses it's own, dedicated vehicleType, that would be best. If nothing is definied for a vehicle the vehicle should use the defaultVehicleType.

@nkuehnel
Copy link
Member

Not sure if this is what you're looking for but this worked for me once, based on:
https://github.com/matsim-org/matsim-libs/blob/master/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/trafficmonitoring/DvrpModeLimitedMaxSpeedTravelTimeModule.java

and

https://github.com/matsim-org/matsim-libs/blob/master/contribs/drt/src/main/java/org/matsim/contrib/drt/run/examples/RunMultiModeDrtExample.java


VehicleType vehicleType = controller.getScenario()
                .getVehicles()
                .getVehicleTypes()
                .get(Id.create(modeConfig.getMode(), VehicleType.class));
double maxSpeed = vehicleType
                .getMaximumVelocity();

  controller.addOverridingModule(
                new DvrpModeLimitedMaxSpeedTravelTimeModule(modeConfig.getMode(), config.qsim().getTimeStepSize(),
                        maxSpeed));
 controller.addOverridingModule(new AbstractDvrpModeModule(modeConfig.getMode()) {
            @Override
            public void install() {
                // max allowed speed for AV
                bindModal(VehicleType.class).toInstance(vehicleType);
            }
        });

@steffenaxer
Copy link
Collaborator

steffenaxer commented Mar 20, 2024

I think the java way is quite clear, @markusstraub asked for a solution that natively works only controlled by configs.
The java way to load your custom stuff requires to overload bindModal(VehicleType.class).toInstance(vehicleType);

@markusstraub
Copy link
Contributor Author

Thank you for the hint, @nkuehnel, I will look into it as immediate solution.

Long-term it would be great if the drt/dvrp modules could pick up what is configured in vehicleDefinitions, I intuitively expected this to be the way to go - and I guess other new users will do the same.

@kainagel
Copy link
Contributor

I very much agree that it would be nice to have this consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants