Upload app.py
Browse files
app.py
CHANGED
|
@@ -341,14 +341,22 @@ def turn_by_turn(steps):
|
|
| 341 |
def solve(G, walking=False, start_lat=None, start_lon=None, coverage="both"):
|
| 342 |
"""Graph in, everything out. Verifies full coverage before returning.
|
| 343 |
|
| 344 |
-
coverage="once": drive every street a single time (minimal driving).
|
| 345 |
-
coverage="
|
| 346 |
-
|
| 347 |
-
|
|
|
|
|
|
|
|
|
|
| 348 |
"""
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
|
| 353 |
start = None
|
| 354 |
if start_lat is not None and start_lon is not None:
|
|
@@ -359,7 +367,7 @@ def solve(G, walking=False, start_lat=None, start_lon=None, coverage="both"):
|
|
| 359 |
|
| 360 |
steps = build_route(E, start)
|
| 361 |
|
| 362 |
-
if coverage == "
|
| 363 |
# append the same loop reversed: swap each move's direction, reverse order.
|
| 364 |
back = [
|
| 365 |
{"u": s["v"], "v": s["u"], "length": s["length"],
|
|
@@ -369,7 +377,6 @@ def solve(G, walking=False, start_lat=None, start_lon=None, coverage="both"):
|
|
| 369 |
steps = steps + back
|
| 370 |
|
| 371 |
# required passes per street, counted straight from the final route
|
| 372 |
-
# (once -> 1 per street, both -> 2: one each direction)
|
| 373 |
needed = defaultdict(int)
|
| 374 |
for s in steps:
|
| 375 |
if not s["deadhead"]:
|
|
@@ -379,14 +386,15 @@ def solve(G, walking=False, start_lat=None, start_lon=None, coverage="both"):
|
|
| 379 |
s["pair"] = pk
|
| 380 |
s["needed"] = 0 if s["deadhead"] else needed.get(pk, 1)
|
| 381 |
|
| 382 |
-
# Hard guarantee: every required street is covered
|
| 383 |
-
|
|
|
|
|
|
|
| 384 |
have = defaultdict(int)
|
| 385 |
for s in steps:
|
| 386 |
if not s["deadhead"]:
|
| 387 |
have[s["pair"]] += 1
|
| 388 |
-
short = [p for p, c in
|
| 389 |
-
if have[p] < want]
|
| 390 |
if short:
|
| 391 |
raise RouteError("Internal: %d streets not fully covered." % len(short))
|
| 392 |
|
|
@@ -519,7 +527,8 @@ PAGE = r'''<!DOCTYPE html>
|
|
| 519 |
<label class="ck"><input type="checkbox" id="svc"> Alleys</label>
|
| 520 |
<select id="coverage" title="How thoroughly to cover each street">
|
| 521 |
<option value="once" selected>Drive each street once</option>
|
| 522 |
-
<option value="
|
|
|
|
| 523 |
</select>
|
| 524 |
<button id="solve" disabled>Solve route</button>
|
| 525 |
</div>
|
|
@@ -793,7 +802,7 @@ function truckIcon(deg){
|
|
| 793 |
}
|
| 794 |
|
| 795 |
function buildLegend(cov){
|
| 796 |
-
const both = cov==
|
| 797 |
let h='<div class="ttl">Coverage</div>'+
|
| 798 |
'<div class="lg"><span class="sw" style="background:'+C_NONE+'"></span>not covered</div>';
|
| 799 |
if (both) h+='<div class="lg"><span class="sw" style="background:'+C_HALF+'"></span>one side done</div>';
|
|
@@ -938,7 +947,7 @@ function onGpsErr(e){ msg('Location error: '+e.message+'. Allow location access
|
|
| 938 |
function renderStats(s, cov){
|
| 939 |
const row=(l,v,c)=>'<div class="stat '+(c||'')+'"><span>'+l+'</span><b>'+v+'</b></div>';
|
| 940 |
let h='';
|
| 941 |
-
h+=row('Mode', cov==='
|
| 942 |
h+=row('Total route', s.total_mi.toFixed(2)+' mi','big');
|
| 943 |
h+=row('Delivering', s.min_mi.toFixed(2)+' mi','good');
|
| 944 |
h+=row('Backtrack (deadhead)', s.deadhead_mi.toFixed(2)+' mi','warn');
|
|
|
|
| 341 |
def solve(G, walking=False, start_lat=None, start_lon=None, coverage="both"):
|
| 342 |
"""Graph in, everything out. Verifies full coverage before returning.
|
| 343 |
|
| 344 |
+
coverage="once" : drive every street a single time (minimal driving).
|
| 345 |
+
coverage="figure8" : drive the whole efficient loop ONE way, then the SAME loop in
|
| 346 |
+
reverse -- a there-and-back so you cover the opposite side of
|
| 347 |
+
every street on the way back. Predictable, no zig-zag.
|
| 348 |
+
coverage="optimal" : shortest-distance route that still covers both directions of
|
| 349 |
+
every two-way street (directed Chinese Postman). Least total
|
| 350 |
+
miles, but interleaves the two sides so it doubles back a lot.
|
| 351 |
"""
|
| 352 |
+
if coverage == "optimal" or coverage == "both":
|
| 353 |
+
# directed both-sides, minimum total distance (the original solver)
|
| 354 |
+
R = build_both_sides(G, walking)
|
| 355 |
+
R, E, repeats = solve_dcpp(R)
|
| 356 |
+
else:
|
| 357 |
+
# both "once" and "figure8" start from the efficient single-pass loop
|
| 358 |
+
R, E, repeats = solve_cpp_once(G, walking)
|
| 359 |
+
R.graph.setdefault("excluded_nodes", 0)
|
| 360 |
|
| 361 |
start = None
|
| 362 |
if start_lat is not None and start_lon is not None:
|
|
|
|
| 367 |
|
| 368 |
steps = build_route(E, start)
|
| 369 |
|
| 370 |
+
if coverage == "figure8":
|
| 371 |
# append the same loop reversed: swap each move's direction, reverse order.
|
| 372 |
back = [
|
| 373 |
{"u": s["v"], "v": s["u"], "length": s["length"],
|
|
|
|
| 377 |
steps = steps + back
|
| 378 |
|
| 379 |
# required passes per street, counted straight from the final route
|
|
|
|
| 380 |
needed = defaultdict(int)
|
| 381 |
for s in steps:
|
| 382 |
if not s["deadhead"]:
|
|
|
|
| 386 |
s["pair"] = pk
|
| 387 |
s["needed"] = 0 if s["deadhead"] else needed.get(pk, 1)
|
| 388 |
|
| 389 |
+
# Hard guarantee: every required street is covered at least what the graph asks for.
|
| 390 |
+
need_ct = defaultdict(int)
|
| 391 |
+
for u, v in R.edges():
|
| 392 |
+
need_ct[_pair(u, v)] += 1
|
| 393 |
have = defaultdict(int)
|
| 394 |
for s in steps:
|
| 395 |
if not s["deadhead"]:
|
| 396 |
have[s["pair"]] += 1
|
| 397 |
+
short = [p for p, c in need_ct.items() if have[p] < c]
|
|
|
|
| 398 |
if short:
|
| 399 |
raise RouteError("Internal: %d streets not fully covered." % len(short))
|
| 400 |
|
|
|
|
| 527 |
<label class="ck"><input type="checkbox" id="svc"> Alleys</label>
|
| 528 |
<select id="coverage" title="How thoroughly to cover each street">
|
| 529 |
<option value="once" selected>Drive each street once</option>
|
| 530 |
+
<option value="figure8">Both sides — loop then reverse (figure-8)</option>
|
| 531 |
+
<option value="optimal">Both sides — shortest distance</option>
|
| 532 |
</select>
|
| 533 |
<button id="solve" disabled>Solve route</button>
|
| 534 |
</div>
|
|
|
|
| 802 |
}
|
| 803 |
|
| 804 |
function buildLegend(cov){
|
| 805 |
+
const both = cov!=='once';
|
| 806 |
let h='<div class="ttl">Coverage</div>'+
|
| 807 |
'<div class="lg"><span class="sw" style="background:'+C_NONE+'"></span>not covered</div>';
|
| 808 |
if (both) h+='<div class="lg"><span class="sw" style="background:'+C_HALF+'"></span>one side done</div>';
|
|
|
|
| 947 |
function renderStats(s, cov){
|
| 948 |
const row=(l,v,c)=>'<div class="stat '+(c||'')+'"><span>'+l+'</span><b>'+v+'</b></div>';
|
| 949 |
let h='';
|
| 950 |
+
h+=row('Mode', cov==='once'?'Each street once':(cov==='optimal'?'Both sides — shortest':'Both sides — figure-8'));
|
| 951 |
h+=row('Total route', s.total_mi.toFixed(2)+' mi','big');
|
| 952 |
h+=row('Delivering', s.min_mi.toFixed(2)+' mi','good');
|
| 953 |
h+=row('Backtrack (deadhead)', s.deadhead_mi.toFixed(2)+' mi','warn');
|