Skip to content

Commit c3dfd71

Browse files
committed
done
1 parent 7215e0a commit c3dfd71

3 files changed

Lines changed: 39 additions & 25 deletions

File tree

665 Bytes
Binary file not shown.

freight_project/backend/app.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
)
1717

1818
sim = None # Global simulation instance
19+
sim_lock = threading.Lock() # Lock for thread-safe simulation access
1920

2021
# Background thread to continuously update truck positions and reassign loads
2122
def run_sim():
@@ -37,25 +38,32 @@ def init_sim(
3738
enable_finance: bool = Query(False, description="Enable finance tracking")
3839
):
3940
global sim
41+
42+
# csv path for city data
43+
csv_path = os.path.join(os.path.dirname(__file__), "uscities.csv")
4044
# Create new FreightSim instance with finance tracking option
41-
sim = FreightSim("backend/uscities.csv", num_trucks=num_trucks, min_pop=min_pop, enable_finance=enable_finance)
45+
sim = FreightSim(csv_path, num_trucks=num_trucks, min_pop=min_pop, enable_finance=enable_finance)
4246

4347
# Create initial random loads
44-
for _ in range(num_loads):
45-
sim.create_random_load()
48+
with sim_lock:
49+
sim = FreightSim(csv_path, num_trucks=num_trucks, min_pop=min_pop, enable_finance=enable_finance)
50+
for _ in range(num_loads):
51+
sim.create_random_load()
4652

4753
return {"message": f"Simulation initialized with {num_trucks} trucks and {num_loads} loads. Finance enabled: {enable_finance}"}
4854

4955
# --- Get current status of trucks and loads ---
5056
@app.get("/status")
5157
def get_status():
52-
if not sim:
53-
return {"trucks": [], "loads": []}
58+
59+
with sim_lock:
60+
if not sim:
61+
return {"trucks": [], "loads": []}
5462

5563
# Build truck status list
56-
trucks = []
57-
for _, truck in sim.trucks.iterrows():
58-
trucks.append({
64+
trucks = []
65+
for _, truck in sim.trucks.iterrows():
66+
trucks.append({
5967
"truck_id": truck["truck_id"],
6068
"lat": truck["lat"], # current latitude
6169
"lon": truck["lng"], # current longitude
@@ -67,11 +75,11 @@ def get_status():
6775
})
6876

6977
# Build load status list
70-
loads = []
71-
for _, load in sim.loads.iterrows():
72-
origin = sim.cities.iloc[load["origin_idx"]]
73-
dest = sim.cities.iloc[load["dest_idx"]]
74-
loads.append({
78+
loads = []
79+
for _, load in sim.loads.iterrows():
80+
origin = sim.cities.iloc[load["origin_idx"]]
81+
dest = sim.cities.iloc[load["dest_idx"]]
82+
loads.append({
7583
"load_id": load["load_id"],
7684
"origin": origin["city"],
7785
"destination": dest["city"],
@@ -87,7 +95,9 @@ def get_status():
8795
# --- Create a new random load dynamically ---
8896
@app.get("/create_load")
8997
def create_load():
90-
if sim:
91-
sim.create_random_load() # Add a new shipment
92-
return {"message": "New load created"}
93-
return {"message": "Simulation not initialized yet."}
98+
with sim_lock:
99+
if not sim:
100+
return {"message": "Simulation not initialized yet."}
101+
sim.create_random_load()
102+
return {"message": "New load created"}
103+

freight_project/readme.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@ OwlHacks 2025 Hackathon Project
44

55
This project is a freight trading simulator I built for OwlHacks 2025. The idea is to mimic how trucks and shipments are matched in real life, so you can see who’s carrying what, track deliveries, and even create new loads on the fly. Everything is interactive and plotted on a map so you can watch the simulation happen in real-time.
66

7+
The simulation also tracks financial metrics for each truck, including profit, expenses, and net profit.
8+
79
Concept
810

9-
Freight trading is all about moving cargo efficiently—matching shipments with transport capacity and making smart delivery decisions. I tried to capture that in this project by:
11+
Freight trading is all about moving cargo efficiently—matching shipments with transport capacity and making smart delivery decisions. This project captures that by:
1012

1113
Treating trucks like transport capacity and loads like shipments.
1214

13-
Automatically assigning trucks to loads based on their capacity and location.
15+
Automatically assigning trucks to loads based on capacity, location, and availability.
16+
17+
Allowing the creation of new loads dynamically, simulating new market opportunities.
1418

15-
Letting you create new loads dynamically, which is like new opportunities popping up in the market.
19+
Running a continuous simulation loop that updates trucks and loads every second.
1620

17-
Updating everything live so you can see trucks move toward their destinations.
21+
Tracking financial metrics for each truck: profit, expenses, and net profit.
1822

19-
It’s a simplified way to explore how freight trading works without having to charter real ships or trucks.
23+
This is a simplified way to explore how freight trading works without using real trucks or logistics systems.
2024

2125
Project Structure
2226
freight_project/
@@ -84,13 +88,13 @@ Enter simulation parameters and click Start Simulation.
8488

8589
Click Create Random Load to add new shipments dynamically.
8690

87-
CLI (Optional)
91+
CLI
8892

8993
Go to the cli/ folder.
9094

9195
Run:
9296

93-
python main.py
97+
python3 main.py
9498

9599

96100
Follow the prompts to simulate trucks and loads step by step.
@@ -105,7 +109,7 @@ Automatic assignment of trucks based on capacity and location.
105109

106110
Dashed lines that show the path from trucks to their assigned loads.
107111

108-
Optional CLI version for terminal-based simulation.
112+
CLI version for terminal-based simulation.
109113

110114
Notes
111115

0 commit comments

Comments
 (0)