Click or drag to resize

Modifying the Routing Strategy

Verizon Connect Logo
Print this page
Learn more about Verizon Connect GeoBase.
Get information about the latest release
Introduction

The GeoBase routing engine exposes powerful functionality through a route's Strategy property. This section of the tutorial describes common adjustments to the behavior of the routing engine.

Preventing U-turns

In many instances a U-turn may be undesirable (such as when routing a large truck) or illegal. To instruct the routing engine to avoid U-turns modify the AllowUTurns property:

C#
/* prohibit U-turns */ 
myRoute.Strategy.AllowUTurns = false;
Creating a Custom Link Cost Evaluation Function

When creating a route the GeoBase routing engine evaluates the possible routes between stops based on the calculated cost for each link in the route. A link with a high cost is unlikely to be chosen in the 'optimum' route. For our example, consider the shown route near a freeway. The routing engine automatically routes along the freeway because using the freeway is seen as being optimum in terms of minimizing the length of the journey.

routing highway

If you want to modify this behavior (say for a user who requests not to be routed along a freeway) you must create a RouteCustomCostFunc function. This allows you to override GeoBase's cost decisions. Freeways typically have at least two lanes in each direction. This means that a freeway link will generally have the LANE_CAT flag set to a value greater than 1.

Tip Tip

A CustomRoutingInfo has a number of properties that are useful in evaluating link costs, most notably using the Flags and ExtraData properties.

Note Note

The RouteCustomCostFunc function can not be used with a GeoStreamRepository.

We create a custom evaluation function as follows, placing the function in the same class as our route:

C#
void myCustomCostFunc(CustomRoutingInfo linkInfo, ref LinkCost linkCost) { 

    /* avoid roads with more than one lane - see Routing.RouteFlag documentation */
    if (linkInfo.Flags.LANE_CAT != '1') {
        linkCost.UndesirableLink = true;
    }
}

Next, we must apply our custom link cost evaluation function to our route:

C#
myRoute.Strategy.CustomCostFunction = new RouteCustomCostFunc(this.myCustomCostFunc);

Now, when GeoBase creates directions we are no longer routed along the freeway:

routing nohighway

Because our cost evaluation function is called after GeoBase has evaluated the cost of a link we can also adjust the cost (instead of simply setting the link as being UndesirableLink). For example to double the cost of traveling a freeway link (to discourage the routing engine from routing along a freeway) we could do the following:

C#
void myCustomCostFunc(CustomRoutingInfo linkInfo, ref LinkCost linkCost) {
    /* avoid roads with more than one lane - see Routing.RouteFlag documentation */
    if (linkInfo.Flags.LANE_CAT != '1') {
        linkCost.addCost(linkCost.Cost);
    }
}
Note Note
The cost of traveling a link should be modified using the addCost(Int32) method.
Note Note

In certain circumstances, it may be necessary to call IRoute.ForceRecalculate after making changes to VehicleSpec or RoutingStrategy. See ForceRecalculate for more information.

Next Topic