diff --git a/scheduling/schedule1.txt b/scheduling/schedule1.txt new file mode 100644 index 0000000..6a4089a --- /dev/null +++ b/scheduling/schedule1.txt @@ -0,0 +1,3 @@ +A -> B | 08:00 -> 10:00 +B -> C | 11:00 -> 12:45 +A -> C | 08:00 -> 11:00 \ No newline at end of file diff --git a/scheduling/testresult.schedule b/scheduling/testresult.schedule new file mode 100644 index 0000000..93ffed4 --- /dev/null +++ b/scheduling/testresult.schedule @@ -0,0 +1,3 @@ +Airplane 2: C -> A +Airplane 2: C -> B +Airplane 2: B -> A diff --git a/scheduling/testresult.txt b/scheduling/testresult.txt new file mode 100644 index 0000000..dfe15b2 --- /dev/null +++ b/scheduling/testresult.txt @@ -0,0 +1,16 @@ +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/dune b/src/dune index ed3eb51..d4dd1fc 100644 --- a/src/dune +++ b/src/dune @@ -4,4 +4,5 @@ ;; Disable some warnings ;; (flags (:standard -w -6)) ;; Link to executable in main dir + (libraries unix) (promote (until-clean))) diff --git a/src/ftest.ml b/src/ftest.ml index 121d9f0..eb1f64e 100644 --- a/src/ftest.ml +++ b/src/ftest.ml @@ -1,37 +1,74 @@ open Gfile open Algo +open Printf +open Unix +open Scheduling let () = + if Array.length Sys.argv < 2 then begin + printf "Usage:\n"; + printf " %s flow infile source sink outfile [svgfile]\n" Sys.argv.(0); + printf " %s scheduling infile crew_count outfile [svgfile]\n" Sys.argv.(0); + exit 1 + end; - (* Check the number of command-line arguments *) - if Array.length Sys.argv <> 5 then - begin - Printf.printf - "\n ✻ Usage: %s infile source sink outfile\n\n%s%!" Sys.argv.(0) - (" 🟄 infile : input file containing a graph\n" ^ - " 🟄 source : identifier of the source vertex (used by the ford-fulkerson algorithm)\n" ^ - " 🟄 sink : identifier of the sink vertex (ditto)\n" ^ - " 🟄 outfile : output file in which the result should be written.\n\n") ; - exit 0 - end ; + let mode = Sys.argv.(1) in + if mode = "flow" then begin + if Array.length Sys.argv < 6 then begin + printf "Usage: %s flow infile source sink outfile [svgfile]\n" Sys.argv.(0); + exit 1 + end; - (* Arguments are : infile(1) source-id(2) sink-id(3) outfile(4) *) + let infile = Sys.argv.(2) in + let source = int_of_string Sys.argv.(3) in + let sink = int_of_string Sys.argv.(4) in + let outfile = Sys.argv.(5) in + let svgfile = + if Array.length Sys.argv >= 7 then Some Sys.argv.(6) else None + in - let infile = Sys.argv.(1) - and outfile = Sys.argv.(4) + let graph = from_file infile in + let result = apply_ford_fulkerson (flow_from_string_graph graph) source sink in + export outfile (printable_flow_graph result); - (* These command-line arguments are not used for the moment. *) - and source = int_of_string Sys.argv.(2) - and sink = int_of_string Sys.argv.(3) - in + (match svgfile with + | Some f -> ignore (system (sprintf "dot -Tsvg %s -o %s" outfile f)) + | None -> ()); - (* Open file *) - let graph = from_file infile in + end + else if mode = "scheduling" then begin + if Array.length Sys.argv < 5 then begin + printf "Usage: %s scheduling infile crew_count outfile [svgfile]\n" Sys.argv.(0); + exit 1 + end; - (* Rewrite the graph that has been read. *) - let result_graph = apply_ford_fulkerson (flow_from_string_graph graph) source sink in - let () = export outfile (printable_flow_graph result_graph) in + let infile = Sys.argv.(2) in + let airplanes_count = int_of_string Sys.argv.(3) in + let outfile = Sys.argv.(4) in + let svgfile = + if Array.length Sys.argv >= 6 then Some Sys.argv.(5) else None + in - () + let flights = read_flights_from_file infile in + printf "Read %d flights, %d airplanes available\n" (List.length flights) airplanes_count; + + match airline_schedule flights airplanes_count with + | None -> + printf "No feasible schedule found.\n"; + exit 1 + | Some g -> + export_schedule_graph (outfile ^ ".txt") g flights; + export_schedule (outfile ^ ".schedule") g flights; + + (match svgfile with + | Some f -> + export_schedule (outfile ^ ".dot") g flights; + ignore (system (sprintf "dot -Tsvg %s.dot -o %s" outfile f)) + | None -> ()) + end + else begin + printf "Unknown mode: %s\n" mode; + exit 1 + end diff --git a/src/scheduling.ml b/src/scheduling.ml index 75f0703..e2b8570 100644 --- a/src/scheduling.ml +++ b/src/scheduling.ml @@ -1,5 +1,7 @@ open Graph open Algo +open Tools +open Printf type flight = { id: int; @@ -22,7 +24,7 @@ let read_flights_from_file path : flight list = try (* AIRPORT -> AIRPORT | HH:MM -> HH:MM *) Scanf.sscanf line "%s -> %s | %d:%d -> %d:%d" - (fun dep_airport arr_airport dep_h dep_m arr_h arr_m -> + (fun departure_airport arrival_airport dep_h dep_m arr_h arr_m -> let dep_time = dep_h * 60 + dep_m in let arr_time = arr_h * 60 + arr_m in let f = { id = !id_counter; @@ -35,9 +37,8 @@ let read_flights_from_file path : flight list = ) with | Scanf.Scan_failure msg - | Failure msg - | End_of_file -> - failwith (Printf.sprintf "Failed to parse line: '%s'" line) + | Failure msg -> + failwith (Printf.sprintf "Failed to parse line: '%s', error: %s" line msg) end done; [] @@ -104,12 +105,12 @@ let build_bounded_graph (flights: flight list) : bounded_flow graph = (* compute and returns demands from lower bounds *) -let compute_demands (g: bounded_flow graph) : int array = +let compute_demands (g: bounded_flow graph): int array = let n = n_fold g (fun acc id -> max acc id) 0 + 1 in let demand = Array.make n 0 in e_iter g (fun arc -> - demand.(arc.src) <- demand.(arc.src) - arc.lbl.lower; - demand.(arc.tgt) <- demand.(arc.tgt) + arc.lbl.lower + demand.(arc.src) <- demand.(arc.src) + arc.lbl.lower; + demand.(arc.tgt) <- demand.(arc.tgt) - arc.lbl.lower ); demand @@ -124,7 +125,7 @@ let add_super_nodes g demand: bounded_flow graph * id * id * int = 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) + (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) else @@ -135,9 +136,11 @@ let add_super_nodes g demand: bounded_flow graph * id * id * int = (g, ss, tt, total_demand) (* recover original flows by adding lower bounds back *) -let recover_flows (g: bounded_flow graph) : unit = - e_iter g (fun arc -> - arc.lbl.flow <- arc.lbl.flow + arc.lbl.lower +let recover_flows (g: bounded_flow graph) (flowg: flow graph) : unit = + e_iter flowg (fun arc -> + match(find_arc g arc.src arc.tgt) with + | None -> (); + | Some barc -> barc.lbl.flow <- arc.lbl.flow + barc.lbl.lower ) type schedule_arc = { @@ -146,7 +149,7 @@ type schedule_arc = { flow: int; } -let graph_to_schedule g flights : schedule_arc list = +let graph_to_schedule (g: bounded_flow graph) flights : schedule_arc list = e_fold g (fun acc 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 @@ -166,24 +169,28 @@ let airline_schedule flights k : bounded_flow graph option = let demand = compute_demands g in (* add SS and TT *) - let (g, ss, tt, total_demand) = add_super_nodes g demand in + let (g, ss, tt, _) = add_super_nodes g demand in - let g = new_arc g {src=s; tgt=t; lbl={flow=0; lower=0; capacity=max_int}} 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 g = apply_ford_fulkerson g ss tt in + let flow_g = apply_ford_fulkerson flow_g ss tt in - (* check if demand is satisfied *) - let flow_from_ss = - List.fold_left (fun acc arc -> acc + arc.lbl.flow) 0 (out_arcs g ss) + 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; - if flow_from_ss <> total_demand then None + if total_flow_on_s > k then None else begin - recover_flows g; Some g end;; -let export_schedule path g flights = +let export_schedule_graph path (g: bounded_flow graph) flights = let ff = open_out path in (* Start DOT graph *) @@ -194,16 +201,26 @@ let export_schedule path g flights = Printf.fprintf ff " rankdir=LR;\n"; Printf.fprintf ff " node [shape=circle];\n"; - (* Helper: get airport name from node id *) + (* get airport name from node id *) let node_name id = match List.find_opt (fun f -> si f.id = id || di f.id = id) flights with - | Some f -> f.departure_airport ^ "_" ^ string_of_int f.id + | Some f -> f.departure_airport | None -> if id = s then "source" else if id = t then "sink" else "unknown" in + let n = n_fold g (fun a _ -> a + 1) 0 in + let ss = n in + let tt = n + 1 in + (* Print each arc with flow as label *) - let print_arc arc = - if arc.lbl.flow > 0 then + let print_arc (arc: bounded_flow arc) = + if arc.lbl.flow > 0 && + arc.src <> s && + arc.tgt <> s && + arc.src <> t && + arc.tgt <> t && + arc.src <> ss && + arc.tgt <> tt then let src_name = node_name arc.src in let tgt_name = node_name arc.tgt in Printf.fprintf ff " \"%s\" -> \"%s\" [label=\"%d\"];\n" src_name tgt_name arc.lbl.flow @@ -211,27 +228,14 @@ let export_schedule path g flights = e_iter g print_arc; - (* Close DOT graph *) Printf.fprintf ff "}\n"; close_out ff;; -let graph_to_schedule g flights : schedule_arc list = - e_fold g (fun acc 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 ff, Some ft -> - {from_airport=ff.arrival_airport; to_airport=ft.departure_airport; flow=arc.lbl.flow} :: acc - | _ -> acc - else acc - ) [] - -let export_schedule_text path g flights = +let export_schedule path (g: bounded_flow graph) flights = let ff = open_out path in - let print_arc arc = + 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 @@ -239,17 +243,17 @@ let export_schedule_text path g flights = 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 ff, Some ft -> + | Some flight_from, Some flight_to -> Printf.fprintf ff "Airplane %d: %s -> %s\n" - arc.lbl.flow ff.arrival_airport ft.departure_airport - | Some ff, None when arc.tgt = t -> + 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 ff.arrival_airport - | None, Some ft when arc.src = s -> + 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 ft.departure_airport + arc.lbl.flow flight_to.departure_airport | _ -> () in diff --git a/src/scheduling.mli b/src/scheduling.mli index fc26c3c..f366ab0 100644 --- a/src/scheduling.mli +++ b/src/scheduling.mli @@ -1,15 +1,17 @@ open Graph -open Algo -(** Airline scheduling module *) - -(** Flight record *) type flight = { id: int; departure_airport: string; arrival_airport: string; - departure_time: int; (** in minutes *) - arrival_time: int; (** in minutes *) + departure_time: int; + arrival_time: int; +} + +type bounded_flow = { + mutable flow: int; + lower: int; + capacity: int; } type schedule_arc = { @@ -18,9 +20,8 @@ type schedule_arc = { flow: int; } -val graph_to_schedule: bounded_flow graph -> flight list -> schedule_arc list - val airline_schedule : flight list -> int -> bounded_flow graph option -val export_schedule : string -> bounded_flow Graph.graph -> flight list -> unit -val export_schedule_text : string -> bounded_flow Graph.graph -> flight list -> unit +val graph_to_schedule : bounded_flow graph -> flight list -> schedule_arc list +val export_schedule : string -> bounded_flow graph -> flight list -> unit +val export_schedule_graph : string -> bounded_flow graph -> flight list -> unit val read_flights_from_file : string -> flight list