A large manufacturing enterprise needs to ship a consignment of 1,000 tons of goods from its factory in City F to a distribution center in City D. There are three potential transportation routes available. Each route is composed of segments using different modes of transport (road or rail). The transportation cost for each segment is calculated based on a per ton-kilometer rate. Furthermore, certain road segments are subject to a congestion fee to account for the social costs of traffic. This fee is non-linear and is calculated as `Congestion Coefficient * (Total Traffic Flow)^2`. The total traffic flow on a segment is the sum of the cargo shipped by the enterprise and the existing background traffic from other companies.

The details for the three routes are provided in the table below. For congested segments, the existing background traffic is also listed.

| Route | Segment | Mode | Distance (km) | Base Fee ($/ton-km) | Congestion Coefficient | Background Traffic (tons) |
| ----- | ------- | ---- | ------------- | ------------------- | ---------------------- | ------------------------- |
| 1     | 1A      | Road | 150           | 2.00                | 5×10−7                 | 2,000                     |
|       | 1B      | Rail | 500           | 0.80                | -                      | -                         |
| 2     | 2A      | Road | 200           | 2.10                | -                      | -                         |
|       | 2B      | Road | 350           | 1.90                | 8×10−7                 | 1,500                     |
| 3     | 3A      | Road | 100           | 2.20                | -                      | -                         |
|       | 3B      | Rail | 600           | 0.75                | -                      | -                         |

How should the enterprise allocate the shipment of 1,000 tons of goods across the three routes to minimize the total transportation cost?

Problem type: NLP (Toy: 3 variables, 1 constraints, 3 nonzeros)

Mathematical model:
Set:
1. Route 
The set of available transportation routes, \( R = \{1, 2, 3\} \)

Parameter:
1. TotalTonnage
# The total amount of goods to be shipped
100 
2. LinearCostPerTon 
# The base transportation cost per ton for each route, excluding congestion fees. This is calculated by summing the costs of all segments for a given route (`distance * base_fee`).  
[700, 1085, 670] # in dollars per ton for Route 1, 2, and 3 respectively.
3. CongestionCoeff 
# The coefficient for calculating the congestion fee on applicable routes.  
[5e-7, 8e-7, 0] # Route 3 has no congestion fee.
4. BackgroundTraffic 
# The existing traffic volume on congested routes.  
[2000, 1500, 0] # in tons. Route 3 has no congested segment.

Decision variable:
1. TonnageOnRoute 
Continuous variable, \( TonnageOnRoute[r] \forall r \in R \), representing the amount of goods in tons transported via route \( r \).

Objective:
1. Minimize the total transportation cost. The objective is to minimize the sum of the linear transportation costs and the non-linear congestion fees for all routes.  
min: \( \sum_{r \in R} (LinearCostPerTon[r] \times TonnageOnRoute[r] + CongestionCoeff[r] \times (BackgroundTraffic[r] + TonnageOnRoute[r])^2) \)

Constraint:
1. Total Shipment Constraint. The sum of goods transported on all routes must equal the total tonnage required to be shipped.  
\( \sum_{r \in R} TonnageOnRoute[r] = TotalTonnage \)
2. Non-negativity constraint. The amount of goods transported on any route cannot be negative.  
\( TonnageOnRoute[r] \geq 0 \forall r \in R \)

Type:
Continuous, Non-linear, Linear  
NLP

Gurobi implementation:
import gurobipy as gp
from gurobipy import GRB


def solve_logistics(
    routes=["Route1", "Route2", "Route3"],
    total_tonnage_to_ship=1000.0,
    linear_cost_per_ton={
        "Route1": 700,
        "Route2": 1085,
        "Route3": 670
    },
    congestion_coeff={
        "Route1": 5e-7,
        "Route2": 8e-7,
        "Route3": 0
    },
    background_traffic={
        "Route1": 2000,
        "Route2": 1500,
        "Route3": 0
    }
):
    """
    Solves the transportation logistics problem with congestion pricing.
    """
    # Create a new model
    model = gp.Model("LogisticsOptimization")

    # --- Decision Variables ---
    # Amount of goods to ship on each route (in tons)
    tonnage_on_route = model.addVars(routes, name="TonnageOnRoute", lb=0)

    # --- Objective Function: Minimize Total Transportation Cost ---
    # Linear cost component
    total_linear_cost = gp.quicksum(
        linear_cost_per_ton[r] * tonnage_on_route[r] for r in routes
    )

    # Congestion cost component (this makes the objective quadratic)
    total_congestion_cost = gp.quicksum(
        congestion_coeff[r] *
        (background_traffic[r] + tonnage_on_route[r]) *
        (background_traffic[r] + tonnage_on_route[r])
        for r in routes if congestion_coeff[r] > 0
    )

    # Set the complete objective function
    model.setObjective(total_linear_cost + total_congestion_cost, GRB.MINIMIZE)

    # --- Constraints ---
    # 1. Total Tonnage Constraint: Must ship the exact total amount of goods
    model.addConstr(
        tonnage_on_route.sum('*') == total_tonnage_to_ship,
        name="TotalTonnageRequirement"
    )

    # Optimize the model
    model.optimize()

    # --- Return Results ---
    if model.status == GRB.OPTIMAL:
        return {"status": "optimal", "obj": model.objVal}
    else:
        return {"status": f"{model.status}"}


# Run the solver function
if __name__ == '__main__':
    result = solve_logistics()
    print(result)

Solution status: OPTIMAL
Optimal objective value: 670003.8