Butanium

Readertsp

Nov 17th, 2021 (edited)
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.62 KB | None | 0 0
  1. module Readertsp = struct
  2.     let open_tsp ?(file_path = "C:/Users/Clement/Documents/prepa/tipe/ocaml-tsp/tsp") tsp_name =
  3.         let city_count = Scanf.sscanf tsp_name "%[^0-9]%d" (fun _ c -> c)
  4.         in
  5.         let cities = Array.make city_count (0., 0.)
  6.         in
  7.         let fill = let i = ref 0 in
  8.             fun x -> cities.(!i) <- x; incr i
  9.         in
  10.         let ic = open_in  @@
  11.             Printf.sprintf "%s/%s.tsp" file_path tsp_name
  12.         in
  13.         let rec loop started = try (let s = String.trim @@ input_line ic in
  14.             if started then (
  15.                 let x,y = Scanf.sscanf s "%d %f %f" (fun _ x y -> (x, y))
  16.                 in
  17.                 fill (x,y);
  18.                 loop true
  19.             ) else loop ("NODE_COORD_SECTION" = s)
  20.             ) with _ -> ();
  21.         in loop false;
  22.         city_count, cities
  23.  
  24.         let open_path ?(file_path = "C:/Users/Clement/Documents/prepa/tipe/ocaml-tsp/tsp") tsp_name =
  25.         let city_count = Scanf.sscanf tsp_name "%[^0-9]%d" (fun _ c -> c)
  26.         in
  27.         let path = Array.make city_count 0
  28.         in
  29.         let fill = let i = ref 0 in
  30.             fun x -> path.(!i) <- x-1; incr i
  31.         in
  32.         let ic = open_in  @@
  33.         Printf.sprintf "%s/%s.opt.tour" file_path tsp_name
  34.         in
  35.         let rec loop started = try (let s = String.trim @@ input_line ic in
  36.             if started then (
  37.                 List.iter fill @@ List.map int_of_string @@ String.split_on_char ' ' s;
  38.                loop true
  39.             ) else loop ("TOUR_SECTION" = s)
  40.             ) with _ -> ();
  41.         in loop false;
  42.         path
  43.  
  44. end;;
  45.  
Add Comment
Please, Sign In to add comment