C# code for BenchmarkRandomNW

C# code for BenchmarkRandomNW

public string BenchmarkRandomNW(string mainDir, Tasker server, int iters, CostType costType, bool useLandmarks, bool bigVehicle)
{
    Random r = new Random(1); // fixed seed so same routes are generated each time
    string msg = "";

    // Loop over list of network to run the benchmark on
    string[] countries = { "germany", "us", "united-kingdom", "italy" };
    foreach (string network in countries)
    {
        // Load network and create router class
        Storage st = new Storage(mainDir, network);
        Router router = new Router(server, 4, st, costType);

        // Landmark or AStar
        router.Settings.RouteAlgorithm = useLandmarks ? RouteAlgorithm.Landmarks : RouteAlgorithm.AStar;

        // Simulate a big vehicle, slightly slower overall speed and tries to stay away from smaller roads
        if (bigVehicle)
        {
            router.Settings.GlobalSpeedModification = 0.9;
            BaseRoadType[] smallRoads = { BaseRoadType.Unpaved, BaseRoadType.LivingStreet, BaseRoadType.LivingStreetNarrow,
                                    BaseRoadType.Path, BaseRoadType.Private, BaseRoadType.Residential, BaseRoadType.ResidentialNarrow };
            foreach (BaseRoadType rt in smallRoads)
                router.Settings.SetRoadTypeModification(rt, DynamicModificationType.Relative, 0.5, 0.01, false);
            router.Settings.UseDynamicRouting = true;
        }

        // Keep all routes to the main landarea, to avoid failed routes
        Subnets s = new Subnets(st);
        Subnets.Subnet[] subnets = s.GetSubnets(Subnets.SubnetConnectivity.None);
        Subnets.Subnet mainSub = subnets[0]; // sorted, so biggest is always on index 0
        Node[] nodesInSub = mainSub.Nodes;

        // Do lots of random routes, and measure execution time
        Watch exeTime = new Watch();
        double totTime = 0, totDist = 0;
        for (int i = 0; i < iters; i++)
        {
            Node n1 = nodesInSub[r.Next(0, mainSub.NrNodes)];
            Node n2 = nodesInSub[r.Next(0, mainSub.NrNodes)];
            DualCost dc = router.RouteDualCost(n1, n2);
            totTime += dc.Time;
            totDist += dc.Distance;
        }
        msg += "Network: " + st.Info.Name + ", Size: " + st.NrLinks + " road segments, ms/route: " + exeTime.MilliSeconds / iters +
                ", Average travel time: " + TimeSpan.FromSeconds(totTime / iters) + ", Avg distance: " + Math.Round(totDist / iters, 1) + " " + 
                st.DistanceUnit.ToString() + ", LM: " + useLandmarks.ToString() + ", BigV: " + bigVehicle.ToString() + Environment.NewLine;
    }
    return msg;
}
Select all code
Close
Copyright © 2012 Xtreme Route - All Rights Reserved. Contact us: