A distribution center needs to assign 4 delivery trucks to 4 customer locations for same-day shipments. Each truck has different transportation costs to each location due to varying distances and fuel consumption rates. The cost matrix (in USD) for assigning trucks to locations is as follows:

| Truck\Location | Loc1 | Loc2 | Loc3 | Loc4 |
|----------------|------|------|------|------|
| Truck1         | 120  | 150  | 180  | 90   |
| Truck2         | 110  | 140  | 170  | 80   |
| Truck3         | 130  | 160  | 190  | 100  |
| Truck4         | 140  | 170  | 200  | 110  |

The distribution center aims to minimize total transportation costs while ensuring each truck is assigned to exactly one location and each location receives exactly one truck.

Problem type: ILP (Small: 16 variables, 8 constraints, 32 nonzeros)

Mathematical model:
Set:
1. Trucks
The set of delivery trucks, \( T = \{1, 2, 3, 4\} \)
2. Locations
The set of customer locations, \( L = \{1, 2, 3, 4\} \)

Parameter:
1. CostMatrix
The transportation cost (USD) when truck \( t \) is assigned to location \( l \), represented as \( c_{tl} \):
- \( c_{11} = 120 \), \( c_{12} = 150 \), \( c_{13} = 180 \), \( c_{14} = 90 \)
- \( c_{21} = 110 \), \( c_{22} = 140 \), \( c_{23} = 170 \), \( c_{24} = 80 \)
- \( c_{31} = 130 \), \( c_{32} = 160 \), \( c_{33} = 190 \), \( c_{34} = 100 \)
- \( c_{41} = 140 \), \( c_{42} = 170 \), \( c_{43} = 200 \), \( c_{44} = 110 \)

Decision variable:
1. Assignment
Binary variable \( x_{tl} \in \{0, 1\} \) for all \( t \in T, l \in L \), where \( x_{tl} =  1 \) if truck \( t \) is assigned to location \( l \), and \( x_{tl} = 0 \) otherwise

Objective:
Minimize total transportation cost
min: \( \sum_{t\in T}\ Tsum_{l\in L}c_{tl}x_{tl} \)

Constraint:
1. Each truck must be assigned to exactly one location
\( \sum_{l\in L}x_{tl} = 1, \forall t \in T \)
2. Each location must receive exactly one truck
\( \sum_{t\in T}x_{tl} = 1, \forall l \in L \)

Type:
Binary, Linear,, Linear
ILP

Gurobi implementation:
import gurobipy as gp
from gurobipy import GRB


def solve_truck_assignment(
    trucks=[1, 2, 3, 4],
    locations=[1, 2, 3, 4],
    cost={
        (1, 1): 120, (1, 2): 150, (1, 3): 180, (1, 4): 90,
        (2, 1): 110, (2, 2): 140, (2, 3): 170, (2, 4): 80,
        (3, 1): 130, (3, 2): 160, (3, 3): 190, (3, 4): 100,
        (4, 1): 140, (4, 2): 170, (4, 3): 200, (4, 4): 110
    }
):
    """
    Solves the truck assignment problem.
    """
    # Create optimization model
    model = gp.Model("TruckAssignment")

    # Decision variables: x[t,l] = 1 if truck t is assigned to location l
    x = model.addVars(trucks, locations, vtype=GRB.BINARY, name="assign")

    # Set objective: minimize total transportation cost
    model.setObjective(gp.quicksum(cost[t, l] * x[t, l] for t in trucks for l in locations), GRB.MINIMIZE)

    # Constraints:
    # Each truck must be assigned to exactly one location
    model.addConstrs((x.sum(t, '*') == 1 for t in trucks), name="truck_assignment")

    # Each location must receive exactly one truck
    model.addConstrs((x.sum('*', l) == 1 for l in locations), name="location_assignment")

    # Optimize the model
    model.optimize()

    # Return results
    if model.status == GRB.OPTIMAL:
        return {"status": "optimal", "obj": model.ObjVal}
    else:
        return {"status": f"{model.status}"}


if __name__ == "__main__":
    result = solve_truck_assignment()
    print(result)

Solution status: OPTIMAL
Optimal objective value: 560