Font size

Phase 3. Add Additional Cost for Each Shipping

In the previous phase of this tutorial, we observed the inside of the product&distance-based calculator, one of the default transportation cost calculators. In this phase we will slightly modify it.

We want to change the way the transportation cost is calculated. Right now, the cost is calculated as:
volume of the product * distance covered by the vehicle during this delivery * transportation cost of 1 product unit per 1 distance unit.

We want to add additional cost as a fixed value per vehicle. The value of the additional cost will be received through the policy parameters.

This customization can be done in several steps.

First, we need to change the name of the calculator to be able to export it to anyLogistix without any conflicts.

Rename calculator

  1. In the @CustomTypeClass annotation change the name value to Phase 3 Additional cost for each.
  2. Now change the class name to Phase3AdditionalCostForEach. This step is required to avoid conflicts when exporting the customized calculator to anyLogistix.

When done, it should look like this

@CustomTypeClass(name = "Phase 3 Additional cost for each")
public class Phase3AdditionalCostForEach implements ITransportationCostCalculator{

A good practice is to save the new version of the customizable object into a new .alp file to later be able to go back to the previous version if required.

To do that you need to right-click the model name, select the Save as… option, and specify the place to save the model.

Save changes

  1. Right-click the model in the projects tree and select Save as….

  2. In the opened dialog specify the location to save the new file to, then click Finish.

Now we will change the calculation logic by adding a new parameter, which will store the value of the additional cost that we want to consider.

Introduce a new calculation parameter

  1. Add the new Additional cost parameter after the last calculator's parameter. You will later see this name in the Cost Calculation Parameters dialog of the anyLogistix user interface.
  2. Set the parameter editor type to DoubleEditor, since it is the most useful type for the cost value
  3. Then create the addCost variable for this parameter. This variable will keep the parameter's value.
    Finally, the parameter should look like this:
    @Parameter(name = "Additional cost", type = EditorType.DoubleEditor)
    	private double addCost;

Updating the cost calculator for the simulation-based experiments

The transportation cost for the simulation experiment is calculated with the cost() method. We need to include the new Additional cost parameter into the cost calculating process.

Add new parameter to the cost calculator

  1. Add the addCost variable of the just created Additional cost parameter at the end of the cost() method, since the additional cost does not depend on the product amount and the distance.
  2. Now we will multiply the added variable by the number of vehicles involved in this shipment, since the additional cost is applied to each vehicle, and the shipment may require more than one vehicle. We can get this value directly from shipment object by using the getNVehicles() method. Like this:
    	public double cost(IShipment shipment, double distance, VehicleType vehicleType) {
    		return getTotalVolume(shipment) * distance * costPerUnit + addCost * shipment.getNVehicles();

    This way the cost will be calculated as if we are using the product&distance-based calculator with the additional cost applied to this shipment.

Updating the cost calculator for the optimization experiment

In case of the Network Optimization experiment the transportation cost is calculated separately for the flow-based costs and the vehicle-based costs. Since the additional cost is applied per vehicle, we will not change the flow-based calculation. We will update the vehicleFlowCost() method only. This method calculates the costs for each vehicle involved in transportation.

Right now, the method simply returns 0, since the vehicle-based cost is not defined in the product&distance-based calculator. We want to apply the additional cost every time a vehicle sets off to deliver a product. So, the vehicleFlowCost() method should simply return the addCost value.

We do not need to multiply the added cost by the number of used vehicles, because the method is designed to return the cost of using 1 vehicle. The value this method returns will be multiplied by the number of vehicles outside of the cost calculator.

Add new parameter to the cost calculator

  1. Add the addCost variable of the just created Additional cost to the end of the vehicleFlowCost() method.
        public double vehicleFlowCost(double flowSize, double distance, VehicleType vehicleType){
        return addCost;

Updating the description of the cost calculator parameters

A description does not affect the calculations, it simply shows how the selected calculator estimates expenses. It is not obligatory to update descriptions, but they make the calculator usage more convenient, so let's do it.

Update the calculator's description

  1. Add the addCost variable and the " per trip" string to the getDescription() method.
        public String getDescription() {
        return costPerUnit + " * product(" + unit + ") * distance + " + addCost + " per trip";
  2. Now save the changes by using the Ctrl + S shortcut.

Now the calculator will consider the additional transportation cost.

How can we improve this article?