From 2c26d061d77b56290b418b7431c36bb41fca9e30 Mon Sep 17 00:00:00 2001 From: kateonbxsh Date: Thu, 15 Jan 2026 00:20:09 +0100 Subject: [PATCH] fix: bugs in airline scheduling --- graphs/graph-to-other.txt | 2 +- scheduling/schedule1.txt | 4 +- scheduling/testresult.schedule | 3 - scheduling/testresult.txt | 16 ---- src/scheduling.ml | 167 ++++++++++++++++++++++++--------- 5 files changed, 128 insertions(+), 64 deletions(-) delete mode 100644 scheduling/testresult.schedule delete mode 100644 scheduling/testresult.txt diff --git a/graphs/graph-to-other.txt b/graphs/graph-to-other.txt index f98df19..8009c11 100644 --- a/graphs/graph-to-other.txt +++ b/graphs/graph-to-other.txt @@ -14,4 +14,4 @@ digraph cool_generated_graph { 2 -> 4 [label = "12"]; 0 -> 1 [label = "7"]; 0 -> 3 [label = "10"]; - 0 -> 2 [label = "8"];} \ No newline at end of file + 0 -> 2 [label = "8"];} diff --git a/scheduling/schedule1.txt b/scheduling/schedule1.txt index 6a4089a..b72fd68 100644 --- a/scheduling/schedule1.txt +++ b/scheduling/schedule1.txt @@ -1,3 +1,5 @@ A -> B | 08:00 -> 10:00 B -> C | 11:00 -> 12:45 -A -> C | 08:00 -> 11:00 \ No newline at end of file +A -> C | 09:43 -> 11:20 +RAK -> TLS | 15:00 -> 17:00 +TLS -> LAX | 20:00 -> 23:00 \ No newline at end of file diff --git a/scheduling/testresult.schedule b/scheduling/testresult.schedule deleted file mode 100644 index 93ffed4..0000000 --- a/scheduling/testresult.schedule +++ /dev/null @@ -1,3 +0,0 @@ -Airplane 2: C -> A -Airplane 2: C -> B -Airplane 2: B -> A diff --git a/scheduling/testresult.txt b/scheduling/testresult.txt deleted file mode 100644 index dfe15b2..0000000 --- a/scheduling/testresult.txt +++ /dev/null @@ -1,16 +0,0 @@ -digraph airline_schedule { - fontname="Helvetica,Arial,sans-serif"; - node [fontname="Helvetica,Arial,sans-serif"]; - edge [fontname="Helvetica,Arial,sans-serif"]; - rankdir=LR; - node [shape=circle]; - "A" -> "unknown" [label="1"]; - "unknown" -> "A" [label="1"]; - "unknown" -> "B" [label="1"]; - "unknown" -> "A" [label="1"]; - "B" -> "unknown" [label="1"]; - "A" -> "unknown" [label="1"]; - "A" -> "A" [label="2"]; - "B" -> "B" [label="2"]; - "A" -> "A" [label="2"]; -} diff --git a/src/scheduling.ml b/src/scheduling.ml index e2b8570..774fb2f 100644 --- a/src/scheduling.ml +++ b/src/scheduling.ml @@ -1,7 +1,7 @@ open Graph open Algo open Tools -open Printf +open Gfile type flight = { id: int; @@ -56,6 +56,9 @@ let t = 1 let si i = 2 + 2*i let di i = 2 + 2*i + 1 +let is_si i = i mod 2 == 0;; +let is_di i = i mod 2 == 1;; + type bounded_flow = { mutable flow: int; lower: int; @@ -63,7 +66,7 @@ type bounded_flow = { } (* Build the bounded flow graph *) -let build_bounded_graph (flights: flight list) : bounded_flow graph = +let build_bounded_graph (flights: flight list) (k: int) : bounded_flow graph = let g = empty_graph in let g = new_node g s in @@ -99,15 +102,17 @@ let build_bounded_graph (flights: flight list) : bounded_flow graph = ) g flights ) g flights in - (* s -> t [0,∞] *) - let g = add_arc g s t 0 max_int in + (* s -> t [0, k] *) + let g = add_arc g s t 0 k in g (* compute and returns demands from lower bounds *) -let compute_demands (g: bounded_flow graph): int array = +let compute_demands (g: bounded_flow graph) (k: int): int array = let n = n_fold g (fun acc id -> max acc id) 0 + 1 in let demand = Array.make n 0 in + demand.(s) <- (-k); + demand.(t) <- (k); e_iter g (fun arc -> demand.(arc.src) <- demand.(arc.src) + arc.lbl.lower; demand.(arc.tgt) <- demand.(arc.tgt) - arc.lbl.lower @@ -124,10 +129,10 @@ let add_super_nodes g demand: bounded_flow graph * id * id * int = let (g, total_demand) = Array.fold_left (fun (g, total) v -> let d = demand.(v) in - if d > 0 then - (new_arc g {src=ss; tgt=v; lbl={flow=0; lower=0; capacity=(d)}}, total + d) - else if d < 0 then - (new_arc g {src=v; tgt=tt; lbl={flow=0; lower=0; capacity=(-d)}}, total) + if d < 0 then + (new_arc g {src=ss; tgt=v; lbl={flow=0; lower=0; capacity=(-d)}}, total) + else if d > 0 then + (new_arc g {src=v; tgt=tt; lbl={flow=0; lower=0; capacity=(d)}}, total + d) else (g, total) ) (g, 0) (Array.init n (fun i -> i)) @@ -164,28 +169,28 @@ let graph_to_schedule (g: bounded_flow graph) flights : schedule_arc list = let airline_schedule flights k : bounded_flow graph option = - let g = build_bounded_graph flights in + let g = build_bounded_graph flights k in - let demand = compute_demands g in + let demand = compute_demands g k in (* add SS and TT *) - let (g, ss, tt, _) = add_super_nodes g demand in + let (g, ss, tt, total_demand) = add_super_nodes g demand in let flow_g = e_fold g (fun org a -> new_arc org {src=a.src; tgt=a.tgt; lbl={flow=a.lbl.flow; capacity=a.lbl.capacity}}) (clone_nodes g) in let flow_g = apply_ford_fulkerson flow_g ss tt in + (* check if demand is satisfied (sum of all flows going out of SS) *) + let total_flow_out_ss = + let arcs = out_arcs flow_g ss in + List.fold_left (fun acc (arc: flow arc) -> acc + arc.lbl.flow) 0 arcs + in + recover_flows g flow_g; - (* check if demand is satisfied (sum of all flows going out of SS) *) - let total_flow_on_s = - let arcs = out_arcs g ss in - List.fold_left (fun acc (arc: bounded_flow arc) -> acc + arc.lbl.flow) 0 arcs - in - - printf "Airplanes needed to operate flights: %d\n" total_flow_on_s; + Printf.printf "total flow out of SS %d, total demand %d\n" total_flow_out_ss total_demand; - if total_flow_on_s > k then None + if total_flow_out_ss <> total_demand then None else begin Some g end;; @@ -234,29 +239,105 @@ let export_schedule_graph path (g: bounded_flow graph) flights = let export_schedule path (g: bounded_flow graph) flights = let ff = open_out path in - - let print_arc (arc: bounded_flow arc) = - if arc.lbl.flow > 0 then - - let f_from = List.find_opt (fun f -> si f.id = arc.src || di f.id = arc.src) flights in - - let f_to = List.find_opt (fun f -> si f.id = arc.tgt || di f.id = arc.tgt) flights in - - match f_from, f_to with - | Some flight_from, Some flight_to -> - Printf.fprintf ff "Airplane %d: %s -> %s\n" - arc.lbl.flow flight_from.arrival_airport flight_to.departure_airport - | Some flight_from, None when arc.tgt = t -> - - Printf.fprintf ff "Airplane %d: %s -> END\n" - arc.lbl.flow flight_from.arrival_airport - | None, Some flight_to when arc.src = s -> - - Printf.fprintf ff "Airplane %d: START -> %s\n" - arc.lbl.flow flight_to.departure_airport - | _ -> () + (* Map node id -> flight, start with di because of di -> si case *) + let airport_of_did id = + List.find_opt (fun f -> di f.id = id) flights in - e_iter g print_arc; - close_out ff;; + let airport_of_sid id = + List.find_opt (fun f -> si f.id = id) flights + in + + let n = n_fold g (fun a _ -> a + 1) 0 in + let ss = n - 2 in + let tt = n - 1 in + + (* Keep track of which arcs were already used in a path *) + let used = Hashtbl.create 100 in + + (* Find next arc with positive flow and not used *) + let next_arc_from v = + List.find_opt (fun (arc: bounded_flow arc) -> + arc.lbl.flow > 0 && not (Hashtbl.mem used (arc.src, arc.tgt)) && + not (List.mem arc.src [ss; tt]) && + not (List.mem arc.tgt [ss; tt; t]) + ) (out_arcs g v) + in + + (* Trace one airplane path starting from s *) + let rec trace_airplane path_nodes current = + match next_arc_from current with + | None -> List.rev path_nodes + | Some arc -> + Hashtbl.add used (arc.src, arc.tgt) (); + trace_airplane (arc :: path_nodes) arc.tgt + in + + let airplane_counter = ref 1 in + + (* Generate all airplane paths *) + let rec trace_all v = + match next_arc_from v with + | None -> () + | Some _ -> + let path = trace_airplane [] v in + (* Print the full route *) + let rec print_path = function + | [] -> () + | (arc: bounded_flow arc) :: rest -> + let airplane = !airplane_counter in + + if arc.src = s then + (* Case 1: starts *) + let airport = + match airport_of_sid arc.tgt with + | Some f -> f.departure_airport + | None -> "?" + in + Printf.fprintf ff + "Airplane %d starts at airport %s\n" + airplane airport + + else if is_di arc.src && is_si arc.tgt then + (* Case 2: waits *) + let airport = + match airport_of_did arc.src with + | Some f -> f.arrival_airport + | None -> "?" + in + Printf.fprintf ff + "Airplane %d waits at airport %s\n" + airplane airport + + else begin + let from_airport = + match airport_of_sid arc.src with + | Some f -> f.departure_airport + | None -> "?" + and to_airport = + match airport_of_did arc.tgt with + | Some f -> f.arrival_airport + | None -> "?" + in + Printf.fprintf ff + "Airplane %d: %s -> %s\n" + airplane from_airport to_airport; + if rest = [] then begin + Printf.fprintf ff + "Airplane %d schedule ends at airport %s\n" + airplane to_airport; + end; + end; + + print_path rest + + in + print_path path; + Printf.fprintf ff "\n"; + airplane_counter := !airplane_counter + 1; + trace_all v + in + + trace_all s; + close_out ff