Skip to content

Instantly share code, notes, and snippets.

@FrankRuns
Created January 4, 2025 13:58
Show Gist options
  • Save FrankRuns/b32c243b3e6eaa89c400f4f7c3ba984c to your computer and use it in GitHub Desktop.
Save FrankRuns/b32c243b3e6eaa89c400f4f7c3ba984c to your computer and use it in GitHub Desktop.
You are a highly capable Python programmer who has access to locations.csv, which contains columns name, longitude, latitude, and type.
Please write a Python script that does the following:
Reads locations.csv into a pandas DataFrame.
Enumerates every possible Origin–Destination (OD) pair, but skips certain flows based on the following rules (via a helper function is_valid_flow(origin_type, dest_type)):
No shipments from Plant -> Customer
No shipments from DC -> Plant
No shipments from Customer -> DC
No shipments from Customer -> Plant
No shipments from Plant -> Plant
For each valid OD pair, compute:
Haversine distance in miles using Earth’s radius ≈ 3958.8 miles (helper function haversine_distance(lat1, lon1, lat2, lon2)).
Transit time with piecewise speed brackets (e.g., “Local” <50 miles gets a different average speed, “Medium” 50–300 miles has another, “Long Haul” >300 miles another, etc.) – implement in calc_transit_time(distance_miles).
Lane cost with tiered or piecewise logic (minimum charge for short runs, decreasing $/mile for longer distances, etc.) – implement in calc_lane_cost(distance_miles, mode="FTL").
A mode field (assume "FTL" for now).
Outputs a long DataFrame in which each row is one valid OD pair, containing columns:
origin_id, origin_type
dest_id, dest_type
distance_miles
transit_time_hours
lane_cost_usd
time_assumption (like “Local,” “Medium Haul,” or “Long Haul”)
cost_assumption (like “Flat $250 min,” “$3.00/mi tier,” etc.)
mode
Prints or exports this DataFrame so it can be used in a network design or flow analysis.
Also define these helper functions clearly in your code:
haversine_distance(lat1, lon1, lat2, lon2)
calc_transit_time(distance_miles)
calc_lane_cost(distance_miles, mode="FTL")
is_valid_flow(origin_type, dest_type)
build_long_od_dataframe(df_locations, mode="FTL")
Finally, provide an example usage that:
Reads locations.csv
Builds the “long” DataFrame
Prints its first few rows and/or writes it out to od_matrix.csv
Important: Make sure the script is complete and self-contained, and that each helper function is fully implemented.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment