-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b20a9b
commit b3c8a1f
Showing
3 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/main/java/org/matsim/run/prepare/PrepareVehicleFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.matsim.run.prepare; | ||
|
||
import org.locationtech.jts.geom.Geometry; | ||
import org.matsim.api.core.v01.Id; | ||
import org.matsim.api.core.v01.TransportMode; | ||
import org.matsim.api.core.v01.network.Link; | ||
import org.matsim.api.core.v01.network.Network; | ||
import org.matsim.application.MATSimAppCommand; | ||
import org.matsim.application.options.ShpOptions; | ||
import org.matsim.core.network.NetworkUtils; | ||
import org.matsim.core.utils.geometry.geotools.MGC; | ||
import org.matsim.vehicles.Vehicle; | ||
import org.matsim.vehicles.VehicleType; | ||
import org.matsim.vehicles.VehicleUtils; | ||
import org.matsim.vehicles.Vehicles; | ||
import picocli.CommandLine; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class PrepareVehicleFile implements MATSimAppCommand { | ||
@CommandLine.Option(names = "--output", description = "path to output vehicles file", required = true) | ||
private String outputFiles; | ||
|
||
@CommandLine.Option(names = "--network", description = "path to network file file", required = true) | ||
private String networkPath; | ||
|
||
@CommandLine.Option(names = "--fleet-size", description = "number of vehicles to generate", defaultValue = "1000") | ||
private int fleetSize; | ||
|
||
@CommandLine.Mixin | ||
private ShpOptions shp = new ShpOptions(); | ||
|
||
public static void main(String[] args) { | ||
new PrepareVehicleFile().execute(args); | ||
} | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
Network network = NetworkUtils.readNetwork(networkPath); | ||
List<Link> startLocations = new ArrayList<>(); | ||
if (shp.isDefined()) { | ||
Geometry serviceArea = shp.getGeometry(); | ||
startLocations.addAll(network.getLinks().values().stream(). | ||
filter(l -> l.getAllowedModes().contains(TransportMode.car)). | ||
filter(l -> serviceArea.contains(MGC.coord2Point(l.getToNode().getCoord()))).toList()); | ||
} else { | ||
startLocations.addAll(network.getLinks().values().stream().filter(l -> l.getAllowedModes().contains(TransportMode.car)).toList()); | ||
} | ||
int numOfLinks = startLocations.size(); | ||
Random random = new Random(); | ||
|
||
// Prepare vehicle files | ||
VehicleType carVehicleType = VehicleUtils.createVehicleType(Id.create("car", VehicleType.class)); | ||
|
||
VehicleType rideVehicleType = VehicleUtils.createVehicleType(Id.create("ride", VehicleType.class)); | ||
|
||
VehicleType freightVehicleType = VehicleUtils.createVehicleType(Id.create("freight", VehicleType.class)); | ||
freightVehicleType.setLength(15); | ||
freightVehicleType.setPcuEquivalents(3.5); | ||
|
||
VehicleType drtVehicleType = VehicleUtils.createVehicleType(Id.create("conventional_vehicle", VehicleType.class)); | ||
drtVehicleType.getCapacity().setSeats(8); | ||
|
||
Vehicles vehicles = new VehiclesImpl(); | ||
vehicles.addVehicleType(carVehicleType); | ||
vehicles.addVehicleType(rideVehicleType); | ||
vehicles.addVehicleType(freightVehicleType); | ||
vehicles.addVehicleType(drtVehicleType); | ||
|
||
for (int i = 1; i <= fleetSize; i++) { | ||
Link startLink = startLocations.get(random.nextInt(numOfLinks)); | ||
|
||
Vehicle drtVehicle = VehicleUtils.createVehicle(Id.createVehicleId("KEXI-" + i), drtVehicleType); | ||
drtVehicle.getAttributes().putAttribute("dvrpMode", TransportMode.drt); | ||
drtVehicle.getAttributes().putAttribute("startLink", startLink.getId().toString()); | ||
drtVehicle.getAttributes().putAttribute("serviceBeginTime", 21600.0); | ||
drtVehicle.getAttributes().putAttribute("serviceEndTime", 82800.0); | ||
|
||
vehicles.addVehicle(drtVehicle); | ||
} | ||
|
||
VehicleUtils.writeVehicles(vehicles, outputFiles); | ||
return 0; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/org/matsim/run/prepare/VehiclesFactoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* *********************************************************************** * | ||
* project: org.matsim.* | ||
* * | ||
* *********************************************************************** * | ||
* * | ||
* copyright : (C) 2008 by the members listed in the COPYING, * | ||
* LICENSE and WARRANTY file. * | ||
* email : info at matsim dot org * | ||
* * | ||
* *********************************************************************** * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* See also COPYING, LICENSE and WARRANTY file * | ||
* * | ||
* *********************************************************************** */ | ||
|
||
package org.matsim.run.prepare; | ||
|
||
import org.matsim.api.core.v01.Id; | ||
import org.matsim.vehicles.Vehicle; | ||
import org.matsim.vehicles.VehicleType; | ||
import org.matsim.vehicles.VehicleUtils; | ||
import org.matsim.vehicles.VehiclesFactory; | ||
|
||
/** | ||
* deliberately non-public since there is an interface. kai, nov'11 | ||
* | ||
* @author dgrether | ||
*/ | ||
final class VehiclesFactoryImpl implements VehiclesFactory { | ||
// The design is roughly as follows: | ||
// * VehicleType and its sub-types VehicleCapacity and CostInformation are no longer behind interfaces. They are so "small" that we will assume that | ||
// we will never optimize them. Which means that they can also be instantiated directly; the methods here are there for historical reasons and for | ||
// convenience. | ||
// * Hierarchical sub-types are gone. E.g. there is no FreighCapacity within VehicleCapacity any more. | ||
// * EngineInformation is deprecated and should go away soon. In practice, the hbefa entries are used, and they are used via Attributable. | ||
// kai/kai, aug'19 | ||
|
||
|
||
/** | ||
* deliberately non-public since there is a factory. kai, nov'11 | ||
*/ | ||
VehiclesFactoryImpl() { | ||
} | ||
|
||
@Override | ||
public Vehicle createVehicle(Id<Vehicle> id, VehicleType type) { | ||
return VehicleUtils.createVehicle(id, type ); | ||
} | ||
|
||
@Override | ||
public VehicleType createVehicleType(Id<VehicleType> typeId) { | ||
return VehicleUtils.createVehicleType(typeId ); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* *********************************************************************** * | ||
* project: org.matsim.* | ||
* BasicVehiclesImpl | ||
* * | ||
* *********************************************************************** * | ||
* * | ||
* copyright : (C) 2009 by the members listed in the COPYING, * | ||
* LICENSE and WARRANTY file. * | ||
* email : info at matsim dot org * | ||
* * | ||
* *********************************************************************** * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* See also COPYING, LICENSE and WARRANTY file * | ||
* * | ||
* *********************************************************************** */ | ||
package org.matsim.run.prepare; | ||
|
||
import org.matsim.api.core.v01.Id; | ||
import org.matsim.api.core.v01.IdMap; | ||
import org.matsim.core.utils.misc.Counter; | ||
import org.matsim.vehicles.*; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
|
||
/** | ||
* @author dgrether | ||
* @author jwjoubert | ||
*/ | ||
final class VehiclesImpl implements Vehicles { | ||
private final Map<Id<VehicleType>, VehicleType> vehicleTypes; | ||
private final Map<Id<Vehicle>, Vehicle> vehicles; | ||
private final VehiclesFactoryImpl builder; | ||
|
||
private final Counter counter = new Counter("[VehiclesImpl] added vehicle # " ); | ||
|
||
/** | ||
* deliberately non-public since there is a factory. kai, nov'11 | ||
*/ | ||
VehiclesImpl(){ | ||
this.vehicleTypes = new IdMap<>(VehicleType.class); // FIXME potential iteration order change | ||
this.builder = new VehiclesFactoryImpl() ; | ||
this.vehicles = new IdMap<>(Vehicle.class); // FIXME potential iteration order change | ||
} | ||
|
||
|
||
@Override | ||
public VehiclesFactory getFactory() { | ||
return this.builder; | ||
} | ||
|
||
@Override | ||
public final Map<Id<Vehicle>, Vehicle> getVehicles() { | ||
return Collections.unmodifiableMap(this.vehicles); | ||
} | ||
|
||
|
||
@Override | ||
public Map<Id<VehicleType>, VehicleType> getVehicleTypes() { | ||
return Collections.unmodifiableMap(this.vehicleTypes); | ||
} | ||
|
||
/** | ||
* Add the vehicle to the container. | ||
* | ||
* @param v | ||
* @throws IllegalArgumentException if another {@link Vehicle} with | ||
* the same {@link Id} already exists in the container. | ||
*/ | ||
@Override | ||
public void addVehicle(final Vehicle v) { | ||
/* Validation. */ | ||
if(this.getVehicles().containsKey(v.getId())){ | ||
throw new IllegalArgumentException("Vehicle with id = " + v.getId() + " already exists."); | ||
} | ||
|
||
/* Check if the VehicleType associated with the vehicle already exist. | ||
* Here only an error message is given. A RuntimeException is thrown | ||
* when the MatsimVehicleWriter is called (JWJ, '14). */ | ||
if(!this.vehicleTypes.containsKey(v.getType().getId())){ | ||
throw new IllegalArgumentException("Cannot add Vehicle with type = " + v.getType().getId().toString() + | ||
" if the VehicleType has not been added to the Vehicles container."); | ||
} | ||
|
||
/* Add the vehicle. */ | ||
this.vehicles.put(v.getId(), v); | ||
this.counter.incCounter(); | ||
} | ||
|
||
/** | ||
* Removes the vehicle with the given Id | ||
* | ||
* @param vehicleId | ||
*/ | ||
@Override | ||
public void removeVehicle(final Id<Vehicle> vehicleId) { | ||
this.vehicles.remove(vehicleId); | ||
} | ||
|
||
/** | ||
* Adds the vehicle type to the container. | ||
* | ||
* @param type | ||
* @throws IllegalArgumentException if another {@link VehicleType} with the | ||
* same {@link Id} already exists in the container. | ||
*/ | ||
@Override | ||
public void addVehicleType(VehicleType type){ | ||
/* Validation. */ | ||
if(this.getVehicleTypes().containsKey(type.getId())){ | ||
throw new IllegalArgumentException("Vehicle type with id = " + type.getId() + " already exists."); | ||
} | ||
|
||
/* Add the vehicle type. */ | ||
this.vehicleTypes.put(type.getId(), type); | ||
} | ||
|
||
@Override | ||
public void removeVehicleType(Id<VehicleType> vehicleTypeId) { | ||
for (Vehicle veh : this.vehicles.values()) { | ||
if (veh.getType().getId().equals(vehicleTypeId)) { | ||
throw new IllegalArgumentException("Cannot remove vehicle type as it is used by at least one vehicle."); | ||
} | ||
} | ||
this.vehicleTypes.remove(vehicleTypeId); | ||
} | ||
} |