Font size

Phase 5. Add Condition for the Additional Cost

In the previous phase we exported the modified calculator and checked its calculations.

In this phase we will adjust the calculator to make it apply the additional cost to the deliveries that exceed certain distance length. In other words, we want to check the distance covered by each vehicle to later apply the additional cost to those who exceed the specified limit.

The first thing we need to do is to rename the calculator and the class.

Rename calculator

  1. In the @CustomTypeClass annotation change the name value to Phase 5 Additional cost for exceeding distance limit.
  2. Now change the class name to Phase5AdditionalCostMinDistance. This step is required to avoid conflicts when exporting the customized calculator to anyLogistix.

    The parameter should now look like this:

    @CustomTypeClass(name = "Phase 5 Additional cost with min distance")
        public class Phase5AdditionalCostMinDistance implements ITransportationCostCalculator{
  3. Now save the new version to a new file by right-clicking the model in the projects tree and selecting Save as….

Now we need to create a new parameter, since we plan to define the minimum distance limit with a parameter.

Create a new parameter

  1. Add the new Min distance for 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.
  3. Then create the minDistance variable for this parameter. This variable will keep the parameter's value.
    Finally, the parameter should look like this:
    @Parameter(name = "Min distance for additional cost", type = EditorType.DoubleEditor)
    	private double minDistance;

Updating the cost calculator for the simulation-based experiments

To define the condition, we will use the Java ternary operator. This is a simplified version of the if…else statement.

Add the condition to the cost calculator

  1. Adjust the cost() method by adding the distance >= minDistance ? addCost * shipment.getNVehicles() :0 condition.
    The meaning behind this construction is simple. Condition ? expression if true : expression if false, i.e. if the distance covered by the vehicle in this shipment is longer than the minimum distance, then the additional cost will be added to the first part, else nothing will be added.
        public double cost(IShipment shipment, double distance, VehicleType vehicleType) {
        return getTotalVolume(shipment) * distance * costPerUnit + (distance >= minDistance ? addCost * shipment.getNVehicles() : 0);

Updating the cost calculator for the Network optimization experiment

For the Network Optimization experiment calculations, the changes will affect only the vehicleFlowCost() method.

Add the condition to the cost calculator

  1. Adjust the the vehicleFlowCost() method by adding the distance >= minDistance ? addCost : 0; condition.
    We use the same ternary operator here. The only difference is that in the vehicleFlowCost() method we do not need to multiply the addCost value by the number of used vehicles, as we have previously explained.
        public double vehicleFlowCost(double flowSize, double distance, VehicleType vehicleType){
        return distance >= minDistance ? addCost : 0;

Updating the description of the cost calculator parameters

The last change we need to make is to add the Minimum distance parameter value in the getDescription() method.

Update the calculator's description

  1. Add the if distance >= " + minDistance condition to the getDescription() method.
        public String getDescription() {
        return costPerUnit + " * product(" + unit + ") * distance + " + addCost + " per trip, if distance >= " + minDistance;
  2. Now save the changes by using the Ctrl + S shortcut.

And we are done. This was all we need to modify in the policy to add the minimum distance condition.

How can we improve this article?