public
Description: Simulated annealing implementation in OCaml
Clone URL: git://github.com/khigia/ocaml-anneal.git
Completed TSP example to calculate SG bus stop services.
khigia (author)
Wed Jun 11 08:12:51 -0700 2008
commit  1e32c07d9b9fc6f878a251e33fe2c5d62ecdba7d
tree    8204b3054cb63e397847fd37ca4d8c91f893783d
parent  88768c8f39323eb34283cba73a53f6db1407907d
...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
...
75
76
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
 
102
103
104
 
 
 
 
105
106
...
47
48
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
51
52
...
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
96
 
 
97
98
99
100
101
102
0
@@ -47,22 +47,6 @@ let path_length pts =
0
         pts
0
     |> snd
0
 
0
-
0
- (*
0
-let objective arr =
0
- Array.fold_left (fun (i,sum) e -> (i + 1, sum + e * i)) (1, 0) arr
0
- |> snd
0
- |> float
0
-
0
-let _ =
0
- let init = [|1;2;3;4;5;6;7;8;9;1;2;3;4;5;6;7;8;9;|] in
0
- let cooling = Anneal.kirkpatrick_seq 0.9999 10.0 in
0
- let (n, sol, score) = Anneal.optimize init objective reversed_section 500000 cooling in
0
- Printf.printf "%d evaluations; score=%f; sol=\n" n score;
0
- Array.iter (Printf.printf "%d ") sol;
0
- print_newline ()
0
- *)
0
-
0
 let _ =
0
     let posfn = ref "coords.txt" in
0
     let usage = Printf.sprintf "Usage: %s [-help] [-coords filename]" Sys.argv.(0) in
0
@@ -75,32 +59,44 @@ let _ =
0
         ]
0
         ignore
0
         usage;
0
+ let doit name path =
0
+ let d = path_length path in
0
+ Printf.eprintf "Path %s:" name;
0
+ Array.iter (Printf.eprintf " %d") path;
0
+ Printf.eprintf ": %f\n" d;
0
+ flush_all ();
0
+ let cooling = Anneal.kirkpatrick_seq 0.9999 10.0 in
0
+ let (n, sol, score) = Anneal.optimize path (fun p -> -. path_length p) reversed_section 500000 cooling in
0
+ Printf.eprintf " Optimization: %d evaluations; score=%f\n" n score;
0
+ Printf.printf "%s " name;
0
+ Array.iter (Printf.printf "%d ") sol;
0
+ print_newline ()
0
+ in
0
+ let read_path () =
0
+ let line = read_line () in
0
+ let re = Str.regexp "[ \t]+" in
0
+ let name::pts = Str.split re line in
0
+ (name, Array.of_list (List.map int_of_string pts))
0
+ in
0
+ let rec loop () =
0
+ let name, p = read_path () in
0
+ match Array.length p with
0
+ | 0 -> ()
0
+ | n when n > 2 ->
0
+ (*TODO bug if p = 2 and p.(0)=p.(1) *)
0
+ doit name p;
0
+ loop ()
0
+ | _ ->
0
+ loop ()
0
+ in
0
     try
0
         let buf = Scanf.Scanning.from_file !posfn in
0
         let _ = read_positions buf in
0
- let doit path =
0
- let d = path_length path in
0
- Printf.printf "Path:";
0
- Array.iter (Printf.printf " %d") path;
0
- Printf.printf ": %f\n" d;
0
- let cooling = Anneal.kirkpatrick_seq 0.9999 10.0 in
0
- let (n, sol, score) = Anneal.optimize path (fun p -> -. path_length p) reversed_section 5000 cooling in
0
- Printf.printf " Optimization: %d evaluations; score=%f; sol=" n score;
0
- Array.iter (Printf.printf "%d ") sol;
0
- print_newline ()
0
- in
0
- (*
0
- let _ = doit [|1; 1;|] in (* TODO something wrong ... infinite loop ... probably in solution generation! *)
0
- let _ = doit [|1; 2;|] in
0
- let _ = doit [|1; 3;|] in
0
- let _ = doit [|2; 3;|] in
0
- let _ = doit [|3; 2;|] in
0
- *)
0
- let _ = doit [|1; 1; 2;|] in
0
- let _ = doit [|1; 2; 3;|] in
0
- ()
0
+ loop ()
0
     with
0
- exn ->
0
- Printf.printf "ERROR:%s\n%s\n" (Printexc.to_string exn) usage;
0
+ | End_of_file ->
0
+ ()
0
+ | exn ->
0
+ Printf.printf "ERROR:%s\n%s\n" (Printexc.to_string exn) usage
0
 
0
 

Comments

    No one has commented yet.