C# sample for fleet route with time windows

C# sample for fleet route with time windows

using XtremeRoute.Shared;
using XtremeRoute.Route;
using XtremeRoute.Network;
using XtremeRoute.Tasks;

// Example class in application for a service station to visit by one vehicle
// Classes like this is in the application and usually created from GUI, DB, network stream, etc
public class OurServiceStation
{
    public string Name { get; set; }
    public float Demand { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public TimeSpan ServiceTime { get; set; }
    public DateTime OpensForService { get; set; }
    public DateTime ClosesForService { get; set; }
}

// Example class in application for the depot (start and stop) for the fleet route
// Classes like this is in the application and usually created from GUI, DB, network stream, etc
public class OurDepot
{
    public string Name { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

// Example class in application for a work day, for example for a packet delivery firm
// Classes like this is in the application and usually created from GUI, DB, network stream, etc
public class OurWorkDay
{
    public OurDepot Depot { get; set; }
    public OurServiceStation[] Stations { get; set; }
    public DateTime DepartureTime { get; set; }
    public bool UseFlexibleDepartureTime { get; set; }
    public TimeSpan MaximumDriverWorkTime { get; set; }
    public double[] VehicleCapacities { get; set; }
    public double DefaultVehicleCapacity { get; set; }
}

// Calculates a fleet route from instances of example classes above and the XtremeRouter.Router class
public static FleetPaths FleetRouteWithTimeWindows(Router router, OurWorkDay workDay)
{
    // Create depot from input 
    Node baseDepotNode = router.Storage.SpatialIndex.FindClosestNode(workDay.Depot.Longitude, workDay.Depot.Latitude, true);
    RouteNode depotNode = new RouteNode(router.Storage, baseDepotNode, RouteNode.RouteNodeType.Start, workDay.Depot.Name, "Our depot");
    depotNode.DepartureTime = workDay.DepartureTime;

    // Add all service stations specified in input instances
    RouteNode[] nodes = new RouteNode[workDay.Stations.Length];
    for (int i = 0; i < nodes.Length; i++)
    {
        OurServiceStation stat = workDay.Stations[i];
        Node baseNode = router.Storage.SpatialIndex.FindClosestNode(stat.Longitude, stat.Latitude, true);
        nodes[i] = new RouteNode(router.Storage, baseNode, RouteNode.RouteNodeType.FleetNode);
        nodes[i].Title = stat.Name;

        // Setup time window for this service station
        nodes[i].ServiceTime = stat.ServiceTime;
        nodes[i].EarliestTimeOfService = stat.OpensForService;
        nodes[i].LatestTimeOfService = stat.ClosesForService;
    }

    // Create the argument class for fleet routing
    Router.RouteFleetArgs fleetArgs = new Router.RouteFleetArgs(depotNode, nodes, FleetRouteType.Roundtrip, workDay.DefaultVehicleCapacity);

    // Set if we allow routes to start later than departure time 
    fleetArgs.DynamicDepartureTime = workDay.UseFlexibleDepartureTime;

    // Set up fixed vehicles (optional)
    if (workDay.VehicleCapacities != null)
    {
        FleetRouteVehicles vehicles = new FleetRouteVehicles();
        vehicles.Vehicles = new FleetRouteVehicle[workDay.VehicleCapacities.Length];
        for (int i = 0; i < workDay.VehicleCapacities.Length; i++)
        {
            vehicles.Vehicles[i] = new FleetRouteVehicle();
            vehicles.Vehicles[i].Capacity = workDay.VehicleCapacities[i];
        }
        fleetArgs.FixedVehicles = vehicles;
    }

    // Set the time (in seconds) that each route are allowed to take
    fleetArgs.MaxSingleRouteCost = workDay.MaximumDriverWorkTime.TotalSeconds;

    // All settings are now set, generate the fleet route
    FleetPaths paths = router.RouteFleet(fleetArgs);

    // paths now contain a collection of NodePaths, where each 
    // NodePaths represents one vehicle in the fleet route.
    return paths;
}
Select all code
Close
Copyright © 2012 Xtreme Route - All Rights Reserved. Contact us: